blob: 81c7204274dc4031920d759fecbb55e0c39f0ec3 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07003 * Copyright 1996-2020 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -070079/*
80 * Preprocessor execution options that can be controlled by %pragma or
81 * other directives. This structure is initialized to zero on each
82 * pass; this *must* reflect the default initial state.
83 */
84static struct pp_opts {
85 bool noaliases;
86 bool sane_empty_expansion;
87} ppopt;
88
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000089typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080090typedef struct MMacro MMacro;
91typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000092typedef struct Context Context;
93typedef struct Token Token;
94typedef struct Line Line;
95typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080096typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000097
98/*
H. Peter Anvin8571f062019-09-23 16:40:03 -070099 * This is the internal form which we break input lines up into.
100 * Typically stored in linked lists.
101 *
102 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
103 * not necessarily used as-is, but is also used to encode the number
104 * and expansion type of substituted parameter. So in the definition
105 *
106 * %define a(x,=y) ( (x) & ~(y) )
107 *
108 * the token representing `x' will have its type changed to
109 * tok_smac_param(0) but the one representing `y' will be
110 * tok_smac_param(1); see the accessor functions below.
111 *
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700112 * TOK_INTERNAL_STRING is a string which has been unquoted, but should
113 * be treated as if it was a quoted string. The code is free to change
114 * one into the other at will. TOK_NAKED_STRING is a text token which
115 * should be treated as a string, but which MUST NOT be turned into a
116 * quoted string. TOK_INTERNAL_STRINGs can contain any character,
117 * including NUL, but TOK_NAKED_STRING must be a valid C string.
H. Peter Anvin8571f062019-09-23 16:40:03 -0700118 */
119enum pp_token_type {
120 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT,
121 TOK_CORRUPT, /* Token text modified in an unsafe manner, now bogus */
122 TOK_BLOCK, /* Storage block pointer, not a real token */
123 TOK_ID,
124 TOK_PREPROC_ID, TOK_MMACRO_PARAM, TOK_LOCAL_SYMBOL,
125 TOK_LOCAL_MACRO, TOK_ENVIRON, TOK_STRING,
126 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700127 TOK_INTERNAL_STRING, TOK_NAKED_STRING,
H. Peter Anvin8571f062019-09-23 16:40:03 -0700128 TOK_PREPROC_Q, TOK_PREPROC_QQ,
129 TOK_PASTE, /* %+ */
130 TOK_COND_COMMA, /* %, */
131 TOK_INDIRECT, /* %[...] */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700132 TOK_XDEF_PARAM, /* Used during %xdefine processing */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700133 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
134 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
135};
136
137static inline enum pp_token_type tok_smac_param(int param)
138{
139 return TOK_SMAC_START_PARAMS + param;
140}
141static int smac_nparam(enum pp_token_type toktype)
142{
143 return toktype - TOK_SMAC_START_PARAMS;
144}
145static bool is_smac_param(enum pp_token_type toktype)
146{
147 return toktype >= TOK_SMAC_START_PARAMS;
148}
149
150#define PP_CONCAT_MASK(x) (1U << (x))
151
152struct tokseq_match {
153 int mask_head;
154 int mask_tail;
155};
156
157/*
158 * This is tuned so struct Token should be 64 bytes on 64-bit
159 * systems and 32 bytes on 32-bit systems. It enables them
160 * to be nicely cache aligned, and the text to still be kept
161 * inline for nearly all tokens.
162 *
163 * We prohibit tokens of length > MAX_TEXT even though
164 * length here is an unsigned int; this avoids problems
165 * if the length is passed through an interface with type "int",
166 * and is absurdly large anyway.
167 *
168 * For the text mode, in pointer mode the pointer is stored at the end
169 * of the union and the pad field is cleared. This allows short tokens
170 * to be unconditionally tested for by only looking at the first text
171 * bytes and not examining the type or len fields.
172 */
173#define INLINE_TEXT (7*sizeof(char *)-sizeof(enum pp_token_type)-sizeof(unsigned int)-1)
174#define MAX_TEXT (INT_MAX-2)
175
176struct Token {
177 Token *next;
178 enum pp_token_type type;
179 unsigned int len;
180 union {
181 char a[INLINE_TEXT+1];
182 struct {
183 char pad[INLINE_TEXT+1 - sizeof(char *)];
184 char *ptr;
185 } p;
186 } text;
187};
188
189/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700190 * Note on the storage of both SMacro and MMacros: the hash table
191 * indexes them case-insensitively, and we then have to go through a
192 * linked list of potential case aliases (and, for MMacros, parameter
193 * ranges); this is to preserve the matching semantics of the earlier
194 * code. If the number of case aliases for a specific macro is a
195 * performance issue, you may want to reconsider your coding style.
196 */
197
198/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700199 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700200 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700201typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700202
203/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000204 * Store the definition of a single-line macro.
205 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700206enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700207 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700208 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
209 SPARM_STR = 2, /* Convert to quoted string ($) */
210 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
211 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
212};
213
214struct smac_param {
H. Peter Anvin8571f062019-09-23 16:40:03 -0700215 Token name;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700216 enum sparmflags flags;
217};
218
H. Peter Anvine2c80182005-01-15 22:15:51 +0000219struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800220 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800221 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700222 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700223 ExpandSMacro expand;
224 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700225 struct smac_param *params;
226 int nparam;
227 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800228 bool casesense;
229 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700230 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000231};
232
233/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700234 * "No listing" flags. Inside a loop (%rep..%endrep) we may have
235 * macro listing suppressed with .nolist, but we still need to
236 * update line numbers for error messages and debug information...
237 * unless we are nested inside an actual .nolist macro.
238 */
239enum nolist_flags {
240 NL_LIST = 1, /* Suppress list output */
241 NL_LINE = 2 /* Don't update line information */
242};
243
244/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800245 * Store the definition of a multi-line macro. This is also used to
246 * store the interiors of `%rep...%endrep' blocks, which are
247 * effectively self-re-invoking multi-line macros which simply
248 * don't have a name or bother to appear in the hash tables. %rep
249 * blocks are signified by having a NULL `name' field.
250 *
251 * In a MMacro describing a `%rep' block, the `in_progress' field
252 * isn't merely boolean, but gives the number of repeats left to
253 * run.
254 *
255 * The `next' field is used for storing MMacros in hash tables; the
256 * `next_active' field is for stacking them on istk entries.
257 *
258 * When a MMacro is being expanded, `params', `iline', `nparam',
259 * `paramlen', `rotate' and `unique' are local to the invocation.
260 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700261
262/*
263 * Expansion stack. Note that .mmac can point back to the macro itself,
264 * whereas .mstk cannot.
265 */
266struct mstk {
267 MMacro *mstk; /* Any expansion, real macro or not */
268 MMacro *mmac; /* Highest level actual mmacro */
269};
270
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800271struct MMacro {
272 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700273#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700275#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800276 char *name;
277 int nparam_min, nparam_max;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700278 enum nolist_flags nolist; /* is this macro listing-inhibited? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800279 bool casesense;
280 bool plus; /* is the last parameter greedy? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700281 bool capture_label; /* macro definition has %00; capture label */
282 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800283 int32_t max_depth; /* maximum number of recursive expansions allowed */
284 Token *dlist; /* All defaults as one list */
285 Token **defaults; /* Parameter default pointers */
286 int ndefs; /* number of default parameters */
287 Line *expansion;
288
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700289 struct mstk mstk; /* Macro expansion stack */
290 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800291 Token **params; /* actual parameters */
292 Token *iline; /* invocation line */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700293 struct src_location where; /* location of definition */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800294 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700295 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800296 int *paramlen;
297 uint64_t unique;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800298 uint64_t condcnt; /* number of if blocks... */
299};
300
301
302/* Store the definition of a multi-line macro, as defined in a
303 * previous recursive macro expansion.
304 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700305#if 0
306
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800307struct MMacroInvocation {
308 MMacroInvocation *prev; /* previous invocation */
309 Token **params; /* actual parameters */
310 Token *iline; /* invocation line */
311 unsigned int nparam, rotate;
312 int *paramlen;
313 uint64_t unique;
314 uint64_t condcnt;
315};
316
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700317#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800318
319/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320 * The context stack is composed of a linked list of these.
321 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000322struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800323 Context *next;
H. Peter Anvin8571f062019-09-23 16:40:03 -0700324 const char *name;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800325 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700326 uint64_t number;
327 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328};
329
H. Peter Anvin8571f062019-09-23 16:40:03 -0700330
331static inline const char *tok_text(const struct Token *t)
332{
333 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
334}
335
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700337 * Returns a mutable pointer to the text buffer. The text can be changed,
338 * but the length MUST NOT CHANGE, in either direction; nor is it permitted
339 * to pad with null characters to create an artificially shorter string.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700341static inline char *tok_text_buf(struct Token *t)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800342{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700343 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800344}
345
H. Peter Anvin8571f062019-09-23 16:40:03 -0700346static inline unsigned int tok_check_len(size_t len)
347{
348 if (unlikely(len > MAX_TEXT))
349 nasm_fatal("impossibly large token");
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400350
H. Peter Anvin8571f062019-09-23 16:40:03 -0700351 return len;
352}
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400353
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700354static inline bool tok_text_match(const struct Token *a, const struct Token *b)
355{
356 return a->len == b->len && !memcmp(tok_text(a), tok_text(b), a->len);
357}
358
H. Peter Anvin (Intel)b8777082020-07-01 20:49:04 -0700359static inline unused_func bool
H. Peter Anvin (Intel)65ab3ab2020-06-30 10:14:21 -0700360tok_match(const struct Token *a, const struct Token *b)
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700361{
362 return a->type == b->type && tok_text_match(a, b);
363}
364
H. Peter Anvin8571f062019-09-23 16:40:03 -0700365/* strlen() variant useful for set_text() and its variants */
366static size_t tok_strlen(const char *str)
367{
368 return strnlen(str, MAX_TEXT+1);
369}
370
371/*
372 * Set the text field to a copy of the given string; the length if
373 * not given should be obtained with tok_strlen().
374 */
375static Token *set_text(struct Token *t, const char *text, size_t len)
376{
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700377 char *textp;
378
H. Peter Anvin8571f062019-09-23 16:40:03 -0700379 if (t->len > INLINE_TEXT)
380 nasm_free(t->text.p.ptr);
381
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700382 nasm_zero(t->text);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700383
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700384 t->len = len = tok_check_len(len);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700385 textp = (len > INLINE_TEXT)
386 ? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
387 memcpy(textp, text, len);
388 textp[len] = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -0700389 return t;
390}
391
392/*
393 * Set the text field to the existing pre-allocated string, either
394 * taking over or freeing the allocation in the process.
395 */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700396static Token *set_text_free(struct Token *t, char *text, unsigned int len)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700397{
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700398 char *textp;
399
H. Peter Anvin8571f062019-09-23 16:40:03 -0700400 if (t->len > INLINE_TEXT)
401 nasm_free(t->text.p.ptr);
402
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700403 nasm_zero(t->text);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700404
H. Peter Anvin (Intel)00335e42020-06-14 19:49:19 -0700405 t->len = len = tok_check_len(len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700406 if (len > INLINE_TEXT) {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700407 textp = t->text.p.ptr = text;
H. Peter Anvin8571f062019-09-23 16:40:03 -0700408 } else {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700409 textp = memcpy(t->text.a, text, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700410 nasm_free(text);
411 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700412 textp[len] = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -0700413
414 return t;
415}
416
417/*
418 * Allocate a new buffer containing a copy of the text field
419 * of the token.
420 */
421static char *dup_text(const struct Token *t)
422{
423 size_t size = t->len + 1;
424 char *p = nasm_malloc(size);
425
426 return memcpy(p, tok_text(t), size);
427}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428
429/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800430 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000431 * these, which is essentially a container to allow several linked
432 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700433 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434 * Note that in this module, linked lists are treated as stacks
435 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800436 * `expansion' field in MMacro structures, so that the linked list,
437 * if walked, would give the macro lines in reverse order; this
438 * means that we can walk the list when expanding a macro, and thus
439 * push the lines on to the `expansion' field in _istk_ in reverse
440 * order (so that when popped back off they are in the right
441 * order). It may seem cockeyed, and it relies on my design having
442 * an even number of steps in, but it works...
443 *
444 * Some of these structures, rather than being actual lines, are
445 * markers delimiting the end of the expansion of a given macro.
446 * This is for use in the cycle-tracking and %rep-handling code.
447 * Such structures have `finishes' non-NULL, and `first' NULL. All
448 * others have `finishes' NULL, but `first' may still be NULL if
449 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000450 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800452 Line *next;
453 MMacro *finishes;
454 Token *first;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700455 struct src_location where; /* Where defined */
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500456};
457
458/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000459 * To handle an arbitrary level of file inclusion, we maintain a
460 * stack (ie linked list) of these things.
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700461 *
462 * Note: when we issue a message for a continuation line, we want to
463 * issue it for the actual *start* of the continuation line. This means
464 * we need to remember how many lines to skip over for the next one.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000465 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800467 Include *next;
468 FILE *fp;
469 Cond *conds;
470 Line *expansion;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700471 uint64_t nolist; /* Listing inhibit counter */
472 uint64_t noline; /* Line number update inhibit counter */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700473 struct mstk mstk;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700474 struct src_location where; /* Filename and current line number */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -0700475 int32_t lineinc; /* Increment given by %line */
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -0700476 int32_t lineskip; /* Accounting for passed continuation lines */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000477};
478
479/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700480 * File real name hash, so we don't have to re-search the include
481 * path for every pass (and potentially more than that if a file
482 * is used more than once.)
483 */
484struct hash_table FileHash;
485
486/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700487 * Counters to trap on insane macro recursion or processing.
488 * Note: for smacros these count *down*, for mmacros they count *up*.
489 */
490struct deadman {
491 int64_t total; /* Total number of macros/tokens */
492 int64_t levels; /* Descent depth across all macros */
493 bool triggered; /* Already triggered, no need for error msg */
494};
495
496static struct deadman smacro_deadman, mmacro_deadman;
497
498/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000499 * Conditional assembly: we maintain a separate stack of these for
500 * each level of file inclusion. (The only reason we keep the
501 * stacks separate is to ensure that a stray `%endif' in a file
502 * included from within the true branch of a `%if' won't terminate
503 * it and cause confusion: instead, rightly, it'll cause an error.)
504 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700505enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000506 /*
507 * These states are for use just after %if or %elif: IF_TRUE
508 * means the condition has evaluated to truth so we are
509 * currently emitting, whereas IF_FALSE means we are not
510 * currently emitting but will start doing so if a %else comes
511 * up. In these states, all directives are admissible: %elif,
512 * %else and %endif. (And of course %if.)
513 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800514 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000515 /*
516 * These states come up after a %else: ELSE_TRUE means we're
517 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
518 * any %elif or %else will cause an error.
519 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800520 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000521 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200522 * These states mean that we're not emitting now, and also that
523 * nothing until %endif will be emitted at all. COND_DONE is
524 * used when we've had our moment of emission
525 * and have now started seeing %elifs. COND_NEVER is used when
526 * the condition construct in question is contained within a
527 * non-emitting branch of a larger condition construct,
528 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000529 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800530 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000531};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700532struct Cond {
533 Cond *next;
534 enum cond_state state;
535};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800536#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000537
H. Peter Anvin70653092007-10-19 14:42:29 -0700538/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000539 * These defines are used as the possible return values for do_directive
540 */
541#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300542#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000543
Keith Kanios852f1ee2009-07-12 00:19:55 -0500544/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000545 * Condition codes. Note that we use c_ prefix not C_ because C_ is
546 * used in nasm.h for the "real" condition codes. At _this_ level,
547 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
548 * ones, so we need a different enum...
549 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700550static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000551 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
552 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000553 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000554};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700555enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000556 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
557 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700558 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
559 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000560};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700561static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000562 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
563 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000564 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000565};
566
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800567/*
568 * Directive names.
569 */
570/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
571static int is_condition(enum preproc_token arg)
572{
573 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
574}
575
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000576/* For TASM compatibility we need to be able to recognise TASM compatible
577 * conditional compilation directives. Using the NASM pre-processor does
578 * not work, so we look for them specifically from the following list and
579 * then jam in the equivalent NASM directive into the input stream.
580 */
581
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000583 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
584 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
585};
586
H. Peter Anvin476d2862007-10-02 22:04:15 -0700587static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000588 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
589 "ifndef", "include", "local"
590};
591
592static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700593static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000594static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800595static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000596
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000597static Context *cstk;
598static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300599static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000600
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300601static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300603static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000604
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800605static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700606static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800607static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000608
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000609/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800610 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800612static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000613
614/*
615 * The current set of single-line macros we have defined.
616 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700617static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000618
619/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800620 * The multi-line macro we are currently defining, or the %rep
621 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000622 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800623static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624
Charles Crayned4200be2008-07-12 16:42:33 -0700625static uint64_t nested_mac_count;
626static uint64_t nested_rep_count;
627
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000628/*
629 * The number of macro parameters to allocate space for at a time.
630 */
631#define PARAM_DELTA 16
632
633/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700634 * The standard macro set: defined in macros.c in a set of arrays.
635 * This gives our position in any macro set, while we are processing it.
636 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000637 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700638static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700639static macros_t **stdmacnext;
640static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300641static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000642
643/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700644 * Map of which %use packages have been loaded
645 */
646static bool *use_loaded;
647
648/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000649 * Forward declarations.
650 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700651static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000652static Token *expand_mmac_params(Token * tline);
653static Token *expand_smacro(Token * tline);
654static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400655static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700656static Token *make_tok_num(Token *next, int64_t val);
657static Token *make_tok_qstr(Token *next, const char *str);
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -0700658static Token *make_tok_qstr_len(Token *next, const char *str, size_t len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700659static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700660static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700661 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700662static Token *new_Token_free(Token * next, enum pp_token_type type,
663 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700664static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700665static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700666static Token *delete_Token(Token *t);
667static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700668static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700669get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -0700670static void mark_smac_params(Token *tline, const SMacro *tmpl,
671 enum pp_token_type type);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000672
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700673/* Safe test for token type, false on x == NULL */
674static inline bool tok_type(const Token *x, enum pp_token_type t)
675{
676 return x && x->type == t;
677}
678
679/* Whitespace token? */
680static inline bool tok_white(const Token *x)
681{
682 return tok_type(x, TOK_WHITESPACE);
683}
684
685/* Skip past any whitespace */
686static inline Token *skip_white(Token *x)
687{
688 while (tok_white(x))
689 x = x->next;
690
691 return x;
692}
693
694/* Delete any whitespace */
695static Token *zap_white(Token *x)
696{
697 while (tok_white(x))
698 x = delete_Token(x);
699
700 return x;
701}
702
H. Peter Anvin8571f062019-09-23 16:40:03 -0700703/*
704 * Single special character tests. The use of & rather than && is intentional; it
705 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
706 * a smart compiler should turn it into a 16-bit memory reference.
707 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700708static inline bool tok_is(const Token *x, char c)
709{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700710 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700711}
712
713/* True if any other kind of token that "c", but not NULL */
714static inline bool tok_isnt(const Token *x, char c)
715{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700716 return x && !((x->text.a[0] == c) & !x->text.a[1]);
717}
718
719/*
720 * Unquote a token if it is a string, and set its type to
721 * TOK_INTERNAL_STRING.
722 */
723static const char *unquote_token(Token *t)
724{
725 if (t->type != TOK_STRING)
726 return tok_text(t);
727
728 t->type = TOK_INTERNAL_STRING;
729
730 if (t->len > INLINE_TEXT) {
731 char *p = t->text.p.ptr;
732
733 t->len = nasm_unquote(p, NULL);
734
735 if (t->len <= INLINE_TEXT) {
736 nasm_zero(t->text.a);
737 memcpy(t->text.a, p, t->len);
738 nasm_free(p);
739 return t->text.a;
740 } else {
741 return p;
742 }
743 } else {
744 t->len = nasm_unquote(t->text.a, NULL);
745 return t->text.a;
746 }
747}
748
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700749/*
750 * Same as unquote_token(), but error out if the resulting string
751 * contains unacceptable control characters.
752 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700753static const char *unquote_token_cstr(Token *t)
754{
755 if (t->type != TOK_STRING)
756 return tok_text(t);
757
758 t->type = TOK_INTERNAL_STRING;
759
760 if (t->len > INLINE_TEXT) {
761 char *p = t->text.p.ptr;
762
763 t->len = nasm_unquote_cstr(p, NULL);
764
765 if (t->len <= INLINE_TEXT) {
766 nasm_zero(t->text.a);
767 memcpy(t->text.a, p, t->len);
768 nasm_free(p);
769 return t->text.a;
770 } else {
771 return p;
772 }
773 } else {
774 t->len = nasm_unquote_cstr(t->text.a, NULL);
775 return t->text.a;
776 }
777}
778
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700779/*
780 * Convert a TOK_INTERNAL_STRING token to a quoted
781 * TOK_STRING tokens.
782 */
783static Token *quote_any_token(Token *t);
H. Peter Anvin (Intel)b8777082020-07-01 20:49:04 -0700784static inline unused_func
H. Peter Anvin (Intel)65ab3ab2020-06-30 10:14:21 -0700785Token *quote_token(Token *t)
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700786{
787 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
788 return t;
789
790 return quote_any_token(t);
791}
792
793/*
794 * Convert *any* kind of token to a quoted
795 * TOK_STRING token.
796 */
797static Token *quote_any_token(Token *t)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700798{
799 size_t len;
800 char *p;
801
802 p = nasm_quote(tok_text(t), &len);
803 t->type = TOK_STRING;
804 return set_text_free(t, p, len);
805}
806
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400807/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700808 * In-place reverse a list of tokens.
809 */
810static Token *reverse_tokens(Token *t)
811{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800812 Token *prev = NULL;
813 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700814
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800815 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400816 next = t->next;
817 t->next = prev;
818 prev = t;
819 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800820 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700821
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800822 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700823}
824
825/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700826 * getenv() variant operating on an input token
827 */
828static const char *pp_getenv(const Token *t, bool warn)
829{
830 const char *txt = tok_text(t);
831 const char *v;
832 char *buf = NULL;
833 bool is_string = false;
834
835 if (!t)
836 return NULL;
837
838 switch (t->type) {
839 case TOK_ENVIRON:
840 txt += 2; /* Skip leading %! */
841 is_string = nasm_isquote(*txt);
842 break;
843
844 case TOK_STRING:
845 is_string = true;
846 break;
847
848 case TOK_INTERNAL_STRING:
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700849 case TOK_NAKED_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -0700850 case TOK_ID:
851 is_string = false;
852 break;
853
854 default:
855 return NULL;
856 }
857
858 if (is_string) {
859 buf = nasm_strdup(txt);
860 nasm_unquote_cstr(buf, NULL);
861 txt = buf;
862 }
863
864 v = getenv(txt);
865 if (warn && !v) {
866 /*!
867 *!environment [on] nonexistent environment variable
868 *! warns if a nonexistent environment variable
869 *! is accessed using the \c{%!} preprocessor
870 *! construct (see \k{getenv}.) Such environment
871 *! variables are treated as empty (with this
872 *! warning issued) starting in NASM 2.15;
873 *! earlier versions of NASM would treat this as
874 *! an error.
875 */
876 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
877 v = "";
878 }
879
880 if (buf)
881 nasm_free(buf);
882
883 return v;
884}
885
886/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300887 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000888 * front of them. We do it here because I could not find any other
889 * place to do it for the moment, and it is a hack (ideally it would
890 * be nice to be able to use the NASM pre-processor to do it).
891 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000892static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000893{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000894 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400895 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000896
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400897 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000898
899 /* Binary search for the directive name */
900 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400901 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400902 q = nasm_skip_word(p);
903 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000904 if (len) {
905 oldchar = p[len];
906 p[len] = 0;
907 while (j - i > 1) {
908 k = (j + i) / 2;
909 m = nasm_stricmp(p, tasm_directives[k]);
910 if (m == 0) {
911 /* We have found a directive, so jam a % in front of it
912 * so that NASM will then recognise it as one if it's own.
913 */
914 p[len] = oldchar;
915 len = strlen(p);
916 oldline = line;
917 line = nasm_malloc(len + 2);
918 line[0] = '%';
919 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700920 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300921 * NASM does not recognise IFDIFI, so we convert
922 * it to %if 0. This is not used in NASM
923 * compatible code, but does need to parse for the
924 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700926 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 } else {
928 memcpy(line + 1, p, len + 1);
929 }
930 nasm_free(oldline);
931 return line;
932 } else if (m < 0) {
933 j = k;
934 } else
935 i = k;
936 }
937 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000938 }
939 return line;
940}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000941
H. Peter Anvin76690a12002-04-30 20:52:49 +0000942/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000943 * The pre-preprocessing stage... This function translates line
944 * number indications as they emerge from GNU cpp (`# lineno "file"
945 * flags') into NASM preprocessor line number indications (`%line
946 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000948static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000949{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000950 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000951 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000952
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 if (line[0] == '#' && line[1] == ' ') {
954 oldline = line;
955 fname = oldline + 2;
956 lineno = atoi(fname);
957 fname += strspn(fname, "0123456789 ");
958 if (*fname == '"')
959 fname++;
960 fnlen = strcspn(fname, "\"");
961 line = nasm_malloc(20 + fnlen);
962 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
963 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000964 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000965 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000967 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000968}
969
970/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000971 * Free a linked list of tokens.
972 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000974{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400975 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000977}
978
979/*
980 * Free a linked list of lines.
981 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000983{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400984 Line *l, *tmp;
985 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 free_tlist(l->first);
987 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000988 }
989}
990
991/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700992 * Free an array of linked lists of tokens
993 */
994static void free_tlist_array(Token **array, size_t nlists)
995{
996 Token **listp = array;
997
998 while (nlists--)
999 free_tlist(*listp++);
1000
1001 nasm_free(array);
1002}
1003
1004/*
1005 * Duplicate a linked list of tokens.
1006 */
1007static Token *dup_tlist(const Token *list, Token ***tailp)
1008{
1009 Token *newlist = NULL;
1010 Token **tailpp = &newlist;
1011 const Token *t;
1012
1013 list_for_each(t, list) {
1014 Token *nt;
1015 *tailpp = nt = dup_Token(NULL, t);
1016 tailpp = &nt->next;
1017 }
1018
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001019 if (tailp) {
1020 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001021 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001022 }
1023
1024 return newlist;
1025}
1026
1027/*
1028 * Duplicate a linked list of tokens with a maximum count
1029 */
1030static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
1031{
1032 Token *newlist = NULL;
1033 Token **tailpp = &newlist;
1034 const Token *t;
1035
1036 list_for_each(t, list) {
1037 Token *nt;
1038 if (!cnt--)
1039 break;
1040 *tailpp = nt = dup_Token(NULL, t);
1041 tailpp = &nt->next;
1042 }
1043
1044 if (tailp) {
1045 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001046 if (newlist)
1047 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001048 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001049
1050 return newlist;
1051}
1052
1053/*
1054 * Duplicate a linked list of tokens in reverse order
1055 */
1056static Token *dup_tlist_reverse(const Token *list, Token *tail)
1057{
1058 const Token *t;
1059
1060 list_for_each(t, list)
1061 tail = dup_Token(tail, t);
1062
1063 return tail;
1064}
1065
1066/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001067 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001068 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001069static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001070{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001071 nasm_free(m->name);
1072 free_tlist(m->dlist);
1073 nasm_free(m->defaults);
1074 free_llist(m->expansion);
1075 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001076}
1077
1078/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001079 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001080 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001081static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001082{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001083 if (s->params) {
1084 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001085 for (i = 0; i < s->nparam; i++) {
1086 if (s->params[i].name.len > INLINE_TEXT)
1087 nasm_free(s->params[i].name.text.p.ptr);
1088 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001089 nasm_free(s->params);
1090 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001091 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001092 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001093}
1094
1095static void clear_smacro(SMacro *s)
1096{
1097 free_smacro_members(s);
1098 /* Wipe everything except the next pointer */
1099 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1100}
1101
1102/*
1103 * Free an SMacro
1104 */
1105static void free_smacro(SMacro *s)
1106{
1107 free_smacro_members(s);
1108 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001109}
1110
1111/*
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001112 * Free all currently defined macros, and free the hash tables if empty
H. Peter Anvin97a23472007-09-16 17:57:25 -07001113 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001114enum clear_what {
1115 CLEAR_NONE = 0,
1116 CLEAR_DEFINE = 1, /* Clear smacros */
1117 CLEAR_DEFALIAS = 2, /* Clear smacro aliases */
1118 CLEAR_ALLDEFINE = CLEAR_DEFINE|CLEAR_DEFALIAS,
1119 CLEAR_MMACRO = 4,
1120 CLEAR_ALL = CLEAR_ALLDEFINE|CLEAR_MMACRO
1121};
1122
1123static void clear_smacro_table(struct hash_table *smt, enum clear_what what)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001124{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001125 struct hash_iterator it;
1126 const struct hash_node *np;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001127 bool empty = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001128
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001129 /*
1130 * Walk the hash table and clear out anything we don't want
1131 */
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001132 hash_for_each(smt, it, np) {
1133 SMacro *tmp;
1134 SMacro *s = np->data;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001135 SMacro **head = (SMacro **)&np->data;
1136
1137 list_for_each_safe(s, tmp, s) {
1138 if (what & ((enum clear_what)s->alias + 1)) {
1139 *head = s->next;
1140 free_smacro(s);
1141 } else {
1142 empty = false;
1143 }
1144 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001145 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001146
1147 /*
1148 * Free the hash table and keys if and only if it is now empty.
1149 * Note: we cannot free keys even for an empty list above, as that
1150 * mucks up the hash algorithm.
1151 */
1152 if (empty)
1153 hash_free_all(smt, true);
1154}
1155
1156static void free_smacro_table(struct hash_table *smt)
1157{
1158 clear_smacro_table(smt, CLEAR_ALLDEFINE);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001159}
1160
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001161static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001162{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001163 struct hash_iterator it;
1164 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001165
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001166 hash_for_each(mmt, it, np) {
1167 MMacro *tmp;
1168 MMacro *m = np->data;
1169 nasm_free((void *)np->key);
1170 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001171 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001172 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001173 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001174}
1175
1176static void free_macros(void)
1177{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001178 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001179 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001180}
1181
1182/*
1183 * Initialize the hash tables
1184 */
1185static void init_macros(void)
1186{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001187}
1188
1189/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001190 * Pop the context stack.
1191 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001193{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001194 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001195
1196 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001197 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001198 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001199 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001200}
1201
H. Peter Anvin072771e2008-05-22 13:17:51 -07001202/*
1203 * Search for a key in the hash index; adding it if necessary
1204 * (in which case we initialize the data pointer to NULL.)
1205 */
1206static void **
1207hash_findi_add(struct hash_table *hash, const char *str)
1208{
1209 struct hash_insert hi;
1210 void **r;
1211 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001212 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001213
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001214 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001215 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001216 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001217
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001218 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1219 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001220 return hash_add(&hi, strx, NULL);
1221}
1222
1223/*
1224 * Like hash_findi, but returns the data element rather than a pointer
1225 * to it. Used only when not adding a new element, hence no third
1226 * argument.
1227 */
1228static void *
1229hash_findix(struct hash_table *hash, const char *str)
1230{
1231 void **p;
1232
1233 p = hash_findi(hash, str, NULL);
1234 return p ? *p : NULL;
1235}
1236
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001237/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001238 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001239 * if there no more left -- return NULL
1240 */
1241static char *line_from_stdmac(void)
1242{
1243 unsigned char c;
1244 const unsigned char *p = stdmacpos;
1245 char *line, *q;
1246 size_t len = 0;
1247
1248 if (!stdmacpos)
1249 return NULL;
1250
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001251 /*
1252 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1253 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1254 */
1255 while ((c = *p++) != 127) {
1256 uint8_t ndir = c - 128;
1257 if (ndir < 256-96)
1258 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001259 else
1260 len++;
1261 }
1262
1263 line = nasm_malloc(len + 1);
1264 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001265
1266 while ((c = *stdmacpos++) != 127) {
1267 uint8_t ndir = c - 128;
1268 if (ndir < 256-96) {
1269 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1270 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001271 *q++ = ' ';
1272 } else {
1273 *q++ = c;
1274 }
1275 }
1276 stdmacpos = p;
1277 *q = '\0';
1278
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001279 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001280 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001281 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001282 if (*stdmacnext) {
1283 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001284 } else if (do_predef) {
1285 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001286
1287 /*
1288 * Nasty hack: here we push the contents of
1289 * `predef' on to the top-level expansion stack,
1290 * since this is the most convenient way to
1291 * implement the pre-include and pre-define
1292 * features.
1293 */
1294 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001295 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001296 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001297 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001298 l->finishes = NULL;
1299
1300 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001301 }
1302 do_predef = false;
1303 }
1304 }
1305
1306 return line;
1307}
1308
H. Peter Anvin6686de22019-08-10 05:33:14 -07001309/*
1310 * Read a line from a file. Return NULL on end of file.
1311 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001312static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001313{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001314 int c;
1315 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001316 const unsigned int delta = 512;
1317 const unsigned int pad = 8;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001318 bool cont = false;
1319 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001320
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001321 istk->where.lineno += istk->lineskip + istk->lineinc;
1322 src_set_linnum(istk->where.lineno);
1323 istk->lineskip = 0;
1324
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001325 size = delta;
1326 p = buffer = nasm_malloc(size);
1327
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001328 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001329 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001330
1331 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001332 case EOF:
1333 if (p == buffer) {
1334 nasm_free(buffer);
1335 return NULL;
1336 }
1337 c = 0;
1338 break;
1339
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001340 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001341 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001342 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001343 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001344 if (cont) {
1345 cont = false;
1346 continue;
1347 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001348 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001349 break;
1350
1351 case '\n':
1352 if (cont) {
1353 cont = false;
1354 continue;
1355 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001356 c = 0;
1357 break;
1358
1359 case 032: /* ^Z = legacy MS-DOS end of file mark */
1360 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001361 break;
1362
1363 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001364 next = fgetc(f);
1365 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001366 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001367 cont = true;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001368 istk->lineskip += istk->lineinc;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001369 continue;
1370 }
1371 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001373
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001374 if (p >= (buffer + size - pad)) {
1375 buffer = nasm_realloc(buffer, size + delta);
1376 p = buffer + size - pad;
1377 size += delta;
1378 }
1379
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001380 *p++ = c;
1381 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001382
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001383 return buffer;
1384}
1385
1386/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001387 * Common read routine regardless of source
1388 */
1389static char *read_line(void)
1390{
1391 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001392 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001393
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001394 if (f)
1395 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001396 else
1397 line = line_from_stdmac();
1398
1399 if (!line)
1400 return NULL;
1401
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001402 if (!istk->nolist)
1403 lfmt->line(LIST_READ, istk->where.lineno, line);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001404
1405 return line;
1406}
1407
1408/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001409 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001410 * don't need to parse the value out of e.g. numeric tokens: we
1411 * simply split one string into many.
1412 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001413static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001414{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001415 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001416 Token *list = NULL;
1417 Token *t, **tail = &list;
1418
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001420 const char *p = line;
1421 const char *ep = NULL; /* End of token, for trimming the end */
1422 size_t toklen;
1423 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001424
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001426 /*
1427 * Preprocessor construct; find the end of the token.
1428 * Classification is handled later, because %{...} can be
1429 * used to create any preprocessor token.
1430 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001432 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001433 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001434 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001435 } else if (nasm_isdigit(*p) ||
1436 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 do {
1438 p++;
1439 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001440 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001441 } else if (*p == '{' || *p == '[') {
1442 /* %{...} or %[...] */
1443 char firstchar = *p;
1444 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001445 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001446 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1447 while (lvl) {
1448 if (*p == firstchar) {
1449 lvl++;
1450 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001451 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001452 } else if (nasm_isquote(*p)) {
1453 p = nasm_skip_string(p);
1454 }
1455
1456 /*
1457 * *p can have been advanced to a null character by
1458 * nasm_skip_string()
1459 */
1460 if (!*p) {
1461 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1462 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001463 break;
1464 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001465 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001466 }
1467 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001468 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001469 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001470 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001471 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001472 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001473 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001474 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001475 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001476 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001477 do {
1478 p++;
1479 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001480 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001481 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001482 p = nasm_skip_string(p);
1483 if (*p)
1484 p++;
1485 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001486 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001487 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001488 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001489 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001490 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001491 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001492 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001493 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001494 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1495 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 do {
1497 p++;
1498 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001499 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001500 } else if (*p == '%') {
1501 /* %% operator */
1502 p++;
1503 }
1504
1505 if (!ep)
1506 ep = p;
1507 toklen = ep - line;
1508
1509 /* Classify here, to handle %{...} correctly */
1510 if (toklen < 2) {
1511 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001513 char c0 = line[1];
1514
1515 switch (c0) {
1516 case '+':
1517 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1518 break;
1519
1520 case '-':
1521 type = TOK_MMACRO_PARAM;
1522 break;
1523
1524 case '?':
1525 if (toklen == 2)
1526 type = TOK_PREPROC_Q;
1527 else if (toklen == 3 && line[2] == '?')
1528 type = TOK_PREPROC_QQ;
1529 else
1530 type = TOK_PREPROC_ID;
1531 break;
1532
1533 case '!':
1534 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1535 break;
1536
1537 case '%':
1538 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1539 break;
1540
1541 case '$':
1542 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1543 break;
1544
1545 case '[':
1546 line += 2; /* Skip %[ */
1547 firstchar = *line; /* Don't clobber */
1548 toklen -= 2;
1549 type = TOK_INDIRECT;
1550 break;
1551
1552 case ',':
1553 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1554 break;
1555
1556 case '\'':
1557 case '\"':
1558 case '`':
1559 /* %{'string'} */
1560 type = TOK_PREPROC_ID;
1561 break;
1562
1563 case ':':
1564 type = TOK_MMACRO_PARAM; /* %{:..} */
1565 break;
1566
1567 default:
1568 if (nasm_isdigit(c0))
1569 type = TOK_MMACRO_PARAM;
1570 else if (nasm_isidchar(c0) || toklen > 2)
1571 type = TOK_PREPROC_ID;
1572 else
1573 type = TOK_OTHER;
1574 break;
1575 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001577 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001578 /*
1579 * An identifier. This includes the ? operator, which is
1580 * treated as a keyword, not as a special character
1581 * operator
1582 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001584 while (nasm_isidchar(*++p))
1585 ;
1586 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 /*
1588 * A string token.
1589 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001591 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001592
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 if (*p) {
1594 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001595 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001596 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 /* Handling unterminated strings by UNV */
1598 /* type = -1; */
1599 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001600 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001601 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001602 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001603 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001604 bool is_hex = false;
1605 bool is_float = false;
1606 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001607 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001608
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001610 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001612
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001613 if (*p == '$') {
1614 p++;
1615 is_hex = true;
1616 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001617
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001618 for (;;) {
1619 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001620
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001621 if (!is_hex && (c == 'e' || c == 'E')) {
1622 has_e = true;
1623 if (*p == '+' || *p == '-') {
1624 /*
1625 * e can only be followed by +/- if it is either a
1626 * prefixed hex number or a floating-point number
1627 */
1628 p++;
1629 is_float = true;
1630 }
1631 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1632 is_hex = true;
1633 } else if (c == 'P' || c == 'p') {
1634 is_float = true;
1635 if (*p == '+' || *p == '-')
1636 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001637 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001638 ; /* just advance */
1639 else if (c == '.') {
1640 /*
1641 * we need to deal with consequences of the legacy
1642 * parser, like "1.nolist" being two tokens
1643 * (TOK_NUMBER, TOK_ID) here; at least give it
1644 * a shot for now. In the future, we probably need
1645 * a flex-based scanner with proper pattern matching
1646 * to do it as well as it can be done. Nothing in
1647 * the world is going to help the person who wants
1648 * 0x123.p16 interpreted as two tokens, though.
1649 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001650 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001651 while (*r == '_')
1652 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001653
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001654 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1655 (!is_hex && (*r == 'e' || *r == 'E')) ||
1656 (*r == 'p' || *r == 'P')) {
1657 p = r;
1658 is_float = true;
1659 } else
1660 break; /* Terminate the token */
1661 } else
1662 break;
1663 }
1664 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001665
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001666 if (p == line+1 && *line == '$') {
1667 type = TOK_OTHER; /* TOKEN_HERE */
1668 } else {
1669 if (has_e && !is_hex) {
1670 /* 1e13 is floating-point, but 1e13h is not */
1671 is_float = true;
1672 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001673
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001674 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1675 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001676 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001677 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001678 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 /*
1680 * Whitespace just before end-of-line is discarded by
1681 * pretending it's a comment; whitespace just before a
1682 * comment gets lumped into the comment.
1683 */
1684 if (!*p || *p == ';') {
1685 type = TOK_COMMENT;
1686 while (*p)
1687 p++;
1688 }
1689 } else if (*p == ';') {
1690 type = TOK_COMMENT;
1691 while (*p)
1692 p++;
1693 } else {
1694 /*
1695 * Anything else is an operator of some kind. We check
1696 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001697 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1698 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001699 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001700 */
1701 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001702 switch (*p++) {
1703 case '>':
1704 if (*p == '>') {
1705 p++;
1706 if (*p == '>')
1707 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001708 } else if (*p == '=') {
1709 p++;
1710 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001711 break;
1712
1713 case '<':
1714 if (*p == '<') {
1715 p++;
1716 if (*p == '<')
1717 p++;
1718 } else if (*p == '=') {
1719 p++;
1720 if (*p == '>')
1721 p++;
1722 } else if (*p == '>') {
1723 p++;
1724 }
1725 break;
1726
1727 case '!':
1728 if (*p == '=')
1729 p++;
1730 break;
1731
1732 case '/':
1733 case '=':
1734 case '&':
1735 case '|':
1736 case '^':
1737 /* These operators can be doubled but nothing else */
1738 if (*p == p[-1])
1739 p++;
1740 break;
1741
1742 default:
1743 break;
1744 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001746
H. Peter Anvin8571f062019-09-23 16:40:03 -07001747 if (type == TOK_WHITESPACE) {
1748 *tail = t = new_White(NULL);
1749 tail = &t->next;
1750 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001751 if (!ep)
1752 ep = p;
1753 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001754 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 tail = &t->next;
1756 }
1757 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001758 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001759 return list;
1760}
1761
H. Peter Anvin8571f062019-09-23 16:40:03 -07001762/*
1763 * Tokens are allocated in blocks to improve speed. Set the blocksize
1764 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1765 *
1766 * alloc_Token() returns a zero-initialized token structure.
1767 */
1768#define TOKEN_BLOCKSIZE 4096
1769
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001770#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001771
1772static Token *freeTokens = NULL;
1773static Token *tokenblocks = NULL;
1774
1775static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001776{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001777 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778
H. Peter Anvin8571f062019-09-23 16:40:03 -07001779 if (unlikely(!t)) {
1780 Token *block;
1781 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782
H. Peter Anvin8571f062019-09-23 16:40:03 -07001783 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001784
H. Peter Anvin8571f062019-09-23 16:40:03 -07001785 /*
1786 * The first entry in each array are a linked list of
1787 * block allocations and is not used for data.
1788 */
1789 block[0].next = tokenblocks;
1790 block[0].type = TOK_BLOCK;
1791 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001792
H. Peter Anvin8571f062019-09-23 16:40:03 -07001793 /*
1794 * Add the rest to the free list
1795 */
1796 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1797 block[i].next = &block[i+1];
1798
1799 freeTokens = &block[2];
1800
1801 /*
1802 * Return the topmost usable token
1803 */
1804 return &block[1];
1805 }
1806
1807 freeTokens = t->next;
1808 t->next = NULL;
1809 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001810}
1811
H. Peter Anvin8571f062019-09-23 16:40:03 -07001812static Token *delete_Token(Token *t)
1813{
1814 Token *next = t->next;
1815
1816 nasm_zero(*t);
1817 t->next = freeTokens;
1818 freeTokens = t;
1819
1820 return next;
1821}
1822
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001824{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001825 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001826
H. Peter Anvin8571f062019-09-23 16:40:03 -07001827 list_for_each_safe(block, blocktmp, tokenblocks)
1828 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001829
H. Peter Anvin8571f062019-09-23 16:40:03 -07001830 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001832
H. Peter Anvin8571f062019-09-23 16:40:03 -07001833#else
1834
1835static inline Token *alloc_Token(void)
1836{
1837 Token *t;
1838 nasm_new(*t);
1839 return t;
1840}
1841
1842static Token *delete_Token(Token *t)
1843{
1844 Token *next = t->next;
1845 nasm_free(t);
1846 return next;
1847}
1848
1849static inline void delete_Blocks(void)
1850{
1851 /* Nothing to do */
1852}
1853
1854#endif
1855
H. Peter Anvin734b1882002-04-30 21:01:08 +00001856/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001857 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001858 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001859 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001860static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001861 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001862{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001863 Token *t = alloc_Token();
1864 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001865
H. Peter Anvin734b1882002-04-30 21:01:08 +00001866 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001867 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001868 if (type == TOK_WHITESPACE) {
1869 t->len = 1;
1870 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001872 if (text && text[0] && !txtlen)
1873 txtlen = tok_strlen(text);
1874
1875 t->len = tok_check_len(txtlen);
1876
1877 if (text) {
1878 textp = (txtlen > INLINE_TEXT)
1879 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1880 memcpy(textp, text, txtlen);
1881 textp[txtlen] = '\0'; /* In case we needed malloc() */
1882 } else {
1883 /*
1884 * Allocate a buffer but do not fill it. The caller
1885 * can fill in text, but must not change the length.
1886 * The filled in text must be exactly txtlen once
1887 * the buffer is filled and before the token is added
1888 * to any line lists.
1889 */
1890 if (txtlen > INLINE_TEXT)
1891 t->text.p.ptr = nasm_zalloc(txtlen+1);
1892 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001893 }
1894 return t;
1895}
1896
H. Peter Anvin8571f062019-09-23 16:40:03 -07001897/*
1898 * Same as new_Token(), but text belongs to the new token and is
1899 * either taken over or freed. This function MUST be called
1900 * with valid txt and txtlen, unlike new_Token().
1901 */
1902static Token *new_Token_free(Token * next, enum pp_token_type type,
1903 char *text, size_t txtlen)
1904{
1905 Token *t = alloc_Token();
1906
1907 t->next = next;
1908 t->type = type;
1909 t->len = tok_check_len(txtlen);
1910
1911 if (txtlen <= INLINE_TEXT) {
1912 memcpy(t->text.a, text, txtlen);
1913 free(text);
1914 } else {
1915 t->text.p.ptr = text;
1916 }
1917
1918 return t;
1919}
1920
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001921static Token *dup_Token(Token *next, const Token *src)
1922{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001923 Token *t = alloc_Token();
1924
1925 memcpy(t, src, sizeof *src);
1926 t->next = next;
1927
1928 if (t->len > INLINE_TEXT) {
1929 t->text.p.ptr = nasm_malloc(t->len + 1);
1930 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1931 }
1932
1933 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001934}
1935
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001936static Token *new_White(Token *next)
1937{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001938 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001939
H. Peter Anvin8571f062019-09-23 16:40:03 -07001940 t->next = next;
1941 t->type = TOK_WHITESPACE;
1942 t->len = 1;
1943 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001944
H. Peter Anvin8571f062019-09-23 16:40:03 -07001945 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001946}
1947
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001948/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001949 * This *transfers* the content from one token to another, leaving the
1950 * next pointer of the latter intact. Unlike dup_Token(), the old
1951 * token is destroyed, except for its next pointer, and the text
1952 * pointer allocation, if any, is simply transferred.
1953 */
1954static Token *steal_Token(Token *dst, Token *src)
1955{
1956 /* Overwrite everything except the next pointers */
1957 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1958 sizeof(Token) - sizeof(Token *));
1959
1960 /* Clear the donor token */
1961 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1962
1963 return dst;
1964}
1965
1966/*
1967 * Convert a line of tokens back into text. This modifies the list
1968 * by expanding environment variables.
1969 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001970 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001971 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001972 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001973static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001974{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001975 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001976 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001977 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001978
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001979 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001980 switch (t->type) {
1981 case TOK_ENVIRON:
1982 {
1983 const char *v = pp_getenv(t, true);
1984 set_text(t, v, tok_strlen(v));
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07001985 t->type = TOK_NAKED_STRING;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001986 break;
1987 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001988
H. Peter Anvin8571f062019-09-23 16:40:03 -07001989 case TOK_LOCAL_MACRO:
1990 case TOK_LOCAL_SYMBOL:
1991 if (expand_locals) {
1992 const char *q;
1993 char *p;
1994 Context *ctx = get_ctx(tok_text(t), &q);
1995 if (ctx) {
1996 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1997 set_text_free(t, p, nasm_last_string_len());
1998 t->type = TOK_ID;
1999 }
2000 }
2001 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07002002
H. Peter Anvin8571f062019-09-23 16:40:03 -07002003 default:
2004 break; /* No modifications */
2005 }
2006
2007 if (debug_level(2)) {
2008 unsigned int t_len = t->len;
2009 unsigned int s_len = tok_strlen(tok_text(t));
2010 if (t_len != s_len) {
2011 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
2012 tok_text(t), t->type, s_len, t_len);
2013 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002014 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002015 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07002016
H. Peter Anvin8571f062019-09-23 16:40:03 -07002017 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002018 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002019
H. Peter Anvin734b1882002-04-30 21:01:08 +00002020 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002021
H. Peter Anvin8571f062019-09-23 16:40:03 -07002022 list_for_each(t, tlist)
2023 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002024 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002025
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002026 return line;
2027}
2028
2029/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002030 * A scanner, suitable for use by the expression evaluator, which
2031 * operates on a line of Tokens. Expects a pointer to a pointer to
2032 * the first token in the line to be passed in as its private_data
2033 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002034 *
2035 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00002036 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002037struct ppscan {
2038 Token *tptr;
2039 int ntokens;
2040};
2041
H. Peter Anvine2c80182005-01-15 22:15:51 +00002042static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002043{
H. Peter Anvin8b262472019-02-26 14:00:54 -08002044 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002045 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002046 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002047
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08002049 if (pps->ntokens && (tline = pps->tptr)) {
2050 pps->ntokens--;
2051 pps->tptr = tline->next;
2052 } else {
2053 pps->tptr = NULL;
2054 pps->ntokens = 0;
2055 return tokval->t_type = TOKEN_EOS;
2056 }
2057 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002058
H. Peter Anvin8571f062019-09-23 16:40:03 -07002059 txt = tok_text(tline);
2060 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002061
H. Peter Anvin8571f062019-09-23 16:40:03 -07002062 if (txt[0] == '$') {
2063 if (!txt[1]) {
2064 return tokval->t_type = TOKEN_HERE;
2065 } else if (txt[1] == '$' && !txt[2]) {
2066 return tokval->t_type = TOKEN_BASE;
2067 } else if (tline->type == TOK_ID) {
2068 tokval->t_charptr++;
2069 return tokval->t_type = TOKEN_ID;
2070 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002071 }
2072
H. Peter Anvin8571f062019-09-23 16:40:03 -07002073 switch (tline->type) {
2074 default:
2075 if (tline->len == 1)
2076 return tokval->t_type = txt[0];
2077 /* fall through */
2078 case TOK_ID:
2079 return nasm_token_hash(txt, tokval);
2080
2081 case TOK_NUMBER:
2082 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002084 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 if (rn_error)
2086 return tokval->t_type = TOKEN_ERRNUM;
2087 else
2088 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002089 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002090
H. Peter Anvin8571f062019-09-23 16:40:03 -07002091 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002093
2094 case TOK_STRING:
2095 tokval->t_charptr = (char *)unquote_token(tline);
2096 tokval->t_inttwo = tline->len;
2097 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002098 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002099}
2100
2101/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002102 * 1. An expression (true if nonzero 0)
2103 * 2. The keywords true, on, yes for true
2104 * 3. The keywords false, off, no for false
2105 * 4. An empty line, for true
2106 *
2107 * On error, return defval (usually the previous value)
2108 */
2109static bool pp_get_boolean_option(Token *tline, bool defval)
2110{
2111 static const char * const noyes[] = {
2112 "no", "yes",
2113 "false", "true",
2114 "off", "on"
2115 };
2116 struct ppscan pps;
2117 struct tokenval tokval;
2118 expr *evalresult;
2119
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002120 tline = skip_white(tline);
2121 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002122 return true;
2123
2124 if (tline->type == TOK_ID) {
2125 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002126 const char *txt = tok_text(tline);
2127
H. Peter Anvind2354082019-08-27 16:38:48 -07002128 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002129 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002130 return i & 1;
2131 }
2132
2133 pps.tptr = NULL;
2134 pps.tptr = tline;
2135 pps.ntokens = -1;
2136 tokval.t_type = TOKEN_INVALID;
2137 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2138
2139 if (!evalresult)
2140 return true;
2141
2142 if (tokval.t_type)
2143 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2144 if (!is_really_simple(evalresult)) {
2145 nasm_nonfatal("boolean flag expression must be a constant");
2146 return defval;
2147 }
2148
2149 return reloc_value(evalresult) != 0;
2150}
2151
2152/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002153 * Compare a string to the name of an existing macro; this is a
2154 * simple wrapper which calls either strcmp or nasm_stricmp
2155 * depending on the value of the `casesense' parameter.
2156 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002157static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002158{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002159 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002160}
2161
2162/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002163 * Compare a string to the name of an existing macro; this is a
2164 * simple wrapper which calls either strcmp or nasm_stricmp
2165 * depending on the value of the `casesense' parameter.
2166 */
2167static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2168{
2169 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2170}
2171
2172/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002173 * Return the Context structure associated with a %$ token. Return
2174 * NULL, having _already_ reported an error condition, if the
2175 * context stack isn't deep enough for the supplied number of $
2176 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002177 *
2178 * If "namep" is non-NULL, set it to the pointer to the macro name
2179 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002180 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002181static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002182{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002183 Context *ctx;
2184 int i;
2185
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002186 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002187 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002188
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002189 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002190 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002191
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002193 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002195 }
2196
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002197 name += 2;
2198 ctx = cstk;
2199 i = 0;
2200 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002201 name++;
2202 i++;
2203 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002204 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002205 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002206 nasm_nonfatal("`%s': context stack is only"
2207 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002208 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002209 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002210
2211 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002212 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002213
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002214 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002215}
2216
2217/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002218 * Open an include file. This routine must always return a valid
2219 * file pointer if it returns - it's responsible for throwing an
2220 * ERR_FATAL and bombing out completely if not. It should also try
2221 * the include path one by one until it finds the file or reaches
2222 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002223 *
2224 * Note: for INC_PROBE the function returns NULL at all times;
2225 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002226 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002227enum incopen_mode {
2228 INC_NEEDED, /* File must exist */
2229 INC_OPTIONAL, /* Missing is OK */
2230 INC_PROBE /* Only an existence probe */
2231};
2232
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002233/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002234static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002235 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002236{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002237 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002238 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002239 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002240 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002241 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002242
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002244 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002245 if (omode == INC_PROBE) {
2246 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002247 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002248 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002249 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002250 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002251 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002252 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002253 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002254 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002255 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002256
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002257 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002258
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002259 if (!ip) {
2260 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002261 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002262 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002263
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002264 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002265 ip = ip->next;
2266 }
2267}
2268
2269/*
2270 * Open a file, or test for the presence of one (depending on omode),
2271 * considering the include path.
2272 */
2273static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002274 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002275 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002276 enum incopen_mode omode,
2277 enum file_flags fmode)
2278{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002279 struct hash_insert hi;
2280 void **hp;
2281 char *path;
2282 FILE *fp = NULL;
2283
2284 hp = hash_find(&FileHash, file, &hi);
2285 if (hp) {
2286 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002287 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002288 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002289 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002290 } else {
2291 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002292 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002293
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002294 /* Positive or negative result */
2295 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002296
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002297 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002298 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002299 */
2300 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002301 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002302 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002303
H. Peter Anvin (Intel)5d68f982020-06-01 12:32:35 -07002304 if (path && !fp && omode != INC_PROBE)
2305 fp = nasm_open_read(path, fmode);
2306
2307 if (omode == INC_NEEDED && !fp) {
2308 if (!path)
2309 errno = ENOENT;
2310
2311 nasm_nonfatal("unable to open include file `%s': %s",
2312 file, strerror(errno));
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002313 }
2314
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002315 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002316 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002317
2318 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002319}
2320
2321/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002322 * Opens an include or input file. Public version, for use by modules
2323 * that get a file:lineno pair and need to look at the file again
2324 * (e.g. the CodeView debug backend). Returns NULL on failure.
2325 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002326FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002327{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002328 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002329}
2330
2331/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002332 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002333 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002334 * return true if _any_ single-line macro of that name is defined.
2335 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002336 * `nparam' or no parameters is defined.
2337 *
2338 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002339 * defined, or nparam is -1, the address of the definition structure
2340 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002341 * is NULL, no action will be taken regarding its contents, and no
2342 * error will occur.
2343 *
2344 * Note that this is also called with nparam zero to resolve
2345 * `ifdef'.
2346 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002347static bool
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002348smacro_defined(Context *ctx, const char *name, int nparam, SMacro **defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002349 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002350{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002351 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002352 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002353
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002354 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvind2354082019-08-27 16:38:48 -07002355
2356restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002357 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002358
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 while (m) {
2360 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002361 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002362 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002363 if (m->alias && !find_alias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002364 if (!ppopt.noaliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002365 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002366 goto restart;
2367 } else {
2368 continue;
2369 }
2370 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002372 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002374 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 }
2376 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002377 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002378
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002379 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002380}
2381
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002382/* param should be a natural number [0; INT_MAX] */
2383static int read_param_count(const char *str)
2384{
2385 int result;
2386 bool err;
2387
2388 result = readnum(str, &err);
2389 if (result < 0 || result > INT_MAX) {
2390 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002391 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2392 str, 0, INT_MAX);
2393 } else if (err)
2394 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002395 return result;
2396}
2397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002398/*
2399 * Count and mark off the parameters in a multi-line macro call.
2400 * This is called both from within the multi-line macro expansion
2401 * code, and also to mark off the default parameters when provided
2402 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002403 *
2404 * Note that we need space in the params array for parameter 0 being
2405 * a possible captured label as well as the final NULL.
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002406 *
2407 * Returns a pointer to the pointer to a terminal comma if present;
2408 * used to drop an empty terminal argument for legacy reasons.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002409 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002410static Token **count_mmac_params(Token *tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002411{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002412 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002413 int nparam = 0;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002414 Token *t;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002415 Token **comma = NULL, **maybe_comma = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002416 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002417
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002418 paramsize = PARAM_DELTA;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002419 nasm_newn(params, paramsize);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002420
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002421 t = skip_white(tline);
2422 if (t) {
2423 while (true) {
2424 /* Need two slots for captured label and NULL */
2425 if (unlikely(nparam+2 >= paramsize)) {
2426 paramsize += PARAM_DELTA;
2427 params = nasm_realloc(params, sizeof(*params) * paramsize);
2428 }
2429 params[++nparam] = t;
2430 if (tok_is(t, '{')) {
2431 int brace = 1;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002432
2433 comma = NULL; /* Non-empty parameter */
2434
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002435 while (brace && (t = t->next)) {
2436 brace += tok_is(t, '{');
2437 brace -= tok_is(t, '}');
2438 }
H. Peter Anvin (Intel)f8639bd2020-06-04 16:29:53 -07002439
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002440 if (t) {
2441 /*
2442 * Now we've found the closing brace, look further
2443 * for the comma.
2444 */
2445 t = skip_white(t->next);
2446 if (tok_isnt(t, ','))
2447 nasm_nonfatal("braces do not enclose all of macro parameter");
2448 } else {
2449 nasm_nonfatal("expecting closing brace in macro parameter");
2450 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002451 }
2452
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002453 /* Advance to the next comma */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002454 maybe_comma = &t->next;
2455 while (tok_isnt(t, ',')) {
2456 if (!tok_white(t))
2457 comma = NULL; /* Non-empty parameter */
2458 maybe_comma = &t->next;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002459 t = t->next;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002460 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002461
2462 if (!t)
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002463 break; /* End of string, no comma */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002464
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002465 comma = maybe_comma; /* Point to comma pointer */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002466 t = skip_white(t->next); /* Eat the comma and whitespace */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002467 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002468 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002469
2470 params[nparam+1] = NULL;
2471 *paramsp = params;
2472 *nparamp = nparam;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002473
2474 return comma;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002475}
2476
2477/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002478 * Determine whether one of the various `if' conditions is true or
2479 * not.
2480 *
2481 * We must free the tline we get passed.
2482 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002483static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002484{
H. Peter Anvin70055962007-10-11 00:05:31 -07002485 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002486 Token *t, *tt, *origline;
2487 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002488 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002489 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002490 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002491 const char *dname = pp_directives[ct];
2492 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002493 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002494
2495 origline = tline;
2496
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002497 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002498 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002499 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002500 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002501 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002502 if (!tline)
2503 break;
2504 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002505 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002506 dname);
2507 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002509 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002510 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 tline = tline->next;
2512 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002513 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002514
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002515 case PP_IFDEF:
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002516 case PP_IFDEFALIAS:
2517 {
2518 bool alias = cond == PP_IFDEFALIAS;
2519 SMacro *smac;
2520 Context *ctx;
2521 const char *mname;
2522
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002523 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002524 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002525 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002527 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002528 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002529 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002532
2533 mname = tok_text(tline);
2534 ctx = get_ctx(mname, &mname);
H. Peter Anvin (Intel)b91e7732020-06-05 12:22:26 -07002535 if (smacro_defined(ctx, mname, -1, &smac, true, alias) && smac
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002536 && smac->alias == alias) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002537 j = true;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002538 break;
2539 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002541 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 break;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002543 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002544
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002545 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002546 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002547 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002548 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002549 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002550 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002551 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002552 tline->type != TOK_INTERNAL_STRING &&
2553 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002554 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002555 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002556 goto fail;
2557 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002558
2559 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002560 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002561 }
2562 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002563
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002564 case PP_IFIDNI:
2565 casesense = false;
2566 /* fall through */
2567 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002568 tline = expand_smacro(tline);
2569 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002570 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002571 tt = tt->next;
2572 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002573 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002574 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 }
2577 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002578 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002579 while (tok_isnt(t, ',') && tt) {
2580 unsigned int l1, l2;
2581 const char *t1, *t2;
2582
2583 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002584 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002585 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 }
2588 if (t->type == TOK_WHITESPACE) {
2589 t = t->next;
2590 continue;
2591 }
2592 if (tt->type == TOK_WHITESPACE) {
2593 tt = tt->next;
2594 continue;
2595 }
2596 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002597 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002598 break;
2599 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002600
H. Peter Anvin8571f062019-09-23 16:40:03 -07002601 t1 = unquote_token(t);
2602 t2 = unquote_token(tt);
2603 l1 = t->len;
2604 l2 = tt->len;
2605
2606 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2607 j = false;
2608 break;
2609 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002610
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 t = t->next;
2612 tt = tt->next;
2613 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002614 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002615 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002617
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002618 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002619 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002621 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002622
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002623 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002625 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002626 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 goto fail;
2628 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002629 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002630 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002632 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 searching.nparam_max = INT_MAX;
2634 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002635 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002636 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002637 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002638 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002639 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 } else {
2641 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002642 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002644 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002646 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002648 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002649 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002650 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002652 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002653 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002654 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002655 searching.nparam_max = searching.nparam_min;
2656 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 }
2658 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002659 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 tline = tline->next;
2661 searching.plus = true;
2662 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002663 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2664 while (mmac) {
2665 if (!strcmp(mmac->name, searching.name) &&
2666 (mmac->nparam_min <= searching.nparam_max
2667 || searching.plus)
2668 && (searching.nparam_min <= mmac->nparam_max
2669 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 found = true;
2671 break;
2672 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002673 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 }
2675 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002676 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 nasm_free(searching.name);
2678 j = found;
2679 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002680 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002681
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002682 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 needtype = TOK_ID;
2684 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002685 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 needtype = TOK_NUMBER;
2687 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002688 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 needtype = TOK_STRING;
2690 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002691
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692iftype:
2693 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002694
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002695 while (tok_white(t) ||
2696 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002698
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002699 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002701
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002702 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002703 tline = expand_smacro(tline);
2704 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002705
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 j = false;
2707 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002708 t = skip_white(t->next); /* Skip the actual token + whitespace */
2709 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 }
2711 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002712
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002713 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002714 tline = expand_smacro(tline);
2715 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 j = !t; /* Should be empty */
2717 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002718
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002719 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002720 pps.tptr = tline = expand_smacro(tline);
2721 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002723 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 if (!evalresult)
2725 return -1;
2726 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002727 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002729 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002730 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002733 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002734 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002735
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002736 case PP_IFUSING:
2737 case PP_IFUSABLE:
2738 {
2739 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002740 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002741
H. Peter Anvin8571f062019-09-23 16:40:03 -07002742 pkg = get_use_pkg(tline, dname, &name);
2743 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002744 goto fail;
2745
2746 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2747 break;
2748 }
2749
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002751 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002752 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002753 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002754
2755 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002756 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002757
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002758fail:
2759 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002760 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002761}
2762
2763/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002764 * Default smacro expansion routine: just returns a copy of the
2765 * expansion list.
2766 */
2767static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002768smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002769{
2770 (void)params;
2771 (void)nparams;
2772
2773 return dup_tlist(s->expansion, NULL);
2774}
2775
2776/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002777 * Emit a macro defintion or undef to the listing file, if
2778 * desired. This is similar to detoken(), but it handles the reverse
2779 * expansion list, does not expand %! or local variable tokens, and
2780 * does some special handling for macro parameters.
2781 */
2782static void
2783list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2784{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002785 Token *t;
2786 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002787 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002788 char *context_prefix = NULL;
2789 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002790
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002791 namelen = strlen(m->name);
2792 size = namelen + 2; /* Include room for space after name + NUL */
2793
2794 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002795 int context_depth = cstk->depth - ctx->depth + 1;
2796 context_prefix =
2797 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2798 ctx->name ? ctx->name : "",
2799 ctx->number, context_depth, "");
2800
2801 context_len = nasm_last_string_len();
2802 memset(context_prefix + context_len - context_depth,
2803 '$', context_depth);
2804 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002805 }
2806
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002807 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002808 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002809
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002810 if (m->nparam) {
2811 /*
2812 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002813 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002814 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002815 int i;
2816
2817 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002818 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002819 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002820 }
2821
2822 def = nasm_malloc(size);
2823 p = def+size;
2824 *--p = '\0';
2825
2826 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002827 p -= t->len;
2828 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002829 }
2830
2831 *--p = ' ';
2832
2833 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002834 int i;
2835
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002836 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002837 for (i = m->nparam-1; i >= 0; i--) {
2838 enum sparmflags flags = m->params[i].flags;
2839 if (flags & SPARM_GREEDY)
2840 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002841 p -= m->params[i].name.len;
2842 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2843
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002844 if (flags & SPARM_NOSTRIP)
2845 *--p = '!';
2846 if (flags & SPARM_STR)
2847 *--p = '&';
2848 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002849 *--p = '=';
2850 *--p = ',';
2851 }
2852 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002853 }
2854
2855 p -= namelen;
2856 memcpy(p, m->name, namelen);
2857
H. Peter Anvin6686de22019-08-10 05:33:14 -07002858 if (context_prefix) {
2859 p -= context_len;
2860 memcpy(p, context_prefix, context_len);
2861 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002862 }
2863
2864 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002865 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002866}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002867
2868/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002869 * Parse smacro arguments, return argument count. If the tmpl argument
2870 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002871 * *tpp is updated to point to the pointer to the first token after the
2872 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002873 *
2874 * The text values from any argument tokens are "stolen" and the
2875 * corresponding text fields set to NULL.
2876 */
2877static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2878{
2879 int nparam = 0;
2880 enum sparmflags flags;
2881 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002882 bool err, done;
2883 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002884 Token **tn = *tpp;
2885 Token *t = *tn;
2886 Token *name;
2887
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002888 /*
2889 * DO NOT skip whitespace here, or we won't be able to distinguish:
2890 *
2891 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2892 * %define bar(a,b) ; two arguments, empty expansion
2893 *
2894 * This ambiguity was inherited from C.
2895 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002896
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002897 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002898 goto finish;
2899
2900 if (tmpl) {
2901 Token *tx = t;
2902 Token **txpp = &tx;
2903 int sparam;
2904
2905 /* Count parameters first */
2906 sparam = parse_smacro_template(&txpp, NULL);
2907 if (!sparam)
2908 goto finish; /* No parameters, we're done */
2909 nasm_newn(params, sparam);
2910 }
2911
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002912 /* Skip leading paren */
2913 tn = &t->next;
2914 t = *tn;
2915
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002916 name = NULL;
2917 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002918 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002919
2920 while (!done) {
2921 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002922 if (name || flags)
2923 nasm_nonfatal("`)' expected to terminate macro template");
2924 else
2925 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002926 break;
2927 }
2928
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002929 switch (t->type) {
2930 case TOK_ID:
2931 if (name)
2932 goto bad;
2933 name = t;
2934 break;
2935
2936 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002937 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002938 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002939 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002940 case '=':
2941 flags |= SPARM_EVAL;
2942 break;
2943 case '&':
2944 flags |= SPARM_STR;
2945 break;
2946 case '!':
2947 flags |= SPARM_NOSTRIP;
2948 break;
2949 case '+':
2950 flags |= SPARM_GREEDY;
2951 greedy = true;
2952 break;
2953 case ',':
2954 if (greedy)
2955 nasm_nonfatal("greedy parameter must be last");
2956 /* fall through */
2957 case ')':
2958 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002959 if (name)
2960 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002961 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002962 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002963 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002964 name = NULL;
2965 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002966 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002967 break;
2968 default:
2969 goto bad;
2970 }
2971 break;
2972
2973 case TOK_WHITESPACE:
2974 break;
2975
2976 default:
2977 bad:
2978 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002979 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002980 err = true;
2981 }
2982 break;
2983 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002984
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002985 tn = &t->next;
2986 t = *tn;
2987 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002988
2989finish:
2990 while (t && t->type == TOK_WHITESPACE) {
2991 tn = &t->next;
2992 t = t->next;
2993 }
2994 *tpp = tn;
2995 if (tmpl) {
2996 tmpl->nparam = nparam;
2997 tmpl->greedy = greedy;
2998 tmpl->params = params;
2999 }
3000 return nparam;
3001}
3002
3003/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003004 * Common code for defining an smacro. The tmpl argument, if not NULL,
3005 * contains any macro parameters that aren't explicit arguments;
3006 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003007 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003008static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003009 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003010{
3011 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003012 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003013 Context *ctx;
3014 bool defining_alias = false;
3015 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07003016
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003017 if (tmpl) {
3018 defining_alias = tmpl->alias;
3019 nparam = tmpl->nparam;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003020 if (nparam && !defining_alias)
3021 mark_smac_params(expansion, tmpl, 0);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003022 }
3023
3024 while (1) {
3025 ctx = get_ctx(mname, &mname);
3026
H. Peter Anvind2354082019-08-27 16:38:48 -07003027 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003028 /* Create a new macro */
3029 smtbl = ctx ? &ctx->localmac : &smacros;
3030 smhead = (SMacro **) hash_findi_add(smtbl, mname);
3031 nasm_new(smac);
3032 smac->next = *smhead;
3033 *smhead = smac;
3034 break;
3035 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003036 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003037 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003038 /*
3039 * Some instances of the old code considered this a failure,
3040 * some others didn't. What is the right thing to do here?
3041 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003042 goto fail;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003043 } else if (!smac->alias || ppopt.noaliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003044 /*
3045 * We're redefining, so we have to take over an
3046 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08003047 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003048 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003049 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003050 break;
3051 } else if (smac->in_progress) {
3052 nasm_nonfatal("macro alias loop");
3053 goto fail;
3054 } else {
3055 /* It is an alias macro; follow the alias link */
3056 SMacro *s;
3057
3058 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003059 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003060 expansion, tmpl);
3061 smac->in_progress = false;
3062 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003063 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003064 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003065
3066 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003067 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003068 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003069 smac->expand = smacro_expand_default;
3070 if (tmpl) {
3071 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003072 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003073 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003074 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003075 if (tmpl->expand)
3076 smac->expand = tmpl->expand;
3077 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003078 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07003079 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
3080 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003081 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003082 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003083
3084fail:
3085 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003086 if (tmpl)
3087 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003088 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003089}
3090
3091/*
3092 * Undefine an smacro
3093 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003094static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003095{
3096 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003097 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003098 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003099
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003100 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003101 smtbl = ctx ? &ctx->localmac : &smacros;
3102 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003103
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003104 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003105 /*
3106 * We now have a macro name... go hunt for it.
3107 */
3108 sp = smhead;
3109 while ((s = *sp) != NULL) {
3110 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003111 if (s->alias && !undefalias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003112 if (!ppopt.noaliases) {
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003113 if (s->in_progress) {
3114 nasm_nonfatal("macro alias loop");
3115 } else {
3116 s->in_progress = true;
3117 undef_smacro(tok_text(s->expansion), false);
3118 s->in_progress = false;
3119 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003120 }
3121 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003122 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003123 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3124 ctx, s);
3125 *sp = s->next;
3126 free_smacro(s);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003127 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003128 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003130 sp = &s->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003131 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003132 }
3133}
3134
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003135/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003136 * Parse a mmacro specification.
3137 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003138static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003139{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003140 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003141 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003142 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003143 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003144 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003146 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003147
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003148#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003149 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003150#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003151 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003152 def->plus = false;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003153 def->nolist = 0;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003154 def->nparam_min = 0;
3155 def->nparam_max = 0;
3156
H. Peter Anvina26433d2008-07-16 14:40:01 -07003157 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003158 tline = skip_white(tline);
3159 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003160 nasm_nonfatal("`%s' expects a parameter count", directive);
3161 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003162 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003163 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003165 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003167 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003168 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003170 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003172 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003173 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 }
3175 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003176 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003177 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 tline = tline->next;
3179 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003180 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003181 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003182 tline->next->len == 7 &&
3183 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 tline = tline->next;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003185 if (!list_option('f'))
3186 def->nolist |= NL_LIST|NL_LINE;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003187 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003188
H. Peter Anvina26433d2008-07-16 14:40:01 -07003189 /*
3190 * Handle default parameters.
3191 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003192 def->ndefs = 0;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003193 if (tline && tline->next) {
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003194 Token **comma;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 def->dlist = tline->next;
3196 tline->next = NULL;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003197 comma = count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
3198 if (!ppopt.sane_empty_expansion && comma) {
3199 *comma = NULL;
3200 def->ndefs--;
3201 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
3202 "dropping trailing empty default parameter in defintion of multi-line macro `%s'",
3203 def->name);
3204 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003205 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003206 def->dlist = NULL;
3207 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003208 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003209 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003210
H. Peter Anvin89cee572009-07-15 09:16:54 -04003211 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003212 !def->plus) {
3213 /*
3214 *!macro-defaults [on] macros with more default than optional parameters
3215 *! warns when a macro has more default parameters than optional parameters.
3216 *! See \k{mlmacdef} for why might want to disable this warning.
3217 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003218 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003219 "too many default macro parameters in macro `%s'", def->name);
3220 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003221
H. Peter Anvina26433d2008-07-16 14:40:01 -07003222 return true;
3223}
3224
3225
3226/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003227 * Decode a size directive
3228 */
3229static int parse_size(const char *str) {
3230 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003231 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003232 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003234 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003235}
3236
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003237/*
3238 * Process a preprocessor %pragma directive. Currently there are none.
3239 * Gets passed the token list starting with the "preproc" token from
3240 * "%pragma preproc".
3241 */
3242static void do_pragma_preproc(Token *tline)
3243{
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003244 const char *txt;
3245
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003246 /* Skip to the real stuff */
3247 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003248 tline = skip_white(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003249
3250 if (!tok_type(tline, TOK_ID))
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003251 return;
3252
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003253 txt = tok_text(tline);
3254 if (!nasm_stricmp(txt, "sane_empty_expansion")) {
3255 tline = skip_white(tline->next);
3256 ppopt.sane_empty_expansion =
3257 pp_get_boolean_option(tline, ppopt.sane_empty_expansion);
3258 } else {
3259 /* Unknown pragma, ignore for now */
3260 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003261}
3262
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003263static bool is_macro_id(const Token *t)
3264{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003265 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003266}
3267
H. Peter Anvin8571f062019-09-23 16:40:03 -07003268static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003269{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003270 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003271 Token *t = *tp;
3272
3273 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003274 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003275 t = expand_id(t);
3276
3277 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003278 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003279 return NULL;
3280 }
3281
H. Peter Anvin8571f062019-09-23 16:40:03 -07003282 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003283 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003284 *tp = t;
3285 return id;
3286}
3287
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003288/* Parse a %use package name and find the package. Set *err on syntax error. */
3289static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003290get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003291{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003292 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003293
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003294 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003295 t = expand_smacro(t);
3296
H. Peter Anvin8571f062019-09-23 16:40:03 -07003297 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003298
H. Peter Anvin8571f062019-09-23 16:40:03 -07003299 if (!t) {
3300 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3301 return NULL;
3302 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3303 nasm_nonfatal("`%s' expects a package name, got `%s'",
3304 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003305 return NULL;
3306 }
3307
H. Peter Anvin8571f062019-09-23 16:40:03 -07003308 *name = id = unquote_token(t);
3309
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003310 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003311 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003312 if (t)
3313 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3314
3315 return nasm_find_use_package(id);
3316}
3317
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003318/*
3319 * Mark parameter tokens in an smacro definition. If the type argument
3320 * is 0, create smac param tokens, otherwise use the type specified;
3321 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3322 * parameter tokens during expansion during %xdefine.
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003323 *
3324 * tmpl may not be NULL here.
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003325 */
3326static void mark_smac_params(Token *tline, const SMacro *tmpl,
3327 enum pp_token_type type)
3328{
3329 const struct smac_param *params = tmpl->params;
3330 int nparam = tmpl->nparam;
3331 Token *t;
3332 int i;
3333
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003334 list_for_each(t, tline) {
3335 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3336 continue;
3337
3338 for (i = 0; i < nparam; i++) {
3339 if (tok_text_match(t, &params[i].name))
3340 t->type = type ? type : tok_smac_param(i);
3341 }
3342 }
3343}
3344
Ed Beroset3ab3f412002-06-11 03:31:49 +00003345/**
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003346 * %clear selected macro sets either globally or in contexts
3347 */
3348static void do_clear(enum clear_what what, bool context)
3349{
3350 if (context) {
3351 if (what & CLEAR_ALLDEFINE) {
3352 Context *ctx;
3353 list_for_each(ctx, cstk)
3354 clear_smacro_table(&ctx->localmac, what);
3355 }
3356 /* Nothing else can be context-local */
3357 } else {
3358 if (what & CLEAR_ALLDEFINE)
3359 clear_smacro_table(&smacros, what);
3360 if (what & CLEAR_MMACRO)
3361 free_mmacro_table(&mmacros);
3362 }
3363}
3364
3365/**
Ed Beroset3ab3f412002-06-11 03:31:49 +00003366 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003367 * Find out if a line contains a preprocessor directive, and deal
3368 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003369 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003370 * If a directive _is_ found, it is the responsibility of this routine
3371 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003372 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003373 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003374 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003375 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003376 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003377 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003378static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003379{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003380 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003381 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003382 bool err;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003383 enum nolist_flags nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003384 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003385 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003386 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003387 const char *p;
3388 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003389 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003390 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003391 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003392 Include *inc;
3393 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003394 Cond *cond;
3395 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003396 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003397 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003398 struct tokenval tokval;
3399 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003400 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003401 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003402 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003403 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003404
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003405 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003406 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003407
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003408 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003409 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3410 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003411
H. Peter Anvin8571f062019-09-23 16:40:03 -07003412 dname = tok_text(tline);
3413 if (dname[1] == '%')
3414 return NO_DIRECTIVE_FOUND;
3415
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003416 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003417
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003418 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003419 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003420 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003421 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003422 }
3423
3424 /*
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003425 * %line directives are always processed immediately and
3426 * unconditionally, as they are intended to reflect position
3427 * in externally preprocessed sources.
3428 */
3429 if (op == PP_LINE) {
3430 /*
3431 * Syntax is `%line nnn[+mmm] [filename]'
3432 */
3433 if (pp_noline || istk->mstk.mstk)
3434 goto done;
3435
3436 tline = tline->next;
3437 tline = skip_white(tline);
3438 if (!tok_type(tline, TOK_NUMBER)) {
3439 nasm_nonfatal("`%s' expects line number", dname);
3440 goto done;
3441 }
3442 k = readnum(tok_text(tline), &err);
3443 m = 1;
3444 tline = tline->next;
3445 if (tok_is(tline, '+') || tok_is(tline, '-')) {
3446 bool minus = tok_is(tline, '-');
3447 tline = tline->next;
3448 if (!tok_type(tline, TOK_NUMBER)) {
3449 nasm_nonfatal("`%s' expects line increment", dname);
3450 goto done;
3451 }
3452 m = readnum(tok_text(tline), &err);
3453 if (minus)
3454 m = -m;
3455 tline = tline->next;
3456 }
3457 tline = skip_white(tline);
3458 if (tline) {
3459 if (tline->type == TOK_STRING) {
3460 src_set_fname(unquote_token(tline));
3461 } else {
3462 char *fname = detoken(tline, false);
3463 src_set_fname(fname);
3464 nasm_free(fname);
3465 }
3466 }
3467 src_set_linnum(k);
3468
3469 istk->where = src_where();
3470 istk->lineinc = m;
3471 goto done;
3472 }
3473
3474 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003475 * If we're in a non-emitting branch of a condition construct,
3476 * or walking to the end of an already terminated %rep block,
3477 * we should ignore all directives except for condition
3478 * directives.
3479 */
3480 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003481 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003482 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003483 return NO_DIRECTIVE_FOUND;
3484 }
3485
3486 /*
3487 * If we're defining a macro or reading a %rep block, we should
3488 * ignore all directives except for %macro/%imacro (which nest),
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003489 * %endm/%endmacro, %line and (only if we're in a %rep block) %endrep.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003490 * If we're in a %rep block, another %rep nests, so should be let through.
3491 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003492 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3493 op != PP_ENDMACRO && op != PP_ENDM &&
3494 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003495 return NO_DIRECTIVE_FOUND;
3496 }
3497
3498 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003499 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003500 nested_mac_count++;
3501 return NO_DIRECTIVE_FOUND;
3502 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003503 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003504 nested_mac_count--;
3505 return NO_DIRECTIVE_FOUND;
3506 }
3507 }
3508 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003509 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003510 nested_rep_count++;
3511 return NO_DIRECTIVE_FOUND;
3512 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003513 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003514 nested_rep_count--;
3515 return NO_DIRECTIVE_FOUND;
3516 }
3517 }
3518 }
3519 }
3520
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003521 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003522 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003523 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003525
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003526 case PP_PRAGMA:
3527 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003528 * %pragma namespace options...
3529 *
3530 * The namespace "preproc" is reserved for the preprocessor;
3531 * all other namespaces generate a [pragma] assembly directive.
3532 *
3533 * Invalid %pragmas are ignored and may have different
3534 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003535 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003536 t = tline;
3537 tline = tline->next;
3538 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003539 tline = zap_white(expand_smacro(tline));
3540 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003541 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003542 /* Preprocessor pragma */
3543 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003544 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003545 } else {
3546 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003547
3548 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003549 for (t = tline; t->next; t = t->next)
3550 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003551 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003552
3553 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003554 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003555 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003556 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003557 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003558 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003559 }
3560 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003561 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003562
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 case PP_STACKSIZE:
3564 /* Directive to tell NASM what the default stack size is. The
3565 * default is for a 16-bit stack, and this can be overriden with
3566 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003568 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003570 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003572 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 /* All subsequent ARG directives are for a 32-bit stack */
3574 StackSize = 4;
3575 StackPointer = "ebp";
3576 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003577 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003578 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003579 /* All subsequent ARG directives are for a 64-bit stack */
3580 StackSize = 8;
3581 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003582 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003583 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003584 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 /* All subsequent ARG directives are for a 16-bit stack,
3586 * far function call.
3587 */
3588 StackSize = 2;
3589 StackPointer = "bp";
3590 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003591 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003592 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 /* All subsequent ARG directives are for a 16-bit stack,
3594 * far function call. We don't support near functions.
3595 */
3596 StackSize = 2;
3597 StackPointer = "bp";
3598 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003599 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003601 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003603 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003604
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 case PP_ARG:
3606 /* TASM like ARG directive to define arguments to functions, in
3607 * the following form:
3608 *
3609 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3610 */
3611 offset = ArgOffset;
3612 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003613 const char *arg;
3614 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003618 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003619 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003620 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003621 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003623 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003624
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 /* Find the argument size type */
3626 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003627 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003628 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003629 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 }
3631 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003632 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003633 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003634 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003636
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003638 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003640 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003641 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003642 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003644 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 }
3646 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003647
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003648 /* Round up to even stack slots */
3649 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003650
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 /* Now define the macro for the argument */
3652 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3653 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003654 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003656
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003658 tline = skip_white(tline->next);
3659 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003660 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003661 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003662
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 case PP_LOCAL:
3664 /* TASM like LOCAL directive to define local variables for a
3665 * function, in the following form:
3666 *
3667 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3668 *
3669 * The '= LocalSize' at the end is ignored by NASM, but is
3670 * required by TASM to define the local parameter size (and used
3671 * by the TASM macro package).
3672 */
3673 offset = LocalOffset;
3674 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003675 const char *local;
3676 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003677 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003678
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003680 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003681 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003682 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003683 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003685 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003686
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 /* Find the argument size type */
3688 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003689 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003690 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003691 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003692 }
3693 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003694 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003695 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003696 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003697 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003698
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003700 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003702 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003703 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003704 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003705 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003706 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 }
3708 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003709
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003710 /* Round up to even stack slots */
3711 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003712
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003713 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003714
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003715 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3717 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003718 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003719
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 /* Now define the assign to setup the enter_c macro correctly */
3721 snprintf(directive, sizeof(directive),
3722 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003723 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003724
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003726 tline = skip_white(tline->next);
3727 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003728 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003729 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003730
H. Peter Anvine2c80182005-01-15 22:15:51 +00003731 case PP_CLEAR:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003732 {
3733 bool context = false;
3734
3735 t = tline->next = expand_smacro(tline->next);
3736 t = skip_white(t);
3737 if (!t) {
3738 /* Emulate legacy behavior */
3739 do_clear(CLEAR_DEFINE|CLEAR_MMACRO, false);
3740 } else {
3741 while ((t = skip_white(t)) && t->type == TOK_ID) {
3742 const char *txt = tok_text(t);
3743 if (!nasm_stricmp(txt, "all")) {
3744 do_clear(CLEAR_ALL, context);
3745 } else if (!nasm_stricmp(txt, "define") ||
3746 !nasm_stricmp(txt, "def") ||
3747 !nasm_stricmp(txt, "smacro")) {
3748 do_clear(CLEAR_DEFINE, context);
3749 } else if (!nasm_stricmp(txt, "defalias") ||
3750 !nasm_stricmp(txt, "alias") ||
3751 !nasm_stricmp(txt, "salias")) {
3752 do_clear(CLEAR_DEFALIAS, context);
3753 } else if (!nasm_stricmp(txt, "alldef") ||
3754 !nasm_stricmp(txt, "alldefine")) {
3755 do_clear(CLEAR_ALLDEFINE, context);
3756 } else if (!nasm_stricmp(txt, "macro") ||
3757 !nasm_stricmp(txt, "mmacro")) {
3758 do_clear(CLEAR_MMACRO, context);
3759 } else if (!nasm_stricmp(txt, "context") ||
3760 !nasm_stricmp(txt, "ctx")) {
3761 context = true;
3762 } else if (!nasm_stricmp(txt, "global")) {
3763 context = false;
3764 } else if (!nasm_stricmp(txt, "nothing") ||
3765 !nasm_stricmp(txt, "none") ||
3766 !nasm_stricmp(txt, "ignore") ||
3767 !nasm_stricmp(txt, "-") ||
3768 !nasm_stricmp(txt, "--")) {
3769 /* Do nothing */
3770 } else {
3771 nasm_nonfatal("invalid option to %s: %s", dname, txt);
3772 t = NULL;
3773 }
3774 }
3775 }
3776
3777 t = skip_white(t);
3778 if (t)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003779 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003780 break;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003781 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003782
H. Peter Anvin418ca702008-05-30 10:42:30 -07003783 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003784 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003785 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003786 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003787 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003788 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003789 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003790 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003791 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003792 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003793
3794 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003795 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003796
3797 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003798 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003799 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003800
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003801 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003802 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003803 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003804 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003806 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003807 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003808 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003809 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003811 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003812 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003813 (pp_mode == PP_DEPS)
3814 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003815 if (!inc->fp) {
3816 /* -MG given but file not found */
3817 nasm_free(inc);
3818 } else {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003819 inc->where = src_where();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003820 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003821 inc->nolist = istk->nolist;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003822 inc->noline = istk->noline;
3823 if (!inc->noline)
3824 src_set(0, found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003825 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003826 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003827 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003828 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003829
H. Peter Anvind2456592008-06-19 15:04:18 -07003830 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003831 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003832 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003833 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003834
H. Peter Anvin8571f062019-09-23 16:40:03 -07003835 pkg = get_use_pkg(tline->next, dname, &name);
3836 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003837 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003838 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003839 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003840 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003841 /*
3842 * Not already included, go ahead and include it.
3843 * Treat it as an include file for the purpose of
3844 * producing a listing.
3845 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003846 use_loaded[pkg->index] = true;
3847 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003848 nasm_new(inc);
3849 inc->next = istk;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003850 inc->nolist = istk->nolist + !list_option('b');
3851 inc->noline = istk->noline;
3852 if (!inc->noline)
3853 src_set(0, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003854 istk = inc;
3855 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003856 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003857 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003858 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003860 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003861 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003862 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003863 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003864 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003865 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003866 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003867 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003868 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003869 }
3870 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003871 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003872 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003873 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003874 } else {
3875 p = NULL; /* Anonymous */
3876 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003877
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003878 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003879 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003880 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003881 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003882 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003883 ctx->number = unique++;
3884 cstk = ctx;
3885 } else {
3886 /* %pop or %repl */
3887 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003888 nasm_nonfatal("`%s': context stack is empty", dname);
3889 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003890 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003891 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003892 "expected %s",
3893 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003894 else
3895 ctx_pop();
3896 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003897 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003898 nasm_free((char *)cstk->name);
3899 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003900 p = NULL;
3901 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003902 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003903 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003904 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003905 severity = ERR_FATAL;
3906 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003908 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003909 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003910 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003911 /*!
3912 *!user [on] %warning directives
3913 *! controls output of \c{%warning} directives (see \k{pperror}).
3914 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003915 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003916 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003917
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003918issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003919 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003920 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003921 tline->next = expand_smacro(tline->next);
3922 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003923 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003924 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003925 t = skip_white(t);
3926 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003927 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003928 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003929 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003930 } else {
3931 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003932 q = detoken(tline, false);
3933 nasm_error(severity, "%s", q);
3934 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003935 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003936 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003937 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003938
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003939 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003940 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003941 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003942 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003943 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003944 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003945 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003946 cond = nasm_malloc(sizeof(Cond));
3947 cond->next = istk->conds;
3948 cond->state = j;
3949 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003950 if(istk->mstk.mstk)
3951 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003952 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003953
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003954 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003955 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003956 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003957 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003958 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003959 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003960 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003961
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003962 case COND_DONE:
3963 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003964 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003965
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003966 case COND_ELSE_TRUE:
3967 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003968 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003969 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003970 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003971 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003972
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003973 case COND_IF_FALSE:
3974 /*
3975 * IMPORTANT: In the case of %if, we will already have
3976 * called expand_mmac_params(); however, if we're
3977 * processing an %elif we must have been in a
3978 * non-emitting mode, which would have inhibited
3979 * the normal invocation of expand_mmac_params().
3980 * Therefore, we have to do it explicitly here.
3981 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003982 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003983 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003984 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003985 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003986 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003987 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003988
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 case PP_ELSE:
3990 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003991 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003992 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003993 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003994 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003995 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003996 case COND_IF_TRUE:
3997 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003998 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003999 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004000
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004001 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004002 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004003
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004004 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004005 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004006 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004007
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004008 case COND_ELSE_TRUE:
4009 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004010 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004011 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004012 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004013 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004014 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004015 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004016
H. Peter Anvine2c80182005-01-15 22:15:51 +00004017 case PP_ENDIF:
4018 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004019 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004020 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004021 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004022 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004023 cond = istk->conds;
4024 istk->conds = cond->next;
4025 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004026 if(istk->mstk.mstk)
4027 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004028 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004029
H. Peter Anvin8b262472019-02-26 14:00:54 -08004030 case PP_RMACRO:
4031 case PP_MACRO:
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004032 {
4033 MMacro *def;
4034
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004035 nasm_assert(!defining);
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004036 nasm_new(def);
4037 def->casesense = casesense;
4038 def->dstk.mmac = defining;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004039 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004040 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
4041 if (!parse_mmacro_spec(tline, def, dname)) {
4042 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004043 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004044 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07004045
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004046 defining = def;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004047 defining->where = istk->where;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07004048
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004049 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
4050 while (mmac) {
4051 if (!strcmp(mmac->name, defining->name) &&
4052 (mmac->nparam_min <= defining->nparam_max
4053 || defining->plus)
4054 && (defining->nparam_min <= mmac->nparam_max
4055 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004056 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004057 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004058 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004059 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004060 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004061 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004062 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004063 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004064
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 case PP_ENDM:
4066 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004067 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004068 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004069 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004070 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004071 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
4072 defining->next = *mmhead;
4073 *mmhead = defining;
4074 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004075 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004076
H. Peter Anvin89cee572009-07-15 09:16:54 -04004077 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004078 /*
4079 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004080 * macro-end marker for a macro with a name. Then we
4081 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004082 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004083 list_for_each(l, istk->expansion)
4084 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004085 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004086
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004087 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004088 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004089 * Remove all conditional entries relative to this
4090 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004091 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004092 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
4093 cond = istk->conds;
4094 istk->conds = cond->next;
4095 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004096 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004097 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004098 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004099 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004100 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004101 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05004102
H. Peter Anvina26433d2008-07-16 14:40:01 -07004103 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004104 casesense = false;
4105 /* fall through */
4106 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07004107 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004108 MMacro **mmac_p;
4109 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004110
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004111 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004112 spec.casesense = casesense;
4113 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004114 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004115 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004116 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
4117 while (mmac_p && *mmac_p) {
4118 mmac = *mmac_p;
4119 if (mmac->casesense == spec.casesense &&
4120 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
4121 mmac->nparam_min == spec.nparam_min &&
4122 mmac->nparam_max == spec.nparam_max &&
4123 mmac->plus == spec.plus) {
4124 *mmac_p = mmac->next;
4125 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004126 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004127 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004128 }
4129 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004130 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004131 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004132 }
4133
H. Peter Anvine2c80182005-01-15 22:15:51 +00004134 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004135 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004136 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004137 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004138 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004139 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004140 return DIRECTIVE_FOUND;
4141 }
4142 t = expand_smacro(tline->next);
4143 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004144 pps.tptr = tline = t;
4145 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004146 tokval.t_type = TOKEN_INVALID;
4147 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004148 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004149 free_tlist(tline);
4150 if (!evalresult)
4151 return DIRECTIVE_FOUND;
4152 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004153 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004154 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004155 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004156 return DIRECTIVE_FOUND;
4157 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004158 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004159 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004160 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004161 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004162 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004163 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004164 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004165
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004166 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004167 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004168 rotate += mmac->nparam;
4169
4170 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004171 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004172 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004173
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004175 {
4176 MMacro *tmp_defining;
4177
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004178 nolist = 0;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004179 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004180 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
4181 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004182 if (!list_option('f'))
4183 nolist |= NL_LIST; /* ... but update line numbers */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004184 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004185 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004186
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08004188 pps.tptr = expand_smacro(tline);
4189 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004191 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004192 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004193 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004194 if (!evalresult)
4195 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004196 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004197 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004199 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004200 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004201 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004202 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004203 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004204 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
4205 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004206 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004207 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004208 /*!
4209 *!negative-rep [on] regative %rep count
4210 *! warns about negative counts given to the \c{%rep}
4211 *! preprocessor directive.
4212 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004213 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004214 "negative `%%rep' count: %"PRId64, count);
4215 count = 0;
4216 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004217 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004218 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004220 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07004221 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004222 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004223 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004224 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004225 defining->nolist = nolist;
4226 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004227 defining->mstk = istk->mstk;
4228 defining->dstk.mstk = tmp_defining;
4229 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004230 defining->where = istk->where;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004231 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004232 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004233
H. Peter Anvine2c80182005-01-15 22:15:51 +00004234 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004235 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004236 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004237 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004239
H. Peter Anvine2c80182005-01-15 22:15:51 +00004240 /*
4241 * Now we have a "macro" defined - although it has no name
4242 * and we won't be entering it in the hash tables - we must
4243 * push a macro-end marker for it on to istk->expansion.
4244 * After that, it will take care of propagating itself (a
4245 * macro-end marker line for a macro which is really a %rep
4246 * block will cause the macro to be re-expanded, complete
4247 * with another macro-end marker to ensure the process
4248 * continues) until the whole expansion is forcibly removed
4249 * from istk->expansion by a %exitrep.
4250 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004251 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004252 l->next = istk->expansion;
4253 l->finishes = defining;
4254 l->first = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004255 l->where = src_where();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004256 istk->expansion = l;
4257
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004258 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004259
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004260 /* A loop does not change istk->noline */
4261 istk->nolist += !!(defining->nolist & NL_LIST);
4262 if (!istk->nolist)
4263 lfmt->uplevel(LIST_MACRO, 0);
4264
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004265 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004266 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004267
H. Peter Anvine2c80182005-01-15 22:15:51 +00004268 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004269 /*
4270 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004271 * macro-end marker for a macro with no name. Then we set
4272 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004273 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004274 list_for_each(l, istk->expansion)
4275 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004276 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004277
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004278 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004279 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004280 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004281 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004282 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004283
H. Peter Anvin8b262472019-02-26 14:00:54 -08004284 case PP_DEFINE:
4285 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004286 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004287 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004288 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004289 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004290 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004291
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004292 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004293 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004294
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004295 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004296 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004297 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004298 tline = *lastp;
4299 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004300
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004301 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004302 macro_start = tline;
4303 if (!is_macro_id(macro_start)) {
4304 nasm_nonfatal("`%s' expects a macro identifier to alias",
4305 dname);
4306 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004308 tt = macro_start->next;
4309 macro_start->next = NULL;
4310 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004311 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004312 if (tline && tline->type) {
4313 nasm_warn(WARN_OTHER,
4314 "trailing garbage after aliasing identifier ignored");
4315 }
4316 free_tlist(tt);
4317 tmpl.alias = true;
4318 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004319 if (op == PP_XDEFINE) {
4320 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004321 if (nparam)
4322 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004323 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004324 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004325 /* NB: Does this still make sense? */
4326 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004328
H. Peter Anvine2c80182005-01-15 22:15:51 +00004329 /*
4330 * Good. We now have a macro name, a parameter count, and a
4331 * token list (in reverse order) for an expansion. We ought
4332 * to be OK just to create an SMacro, store it, and let
4333 * free_tlist have the rest of the line (which we have
4334 * carefully re-terminated after chopping off the expansion
4335 * from the end).
4336 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004337 define_smacro(mname, casesense, macro_start, &tmpl);
4338 break;
4339 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004340
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004342 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004343 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004344 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004345 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004346 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004347
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004348 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004349 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004350
H. Peter Anvin8b262472019-02-26 14:00:54 -08004351 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004352 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004353 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004354
H. Peter Anvin9e200162008-06-04 17:23:14 -07004355 last = tline;
4356 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004357 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004358
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004359 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004360 q = detoken(tline, false);
4361 macro_start = make_tok_qstr(NULL, q);
4362 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004363
4364 /*
4365 * We now have a macro name, an implicit parameter count of
4366 * zero, and a string token to use as an expansion. Create
4367 * and store an SMacro.
4368 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004369 define_smacro(mname, casesense, macro_start, NULL);
4370 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004371
H. Peter Anvin8b262472019-02-26 14:00:54 -08004372 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004373 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004374 goto done;
4375
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004376 last = tline;
4377 tline = expand_smacro(tline->next);
4378 last->next = NULL;
4379
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004380 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004381 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004382 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004383 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004384 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004385 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004386 }
4387
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004388 /*
4389 * Convert the string to a token stream. Note that smacros
4390 * are stored with the token stream reversed, so we have to
4391 * reverse the output of tokenize().
4392 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004393 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004394
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004395 /*
4396 * We now have a macro name, an implicit parameter count of
4397 * zero, and a numeric token to use as an expansion. Create
4398 * and store an SMacro.
4399 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004400 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004401 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004402 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004403
H. Peter Anvin418ca702008-05-30 10:42:30 -07004404 case PP_PATHSEARCH:
4405 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004406 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004407
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004408 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004409 goto done;
4410
H. Peter Anvin418ca702008-05-30 10:42:30 -07004411 last = tline;
4412 tline = expand_smacro(tline->next);
4413 last->next = NULL;
4414
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004415 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004416 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004417 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004418 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004419 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004420 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004421 }
4422 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004423 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004424
4425 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004426
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004427 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004428 if (!found_path)
4429 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004430 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004431
4432 /*
4433 * We now have a macro name, an implicit parameter count of
4434 * zero, and a string token to use as an expansion. Create
4435 * and store an SMacro.
4436 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004437 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004438 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004439 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004440 }
4441
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004443 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004444 goto done;
4445
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 last = tline;
4447 tline = expand_smacro(tline->next);
4448 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004449
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004450 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004451 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004452 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004453 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454 free_tlist(tline);
4455 free_tlist(origline);
4456 return DIRECTIVE_FOUND;
4457 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004458
H. Peter Anvin8571f062019-09-23 16:40:03 -07004459 unquote_token(t);
4460 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004461
H. Peter Anvine2c80182005-01-15 22:15:51 +00004462 /*
4463 * We now have a macro name, an implicit parameter count of
4464 * zero, and a numeric token to use as an expansion. Create
4465 * and store an SMacro.
4466 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004467 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004468 free_tlist(tline);
4469 free_tlist(origline);
4470 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004471
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004472 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004473 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004474 goto done;
4475
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004476 last = tline;
4477 tline = expand_smacro(tline->next);
4478 last->next = NULL;
4479
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004480 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004481 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004482 switch (t->type) {
4483 case TOK_WHITESPACE:
4484 break;
4485 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004486 unquote_token(t);
4487 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004488 break;
4489 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004490 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004491 break;
4492 /* else fall through */
4493 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004494 nasm_nonfatal("non-string passed to `%s': %s", dname,
4495 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004496 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004497 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004498 }
4499 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004500
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004501 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004502 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004503 if (t->type == TOK_INTERNAL_STRING)
4504 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004505 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004506 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004507
4508 /*
4509 * We now have a macro name, an implicit parameter count of
4510 * zero, and a numeric token to use as an expansion. Create
4511 * and store an SMacro.
4512 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004513 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004514 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004515 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004516 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004517 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004518
H. Peter Anvine2c80182005-01-15 22:15:51 +00004519 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004520 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004521 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004522 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004523 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004524
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004525 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004526 goto done;
4527
H. Peter Anvine2c80182005-01-15 22:15:51 +00004528 last = tline;
4529 tline = expand_smacro(tline->next);
4530 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004531
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004532 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004533 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004534
4535 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004536
H. Peter Anvine2c80182005-01-15 22:15:51 +00004537 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004538 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004539 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004540 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004541 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004542 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004543
H. Peter Anvin8b262472019-02-26 14:00:54 -08004544 pps.tptr = t->next;
4545 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004547 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004548 if (!evalresult) {
4549 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004550 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004551 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004552 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004554 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004556 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004557
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004558 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004559 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004560 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004561 } else {
4562 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004563 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004564 if (!evalresult) {
4565 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004566 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004567 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004568 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004569 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004570 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004571 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004572 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004573 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004574
H. Peter Anvin8571f062019-09-23 16:40:03 -07004575 unquote_token(t);
4576 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004577
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004578 /* make start and count being in range */
4579 if (start < 0)
4580 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004581 if (count < 0)
4582 count = len + count + 1 - start;
4583 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004584 count = len - start;
4585 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004586 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004587
H. Peter Anvin8571f062019-09-23 16:40:03 -07004588 txt = (start < 0) ? "" : tok_text(t) + start;
4589 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004590 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004591
H. Peter Anvine2c80182005-01-15 22:15:51 +00004592 /*
4593 * We now have a macro name, an implicit parameter count of
4594 * zero, and a numeric token to use as an expansion. Create
4595 * and store an SMacro.
4596 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004597 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004598 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004599 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004600 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004601
H. Peter Anvin8b262472019-02-26 14:00:54 -08004602 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004603 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004604 goto done;
4605
H. Peter Anvine2c80182005-01-15 22:15:51 +00004606 last = tline;
4607 tline = expand_smacro(tline->next);
4608 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004609
H. Peter Anvin8b262472019-02-26 14:00:54 -08004610 pps.tptr = tline;
4611 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004612 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004613 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004614 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004615 if (!evalresult)
4616 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004617
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004619 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004620
H. Peter Anvine2c80182005-01-15 22:15:51 +00004621 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004622 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004623 free_tlist(origline);
4624 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004625 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004626
H. Peter Anvin8571f062019-09-23 16:40:03 -07004627 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004628
H. Peter Anvine2c80182005-01-15 22:15:51 +00004629 /*
4630 * We now have a macro name, an implicit parameter count of
4631 * zero, and a numeric token to use as an expansion. Create
4632 * and store an SMacro.
4633 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004634 define_smacro(mname, casesense, macro_start, NULL);
4635 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004636
H. Peter Anvind2354082019-08-27 16:38:48 -07004637 case PP_ALIASES:
4638 tline = tline->next;
4639 tline = expand_smacro(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07004640 ppopt.noaliases = !pp_get_boolean_option(tline, !ppopt.noaliases);
H. Peter Anvind2354082019-08-27 16:38:48 -07004641 break;
4642
H. Peter Anvine2c80182005-01-15 22:15:51 +00004643 case PP_LINE:
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004644 nasm_panic("`%s' directive not preprocessed early", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004645 break;
4646 }
4647
4648done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004649 free_tlist(origline);
4650 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004651}
4652
4653/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004654 * Ensure that a macro parameter contains a condition code and
4655 * nothing else. Return the condition code index if so, or -1
4656 * otherwise.
4657 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004659{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004660 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004661
H. Peter Anvin25a99342007-09-22 17:45:45 -07004662 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004663 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004664
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004665 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004666 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004667 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004668 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004669 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004670 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004671 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004672
H. Peter Anvin8571f062019-09-23 16:40:03 -07004673 return bsii(tok_text(t), (const char **)conditions,
4674 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004675}
4676
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004677static inline bool pp_concat_match(const Token *t, unsigned int mask)
4678{
4679 return t && (PP_CONCAT_MASK(t->type) & mask);
4680}
4681
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004682/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004683 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004684 * pasting, if @handle_explicit passed then explicit pasting
4685 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004686 * The @m array can contain a series of token types which are
4687 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004688 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004689static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004690 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004691{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004692 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004693 bool pasted = false;
4694 char *buf, *p;
4695 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004696
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004697 /*
4698 * The last token before pasting. We need it
4699 * to be able to connect new handled tokens.
4700 * In other words if there were a tokens stream
4701 *
4702 * A -> B -> C -> D
4703 *
4704 * and we've joined tokens B and C, the resulting
4705 * stream should be
4706 *
4707 * A -> BC -> D
4708 */
4709 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004710 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004711
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004712 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004713 prev_nonspace = NULL;
4714
4715 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004716 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004717
4718 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004719 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004720 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004721 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004722 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004723
4724 case TOK_PASTE:
4725 /* Explicit pasting */
4726 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004727 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004728
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004729 /* Left pasting token is start of line, just drop %+ */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004730 if (!prev_nonspace) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004731 tok = delete_Token(tok);
4732 break;
4733 }
4734
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004735 did_paste = true;
4736
4737 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004738 t = *prev_nonspace;
4739
4740 /* Delete leading whitespace */
4741 next = zap_white(t->next);
4742
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004743 /*
4744 * Delete the %+ token itself, followed by any whitespace.
4745 * In a sequence of %+ ... %+ ... %+ pasting sequences where
4746 * some expansions in the middle have ended up empty,
4747 * we can end up having multiple %+ tokens in a row;
4748 * just drop whem in that case.
4749 */
4750 while (next) {
4751 if (next->type == TOK_PASTE || next->type == TOK_WHITESPACE)
4752 next = delete_Token(next);
4753 else
4754 break;
4755 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004756
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004757 /*
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004758 * Nothing after? Just leave the existing token.
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004759 */
4760 if (!next) {
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004761 t->next = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004762 break;
4763 }
4764
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004765 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004766 p = mempcpy(p, tok_text(t), t->len);
4767 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004768 *p = '\0';
4769 delete_Token(t);
4770 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004771 nasm_free(buf);
4772
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004773 if (unlikely(!t)) {
4774 /*
4775 * No output at all? Replace with a single whitespace.
4776 * This should never happen.
4777 */
4778 t = new_White(NULL);
4779 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004780
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004781 *prev_nonspace = tok = t;
4782 while (t->next)
4783 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004784
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004785 /* Delete the second token and attach to the end of the list */
4786 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004787
4788 /* We want to restart from the head of the pasted token */
4789 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004790 break;
4791
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004792 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004793 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004794 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004795 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004796 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004797 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004798
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004799 if (i >= mnum)
4800 break;
4801
4802 len = tok->len;
4803 while (pp_concat_match(next, m[i].mask_tail)) {
4804 len += next->len;
4805 next = next->next;
4806 }
4807
4808 /* No match or no text to process */
4809 if (len == tok->len)
4810 break;
4811
4812 p = buf = nasm_malloc(len + 1);
4813 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004814 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004815 tok = delete_Token(tok);
4816 }
4817 *p = '\0';
4818 *prev_next = tok = t = tokenize(buf);
4819 nasm_free(buf);
4820
4821 /*
4822 * Connect pasted into original stream,
4823 * ie A -> new-tokens -> B
4824 */
4825 while (t->next)
4826 t = t->next;
4827 t->next = next;
4828 prev_next = prev_nonspace = &t->next;
4829 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004830 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004831 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004832
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004833 if (did_paste) {
4834 pasted = true;
4835 } else {
4836 prev_next = &tok->next;
4837 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4838 prev_nonspace = prev_next;
4839 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004840
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004841 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004842 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004843
4844 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004845}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004846
4847/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004848 * Computes the proper rotation of mmacro parameters
4849 */
4850static int mmac_rotate(const MMacro *mac, unsigned int n)
4851{
4852 if (--n < mac->nparam)
4853 n = (n + mac->rotate) % mac->nparam;
4854
4855 return n+1;
4856}
4857
4858/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004859 * expands to a list of tokens from %{x:y}
4860 */
H. Peter Anvin (Intel)c0d0f882020-06-30 17:33:39 -07004861static void expand_mmac_params_range(MMacro *mac, Token *tline, Token ***tail)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004862{
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004863 Token *t;
4864 const char *arg = tok_text(tline) + 1;
4865 int fst, lst, incr, n;
4866 int parsed;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004867
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004868 parsed = sscanf(arg, "%d:%d", &fst, &lst);
4869 nasm_assert(parsed == 2);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004870
4871 /*
4872 * only macros params are accounted so
4873 * if someone passes %0 -- we reject such
4874 * value(s)
4875 */
4876 if (lst == 0 || fst == 0)
4877 goto err;
4878
4879 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004880 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4881 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004882 goto err;
4883
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004884 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4885 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004886
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004887 /*
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004888 * It will be at least one parameter, as we can loop
4889 * in either direction.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004890 */
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004891 incr = (fst < lst) ? 1 : -1;
4892
4893 while (true) {
4894 n = mmac_rotate(mac, fst);
4895 dup_tlistn(mac->params[n], mac->paramlen[n], tail);
4896 if (fst == lst)
4897 break;
4898 t = make_tok_char(NULL, ',');
4899 **tail = t;
4900 *tail = &t->next;
4901 fst += incr;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004902 }
4903
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004904 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004905
4906err:
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004907 nasm_nonfatal("`%%{%s}': macro parameters out of range", arg);
4908 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004909}
4910
H. Peter Anvin76690a12002-04-30 20:52:49 +00004911/*
4912 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004913 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004914 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004915 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004916static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004917{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004918 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004919 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004920 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004921
4922 tail = &thead;
4923 thead = NULL;
4924
H. Peter Anvine2c80182005-01-15 22:15:51 +00004925 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004926 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004927 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004928 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004929 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004930 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004931
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004932 tline = tline->next;
4933 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004934
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004935 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004936 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004937 change = true;
4938
4939 if (!mac) {
4940 err_not_mac = true;
4941 break;
4942 }
4943
H. Peter Anvin8571f062019-09-23 16:40:03 -07004944 type = TOK_ID;
4945 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004946 break;
4947 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004948 {
4949 Token *tt = NULL;
4950
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004951 change = true;
4952
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004953 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004954 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004955 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004956 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004957
4958 if (strchr(text, ':')) {
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004959 /* It is a range */
4960 expand_mmac_params_range(mac, t, &tail);
4961 text = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004962 break;
4963 }
4964
4965 switch (text[1]) {
4966 /*
4967 * We have to make a substitution of one of the
4968 * forms %1, %-1, %+1, %%foo, %0, %00.
4969 */
4970 case '0':
4971 if (!text[2]) {
4972 type = TOK_NUMBER;
4973 text = nasm_asprintf("%d", mac->nparam);
4974 break;
4975 }
4976 if (text[2] != '0' || text[3])
4977 goto invalid;
4978 /* a possible captured label == mac->params[0] */
4979 /* fall through */
4980 default:
4981 {
4982 unsigned long n;
4983 char *ep;
4984
4985 n = strtoul(text + 1, &ep, 10);
4986 if (unlikely(*ep))
4987 goto invalid;
4988
4989 if (n <= mac->nparam) {
4990 n = mmac_rotate(mac, n);
4991 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4992 }
4993 text = NULL;
4994 break;
4995 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004996 case '-':
4997 case '+':
4998 {
4999 int cc;
5000 unsigned long n;
5001 char *ep;
5002
H. Peter Anvin8571f062019-09-23 16:40:03 -07005003 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005004 if (unlikely(*ep))
5005 goto invalid;
5006
Chang S. Bae057b8322020-04-18 23:11:21 +00005007 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005008 n = mmac_rotate(mac, n);
5009 tt = mac->params[n];
5010 }
5011 cc = find_cc(tt);
5012 if (cc == -1) {
5013 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005014 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005015 text = NULL;
5016 break;
5017 }
5018
5019 type = TOK_ID;
5020 if (text[1] == '-') {
5021 int ncc = inverse_ccs[cc];
5022 if (unlikely(ncc == -1)) {
5023 nasm_nonfatal("condition code `%s' is not invertible",
5024 conditions[cc]);
5025 break;
5026 }
5027 cc = ncc;
5028 }
5029 text = nasm_strdup(conditions[cc]);
5030 break;
5031 }
5032
5033 invalid:
5034 nasm_nonfatal("invalid macro parameter: `%s'", text);
5035 text = NULL;
5036 break;
5037 }
5038 break;
5039 }
5040
5041 case TOK_PREPROC_Q:
5042 if (mac) {
5043 type = TOK_ID;
5044 text = nasm_strdup(mac->iname);
5045 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005046 } else {
5047 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005048 }
5049 break;
5050
5051 case TOK_PREPROC_QQ:
5052 if (mac) {
5053 type = TOK_ID;
5054 text = nasm_strdup(mac->name);
5055 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005056 } else {
5057 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005058 }
5059 break;
5060
5061 case TOK_INDIRECT:
5062 {
5063 Token *tt;
5064
H. Peter Anvin8571f062019-09-23 16:40:03 -07005065 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005066 tt = expand_mmac_params(tt);
5067 tt = expand_smacro(tt);
5068 /* Why dup_tlist() here? We should own tt... */
5069 dup_tlist(tt, &tail);
5070 text = NULL;
5071 change = true;
5072 break;
5073 }
5074
5075 default:
5076 change = false;
5077 break;
5078 }
5079
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07005080 if (err_not_mac) {
5081 nasm_nonfatal("`%s': not in a macro call", text);
5082 text = NULL;
5083 change = true;
5084 }
5085
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005086 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005087 if (!text) {
5088 delete_Token(t);
5089 } else {
5090 *tail = t;
5091 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005092 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005093 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005094 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005095 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005096 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005097 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005098 tail = &t->next;
5099 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005100 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005101
H. Peter Anvineba20a72002-04-30 20:53:55 +00005102 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07005103
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005104 if (changed) {
5105 const struct tokseq_match t[] = {
5106 {
5107 PP_CONCAT_MASK(TOK_ID) |
5108 PP_CONCAT_MASK(TOK_FLOAT), /* head */
5109 PP_CONCAT_MASK(TOK_ID) |
5110 PP_CONCAT_MASK(TOK_NUMBER) |
5111 PP_CONCAT_MASK(TOK_FLOAT) |
5112 PP_CONCAT_MASK(TOK_OTHER) /* tail */
5113 },
5114 {
5115 PP_CONCAT_MASK(TOK_NUMBER), /* head */
5116 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5117 }
5118 };
5119 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
5120 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07005121
H. Peter Anvin76690a12002-04-30 20:52:49 +00005122 return thead;
5123}
5124
H. Peter Anvin322bee02019-08-10 01:38:06 -07005125static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005126
H. Peter Anvin76690a12002-04-30 20:52:49 +00005127/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005128 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005129 * a macro at all, it is simply copied to the output and the pointer
5130 * advanced. tpp should be a pointer to a pointer (usually the next
5131 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005132 * to point to the first token of the expansion, and *tpp updated to
5133 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005134 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005135 * If the expansion is empty, *tpp will be unchanged but **tpp will
5136 * be advanced past the macro call.
5137 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07005138 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005139 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005140static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005141{
5142 Token **params = NULL;
5143 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005144 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005145 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005146 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005147 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005148 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005149 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005150 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005151
5152 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005153 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005154
H. Peter Anvin8571f062019-09-23 16:40:03 -07005155 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005156
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005157 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005158 smacro_deadman.levels--;
5159
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005160 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005161 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005162 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005163 smacro_deadman.triggered = true;
5164 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005165 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005166 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005167 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005168 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005169 Context *ctx = get_ctx(mname, &mname);
5170 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5171 } else {
5172 goto not_a_macro;
5173 }
5174
5175 /*
5176 * We've hit an identifier of some sort. First check whether the
5177 * identifier is a single-line macro at all, then think about
5178 * checking for parameters if necessary.
5179 */
5180 list_for_each(m, head) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005181 if (unlikely(m->alias && ppopt.noaliases))
H. Peter Anvind2354082019-08-27 16:38:48 -07005182 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005183 if (!mstrcmp(m->name, mname, m->casesense))
5184 break;
5185 }
5186
5187 if (!m) {
5188 goto not_a_macro;
5189 }
5190
5191 /* Parse parameters, if applicable */
5192
5193 params = NULL;
5194 nparam = 0;
5195
5196 if (m->nparam == 0) {
5197 /*
5198 * Simple case: the macro is parameterless.
5199 * Nothing to parse; the expansion code will
5200 * drop the macro name token.
5201 */
5202 } else {
5203 /*
5204 * Complicated case: at least one macro with this name
5205 * exists and takes parameters. We must find the
5206 * parameters in the call, count them, find the SMacro
5207 * that corresponds to that form of the macro call, and
5208 * substitute for the parameters when we expand. What a
5209 * pain.
5210 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005211 Token *t;
5212 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005213
5214 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005215 tline = skip_white(tline);
5216 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005217 /*
5218 * This macro wasn't called with parameters: ignore
5219 * the call. (Behaviour borrowed from gnu cpp.)
5220 */
5221 goto not_a_macro;
5222 }
5223
5224 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005225 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005226 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005227 t = tline; /* tline points to leading ( */
5228
5229 while (paren) {
5230 t = t->next;
5231
5232 if (!t) {
5233 nasm_nonfatal("macro call expects terminating `)'");
5234 goto not_a_macro;
5235 }
5236
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005237 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005238 continue;
5239
H. Peter Anvin8571f062019-09-23 16:40:03 -07005240 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005241 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005242 if (!brackets && paren == 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005243 nparam++;
5244 break;
5245
5246 case '{':
5247 brackets++;
5248 break;
5249
5250 case '}':
5251 if (brackets > 0)
5252 brackets--;
5253 break;
5254
5255 case '(':
5256 if (!brackets)
5257 paren++;
5258 break;
5259
5260 case ')':
5261 if (!brackets)
5262 paren--;
5263 break;
5264
5265 default:
5266 break; /* Normal token */
5267 }
5268 }
5269
5270 /*
5271 * Look for a macro matching in both name and parameter count.
5272 * We already know any matches cannot be anywhere before the
5273 * current position of "m", so there is no reason to
5274 * backtrack.
5275 */
5276 while (1) {
5277 if (!m) {
5278 /*!
5279 *!macro-params-single [on] single-line macro calls with wrong parameter count
5280 *! warns about \i{single-line macros} being invoked
5281 *! with the wrong number of parameters.
5282 */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005283 nasm_warn(WARN_MACRO_PARAMS_SINGLE|ERR_HOLD,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005284 "single-line macro `%s' exists, "
5285 "but not taking %d parameter%s",
5286 mname, nparam, (nparam == 1) ? "" : "s");
5287 goto not_a_macro;
5288 }
5289
5290 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005291 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005292 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005293 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005294 break; /* Also good */
5295 }
5296 m = m->next;
5297 }
5298 }
5299
5300 if (m->in_progress)
5301 goto not_a_macro;
5302
5303 /* Expand the macro */
5304 m->in_progress = true;
5305
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005306 if (nparam) {
5307 /* Extract parameters */
5308 Token **phead, **pep;
5309 int white = 0;
5310 int brackets = 0;
5311 int paren;
5312 bool bracketed = false;
5313 bool bad_bracket = false;
5314 enum sparmflags flags;
5315
5316 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005317 paren = 1;
5318 nasm_newn(params, nparam);
5319 i = 0;
5320 flags = m->params[i].flags;
5321 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005322 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005323
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005324 while (paren) {
5325 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005326 char ch;
5327
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005328 tline = tline->next;
5329
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005330 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005331 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005332
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005333 ch = 0;
5334 skip = false;
5335
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005336
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005337 switch (tline->type) {
5338 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005339 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005340 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005341 break;
5342
5343 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005344 if (!(flags & SPARM_NOSTRIP)) {
5345 if (brackets || *phead)
5346 white++; /* Keep interior whitespace */
5347 skip = true;
5348 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005349 break;
5350
5351 default:
5352 break;
5353 }
5354
5355 switch (ch) {
5356 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005357 if (!brackets && paren == 1 && !(flags & SPARM_GREEDY)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005358 i++;
5359 nasm_assert(i < nparam);
5360 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005361 *pep = NULL;
5362 bracketed = false;
5363 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005364 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005365 }
5366 break;
5367
5368 case '{':
5369 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005370 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5371 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005372 }
5373 brackets++;
5374 break;
5375
5376 case '}':
5377 if (brackets > 0) {
5378 if (!--brackets)
5379 skip = bracketed;
5380 }
5381 break;
5382
5383 case '(':
5384 if (!brackets)
5385 paren++;
5386 break;
5387
5388 case ')':
5389 if (!brackets) {
5390 paren--;
5391 if (!paren) {
5392 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005393 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005394 }
5395 }
5396 break;
5397
5398 default:
5399 break; /* Normal token */
5400 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005401
5402 if (!skip) {
5403 Token *t;
5404
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005405 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005406
5407 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005408 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005409 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005410 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005411 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005412 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005413 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005414 }
5415 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005416
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005417 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005418 * Possible further processing of parameters. Note that the
5419 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005420 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005421 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005422 enum sparmflags flags = m->params[i].flags;
5423
5424 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005425 /* Evaluate this parameter as a number */
5426 struct ppscan pps;
5427 struct tokenval tokval;
5428 expr *evalresult;
5429 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005430
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005431 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5432 pps.ntokens = -1;
5433 tokval.t_type = TOKEN_INVALID;
5434 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005435
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005436 free_tlist(eval_param);
5437 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005438
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005439 if (!evalresult) {
5440 /* Nothing meaningful to do */
5441 } else if (tokval.t_type) {
5442 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5443 } else if (!is_simple(evalresult)) {
5444 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5445 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005446 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005447 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005448 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005449
5450 if (flags & SPARM_STR) {
5451 /* Convert expansion to a quoted string */
5452 char *arg;
5453 Token *qs;
5454
5455 qs = expand_smacro_noreset(params[i]);
5456 arg = detoken(qs, false);
5457 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005458 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005459 nasm_free(arg);
5460 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005461 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005462 }
5463
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005464 /* Note: we own the expansion this returns. */
5465 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005466
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005467 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005468 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005469 tline = tafter;
5470
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005471 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005472 cond_comma = false;
5473
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005474 while (t) {
5475 enum pp_token_type type = t->type;
5476 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005477
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005478 switch (type) {
5479 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005480 delete_Token(t);
5481 t = dup_Token(tline, mstart);
5482 break;
5483
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005484 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005485 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005486 size_t mlen = strlen(m->name);
5487 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005488 char *p;
5489
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005490 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005491 if (t->type == TOK_LOCAL_MACRO) {
5492 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005493 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005494 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005495
H. Peter Anvin8571f062019-09-23 16:40:03 -07005496 psp = tok_text(mstart);
5497 get_ctx(psp, &pep);
5498 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005499
H. Peter Anvin8571f062019-09-23 16:40:03 -07005500 len = mlen + plen;
5501 p = nasm_malloc(len + 1);
5502 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005503 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005504 len = mlen;
5505 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005506 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005507 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005508 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005509 set_text_free(t, p, len);
5510
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005511 t->next = tline;
5512 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005513 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005514
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005515 case TOK_COND_COMMA:
5516 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005517 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005518 break;
5519
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005520 case TOK_ID:
5521 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005522 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005523 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005524 /*
5525 * Chain this into the target line *before* expanding,
5526 * that way we pick up any arguments to the new macro call,
5527 * if applicable.
5528 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005529 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005530 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005531 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005532 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005533 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005534 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005535 default:
5536 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005537 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005538 nasm_assert(!tup && param < nparam);
5539 delete_Token(t);
5540 t = NULL;
5541 tup = tnext;
5542 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005543 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005544 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005545 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005546 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005547 }
5548
5549 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005550 Token *endt = tline;
5551
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005552 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005553 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005554 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005555 t = t->next;
5556 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005557 }
5558
5559 if (tnext) {
5560 t = tnext;
5561 } else {
5562 t = tup;
5563 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005564 }
5565 }
5566
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005567 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005568 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005569 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005570
5571 m->in_progress = false;
5572
5573 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005574 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005575 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005576
5577 /*
5578 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005579 * and then advance to the next input token. Note that this is
5580 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005581 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005582not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005583 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005584 m = NULL;
5585done:
5586 smacro_deadman.levels++;
5587 if (unlikely(params))
5588 free_tlist_array(params, nparam);
5589 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005590}
5591
5592/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005593 * Expand all single-line macro calls made in the given line.
5594 * Return the expanded version of the line. The original is deemed
5595 * to be destroyed in the process. (In reality we'll just move
5596 * Tokens from input to output a lot of the time, rather than
5597 * actually bothering to destroy and replicate.)
5598 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005599static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005600{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005601 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005602 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5603 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005604 return expand_smacro_noreset(tline);
5605}
5606
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005607static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005608{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005609 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005610 bool expanded;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005611 errhold errhold; /* Hold warning/errors during expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005612
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005613 if (!org_tline)
5614 return NULL; /* Empty input */
5615
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005616 /*
5617 * Trick: we should avoid changing the start token pointer since it can
5618 * be contained in "next" field of other token. Because of this
5619 * we allocate a copy of first token and work with it; at the end of
5620 * routine we copy it back
5621 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005622 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005623
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005624 /*
5625 * Pretend that we always end up doing expansion on the first pass;
5626 * that way %+ get processed. However, if we process %+ before the
5627 * first pass, we end up with things like MACRO %+ TAIL trying to
5628 * look up the macro "MACROTAIL", which we don't want.
5629 */
5630 expanded = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005631
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005632 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005633 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005634 {
5635 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005636 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5637 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005638 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5639 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005640 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5641 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005642 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5643 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5644 }
5645 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005646 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005647
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005648 /*
5649 * We hold warnings/errors until we are done this this loop. It is
5650 * possible for nuisance warnings to appear that disappear on later
5651 * passes.
5652 */
5653 errhold = nasm_error_hold_push();
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005654
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005655 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005656 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005657
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005658 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005659 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005660
5661 /*
5662 * Now scan the entire line and look for successive TOK_IDs
5663 * that resulted after expansion (they can't be produced by
5664 * tokenize()). The successive TOK_IDs should be concatenated.
5665 * Also we look for %+ tokens and concatenate the tokens
5666 * before and after them (without white spaces in between).
5667 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005668 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5669 break; /* Done again! */
5670
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005671 nasm_error_hold_pop(errhold, false);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005672 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005673 }
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005674 nasm_error_hold_pop(errhold, true);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005675
H. Peter Anvin8571f062019-09-23 16:40:03 -07005676 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005677 /*
5678 * The expression expanded to empty line;
5679 * we can't return NULL because of the "trick" above.
5680 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005681 */
5682
5683 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005684 }
5685
H. Peter Anvin8571f062019-09-23 16:40:03 -07005686 steal_Token(org_tline, tline);
5687 org_tline->next = tline->next;
5688 delete_Token(tline);
5689
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005690 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005691}
5692
5693/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005694 * Similar to expand_smacro but used exclusively with macro identifiers
5695 * right before they are fetched in. The reason is that there can be
5696 * identifiers consisting of several subparts. We consider that if there
5697 * are more than one element forming the name, user wants a expansion,
5698 * otherwise it will be left as-is. Example:
5699 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005700 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005701 *
5702 * the identifier %$abc will be left as-is so that the handler for %define
5703 * will suck it and define the corresponding value. Other case:
5704 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005705 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005706 *
5707 * In this case user wants name to be expanded *before* %define starts
5708 * working, so we'll expand %$abc into something (if it has a value;
5709 * otherwise it will be left as-is) then concatenate all successive
5710 * PP_IDs into one.
5711 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005712static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005713{
5714 Token *cur, *oldnext = NULL;
5715
H. Peter Anvin734b1882002-04-30 21:01:08 +00005716 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005717 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005718
5719 cur = tline;
5720 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005721 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5722 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005723 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005724
5725 /* If identifier consists of just one token, don't expand */
5726 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005727 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005728
H. Peter Anvine2c80182005-01-15 22:15:51 +00005729 if (cur) {
5730 oldnext = cur->next; /* Detach the tail past identifier */
5731 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005732 }
5733
H. Peter Anvin734b1882002-04-30 21:01:08 +00005734 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005735
H. Peter Anvine2c80182005-01-15 22:15:51 +00005736 if (cur) {
5737 /* expand_smacro possibly changhed tline; re-scan for EOL */
5738 cur = tline;
5739 while (cur && cur->next)
5740 cur = cur->next;
5741 if (cur)
5742 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005743 }
5744
5745 return tline;
5746}
5747
5748/*
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005749 * This is called from find_mmacro_in_list() after finding a suitable macro.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005750 */
5751static MMacro *use_mmacro(MMacro *m, int *nparamp, Token ***paramsp)
5752{
5753 int nparam = *nparamp;
5754 Token **params = *paramsp;
5755
5756 /*
5757 * This one is right. Just check if cycle removal
5758 * prohibits us using it before we actually celebrate...
5759 */
5760 if (m->in_progress > m->max_depth) {
5761 if (m->max_depth > 0) {
5762 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
5763 m->max_depth);
5764 }
5765 nasm_free(params);
5766 *nparamp = 0;
5767 *paramsp = NULL;
5768 return NULL;
5769 }
5770
5771 /*
5772 * It's right, and we can use it. Add its default
5773 * parameters to the end of our list if necessary.
5774 */
5775 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
5776 int newnparam = m->nparam_min + m->ndefs;
5777 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5778 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5779 (newnparam - nparam) * sizeof(*params));
5780 nparam = newnparam;
5781 }
5782 /*
5783 * If we've gone over the maximum parameter count (and
5784 * we're in Plus mode), ignore parameters beyond
5785 * nparam_max.
5786 */
5787 if (m->plus && nparam > m->nparam_max)
5788 nparam = m->nparam_max;
5789
5790 /*
5791 * If nparam was adjusted above, make sure the list is still
5792 * NULL-terminated.
5793 */
5794 params[nparam+1] = NULL;
5795
5796 /* Done! */
5797 *paramsp = params;
5798 *nparamp = nparam;
5799 return m;
5800}
5801
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005802/*
5803 * Search a macro list and try to find a match. If matching, call
5804 * use_mmacro() to set up the macro call. m points to the list of
5805 * search, which is_mmacro() sets to the first *possible* match.
5806 */
5807static MMacro *
5808find_mmacro_in_list(MMacro *m, const char *finding,
5809 int *nparamp, Token ***paramsp)
5810{
5811 int nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005812
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005813 while (m) {
5814 if (m->nparam_min <= nparam
5815 && (m->plus || nparam <= m->nparam_max)) {
5816 /*
5817 * This one matches, use it.
5818 */
5819 return use_mmacro(m, nparamp, paramsp);
5820 }
5821
5822 /*
5823 * Otherwise search for the next one with a name match.
5824 */
5825 list_for_each(m, m->next) {
5826 if (!mstrcmp(m->name, finding, m->casesense))
5827 break;
5828 }
5829 }
5830
5831 return NULL;
5832}
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005833
5834/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005835 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005836 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005837 * to check for an initial label - that's taken care of in
5838 * expand_mmacro - but must check numbers of parameters. Guaranteed
5839 * to be called with tline->type == TOK_ID, so the putative macro
5840 * name is easy to find.
5841 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005842static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005843{
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005844 MMacro *head, *m, *found;
5845 Token **params, **comma;
5846 int raw_nparam, nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005847 const char *finding = tok_text(tline);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005848 bool empty_args = !tline->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005849
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005850 *nparamp = 0;
5851 *paramsp = NULL;
5852
H. Peter Anvin8571f062019-09-23 16:40:03 -07005853 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005854
5855 /*
5856 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005857 * name which isn't already excluded by macro cycle removal.
5858 * (The cycle removal test here helps optimize the case of wrapping
5859 * instructions, and is cheap to do here.)
5860 *
5861 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005862 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005863 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005864 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005865 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005866 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005867 (m->in_progress != 1 || m->max_depth > 0))
5868 break; /* Found something that needs consideration */
5869 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005870 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005871 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005872
5873 /*
5874 * OK, we have a potential macro. Count and demarcate the
5875 * parameters.
5876 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005877 comma = count_mmac_params(tline->next, nparamp, paramsp);
5878 raw_nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005879
5880 /*
5881 * Search for an exact match. This cannot come *before* the m
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005882 * found in the list search before, so we can start there.
5883 *
5884 * If found is NULL and *paramsp has been cleared, then we
5885 * encountered an error for which we have already issued a
5886 * diagnostic, so we should not proceed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005887 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005888 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5889 if (!*paramsp)
5890 return NULL;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005891
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005892 nparam = *nparamp;
5893 params = *paramsp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005894
5895 /*
5896 * Special weirdness: in NASM < 2.15, an expansion of
5897 * *only* whitespace, as can happen during macro expansion under
5898 * certain circumstances, is counted as zero arguments for the
5899 * purpose of %0, but one argument for the purpose of macro
5900 * matching! In particular, this affects:
5901 *
5902 * foobar %1
5903 *
5904 * ... with %1 being empty; this would call the one-argument
5905 * version of "foobar" with an empty argument, equivalent to
5906 *
5907 * foobar {%1}
5908 *
5909 * ... except that %0 would be set to 0 inside foobar, even if
5910 * foobar is declared with "%macro foobar 1" or equivalent!
5911 *
5912 * The proper way to do that is to define "%macro foobar 0-1".
5913 *
5914 * To be compatible without doing something too stupid, try to
5915 * match a zero-argument macro first, but if that fails, try
5916 * for a one-argument macro with the above behavior.
5917 *
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005918 * Furthermore, NASM < 2.15 will match stripping a tailing empty
5919 * argument, but in that case %0 *does* reflect that this argument
5920 * have been stripped; this is handled in count_mmac_params().
5921 *
5922 * To disable these insane legacy behaviors, use:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005923 *
5924 * %pragma preproc sane_empty_expansion yes
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005925 *
5926 *!macro-params-legacy [on] improperly calling multi-line macro for legacy support
5927 *! warns about \i{multi-line macros} being invoked
5928 *! with the wrong number of parameters, but for bug-compatibility
5929 *! with NASM versions older than 2.15, NASM tried to fix up the
5930 *! parameters to match the legacy behavior and call the macro anyway.
5931 *! This can happen in certain cases where there are empty arguments
5932 *! without braces, sometimes as a result of macro expansion.
5933 *!-
5934 *! The legacy behavior is quite strange and highly context-dependent,
5935 *! and can be disabled with:
5936 *!-
H. Peter Anvin (Intel)de8817d2020-06-27 22:30:50 -07005937 *! \c %pragma preproc sane_empty_expansion true
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005938 *!-
5939 *! It is highly recommended to use this option in new code.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005940 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005941 if (!ppopt.sane_empty_expansion) {
5942 if (!found) {
5943 if (raw_nparam == 0 && !empty_args) {
5944 /*
5945 * A single all-whitespace parameter as the only thing?
5946 * Look for a one-argument macro, but don't adjust
5947 * *nparamp.
5948 */
5949 int bogus_nparam = 1;
5950 params[2] = NULL;
5951 found = find_mmacro_in_list(m, finding, &bogus_nparam, paramsp);
5952 } else if (raw_nparam > 1 && comma) {
5953 Token *comma_tail = *comma;
5954
5955 /*
5956 * Drop the terminal argument and try again.
5957 * If we fail, we need to restore the comma to
5958 * preserve tlist.
5959 */
5960 *comma = NULL;
5961 *nparamp = raw_nparam - 1;
5962 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5963 if (found)
5964 free_tlist(comma_tail);
5965 else
5966 *comma = comma_tail;
5967 }
5968
5969 if (!*paramsp)
5970 return NULL;
5971 } else if (comma) {
5972 free_tlist(*comma);
5973 *comma = NULL;
5974 if (raw_nparam > found->nparam_min &&
5975 raw_nparam <= found->nparam_min + found->ndefs) {
5976 /* Replace empty argument with default parameter */
5977 params[raw_nparam] =
5978 found->defaults[raw_nparam - found->nparam_min];
5979 } else if (raw_nparam > found->nparam_max && found->plus) {
5980 /* Just drop the comma, don't adjust argument count */
5981 } else {
5982 /* Drop argument. This may cause nparam < nparam_min. */
5983 params[raw_nparam] = NULL;
5984 *nparamp = nparam = raw_nparam - 1;
5985 }
5986 }
5987
5988 if (found) {
5989 if (raw_nparam < found->nparam_min ||
5990 (raw_nparam > found->nparam_max && !found->plus)) {
5991 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5992 "improperly calling multi-line macro `%s' with %d parameters",
5993 found->name, raw_nparam);
5994 } else if (comma) {
5995 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
5996 "dropping trailing empty parameter in call to multi-line macro `%s'", found->name);
5997 }
5998 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005999 }
6000
6001 /*
6002 * After all that, we didn't find one with the right number of
6003 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006004 *!
6005 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
6006 *! warns about \i{multi-line macros} being invoked
6007 *! with the wrong number of parameters. See \k{mlmacover} for an
6008 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006009 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006010 if (found)
6011 return found;
6012
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006013 nasm_warn(WARN_MACRO_PARAMS_MULTI,
6014 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006015 finding, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006016 nasm_free(*paramsp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006017 return NULL;
6018}
6019
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006020
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006021#if 0
6022
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006023/*
6024 * Save MMacro invocation specific fields in
6025 * preparation for a recursive macro expansion
6026 */
6027static void push_mmacro(MMacro *m)
6028{
6029 MMacroInvocation *i;
6030
6031 i = nasm_malloc(sizeof(MMacroInvocation));
6032 i->prev = m->prev;
6033 i->params = m->params;
6034 i->iline = m->iline;
6035 i->nparam = m->nparam;
6036 i->rotate = m->rotate;
6037 i->paramlen = m->paramlen;
6038 i->unique = m->unique;
6039 i->condcnt = m->condcnt;
6040 m->prev = i;
6041}
6042
6043
6044/*
6045 * Restore MMacro invocation specific fields that were
6046 * saved during a previous recursive macro expansion
6047 */
6048static void pop_mmacro(MMacro *m)
6049{
6050 MMacroInvocation *i;
6051
6052 if (m->prev) {
6053 i = m->prev;
6054 m->prev = i->prev;
6055 m->params = i->params;
6056 m->iline = i->iline;
6057 m->nparam = i->nparam;
6058 m->rotate = i->rotate;
6059 m->paramlen = i->paramlen;
6060 m->unique = i->unique;
6061 m->condcnt = i->condcnt;
6062 nasm_free(i);
6063 }
6064}
6065
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006066#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006067
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006068/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07006069 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006070 */
6071static void list_mmacro_call(const MMacro *m)
6072{
6073 const char prefix[] = " ;;; [macro] ";
6074 size_t namelen, size;
6075 char *buf, *p;
6076 unsigned int i;
6077 const Token *t;
6078
6079 namelen = strlen(m->iname);
6080 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
6081
6082 for (i = 1; i <= m->nparam; i++) {
6083 int j = 0;
6084 size += 3; /* Braces and space/comma */
6085 list_for_each(t, m->params[i]) {
6086 if (j++ >= m->paramlen[i])
6087 break;
6088 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
6089 }
6090 }
6091
6092 buf = p = nasm_malloc(size);
6093 p = mempcpy(p, prefix, sizeof(prefix) - 1);
6094 p = mempcpy(p, m->iname, namelen);
6095 *p++ = ' ';
6096
6097 for (i = 1; i <= m->nparam; i++) {
6098 int j = 0;
6099 *p++ = '{';
6100 list_for_each(t, m->params[i]) {
6101 if (j++ >= m->paramlen[i])
6102 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006103 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006104 }
6105 *p++ = '}';
6106 *p++ = ',';
6107 }
6108
6109 *--p = '\0'; /* Replace last delimeter with null */
6110 lfmt->line(LIST_MACRO, -1, buf);
6111 nasm_free(buf);
6112}
6113
6114/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006115 * Expand the multi-line macro call made by the given line, if
6116 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006117 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006118 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006119static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006120{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006121 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006122 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006123 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006124 Token **params, *t, *tt;
6125 MMacro *m;
6126 Line *l, *ll;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006127 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07006128 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006129 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006130
6131 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006132 t = skip_white(t);
6133 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07006134 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006135 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006136 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006137 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006138 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07006139 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006140 Token *last;
6141 /*
6142 * We have an id which isn't a macro call. We'll assume
6143 * it might be a label; we'll also check to see if a
6144 * colon follows it. Then, if there's another id after
6145 * that lot, we'll check it again for macro-hood.
6146 */
6147 label = last = t;
6148 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006149 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006150 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006151 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006152 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006153 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006154 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006155 last = t, t = t->next;
6156 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006157 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006158 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006159 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006160 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006161 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006162 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006163
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006164 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
6165 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
6166 if (!mmacro_deadman.triggered) {
6167 nasm_nonfatal("interminable multiline macro recursion");
6168 mmacro_deadman.triggered = true;
6169 }
6170 return 0;
6171 }
6172
6173 mmacro_deadman.total++;
6174 mmacro_deadman.levels++;
6175
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006176 /*
6177 * Fix up the parameters: this involves stripping leading and
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006178 * trailing whitespace and stripping braces if they are present.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006179 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006180 nasm_newn(paramlen, nparam+1);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006181
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006182 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006183 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08006184 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006185 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006186 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006187
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006188 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006189 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006190 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006191 brace = 1;
6192 braced = true;
6193 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006194 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006195
6196 params[i] = t;
6197 for (; t; t = t->next) {
6198 if (tok_white(t)) {
6199 white++;
6200 continue;
6201 }
6202
6203 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006204 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006205 case ',':
6206 if (comma && !brace)
6207 goto endparam;
6208 break;
6209
6210 case '{':
6211 brace++;
6212 break;
6213
6214 case '}':
6215 brace--;
6216 if (braced && !brace) {
6217 paramlen[i] += white;
6218 goto endparam;
6219 }
6220 break;
6221
6222 default:
6223 break;
6224 }
6225 }
6226
6227 paramlen[i] += white + 1;
6228 white = 0;
6229 }
6230 endparam:
6231 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006232 }
6233
6234 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006235 * OK, we have a MMacro structure together with a set of
6236 * parameters. We must now go through the expansion and push
6237 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00006238 * parameter tokens and macro-local tokens doesn't get done
6239 * until the single-line macro substitution process; this is
6240 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006241 * later through %rotate and give the right semantics for
6242 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006243 *
6244 * First, push an end marker on to istk->expansion, mark this
6245 * macro as in progress, and set up its invocation-specific
6246 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006247 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006248 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006249 ll->next = istk->expansion;
6250 ll->finishes = m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006251 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006252 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006253
6254 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006255 * Save the previous MMacro expansion in the case of
6256 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006257 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006258#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006259 if (m->max_depth && m->in_progress)
6260 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006261#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006262
6263 m->in_progress ++;
6264 m->params = params;
6265 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006266 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006267 m->nparam = nparam;
6268 m->rotate = 0;
6269 m->paramlen = paramlen;
6270 m->unique = unique++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006271 m->condcnt = 0;
6272
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006273 m->mstk = istk->mstk;
6274 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006275
6276 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006277 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006278 ll->next = istk->expansion;
6279 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006280 ll->first = dup_tlist(l->first, NULL);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006281 ll->where = l->where;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006282 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006283
6284 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006285 * If we had a label, and this macro definition does not include
6286 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00006287 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006288 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006289 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006290 /*
6291 * We had a label. If this macro contains an %00 parameter,
6292 * save the value as a special parameter (which is what it
6293 * is), otherwise push it as the first line of the macro
6294 * expansion.
6295 */
6296 if (m->capture_label) {
6297 params[0] = dup_Token(NULL, label);
6298 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006299 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006300 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006301 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006302 ll->finishes = NULL;
6303 ll->next = istk->expansion;
6304 istk->expansion = ll;
6305 ll->first = startline;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006306 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006307 if (!dont_prepend) {
6308 while (label->next)
6309 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006310 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006311 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006312 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006313 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006314
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006315 istk->nolist += !!(m->nolist & NL_LIST);
6316 istk->noline += !!(m->nolist & NL_LINE);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006317
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006318 if (!istk->nolist) {
6319 lfmt->uplevel(LIST_MACRO, 0);
6320
6321 if (list_option('m'))
6322 list_mmacro_call(m);
6323 }
6324
6325 if (!istk->noline)
6326 src_macro_push(m, istk->where);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006327
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006328 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006329}
6330
H. Peter Anvin130736c2016-02-17 20:27:41 -08006331/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006332 * This function decides if an error message should be suppressed.
6333 * It will never be called with a severity level of ERR_FATAL or
6334 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006335 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006336static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006337{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006338 /*
6339 * If we're in a dead branch of IF or something like it, ignore the error.
6340 * However, because %else etc are evaluated in the state context
6341 * of the previous branch, errors might get lost:
6342 * %if 0 ... %else trailing garbage ... %endif
6343 * So %else etc should set the ERR_PP_PRECOND flag.
6344 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006345 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006346 ((severity & ERR_PP_PRECOND) ?
6347 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006348 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006349 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006350
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006351 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006352}
6353
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006354static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006355stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006356{
6357 (void)s;
6358 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006359 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006360
H. Peter Anvin8571f062019-09-23 16:40:03 -07006361 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006362}
6363
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006364static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006365stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006366{
6367 (void)s;
6368 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006369 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006370
H. Peter Anvin8571f062019-09-23 16:40:03 -07006371 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006372}
6373
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006374static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006375stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006376{
6377 (void)s;
6378 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006379 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006380
H. Peter Anvin8571f062019-09-23 16:40:03 -07006381 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006382}
6383
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006384static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006385stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006386{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006387 (void)s;
6388 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006389 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006390
6391 switch (globalbits) {
6392 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006393 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006394 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006395 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006396 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006397 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006398 default:
6399 panic();
6400 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006401}
6402
H. Peter Anvin8b262472019-02-26 14:00:54 -08006403/* Add magic standard macros */
6404struct magic_macros {
6405 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006406 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006407 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006408};
6409static const struct magic_macros magic_macros[] =
6410{
H. Peter Anvind2354082019-08-27 16:38:48 -07006411 { "__?FILE?__", 0, stdmac_file },
6412 { "__?LINE?__", 0, stdmac_line },
6413 { "__?BITS?__", 0, stdmac_bits },
6414 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006415 { NULL, 0, NULL }
6416};
6417
6418static void pp_add_magic_stdmac(void)
6419{
6420 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006421 SMacro tmpl;
6422
6423 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006424
6425 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006426 tmpl.nparam = m->nparam;
6427 tmpl.expand = m->func;
6428 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006429 }
6430}
6431
H. Peter Anvin734b1882002-04-30 21:01:08 +00006432static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006433pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006434{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006435 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006436 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006437
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006438 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006439 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006440 nested_mac_count = 0;
6441 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006442 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006443 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006444 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006445 pp_mode = mode;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006446
6447 /* Reset options to default */
6448 nasm_zero(ppopt);
H. Peter Anvinf7606612016-07-13 14:23:48 -07006449
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006450 if (!use_loaded)
6451 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6452 memset(use_loaded, 0, use_package_count * sizeof(bool));
6453
H. Peter Anvin6686de22019-08-10 05:33:14 -07006454 /* First set up the top level input file */
6455 nasm_new(istk);
6456 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006457 if (!istk->fp) {
6458 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'%s%s",
6459 file, errno ? " " : "", errno ? strerror(errno) : "");
6460 }
H. Peter Anvin6686de22019-08-10 05:33:14 -07006461 src_set(0, file);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006462 istk->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006463 istk->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006464
6465 strlist_add(deplist, file);
6466
6467 /*
6468 * Set up the stdmac packages as a virtual include file,
6469 * indicated by a null file pointer.
6470 */
6471 nasm_new(inc);
6472 inc->next = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006473 src_set(0, NULL);
6474 inc->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006475 inc->nolist = !list_option('b');
6476 istk = inc;
6477 lfmt->uplevel(LIST_INCLUDE, 0);
6478
H. Peter Anvin8b262472019-02-26 14:00:54 -08006479 pp_add_magic_stdmac();
6480
H. Peter Anvinf7606612016-07-13 14:23:48 -07006481 if (tasm_compatible_mode)
6482 pp_add_stdmac(nasm_stdmac_tasm);
6483
6484 pp_add_stdmac(nasm_stdmac_nasm);
6485 pp_add_stdmac(nasm_stdmac_version);
6486
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006487 if (extrastdmac)
6488 pp_add_stdmac(extrastdmac);
6489
H. Peter Anvinf7606612016-07-13 14:23:48 -07006490 stdmacpos = stdmacros[0];
6491 stdmacnext = &stdmacros[1];
6492
H. Peter Anvind2456592008-06-19 15:04:18 -07006493 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006494
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006495 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006496 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006497 * other builtins, because it is special -- it varies between
6498 * passes -- but there is really no particular reason to make it
6499 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006500 *
6501 * 0 = dependencies only
6502 * 1 = preparatory passes
6503 * 2 = final pass
6504 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006505 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006506 switch (mode) {
6507 case PP_NORMAL:
6508 apass = pass_final() ? 2 : 1;
6509 break;
6510 case PP_DEPS:
6511 apass = 0;
6512 break;
6513 case PP_PREPROC:
6514 apass = 3;
6515 break;
6516 default:
6517 panic();
6518 }
6519
H. Peter Anvin8571f062019-09-23 16:40:03 -07006520 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006521}
6522
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006523static void pp_init(void)
6524{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006525}
6526
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006527/*
6528 * Get a line of tokens. If we popped the macro expansion/include stack,
6529 * we return a pointer to the dummy token tok_pop; at that point if
6530 * istk is NULL then we have reached end of input;
6531 */
6532static Token tok_pop; /* Dummy token placeholder */
6533
6534static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006535{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006536 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006537 Line *l = istk->expansion;
6538 Token *tline = NULL;
6539 Token *dtline;
6540
H. Peter Anvine2c80182005-01-15 22:15:51 +00006541 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006542 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006543 * buffer or from the input file.
6544 */
6545 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006546 while (l && l->finishes) {
6547 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006548
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006549 nasm_assert(fm == istk->mstk.mstk);
6550
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006551 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006552 /*
6553 * This is a macro-end marker for a macro with no
6554 * name, which means it's not really a macro at all
6555 * but a %rep block, and the `in_progress' field is
6556 * more than 1, meaning that we still need to
6557 * repeat. (1 means the natural last repetition; 0
6558 * means termination by %exitrep.) We have
6559 * therefore expanded up to the %endrep, and must
6560 * push the whole block on to the expansion buffer
6561 * again. We don't bother to remove the macro-end
6562 * marker: we'd only have to generate another one
6563 * if we did.
6564 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006565 fm->in_progress--;
6566 list_for_each(l, fm->expansion) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006567 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006568
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006569 nasm_new(ll);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006570 ll->next = istk->expansion;
6571 ll->first = dup_tlist(l->first, NULL);
6572 ll->where = l->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006573 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006574 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006575 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006576 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006577 MMacro *m = istk->mstk.mstk;
6578
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006579 /*
6580 * Check whether a `%rep' was started and not ended
6581 * within this macro expansion. This can happen and
6582 * should be detected. It's a fatal error because
6583 * I'm too confused to work out how to recover
6584 * sensibly from it.
6585 */
6586 if (defining) {
6587 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006588 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006589 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006590 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006591 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006592 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006593
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006594 /*
6595 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006596 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006597 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006598 istk->mstk = m->mstk;
6599 if (m->name) {
6600 /*
6601 * This was a real macro call, not a %rep, and
6602 * therefore the parameter information needs to
6603 * be freed and the iteration count/nesting
6604 * depth adjusted.
6605 */
6606
6607 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006608 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006609 * If all mmacro processing done,
6610 * clear all counters and the deadman
6611 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006612 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006613 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006614 }
6615
Adam Majer91e72402017-07-25 10:42:01 +02006616#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006617 if (m->prev) {
6618 pop_mmacro(m);
6619 fm->in_progress --;
6620 } else
Adam Majer91e72402017-07-25 10:42:01 +02006621#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006622 {
6623 nasm_free(m->params);
6624 free_tlist(m->iline);
6625 nasm_free(m->paramlen);
6626 fm->in_progress = 0;
6627 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006628 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006629
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006630 if (fm->nolist & NL_LIST) {
6631 istk->nolist--;
6632 } else if (!istk->nolist) {
6633 lfmt->downlevel(LIST_MACRO);
6634 }
6635
6636 if (fm->nolist & NL_LINE) {
6637 istk->noline--;
6638 } else if (!istk->noline) {
6639 if (fm == src_macro_current())
6640 src_macro_pop();
6641 src_update(l->where);
6642 }
6643
6644 istk->where = l->where;
6645
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006646 /*
6647 * FIXME It is incorrect to always free_mmacro here.
6648 * It leads to usage-after-free.
6649 *
6650 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6651 */
6652#if 0
6653 else
6654 free_mmacro(m);
6655#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006656 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006657 istk->expansion = l->next;
6658 nasm_free(l);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006659
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006660 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006661 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006662
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006663 do { /* until we get a line we can use */
6664 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006665
6666 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006667 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006668
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006669 istk->expansion = l->next;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006670 istk->where = l->where;
6671 tline = l->first;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006672 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006673
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006674 if (!istk->noline)
6675 src_update(istk->where);
6676
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006677 if (!istk->nolist) {
6678 line = detoken(tline, false);
6679 lfmt->line(LIST_MACRO, istk->where.lineno, line);
6680 nasm_free(line);
6681 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006682 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006683 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006684 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006685 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006686 } else {
6687 /*
6688 * The current file has ended; work down the istk
6689 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006690 Include *i = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006691 Include *is;
6692
H. Peter Anvin6686de22019-08-10 05:33:14 -07006693 if (i->fp)
6694 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006695 if (i->conds) {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006696 /* nasm_fatal can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006697 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006698 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006699
6700 list_for_each(is, i->next) {
6701 if (is->fp) {
6702 lfmt->downlevel(LIST_INCLUDE);
6703 src_update(is->where);
6704 break;
6705 }
6706 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006707 istk = i->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006708 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006709 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006710 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006711 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006712
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006713 /*
6714 * We must expand MMacro parameters and MMacro-local labels
6715 * _before_ we plunge into directive processing, to cope
6716 * with things like `%define something %1' such as STRUC
6717 * uses. Unless we're _defining_ a MMacro, in which case
6718 * those tokens should be left alone to go into the
6719 * definition; and unless we're in a non-emitting
6720 * condition, in which case we don't want to meddle with
6721 * anything.
6722 */
H. Peter Anvin (Intel)bacf04a2020-06-08 13:29:06 -07006723 if (!defining &&
6724 !(istk->conds && !emitting(istk->conds->state)) &&
6725 !(istk->mstk.mmac && !istk->mstk.mmac->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006726 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006727 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006728
H. Peter Anvine2c80182005-01-15 22:15:51 +00006729 /*
6730 * Check the line to see if it's a preprocessor directive.
6731 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006732 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6733 if (dtline)
6734 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006735 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006736 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006737 * We're defining a multi-line macro. We emit nothing
6738 * at all, and just
6739 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006740 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006741 MMacro *mmac = defining->dstk.mmac;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006742 Line *l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006743
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006744 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006745 l->next = defining->expansion;
6746 l->first = tline;
6747 l->finishes = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006748 l->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006749 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006750
6751 /*
6752 * Remember if this mmacro expansion contains %00:
6753 * if it does, we will have to handle leading labels
6754 * specially.
6755 */
6756 if (mmac) {
6757 const Token *t;
6758 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006759 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006760 mmac->capture_label = true;
6761 }
6762 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006763 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006764 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006765 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006766 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006767 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006768 * directive so we keep our place correctly.
6769 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006770 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006771 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006772 /*
6773 * We're in a %rep block which has been terminated, so
6774 * we're walking through to the %endrep without
6775 * emitting anything. Emit nothing at all, not even a
6776 * blank line: when we emerge from the %rep block we'll
6777 * give a line-number directive so we keep our place
6778 * correctly.
6779 */
6780 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006781 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006782 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006783 if (!expand_mmacro(tline))
6784 return tline;
6785 }
6786 }
6787}
6788
6789static char *pp_getline(void)
6790{
6791 char *line = NULL;
6792 Token *tline;
6793
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006794 while (true) {
6795 tline = pp_tokline();
6796 if (tline == &tok_pop) {
6797 /*
6798 * We popped the macro/include stack. If istk is empty,
6799 * we are at end of input, otherwise just loop back.
6800 */
6801 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006802 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006803 } else {
6804 /*
6805 * De-tokenize the line and emit it.
6806 */
6807 line = detoken(tline, true);
6808 free_tlist(tline);
6809 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006810 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006811 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006812
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006813 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006814 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006815 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006816 nasm_free(buf);
6817 }
6818
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006819 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006820}
6821
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006822static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006823{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006824 if (defining) {
6825 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006826 nasm_nonfatal("end of file while still defining macro `%s'",
6827 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006828 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006829 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006830 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006831
6832 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006833 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006834 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006835
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006836 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006837 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006838 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006839 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006840 Include *i = istk;
6841 istk = istk->next;
6842 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006843 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006844 }
6845 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006846 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006847 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006848}
6849
6850static void pp_cleanup_session(void)
6851{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006852 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006853 free_llist(predef);
6854 predef = NULL;
6855 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006856 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006857}
6858
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006859static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006860{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006861 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006862}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006863
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006864static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006865{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006866 Token *inc, *space, *name;
6867 Line *l;
6868
H. Peter Anvin734b1882002-04-30 21:01:08 +00006869 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006870 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006871 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006872
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006873 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006874 l->next = predef;
6875 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006876 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006877 predef = l;
6878}
6879
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006880static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006881{
6882 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006883 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006884 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006885
6886 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006887 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006888 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006889 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006890 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006891 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006892 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006893 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006894
H. Peter Anvin8571f062019-09-23 16:40:03 -07006895 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006896 if (space->next->type != TOK_PREPROC_ID &&
6897 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006898 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006899
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006900 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006901 l->next = predef;
6902 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006903 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006904 predef = l;
6905}
6906
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006907static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006908{
6909 Token *def, *space;
6910 Line *l;
6911
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006912 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006913 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006914 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006915
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006916 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006917 l->next = predef;
6918 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006919 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006920 predef = l;
6921}
6922
H. Peter Anvin05990342018-06-11 13:32:42 -07006923/* Insert an early preprocessor command that doesn't need special handling */
6924static void pp_pre_command(const char *what, char *string)
6925{
6926 char *cmd;
6927 Token *def, *space;
6928 Line *l;
6929
6930 def = tokenize(string);
6931 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006932 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006933 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6934 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6935 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006936 }
6937
6938 l = nasm_malloc(sizeof(Line));
6939 l->next = predef;
6940 l->first = def;
6941 l->finishes = NULL;
6942 predef = l;
6943}
6944
H. Peter Anvinf7606612016-07-13 14:23:48 -07006945static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006946{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006947 macros_t **mp;
6948
6949 /* Find the end of the list and avoid duplicates */
6950 for (mp = stdmacros; *mp; mp++) {
6951 if (*mp == macros)
6952 return; /* Nothing to do */
6953 }
6954
6955 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6956
6957 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006958}
6959
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006960static void pp_extra_stdmac(macros_t *macros)
6961{
6962 extrastdmac = macros;
6963}
6964
H. Peter Anvin8571f062019-09-23 16:40:03 -07006965/* Create a numeric token */
6966static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006967{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006968 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006969 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006970 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006971}
6972
H. Peter Anvin8571f062019-09-23 16:40:03 -07006973/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006974static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006975{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006976 char *p = nasm_quote(str, &len);
6977 return new_Token_free(next, TOK_STRING, p, len);
6978}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006979static Token *make_tok_qstr(Token *next, const char *str)
6980{
6981 return make_tok_qstr_len(next, str, strlen(str));
6982}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006983
6984/* Create a single-character operator token */
6985static Token *make_tok_char(Token *next, char op)
6986{
6987 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6988 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006989 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006990}
6991
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006992/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006993 * Descent the macro hierarchy and display the expansion after
6994 * encountering an error message.
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006995 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006996static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006997{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006998 const MMacro *m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006999
H. Peter Anvinddb29062018-12-11 00:06:29 -08007000 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007001
7002 while ((m = src_error_down())) {
7003 nasm_error(severity, "... from macro `%s' defined", m->name);
7004 }
7005
7006 src_error_reset();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007007}
7008
H. Peter Anvine7469712016-02-18 02:20:59 -08007009const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07007010 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007011 pp_reset,
7012 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08007013 pp_cleanup_pass,
7014 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03007015 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04007016 pp_pre_define,
7017 pp_pre_undefine,
7018 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07007019 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007020 pp_include_path,
7021 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07007022 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007023};