blob: 693cbcbb7eee2d0d2eda65ada622c1ddf55ef030 [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 Anvin (Intel)fcd3cb82020-07-10 17:22:47 -07002003 case TOK_INDIRECT:
2004 /*
2005 * This won't happen in when emitting to the assembler,
2006 * but can happen when emitting output for some of the
2007 * list options. The token string doesn't actually include
2008 * the brackets in this case.
2009 */
2010 len += 3; /* %[] */
2011 break;
2012
H. Peter Anvin8571f062019-09-23 16:40:03 -07002013 default:
2014 break; /* No modifications */
2015 }
2016
2017 if (debug_level(2)) {
2018 unsigned int t_len = t->len;
2019 unsigned int s_len = tok_strlen(tok_text(t));
2020 if (t_len != s_len) {
2021 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
2022 tok_text(t), t->type, s_len, t_len);
2023 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002024 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07002026
H. Peter Anvin8571f062019-09-23 16:40:03 -07002027 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002028 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002029
H. Peter Anvin734b1882002-04-30 21:01:08 +00002030 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002031
H. Peter Anvin (Intel)fcd3cb82020-07-10 17:22:47 -07002032 list_for_each(t, tlist) {
2033 switch (t->type) {
2034 case TOK_INDIRECT:
2035 *p++ = '%';
2036 *p++ = '[';
2037 p = mempcpy(p, tok_text(t), t->len);
2038 *p++ = ']';
2039 break;
2040
2041 default:
2042 p = mempcpy(p, tok_text(t), t->len);
2043 }
2044 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002045 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002046
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002047 return line;
2048}
2049
2050/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002051 * A scanner, suitable for use by the expression evaluator, which
2052 * operates on a line of Tokens. Expects a pointer to a pointer to
2053 * the first token in the line to be passed in as its private_data
2054 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002055 *
2056 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00002057 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002058struct ppscan {
2059 Token *tptr;
2060 int ntokens;
2061};
2062
H. Peter Anvine2c80182005-01-15 22:15:51 +00002063static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002064{
H. Peter Anvin8b262472019-02-26 14:00:54 -08002065 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002066 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002067 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002068
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08002070 if (pps->ntokens && (tline = pps->tptr)) {
2071 pps->ntokens--;
2072 pps->tptr = tline->next;
2073 } else {
2074 pps->tptr = NULL;
2075 pps->ntokens = 0;
2076 return tokval->t_type = TOKEN_EOS;
2077 }
2078 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002079
H. Peter Anvin8571f062019-09-23 16:40:03 -07002080 txt = tok_text(tline);
2081 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002082
H. Peter Anvin8571f062019-09-23 16:40:03 -07002083 if (txt[0] == '$') {
2084 if (!txt[1]) {
2085 return tokval->t_type = TOKEN_HERE;
2086 } else if (txt[1] == '$' && !txt[2]) {
2087 return tokval->t_type = TOKEN_BASE;
2088 } else if (tline->type == TOK_ID) {
2089 tokval->t_charptr++;
2090 return tokval->t_type = TOKEN_ID;
2091 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002092 }
2093
H. Peter Anvin8571f062019-09-23 16:40:03 -07002094 switch (tline->type) {
2095 default:
2096 if (tline->len == 1)
2097 return tokval->t_type = txt[0];
2098 /* fall through */
2099 case TOK_ID:
2100 return nasm_token_hash(txt, tokval);
2101
2102 case TOK_NUMBER:
2103 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002104 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002105 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 if (rn_error)
2107 return tokval->t_type = TOKEN_ERRNUM;
2108 else
2109 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002110 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002111
H. Peter Anvin8571f062019-09-23 16:40:03 -07002112 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002114
2115 case TOK_STRING:
2116 tokval->t_charptr = (char *)unquote_token(tline);
2117 tokval->t_inttwo = tline->len;
2118 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002119 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002120}
2121
2122/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002123 * 1. An expression (true if nonzero 0)
2124 * 2. The keywords true, on, yes for true
2125 * 3. The keywords false, off, no for false
2126 * 4. An empty line, for true
2127 *
2128 * On error, return defval (usually the previous value)
2129 */
2130static bool pp_get_boolean_option(Token *tline, bool defval)
2131{
2132 static const char * const noyes[] = {
2133 "no", "yes",
2134 "false", "true",
2135 "off", "on"
2136 };
2137 struct ppscan pps;
2138 struct tokenval tokval;
2139 expr *evalresult;
2140
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002141 tline = skip_white(tline);
2142 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002143 return true;
2144
2145 if (tline->type == TOK_ID) {
2146 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002147 const char *txt = tok_text(tline);
2148
H. Peter Anvind2354082019-08-27 16:38:48 -07002149 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002150 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002151 return i & 1;
2152 }
2153
2154 pps.tptr = NULL;
2155 pps.tptr = tline;
2156 pps.ntokens = -1;
2157 tokval.t_type = TOKEN_INVALID;
2158 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2159
2160 if (!evalresult)
2161 return true;
2162
2163 if (tokval.t_type)
2164 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2165 if (!is_really_simple(evalresult)) {
2166 nasm_nonfatal("boolean flag expression must be a constant");
2167 return defval;
2168 }
2169
2170 return reloc_value(evalresult) != 0;
2171}
2172
2173/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002174 * Compare a string to the name of an existing macro; this is a
2175 * simple wrapper which calls either strcmp or nasm_stricmp
2176 * depending on the value of the `casesense' parameter.
2177 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002178static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002179{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002180 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002181}
2182
2183/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002184 * Compare a string to the name of an existing macro; this is a
2185 * simple wrapper which calls either strcmp or nasm_stricmp
2186 * depending on the value of the `casesense' parameter.
2187 */
2188static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2189{
2190 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2191}
2192
2193/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002194 * Return the Context structure associated with a %$ token. Return
2195 * NULL, having _already_ reported an error condition, if the
2196 * context stack isn't deep enough for the supplied number of $
2197 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002198 *
2199 * If "namep" is non-NULL, set it to the pointer to the macro name
2200 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002201 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002202static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002203{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002204 Context *ctx;
2205 int i;
2206
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002207 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002208 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002209
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002210 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002211 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002212
H. Peter Anvine2c80182005-01-15 22:15:51 +00002213 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002214 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002215 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002216 }
2217
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002218 name += 2;
2219 ctx = cstk;
2220 i = 0;
2221 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002222 name++;
2223 i++;
2224 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002225 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002226 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002227 nasm_nonfatal("`%s': context stack is only"
2228 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002229 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002230 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002231
2232 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002233 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002234
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002235 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002236}
2237
2238/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002239 * Open an include file. This routine must always return a valid
2240 * file pointer if it returns - it's responsible for throwing an
2241 * ERR_FATAL and bombing out completely if not. It should also try
2242 * the include path one by one until it finds the file or reaches
2243 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002244 *
2245 * Note: for INC_PROBE the function returns NULL at all times;
2246 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002247 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002248enum incopen_mode {
2249 INC_NEEDED, /* File must exist */
2250 INC_OPTIONAL, /* Missing is OK */
2251 INC_PROBE /* Only an existence probe */
2252};
2253
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002254/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002255static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002256 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002257{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002258 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002259 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002260 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002261 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002262 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002263
H. Peter Anvine2c80182005-01-15 22:15:51 +00002264 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002265 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002266 if (omode == INC_PROBE) {
2267 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002268 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002269 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002270 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002271 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002272 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002273 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002274 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002275 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002276 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002277
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002278 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002279
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002280 if (!ip) {
2281 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002282 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002283 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002284
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002285 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002286 ip = ip->next;
2287 }
2288}
2289
2290/*
2291 * Open a file, or test for the presence of one (depending on omode),
2292 * considering the include path.
2293 */
2294static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002295 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002296 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002297 enum incopen_mode omode,
2298 enum file_flags fmode)
2299{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002300 struct hash_insert hi;
2301 void **hp;
2302 char *path;
2303 FILE *fp = NULL;
2304
2305 hp = hash_find(&FileHash, file, &hi);
2306 if (hp) {
2307 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002308 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002309 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002310 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002311 } else {
2312 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002313 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002314
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002315 /* Positive or negative result */
2316 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002317
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002318 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002319 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002320 */
2321 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002322 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002323 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002324
H. Peter Anvin (Intel)5d68f982020-06-01 12:32:35 -07002325 if (path && !fp && omode != INC_PROBE)
2326 fp = nasm_open_read(path, fmode);
2327
2328 if (omode == INC_NEEDED && !fp) {
2329 if (!path)
2330 errno = ENOENT;
2331
2332 nasm_nonfatal("unable to open include file `%s': %s",
2333 file, strerror(errno));
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002334 }
2335
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002336 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002337 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002338
2339 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002340}
2341
2342/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002343 * Opens an include or input file. Public version, for use by modules
2344 * that get a file:lineno pair and need to look at the file again
2345 * (e.g. the CodeView debug backend). Returns NULL on failure.
2346 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002347FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002348{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002349 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002350}
2351
2352/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002353 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002354 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002355 * return true if _any_ single-line macro of that name is defined.
2356 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002357 * `nparam' or no parameters is defined.
2358 *
2359 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002360 * defined, or nparam is -1, the address of the definition structure
2361 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002362 * is NULL, no action will be taken regarding its contents, and no
2363 * error will occur.
2364 *
2365 * Note that this is also called with nparam zero to resolve
2366 * `ifdef'.
2367 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002368static bool
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002369smacro_defined(Context *ctx, const char *name, int nparam, SMacro **defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002370 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002371{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002372 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002373 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002374
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002375 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvind2354082019-08-27 16:38:48 -07002376
2377restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002378 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002379
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 while (m) {
2381 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002382 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002383 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002384 if (m->alias && !find_alias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002385 if (!ppopt.noaliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002386 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002387 goto restart;
2388 } else {
2389 continue;
2390 }
2391 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002393 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002394 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002395 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396 }
2397 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002398 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002399
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002400 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002401}
2402
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002403/* param should be a natural number [0; INT_MAX] */
2404static int read_param_count(const char *str)
2405{
2406 int result;
2407 bool err;
2408
2409 result = readnum(str, &err);
2410 if (result < 0 || result > INT_MAX) {
2411 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002412 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2413 str, 0, INT_MAX);
2414 } else if (err)
2415 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002416 return result;
2417}
2418
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002419/*
2420 * Count and mark off the parameters in a multi-line macro call.
2421 * This is called both from within the multi-line macro expansion
2422 * code, and also to mark off the default parameters when provided
2423 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002424 *
2425 * Note that we need space in the params array for parameter 0 being
2426 * a possible captured label as well as the final NULL.
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002427 *
2428 * Returns a pointer to the pointer to a terminal comma if present;
2429 * used to drop an empty terminal argument for legacy reasons.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002430 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002431static Token **count_mmac_params(Token *tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002432{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002433 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002434 int nparam = 0;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002435 Token *t;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002436 Token **comma = NULL, **maybe_comma = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002437 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002438
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002439 paramsize = PARAM_DELTA;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002440 nasm_newn(params, paramsize);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002441
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002442 t = skip_white(tline);
2443 if (t) {
2444 while (true) {
2445 /* Need two slots for captured label and NULL */
2446 if (unlikely(nparam+2 >= paramsize)) {
2447 paramsize += PARAM_DELTA;
2448 params = nasm_realloc(params, sizeof(*params) * paramsize);
2449 }
2450 params[++nparam] = t;
2451 if (tok_is(t, '{')) {
2452 int brace = 1;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002453
2454 comma = NULL; /* Non-empty parameter */
2455
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002456 while (brace && (t = t->next)) {
2457 brace += tok_is(t, '{');
2458 brace -= tok_is(t, '}');
2459 }
H. Peter Anvin (Intel)f8639bd2020-06-04 16:29:53 -07002460
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002461 if (t) {
2462 /*
2463 * Now we've found the closing brace, look further
2464 * for the comma.
2465 */
2466 t = skip_white(t->next);
2467 if (tok_isnt(t, ','))
2468 nasm_nonfatal("braces do not enclose all of macro parameter");
2469 } else {
2470 nasm_nonfatal("expecting closing brace in macro parameter");
2471 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002472 }
2473
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002474 /* Advance to the next comma */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002475 maybe_comma = &t->next;
2476 while (tok_isnt(t, ',')) {
2477 if (!tok_white(t))
2478 comma = NULL; /* Non-empty parameter */
2479 maybe_comma = &t->next;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002480 t = t->next;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002481 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002482
2483 if (!t)
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002484 break; /* End of string, no comma */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002485
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002486 comma = maybe_comma; /* Point to comma pointer */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002487 t = skip_white(t->next); /* Eat the comma and whitespace */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002488 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002489 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002490
2491 params[nparam+1] = NULL;
2492 *paramsp = params;
2493 *nparamp = nparam;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002494
2495 return comma;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002496}
2497
2498/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002499 * Determine whether one of the various `if' conditions is true or
2500 * not.
2501 *
2502 * We must free the tline we get passed.
2503 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002504static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002505{
H. Peter Anvin70055962007-10-11 00:05:31 -07002506 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002507 Token *t, *tt, *origline;
2508 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002509 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002510 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002511 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002512 const char *dname = pp_directives[ct];
2513 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002514 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002515
2516 origline = tline;
2517
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002518 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002519 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002520 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002521 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002522 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002523 if (!tline)
2524 break;
2525 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002526 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002527 dname);
2528 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002530 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002531 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 tline = tline->next;
2533 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002535
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002536 case PP_IFDEF:
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002537 case PP_IFDEFALIAS:
2538 {
2539 bool alias = cond == PP_IFDEFALIAS;
2540 SMacro *smac;
2541 Context *ctx;
2542 const char *mname;
2543
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002544 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002545 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002546 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002548 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002549 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002550 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002553
2554 mname = tok_text(tline);
2555 ctx = get_ctx(mname, &mname);
H. Peter Anvin (Intel)b91e7732020-06-05 12:22:26 -07002556 if (smacro_defined(ctx, mname, -1, &smac, true, alias) && smac
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002557 && smac->alias == alias) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002558 j = true;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002559 break;
2560 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002562 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 break;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002564 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002565
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002566 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002567 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002568 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002569 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002570 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002571 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002572 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002573 tline->type != TOK_INTERNAL_STRING &&
2574 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002575 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002576 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002577 goto fail;
2578 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002579
2580 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002581 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002582 }
2583 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002584
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002585 case PP_IFIDNI:
2586 casesense = false;
2587 /* fall through */
2588 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 tline = expand_smacro(tline);
2590 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002591 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002592 tt = tt->next;
2593 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002594 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002595 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002597 }
2598 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002599 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002600 while (tok_isnt(t, ',') && tt) {
2601 unsigned int l1, l2;
2602 const char *t1, *t2;
2603
2604 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002605 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002606 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002608 }
2609 if (t->type == TOK_WHITESPACE) {
2610 t = t->next;
2611 continue;
2612 }
2613 if (tt->type == TOK_WHITESPACE) {
2614 tt = tt->next;
2615 continue;
2616 }
2617 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002618 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 break;
2620 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002621
H. Peter Anvin8571f062019-09-23 16:40:03 -07002622 t1 = unquote_token(t);
2623 t2 = unquote_token(tt);
2624 l1 = t->len;
2625 l2 = tt->len;
2626
2627 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2628 j = false;
2629 break;
2630 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002631
H. Peter Anvine2c80182005-01-15 22:15:51 +00002632 t = t->next;
2633 tt = tt->next;
2634 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002635 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002636 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002638
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002639 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002640 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002642 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002643
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002644 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002646 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002647 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002648 goto fail;
2649 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002650 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002651 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002653 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 searching.nparam_max = INT_MAX;
2655 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002656 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002657 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002658 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002659 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002660 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 } else {
2662 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002663 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002665 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002666 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002667 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002669 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002670 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002671 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002673 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002674 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002675 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002676 searching.nparam_max = searching.nparam_min;
2677 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 }
2679 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002680 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 tline = tline->next;
2682 searching.plus = true;
2683 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002684 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2685 while (mmac) {
2686 if (!strcmp(mmac->name, searching.name) &&
2687 (mmac->nparam_min <= searching.nparam_max
2688 || searching.plus)
2689 && (searching.nparam_min <= mmac->nparam_max
2690 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 found = true;
2692 break;
2693 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002694 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002695 }
2696 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002697 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002698 nasm_free(searching.name);
2699 j = found;
2700 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002701 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002702
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002703 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 needtype = TOK_ID;
2705 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002706 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 needtype = TOK_NUMBER;
2708 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002709 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 needtype = TOK_STRING;
2711 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002712
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713iftype:
2714 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002715
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002716 while (tok_white(t) ||
2717 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002719
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002720 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002722
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002723 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002724 tline = expand_smacro(tline);
2725 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002726
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 j = false;
2728 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002729 t = skip_white(t->next); /* Skip the actual token + whitespace */
2730 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 }
2732 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002733
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002734 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002735 tline = expand_smacro(tline);
2736 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002737 j = !t; /* Should be empty */
2738 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002739
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002740 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002741 pps.tptr = tline = expand_smacro(tline);
2742 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002744 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 if (!evalresult)
2746 return -1;
2747 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002748 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002750 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002751 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002752 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002754 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002755 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002756
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002757 case PP_IFUSING:
2758 case PP_IFUSABLE:
2759 {
2760 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002761 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002762
H. Peter Anvin8571f062019-09-23 16:40:03 -07002763 pkg = get_use_pkg(tline, dname, &name);
2764 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002765 goto fail;
2766
2767 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2768 break;
2769 }
2770
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002772 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002773 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002774 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002775
2776 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002777 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002778
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002779fail:
2780 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002781 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002782}
2783
2784/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002785 * Default smacro expansion routine: just returns a copy of the
2786 * expansion list.
2787 */
2788static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002789smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002790{
2791 (void)params;
2792 (void)nparams;
2793
2794 return dup_tlist(s->expansion, NULL);
2795}
2796
2797/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002798 * Emit a macro defintion or undef to the listing file, if
2799 * desired. This is similar to detoken(), but it handles the reverse
2800 * expansion list, does not expand %! or local variable tokens, and
2801 * does some special handling for macro parameters.
2802 */
2803static void
2804list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2805{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002806 Token *t;
2807 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002808 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002809 char *context_prefix = NULL;
2810 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002811
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002812 namelen = strlen(m->name);
2813 size = namelen + 2; /* Include room for space after name + NUL */
2814
2815 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002816 int context_depth = cstk->depth - ctx->depth + 1;
2817 context_prefix =
2818 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2819 ctx->name ? ctx->name : "",
2820 ctx->number, context_depth, "");
2821
2822 context_len = nasm_last_string_len();
2823 memset(context_prefix + context_len - context_depth,
2824 '$', context_depth);
2825 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002826 }
2827
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002828 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002829 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002830
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002831 if (m->nparam) {
2832 /*
2833 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002834 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002835 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002836 int i;
2837
2838 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002839 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002840 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002841 }
2842
2843 def = nasm_malloc(size);
2844 p = def+size;
2845 *--p = '\0';
2846
2847 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002848 p -= t->len;
2849 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002850 }
2851
2852 *--p = ' ';
2853
2854 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002855 int i;
2856
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002857 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002858 for (i = m->nparam-1; i >= 0; i--) {
2859 enum sparmflags flags = m->params[i].flags;
2860 if (flags & SPARM_GREEDY)
2861 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002862 p -= m->params[i].name.len;
2863 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2864
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002865 if (flags & SPARM_NOSTRIP)
2866 *--p = '!';
2867 if (flags & SPARM_STR)
2868 *--p = '&';
2869 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002870 *--p = '=';
2871 *--p = ',';
2872 }
2873 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002874 }
2875
2876 p -= namelen;
2877 memcpy(p, m->name, namelen);
2878
H. Peter Anvin6686de22019-08-10 05:33:14 -07002879 if (context_prefix) {
2880 p -= context_len;
2881 memcpy(p, context_prefix, context_len);
2882 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002883 }
2884
2885 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002886 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002887}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002888
2889/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002890 * Parse smacro arguments, return argument count. If the tmpl argument
2891 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002892 * *tpp is updated to point to the pointer to the first token after the
2893 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002894 *
2895 * The text values from any argument tokens are "stolen" and the
2896 * corresponding text fields set to NULL.
2897 */
2898static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2899{
2900 int nparam = 0;
2901 enum sparmflags flags;
2902 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002903 bool err, done;
2904 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002905 Token **tn = *tpp;
2906 Token *t = *tn;
2907 Token *name;
2908
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002909 /*
2910 * DO NOT skip whitespace here, or we won't be able to distinguish:
2911 *
2912 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2913 * %define bar(a,b) ; two arguments, empty expansion
2914 *
2915 * This ambiguity was inherited from C.
2916 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002917
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002918 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002919 goto finish;
2920
2921 if (tmpl) {
2922 Token *tx = t;
2923 Token **txpp = &tx;
2924 int sparam;
2925
2926 /* Count parameters first */
2927 sparam = parse_smacro_template(&txpp, NULL);
2928 if (!sparam)
2929 goto finish; /* No parameters, we're done */
2930 nasm_newn(params, sparam);
2931 }
2932
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002933 /* Skip leading paren */
2934 tn = &t->next;
2935 t = *tn;
2936
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002937 name = NULL;
2938 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002939 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002940
2941 while (!done) {
2942 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002943 if (name || flags)
2944 nasm_nonfatal("`)' expected to terminate macro template");
2945 else
2946 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002947 break;
2948 }
2949
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002950 switch (t->type) {
2951 case TOK_ID:
2952 if (name)
2953 goto bad;
2954 name = t;
2955 break;
2956
2957 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002958 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002959 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002960 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002961 case '=':
2962 flags |= SPARM_EVAL;
2963 break;
2964 case '&':
2965 flags |= SPARM_STR;
2966 break;
2967 case '!':
2968 flags |= SPARM_NOSTRIP;
2969 break;
2970 case '+':
2971 flags |= SPARM_GREEDY;
2972 greedy = true;
2973 break;
2974 case ',':
2975 if (greedy)
2976 nasm_nonfatal("greedy parameter must be last");
2977 /* fall through */
2978 case ')':
2979 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002980 if (name)
2981 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002982 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002983 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002984 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002985 name = NULL;
2986 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002987 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002988 break;
2989 default:
2990 goto bad;
2991 }
2992 break;
2993
2994 case TOK_WHITESPACE:
2995 break;
2996
2997 default:
2998 bad:
2999 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003000 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003001 err = true;
3002 }
3003 break;
3004 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003005
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07003006 tn = &t->next;
3007 t = *tn;
3008 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003009
3010finish:
3011 while (t && t->type == TOK_WHITESPACE) {
3012 tn = &t->next;
3013 t = t->next;
3014 }
3015 *tpp = tn;
3016 if (tmpl) {
3017 tmpl->nparam = nparam;
3018 tmpl->greedy = greedy;
3019 tmpl->params = params;
3020 }
3021 return nparam;
3022}
3023
3024/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003025 * Common code for defining an smacro. The tmpl argument, if not NULL,
3026 * contains any macro parameters that aren't explicit arguments;
3027 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003028 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003029static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003030 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003031{
3032 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003033 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003034 Context *ctx;
3035 bool defining_alias = false;
3036 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07003037
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003038 if (tmpl) {
3039 defining_alias = tmpl->alias;
3040 nparam = tmpl->nparam;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003041 if (nparam && !defining_alias)
3042 mark_smac_params(expansion, tmpl, 0);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003043 }
3044
3045 while (1) {
3046 ctx = get_ctx(mname, &mname);
3047
H. Peter Anvind2354082019-08-27 16:38:48 -07003048 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003049 /* Create a new macro */
3050 smtbl = ctx ? &ctx->localmac : &smacros;
3051 smhead = (SMacro **) hash_findi_add(smtbl, mname);
3052 nasm_new(smac);
3053 smac->next = *smhead;
3054 *smhead = smac;
3055 break;
3056 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003057 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003058 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003059 /*
3060 * Some instances of the old code considered this a failure,
3061 * some others didn't. What is the right thing to do here?
3062 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003063 goto fail;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003064 } else if (!smac->alias || ppopt.noaliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003065 /*
3066 * We're redefining, so we have to take over an
3067 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08003068 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003069 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003070 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003071 break;
3072 } else if (smac->in_progress) {
3073 nasm_nonfatal("macro alias loop");
3074 goto fail;
3075 } else {
3076 /* It is an alias macro; follow the alias link */
3077 SMacro *s;
3078
3079 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003080 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003081 expansion, tmpl);
3082 smac->in_progress = false;
3083 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003084 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003085 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003086
3087 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003088 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003089 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003090 smac->expand = smacro_expand_default;
3091 if (tmpl) {
3092 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003093 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003094 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003095 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003096 if (tmpl->expand)
3097 smac->expand = tmpl->expand;
3098 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003099 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07003100 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
3101 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003102 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003103 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003104
3105fail:
3106 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003107 if (tmpl)
3108 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003109 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003110}
3111
3112/*
3113 * Undefine an smacro
3114 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003115static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003116{
3117 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003118 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003119 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003120
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003121 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003122 smtbl = ctx ? &ctx->localmac : &smacros;
3123 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003124
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003125 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003126 /*
3127 * We now have a macro name... go hunt for it.
3128 */
3129 sp = smhead;
3130 while ((s = *sp) != NULL) {
3131 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003132 if (s->alias && !undefalias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003133 if (!ppopt.noaliases) {
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003134 if (s->in_progress) {
3135 nasm_nonfatal("macro alias loop");
3136 } else {
3137 s->in_progress = true;
3138 undef_smacro(tok_text(s->expansion), false);
3139 s->in_progress = false;
3140 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003141 }
3142 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003143 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003144 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3145 ctx, s);
3146 *sp = s->next;
3147 free_smacro(s);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003148 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003149 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003151 sp = &s->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003152 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003153 }
3154}
3155
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003156/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003157 * Parse a mmacro specification.
3158 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003159static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003160{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003161 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003162 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003163 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003164 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003165 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003167 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003168
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003169#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003170 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003171#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003172 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003173 def->plus = false;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003174 def->nolist = 0;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003175 def->nparam_min = 0;
3176 def->nparam_max = 0;
3177
H. Peter Anvina26433d2008-07-16 14:40:01 -07003178 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003179 tline = skip_white(tline);
3180 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003181 nasm_nonfatal("`%s' expects a parameter count", directive);
3182 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003183 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003184 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003186 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003187 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003188 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003189 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003190 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003191 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003193 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003194 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195 }
3196 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003197 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003198 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003199 tline = tline->next;
3200 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003201 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003202 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003203 tline->next->len == 7 &&
3204 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003205 tline = tline->next;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003206 if (!list_option('f'))
3207 def->nolist |= NL_LIST|NL_LINE;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003208 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003209
H. Peter Anvina26433d2008-07-16 14:40:01 -07003210 /*
3211 * Handle default parameters.
3212 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003213 def->ndefs = 0;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003214 if (tline && tline->next) {
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003215 Token **comma;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003216 def->dlist = tline->next;
3217 tline->next = NULL;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003218 comma = count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
3219 if (!ppopt.sane_empty_expansion && comma) {
3220 *comma = NULL;
3221 def->ndefs--;
3222 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
3223 "dropping trailing empty default parameter in defintion of multi-line macro `%s'",
3224 def->name);
3225 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003226 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003227 def->dlist = NULL;
3228 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003229 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003230 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003231
H. Peter Anvin89cee572009-07-15 09:16:54 -04003232 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003233 !def->plus) {
3234 /*
3235 *!macro-defaults [on] macros with more default than optional parameters
3236 *! warns when a macro has more default parameters than optional parameters.
3237 *! See \k{mlmacdef} for why might want to disable this warning.
3238 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003239 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003240 "too many default macro parameters in macro `%s'", def->name);
3241 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003242
H. Peter Anvina26433d2008-07-16 14:40:01 -07003243 return true;
3244}
3245
3246
3247/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003248 * Decode a size directive
3249 */
3250static int parse_size(const char *str) {
3251 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003252 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003253 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003254 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003255 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003256}
3257
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003258/*
3259 * Process a preprocessor %pragma directive. Currently there are none.
3260 * Gets passed the token list starting with the "preproc" token from
3261 * "%pragma preproc".
3262 */
3263static void do_pragma_preproc(Token *tline)
3264{
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003265 const char *txt;
3266
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003267 /* Skip to the real stuff */
3268 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003269 tline = skip_white(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003270
3271 if (!tok_type(tline, TOK_ID))
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003272 return;
3273
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003274 txt = tok_text(tline);
3275 if (!nasm_stricmp(txt, "sane_empty_expansion")) {
3276 tline = skip_white(tline->next);
3277 ppopt.sane_empty_expansion =
3278 pp_get_boolean_option(tline, ppopt.sane_empty_expansion);
3279 } else {
3280 /* Unknown pragma, ignore for now */
3281 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003282}
3283
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003284static bool is_macro_id(const Token *t)
3285{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003286 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003287}
3288
H. Peter Anvin8571f062019-09-23 16:40:03 -07003289static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003290{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003291 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003292 Token *t = *tp;
3293
3294 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003295 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003296 t = expand_id(t);
3297
3298 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003299 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003300 return NULL;
3301 }
3302
H. Peter Anvin8571f062019-09-23 16:40:03 -07003303 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003304 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003305 *tp = t;
3306 return id;
3307}
3308
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003309/* Parse a %use package name and find the package. Set *err on syntax error. */
3310static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003311get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003312{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003313 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003314
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003315 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003316 t = expand_smacro(t);
3317
H. Peter Anvin8571f062019-09-23 16:40:03 -07003318 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003319
H. Peter Anvin8571f062019-09-23 16:40:03 -07003320 if (!t) {
3321 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3322 return NULL;
3323 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3324 nasm_nonfatal("`%s' expects a package name, got `%s'",
3325 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003326 return NULL;
3327 }
3328
H. Peter Anvin8571f062019-09-23 16:40:03 -07003329 *name = id = unquote_token(t);
3330
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003331 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003332 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003333 if (t)
3334 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3335
3336 return nasm_find_use_package(id);
3337}
3338
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003339/*
3340 * Mark parameter tokens in an smacro definition. If the type argument
3341 * is 0, create smac param tokens, otherwise use the type specified;
3342 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3343 * parameter tokens during expansion during %xdefine.
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003344 *
3345 * tmpl may not be NULL here.
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003346 */
3347static void mark_smac_params(Token *tline, const SMacro *tmpl,
3348 enum pp_token_type type)
3349{
3350 const struct smac_param *params = tmpl->params;
3351 int nparam = tmpl->nparam;
3352 Token *t;
3353 int i;
3354
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003355 list_for_each(t, tline) {
3356 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3357 continue;
3358
3359 for (i = 0; i < nparam; i++) {
3360 if (tok_text_match(t, &params[i].name))
3361 t->type = type ? type : tok_smac_param(i);
3362 }
3363 }
3364}
3365
Ed Beroset3ab3f412002-06-11 03:31:49 +00003366/**
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003367 * %clear selected macro sets either globally or in contexts
3368 */
3369static void do_clear(enum clear_what what, bool context)
3370{
3371 if (context) {
3372 if (what & CLEAR_ALLDEFINE) {
3373 Context *ctx;
3374 list_for_each(ctx, cstk)
3375 clear_smacro_table(&ctx->localmac, what);
3376 }
3377 /* Nothing else can be context-local */
3378 } else {
3379 if (what & CLEAR_ALLDEFINE)
3380 clear_smacro_table(&smacros, what);
3381 if (what & CLEAR_MMACRO)
3382 free_mmacro_table(&mmacros);
3383 }
3384}
3385
3386/**
Ed Beroset3ab3f412002-06-11 03:31:49 +00003387 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003388 * Find out if a line contains a preprocessor directive, and deal
3389 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003390 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003391 * If a directive _is_ found, it is the responsibility of this routine
3392 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003393 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003394 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003395 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003396 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003397 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003398 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003399static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003400{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003401 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003402 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003403 bool err;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003404 enum nolist_flags nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003405 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003406 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003407 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003408 const char *p;
3409 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003410 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003411 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003412 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003413 Include *inc;
3414 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003415 Cond *cond;
3416 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003417 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003418 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003419 struct tokenval tokval;
3420 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003421 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003422 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003423 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003424 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003425
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003426 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003427 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003428
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003429 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003430 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3431 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003432
H. Peter Anvin8571f062019-09-23 16:40:03 -07003433 dname = tok_text(tline);
3434 if (dname[1] == '%')
3435 return NO_DIRECTIVE_FOUND;
3436
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003437 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003438
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003439 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003440 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003441 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003442 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003443 }
3444
3445 /*
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003446 * %line directives are always processed immediately and
3447 * unconditionally, as they are intended to reflect position
3448 * in externally preprocessed sources.
3449 */
3450 if (op == PP_LINE) {
3451 /*
3452 * Syntax is `%line nnn[+mmm] [filename]'
3453 */
3454 if (pp_noline || istk->mstk.mstk)
3455 goto done;
3456
3457 tline = tline->next;
3458 tline = skip_white(tline);
3459 if (!tok_type(tline, TOK_NUMBER)) {
3460 nasm_nonfatal("`%s' expects line number", dname);
3461 goto done;
3462 }
3463 k = readnum(tok_text(tline), &err);
3464 m = 1;
3465 tline = tline->next;
3466 if (tok_is(tline, '+') || tok_is(tline, '-')) {
3467 bool minus = tok_is(tline, '-');
3468 tline = tline->next;
3469 if (!tok_type(tline, TOK_NUMBER)) {
3470 nasm_nonfatal("`%s' expects line increment", dname);
3471 goto done;
3472 }
3473 m = readnum(tok_text(tline), &err);
3474 if (minus)
3475 m = -m;
3476 tline = tline->next;
3477 }
3478 tline = skip_white(tline);
3479 if (tline) {
3480 if (tline->type == TOK_STRING) {
3481 src_set_fname(unquote_token(tline));
3482 } else {
3483 char *fname = detoken(tline, false);
3484 src_set_fname(fname);
3485 nasm_free(fname);
3486 }
3487 }
3488 src_set_linnum(k);
3489
3490 istk->where = src_where();
3491 istk->lineinc = m;
3492 goto done;
3493 }
3494
3495 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003496 * If we're in a non-emitting branch of a condition construct,
3497 * or walking to the end of an already terminated %rep block,
3498 * we should ignore all directives except for condition
3499 * directives.
3500 */
3501 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003502 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003503 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003504 return NO_DIRECTIVE_FOUND;
3505 }
3506
3507 /*
3508 * If we're defining a macro or reading a %rep block, we should
3509 * ignore all directives except for %macro/%imacro (which nest),
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003510 * %endm/%endmacro, %line and (only if we're in a %rep block) %endrep.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003511 * If we're in a %rep block, another %rep nests, so should be let through.
3512 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003513 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3514 op != PP_ENDMACRO && op != PP_ENDM &&
3515 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003516 return NO_DIRECTIVE_FOUND;
3517 }
3518
3519 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003520 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003521 nested_mac_count++;
3522 return NO_DIRECTIVE_FOUND;
3523 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003524 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003525 nested_mac_count--;
3526 return NO_DIRECTIVE_FOUND;
3527 }
3528 }
3529 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003530 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003531 nested_rep_count++;
3532 return NO_DIRECTIVE_FOUND;
3533 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003534 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003535 nested_rep_count--;
3536 return NO_DIRECTIVE_FOUND;
3537 }
3538 }
3539 }
3540 }
3541
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003542 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003543 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003544 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003546
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003547 case PP_PRAGMA:
3548 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003549 * %pragma namespace options...
3550 *
3551 * The namespace "preproc" is reserved for the preprocessor;
3552 * all other namespaces generate a [pragma] assembly directive.
3553 *
3554 * Invalid %pragmas are ignored and may have different
3555 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003556 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003557 t = tline;
3558 tline = tline->next;
3559 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003560 tline = zap_white(expand_smacro(tline));
3561 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003562 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003563 /* Preprocessor pragma */
3564 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003565 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003566 } else {
3567 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003568
3569 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003570 for (t = tline; t->next; t = t->next)
3571 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003572 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003573
3574 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003575 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003576 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003577 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003578 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003579 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003580 }
3581 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003582 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003583
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 case PP_STACKSIZE:
3585 /* Directive to tell NASM what the default stack size is. The
3586 * default is for a 16-bit stack, and this can be overriden with
3587 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003589 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003591 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003593 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 /* All subsequent ARG directives are for a 32-bit stack */
3595 StackSize = 4;
3596 StackPointer = "ebp";
3597 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003598 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003599 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003600 /* All subsequent ARG directives are for a 64-bit stack */
3601 StackSize = 8;
3602 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003603 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003604 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003605 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003606 /* All subsequent ARG directives are for a 16-bit stack,
3607 * far function call.
3608 */
3609 StackSize = 2;
3610 StackPointer = "bp";
3611 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003612 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003613 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 /* All subsequent ARG directives are for a 16-bit stack,
3615 * far function call. We don't support near functions.
3616 */
3617 StackSize = 2;
3618 StackPointer = "bp";
3619 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003620 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003622 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003624 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003625
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 case PP_ARG:
3627 /* TASM like ARG directive to define arguments to functions, in
3628 * the following form:
3629 *
3630 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3631 */
3632 offset = ArgOffset;
3633 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003634 const char *arg;
3635 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003637
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003639 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003641 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003642 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003644 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003645
H. Peter Anvine2c80182005-01-15 22:15:51 +00003646 /* Find the argument size type */
3647 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003648 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003649 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003650 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 }
3652 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003653 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003654 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003655 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003657
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003659 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003661 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003662 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003663 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003665 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003666 }
3667 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003668
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003669 /* Round up to even stack slots */
3670 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003671
H. Peter Anvine2c80182005-01-15 22:15:51 +00003672 /* Now define the macro for the argument */
3673 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3674 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003675 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003677
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003679 tline = skip_white(tline->next);
3680 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003681 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003682 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003683
H. Peter Anvine2c80182005-01-15 22:15:51 +00003684 case PP_LOCAL:
3685 /* TASM like LOCAL directive to define local variables for a
3686 * function, in the following form:
3687 *
3688 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3689 *
3690 * The '= LocalSize' at the end is ignored by NASM, but is
3691 * required by TASM to define the local parameter size (and used
3692 * by the TASM macro package).
3693 */
3694 offset = LocalOffset;
3695 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003696 const char *local;
3697 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003698 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003699
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003701 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003702 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003703 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003704 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003705 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003706 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003707
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 /* Find the argument size type */
3709 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003710 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003711 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003712 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003713 }
3714 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003715 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003716 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003717 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003719
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003721 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003722 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003723 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003724 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003725 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003726 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003727 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 }
3729 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003730
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003731 /* Round up to even stack slots */
3732 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003733
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003734 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003735
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003736 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3738 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003739 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003740
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 /* Now define the assign to setup the enter_c macro correctly */
3742 snprintf(directive, sizeof(directive),
3743 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003744 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003745
H. Peter Anvine2c80182005-01-15 22:15:51 +00003746 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003747 tline = skip_white(tline->next);
3748 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003749 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003750 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003751
H. Peter Anvine2c80182005-01-15 22:15:51 +00003752 case PP_CLEAR:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003753 {
3754 bool context = false;
3755
3756 t = tline->next = expand_smacro(tline->next);
3757 t = skip_white(t);
3758 if (!t) {
3759 /* Emulate legacy behavior */
3760 do_clear(CLEAR_DEFINE|CLEAR_MMACRO, false);
3761 } else {
3762 while ((t = skip_white(t)) && t->type == TOK_ID) {
3763 const char *txt = tok_text(t);
3764 if (!nasm_stricmp(txt, "all")) {
3765 do_clear(CLEAR_ALL, context);
3766 } else if (!nasm_stricmp(txt, "define") ||
3767 !nasm_stricmp(txt, "def") ||
3768 !nasm_stricmp(txt, "smacro")) {
3769 do_clear(CLEAR_DEFINE, context);
3770 } else if (!nasm_stricmp(txt, "defalias") ||
3771 !nasm_stricmp(txt, "alias") ||
3772 !nasm_stricmp(txt, "salias")) {
3773 do_clear(CLEAR_DEFALIAS, context);
3774 } else if (!nasm_stricmp(txt, "alldef") ||
3775 !nasm_stricmp(txt, "alldefine")) {
3776 do_clear(CLEAR_ALLDEFINE, context);
3777 } else if (!nasm_stricmp(txt, "macro") ||
3778 !nasm_stricmp(txt, "mmacro")) {
3779 do_clear(CLEAR_MMACRO, context);
3780 } else if (!nasm_stricmp(txt, "context") ||
3781 !nasm_stricmp(txt, "ctx")) {
3782 context = true;
3783 } else if (!nasm_stricmp(txt, "global")) {
3784 context = false;
3785 } else if (!nasm_stricmp(txt, "nothing") ||
3786 !nasm_stricmp(txt, "none") ||
3787 !nasm_stricmp(txt, "ignore") ||
3788 !nasm_stricmp(txt, "-") ||
3789 !nasm_stricmp(txt, "--")) {
3790 /* Do nothing */
3791 } else {
3792 nasm_nonfatal("invalid option to %s: %s", dname, txt);
3793 t = NULL;
3794 }
3795 }
3796 }
3797
3798 t = skip_white(t);
3799 if (t)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003800 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003801 break;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003802 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003803
H. Peter Anvin418ca702008-05-30 10:42:30 -07003804 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003805 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003806 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003807 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003808 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003809 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003810 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003811 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003812 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003813 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003814
3815 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003816 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003817
3818 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003819 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003820 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003821
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003822 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003823 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003824 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003825 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003826 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003827 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003828 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003829 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003830 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003831 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003832 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003833 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003834 (pp_mode == PP_DEPS)
3835 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003836 if (!inc->fp) {
3837 /* -MG given but file not found */
3838 nasm_free(inc);
3839 } else {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003840 inc->where = src_where();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003841 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003842 inc->nolist = istk->nolist;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003843 inc->noline = istk->noline;
3844 if (!inc->noline)
3845 src_set(0, found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003846 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003847 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003848 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003849 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003850
H. Peter Anvind2456592008-06-19 15:04:18 -07003851 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003852 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003853 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003854 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003855
H. Peter Anvin8571f062019-09-23 16:40:03 -07003856 pkg = get_use_pkg(tline->next, dname, &name);
3857 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003858 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003859 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003860 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003861 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003862 /*
3863 * Not already included, go ahead and include it.
3864 * Treat it as an include file for the purpose of
3865 * producing a listing.
3866 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003867 use_loaded[pkg->index] = true;
3868 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003869 nasm_new(inc);
3870 inc->next = istk;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003871 inc->nolist = istk->nolist + !list_option('b');
3872 inc->noline = istk->noline;
3873 if (!inc->noline)
3874 src_set(0, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003875 istk = inc;
3876 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003877 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003878 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003879 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003880 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003881 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003882 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003883 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003884 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003886 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003887 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003888 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003889 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003890 }
3891 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003892 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003893 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003894 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003895 } else {
3896 p = NULL; /* Anonymous */
3897 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003898
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003899 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003900 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003901 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003902 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003903 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003904 ctx->number = unique++;
3905 cstk = ctx;
3906 } else {
3907 /* %pop or %repl */
3908 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003909 nasm_nonfatal("`%s': context stack is empty", dname);
3910 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003911 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003912 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003913 "expected %s",
3914 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003915 else
3916 ctx_pop();
3917 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003918 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003919 nasm_free((char *)cstk->name);
3920 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003921 p = NULL;
3922 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003923 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003924 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003925 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003926 severity = ERR_FATAL;
3927 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003929 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003930 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003931 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003932 /*!
3933 *!user [on] %warning directives
3934 *! controls output of \c{%warning} directives (see \k{pperror}).
3935 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003936 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003937 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003938
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003939issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003940 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003941 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003942 tline->next = expand_smacro(tline->next);
3943 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003944 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003945 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003946 t = skip_white(t);
3947 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003948 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003949 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003950 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003951 } else {
3952 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003953 q = detoken(tline, false);
3954 nasm_error(severity, "%s", q);
3955 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003956 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003957 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003958 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003959
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003960 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003961 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003962 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003963 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003964 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003965 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003966 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003967 cond = nasm_malloc(sizeof(Cond));
3968 cond->next = istk->conds;
3969 cond->state = j;
3970 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003971 if(istk->mstk.mstk)
3972 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003973 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003974
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003975 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003976 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003977 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003978 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003979 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003980 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003981 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003982
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003983 case COND_DONE:
3984 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003985 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003986
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003987 case COND_ELSE_TRUE:
3988 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003989 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003990 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003991 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003992 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003993
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003994 case COND_IF_FALSE:
3995 /*
3996 * IMPORTANT: In the case of %if, we will already have
3997 * called expand_mmac_params(); however, if we're
3998 * processing an %elif we must have been in a
3999 * non-emitting mode, which would have inhibited
4000 * the normal invocation of expand_mmac_params().
4001 * Therefore, we have to do it explicitly here.
4002 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004003 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004004 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07004005 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004006 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004007 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07004008 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004009
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 case PP_ELSE:
4011 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004012 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004013 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004014 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07004015 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004016 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004017 case COND_IF_TRUE:
4018 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004019 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004020 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004021
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004022 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004023 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004024
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004025 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004026 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004027 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004028
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004029 case COND_ELSE_TRUE:
4030 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004031 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004032 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004033 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004034 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004035 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004036 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004037
H. Peter Anvine2c80182005-01-15 22:15:51 +00004038 case PP_ENDIF:
4039 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004040 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004041 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004042 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004043 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004044 cond = istk->conds;
4045 istk->conds = cond->next;
4046 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004047 if(istk->mstk.mstk)
4048 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004049 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004050
H. Peter Anvin8b262472019-02-26 14:00:54 -08004051 case PP_RMACRO:
4052 case PP_MACRO:
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004053 {
4054 MMacro *def;
4055
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004056 nasm_assert(!defining);
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004057 nasm_new(def);
4058 def->casesense = casesense;
H. Peter Anvin (Intel)23abe9f2020-07-10 01:52:49 -07004059 /*
4060 * dstk.mstk points to the previous definition bracket,
4061 * whereas dstk.mmac points to the topmost mmacro, which
4062 * in this case is the one we are just starting to create.
4063 */
4064 def->dstk.mstk = defining;
4065 def->dstk.mmac = def;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004066 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004067 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
4068 if (!parse_mmacro_spec(tline, def, dname)) {
4069 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004070 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004071 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07004072
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004073 defining = def;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004074 defining->where = istk->where;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07004075
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004076 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
4077 while (mmac) {
4078 if (!strcmp(mmac->name, defining->name) &&
4079 (mmac->nparam_min <= defining->nparam_max
4080 || defining->plus)
4081 && (defining->nparam_min <= mmac->nparam_max
4082 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004083 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004084 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004085 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004086 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004087 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004088 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004089 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004090 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004091
H. Peter Anvine2c80182005-01-15 22:15:51 +00004092 case PP_ENDM:
4093 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004094 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004095 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004096 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004097 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004098 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
4099 defining->next = *mmhead;
4100 *mmhead = defining;
4101 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004102 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004103
H. Peter Anvin89cee572009-07-15 09:16:54 -04004104 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004105 /*
4106 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004107 * macro-end marker for a macro with a name. Then we
4108 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004109 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004110 list_for_each(l, istk->expansion)
4111 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004112 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004113
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004114 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004115 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004116 * Remove all conditional entries relative to this
4117 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004118 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004119 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
4120 cond = istk->conds;
4121 istk->conds = cond->next;
4122 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004123 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004124 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004125 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004126 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004127 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004128 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05004129
H. Peter Anvina26433d2008-07-16 14:40:01 -07004130 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004131 casesense = false;
4132 /* fall through */
4133 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07004134 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004135 MMacro **mmac_p;
4136 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004137
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004138 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004139 spec.casesense = casesense;
4140 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004141 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004142 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004143 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
4144 while (mmac_p && *mmac_p) {
4145 mmac = *mmac_p;
4146 if (mmac->casesense == spec.casesense &&
4147 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
4148 mmac->nparam_min == spec.nparam_min &&
4149 mmac->nparam_max == spec.nparam_max &&
4150 mmac->plus == spec.plus) {
4151 *mmac_p = mmac->next;
4152 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004153 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004154 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004155 }
4156 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004157 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004158 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004159 }
4160
H. Peter Anvine2c80182005-01-15 22:15:51 +00004161 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004162 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004163 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004164 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004165 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004166 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004167 return DIRECTIVE_FOUND;
4168 }
4169 t = expand_smacro(tline->next);
4170 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004171 pps.tptr = tline = t;
4172 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 tokval.t_type = TOKEN_INVALID;
4174 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004175 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004176 free_tlist(tline);
4177 if (!evalresult)
4178 return DIRECTIVE_FOUND;
4179 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004180 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004181 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004182 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004183 return DIRECTIVE_FOUND;
4184 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004185 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004186 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004187 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004188 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004189 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004190 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004191 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004192
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004193 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004194 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004195 rotate += mmac->nparam;
4196
4197 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004198 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004199 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004200
H. Peter Anvine2c80182005-01-15 22:15:51 +00004201 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004202 {
4203 MMacro *tmp_defining;
4204
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004205 nolist = 0;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004206 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004207 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
4208 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004209 if (!list_option('f'))
4210 nolist |= NL_LIST; /* ... but update line numbers */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004211 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004213
H. Peter Anvine2c80182005-01-15 22:15:51 +00004214 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08004215 pps.tptr = expand_smacro(tline);
4216 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004217 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004218 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004220 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004221 if (!evalresult)
4222 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004223 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004224 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004226 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004227 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004228 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004229 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004230 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004231 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
4232 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004233 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004234 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004235 /*!
4236 *!negative-rep [on] regative %rep count
4237 *! warns about negative counts given to the \c{%rep}
4238 *! preprocessor directive.
4239 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004240 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004241 "negative `%%rep' count: %"PRId64, count);
4242 count = 0;
4243 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004244 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004245 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004246 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004247 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07004248 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004249 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004250 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004251 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004252 defining->nolist = nolist;
4253 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004254 defining->mstk = istk->mstk;
4255 defining->dstk.mstk = tmp_defining;
4256 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004257 defining->where = istk->where;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004258 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004259 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004260
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004262 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004263 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004264 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004265 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004266
H. Peter Anvine2c80182005-01-15 22:15:51 +00004267 /*
4268 * Now we have a "macro" defined - although it has no name
4269 * and we won't be entering it in the hash tables - we must
4270 * push a macro-end marker for it on to istk->expansion.
4271 * After that, it will take care of propagating itself (a
4272 * macro-end marker line for a macro which is really a %rep
4273 * block will cause the macro to be re-expanded, complete
4274 * with another macro-end marker to ensure the process
4275 * continues) until the whole expansion is forcibly removed
4276 * from istk->expansion by a %exitrep.
4277 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004278 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004279 l->next = istk->expansion;
4280 l->finishes = defining;
4281 l->first = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004282 l->where = src_where();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004283 istk->expansion = l;
4284
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004285 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004286
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004287 /* A loop does not change istk->noline */
4288 istk->nolist += !!(defining->nolist & NL_LIST);
4289 if (!istk->nolist)
4290 lfmt->uplevel(LIST_MACRO, 0);
4291
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004292 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004293 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004294
H. Peter Anvine2c80182005-01-15 22:15:51 +00004295 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004296 /*
4297 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004298 * macro-end marker for a macro with no name. Then we set
4299 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004300 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004301 list_for_each(l, istk->expansion)
4302 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004303 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004304
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004305 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004306 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004307 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004308 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004309 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004310
H. Peter Anvin8b262472019-02-26 14:00:54 -08004311 case PP_DEFINE:
4312 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004313 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004314 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004315 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004316 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004317 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004318
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004319 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004320 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004321
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004322 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004323 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004324 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004325 tline = *lastp;
4326 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004327
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004328 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004329 macro_start = tline;
4330 if (!is_macro_id(macro_start)) {
4331 nasm_nonfatal("`%s' expects a macro identifier to alias",
4332 dname);
4333 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004335 tt = macro_start->next;
4336 macro_start->next = NULL;
4337 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004338 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004339 if (tline && tline->type) {
4340 nasm_warn(WARN_OTHER,
4341 "trailing garbage after aliasing identifier ignored");
4342 }
4343 free_tlist(tt);
4344 tmpl.alias = true;
4345 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004346 if (op == PP_XDEFINE) {
4347 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004348 if (nparam)
4349 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004350 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004351 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004352 /* NB: Does this still make sense? */
4353 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004354 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004355
H. Peter Anvine2c80182005-01-15 22:15:51 +00004356 /*
4357 * Good. We now have a macro name, a parameter count, and a
4358 * token list (in reverse order) for an expansion. We ought
4359 * to be OK just to create an SMacro, store it, and let
4360 * free_tlist have the rest of the line (which we have
4361 * carefully re-terminated after chopping off the expansion
4362 * from the end).
4363 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004364 define_smacro(mname, casesense, macro_start, &tmpl);
4365 break;
4366 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004367
H. Peter Anvine2c80182005-01-15 22:15:51 +00004368 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004369 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004370 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004371 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004372 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004373 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004374
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004375 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004376 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004377
H. Peter Anvin8b262472019-02-26 14:00:54 -08004378 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004379 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004380 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004381
H. Peter Anvin9e200162008-06-04 17:23:14 -07004382 last = tline;
4383 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004384 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004385
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004386 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004387 q = detoken(tline, false);
4388 macro_start = make_tok_qstr(NULL, q);
4389 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004390
4391 /*
4392 * We now have a macro name, an implicit parameter count of
4393 * zero, and a string token to use as an expansion. Create
4394 * and store an SMacro.
4395 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004396 define_smacro(mname, casesense, macro_start, NULL);
4397 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004398
H. Peter Anvin8b262472019-02-26 14:00:54 -08004399 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004400 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004401 goto done;
4402
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004403 last = tline;
4404 tline = expand_smacro(tline->next);
4405 last->next = NULL;
4406
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004407 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004408 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004409 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004410 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004411 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004412 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004413 }
4414
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004415 /*
4416 * Convert the string to a token stream. Note that smacros
4417 * are stored with the token stream reversed, so we have to
4418 * reverse the output of tokenize().
4419 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004420 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004421
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004422 /*
4423 * We now have a macro name, an implicit parameter count of
4424 * zero, and a numeric token to use as an expansion. Create
4425 * and store an SMacro.
4426 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004427 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004428 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004429 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004430
H. Peter Anvin418ca702008-05-30 10:42:30 -07004431 case PP_PATHSEARCH:
4432 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004433 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004434
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004435 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004436 goto done;
4437
H. Peter Anvin418ca702008-05-30 10:42:30 -07004438 last = tline;
4439 tline = expand_smacro(tline->next);
4440 last->next = NULL;
4441
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004442 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004443 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004444 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004445 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004446 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004447 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004448 }
4449 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004450 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004451
4452 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004453
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004454 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004455 if (!found_path)
4456 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004457 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004458
4459 /*
4460 * We now have a macro name, an implicit parameter count of
4461 * zero, and a string token to use as an expansion. Create
4462 * and store an SMacro.
4463 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004464 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004465 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004466 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004467 }
4468
H. Peter Anvine2c80182005-01-15 22:15:51 +00004469 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004470 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004471 goto done;
4472
H. Peter Anvine2c80182005-01-15 22:15:51 +00004473 last = tline;
4474 tline = expand_smacro(tline->next);
4475 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004476
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004477 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004478 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004479 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004480 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004481 free_tlist(tline);
4482 free_tlist(origline);
4483 return DIRECTIVE_FOUND;
4484 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004485
H. Peter Anvin8571f062019-09-23 16:40:03 -07004486 unquote_token(t);
4487 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004488
H. Peter Anvine2c80182005-01-15 22:15:51 +00004489 /*
4490 * We now have a macro name, an implicit parameter count of
4491 * zero, and a numeric token to use as an expansion. Create
4492 * and store an SMacro.
4493 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004494 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004495 free_tlist(tline);
4496 free_tlist(origline);
4497 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004498
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004499 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004500 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004501 goto done;
4502
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004503 last = tline;
4504 tline = expand_smacro(tline->next);
4505 last->next = NULL;
4506
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004507 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004508 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004509 switch (t->type) {
4510 case TOK_WHITESPACE:
4511 break;
4512 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004513 unquote_token(t);
4514 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004515 break;
4516 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004517 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004518 break;
4519 /* else fall through */
4520 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004521 nasm_nonfatal("non-string passed to `%s': %s", dname,
4522 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004523 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004524 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004525 }
4526 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004527
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004528 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004529 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004530 if (t->type == TOK_INTERNAL_STRING)
4531 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004532 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004533 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004534
4535 /*
4536 * We now have a macro name, an implicit parameter count of
4537 * zero, and a numeric token to use as an expansion. Create
4538 * and store an SMacro.
4539 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004540 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004541 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004542 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004543 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004544 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004545
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004547 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004548 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004549 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004550 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004551
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004552 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004553 goto done;
4554
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555 last = tline;
4556 tline = expand_smacro(tline->next);
4557 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004558
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004559 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004560 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004561
4562 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004563
H. Peter Anvine2c80182005-01-15 22:15:51 +00004564 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004565 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004566 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004567 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004568 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004569 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004570
H. Peter Anvin8b262472019-02-26 14:00:54 -08004571 pps.tptr = t->next;
4572 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004574 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004575 if (!evalresult) {
4576 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004577 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004578 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004579 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004580 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004581 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004582 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004583 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004584
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004585 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004586 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004587 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004588 } else {
4589 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004590 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004591 if (!evalresult) {
4592 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004593 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004594 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004595 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004596 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004597 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004598 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004599 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004600 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004601
H. Peter Anvin8571f062019-09-23 16:40:03 -07004602 unquote_token(t);
4603 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004604
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004605 /* make start and count being in range */
4606 if (start < 0)
4607 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004608 if (count < 0)
4609 count = len + count + 1 - start;
4610 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004611 count = len - start;
4612 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004613 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004614
H. Peter Anvin8571f062019-09-23 16:40:03 -07004615 txt = (start < 0) ? "" : tok_text(t) + start;
4616 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004617 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004618
H. Peter Anvine2c80182005-01-15 22:15:51 +00004619 /*
4620 * We now have a macro name, an implicit parameter count of
4621 * zero, and a numeric token to use as an expansion. Create
4622 * and store an SMacro.
4623 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004624 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004625 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004626 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004627 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004628
H. Peter Anvin8b262472019-02-26 14:00:54 -08004629 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004630 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004631 goto done;
4632
H. Peter Anvine2c80182005-01-15 22:15:51 +00004633 last = tline;
4634 tline = expand_smacro(tline->next);
4635 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004636
H. Peter Anvin8b262472019-02-26 14:00:54 -08004637 pps.tptr = tline;
4638 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004639 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004640 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004641 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004642 if (!evalresult)
4643 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004644
H. Peter Anvine2c80182005-01-15 22:15:51 +00004645 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004646 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004647
H. Peter Anvine2c80182005-01-15 22:15:51 +00004648 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004649 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004650 free_tlist(origline);
4651 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004652 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004653
H. Peter Anvin8571f062019-09-23 16:40:03 -07004654 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004655
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 /*
4657 * We now have a macro name, an implicit parameter count of
4658 * zero, and a numeric token to use as an expansion. Create
4659 * and store an SMacro.
4660 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004661 define_smacro(mname, casesense, macro_start, NULL);
4662 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004663
H. Peter Anvind2354082019-08-27 16:38:48 -07004664 case PP_ALIASES:
4665 tline = tline->next;
4666 tline = expand_smacro(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07004667 ppopt.noaliases = !pp_get_boolean_option(tline, !ppopt.noaliases);
H. Peter Anvind2354082019-08-27 16:38:48 -07004668 break;
4669
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 case PP_LINE:
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004671 nasm_panic("`%s' directive not preprocessed early", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004672 break;
H. Peter Anvin (Intel)a79a7002020-07-10 02:55:50 -07004673
4674 case PP_NULL:
4675 /* Goes nowhere, does nothing... */
4676 break;
4677
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004678 }
4679
4680done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004681 free_tlist(origline);
4682 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004683}
4684
4685/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004686 * Ensure that a macro parameter contains a condition code and
4687 * nothing else. Return the condition code index if so, or -1
4688 * otherwise.
4689 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004690static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004691{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004692 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004693
H. Peter Anvin25a99342007-09-22 17:45:45 -07004694 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004695 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004696
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004697 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004698 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004699 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004700 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004701 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004702 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004703 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004704
H. Peter Anvin8571f062019-09-23 16:40:03 -07004705 return bsii(tok_text(t), (const char **)conditions,
4706 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004707}
4708
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004709static inline bool pp_concat_match(const Token *t, unsigned int mask)
4710{
4711 return t && (PP_CONCAT_MASK(t->type) & mask);
4712}
4713
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004714/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004715 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004716 * pasting, if @handle_explicit passed then explicit pasting
4717 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004718 * The @m array can contain a series of token types which are
4719 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004720 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004721static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004722 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004723{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004724 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004725 bool pasted = false;
4726 char *buf, *p;
4727 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004728
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004729 /*
4730 * The last token before pasting. We need it
4731 * to be able to connect new handled tokens.
4732 * In other words if there were a tokens stream
4733 *
4734 * A -> B -> C -> D
4735 *
4736 * and we've joined tokens B and C, the resulting
4737 * stream should be
4738 *
4739 * A -> BC -> D
4740 */
4741 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004742 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004743
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004744 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004745 prev_nonspace = NULL;
4746
4747 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004748 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004749
4750 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004751 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004752 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004753 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004754 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004755
4756 case TOK_PASTE:
4757 /* Explicit pasting */
4758 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004759 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004760
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004761 /* Left pasting token is start of line, just drop %+ */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004762 if (!prev_nonspace) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004763 tok = delete_Token(tok);
4764 break;
4765 }
4766
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004767 did_paste = true;
4768
4769 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004770 t = *prev_nonspace;
4771
4772 /* Delete leading whitespace */
4773 next = zap_white(t->next);
4774
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004775 /*
4776 * Delete the %+ token itself, followed by any whitespace.
4777 * In a sequence of %+ ... %+ ... %+ pasting sequences where
4778 * some expansions in the middle have ended up empty,
4779 * we can end up having multiple %+ tokens in a row;
4780 * just drop whem in that case.
4781 */
4782 while (next) {
4783 if (next->type == TOK_PASTE || next->type == TOK_WHITESPACE)
4784 next = delete_Token(next);
4785 else
4786 break;
4787 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004788
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004789 /*
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004790 * Nothing after? Just leave the existing token.
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004791 */
4792 if (!next) {
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004793 t->next = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004794 break;
4795 }
4796
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004797 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004798 p = mempcpy(p, tok_text(t), t->len);
4799 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004800 *p = '\0';
4801 delete_Token(t);
4802 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004803 nasm_free(buf);
4804
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004805 if (unlikely(!t)) {
4806 /*
4807 * No output at all? Replace with a single whitespace.
4808 * This should never happen.
4809 */
4810 t = new_White(NULL);
4811 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004812
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004813 *prev_nonspace = tok = t;
4814 while (t->next)
4815 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004816
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004817 /* Delete the second token and attach to the end of the list */
4818 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004819
4820 /* We want to restart from the head of the pasted token */
4821 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004822 break;
4823
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004824 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004825 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004826 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004827 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004828 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004829 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004830
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004831 if (i >= mnum)
4832 break;
4833
4834 len = tok->len;
4835 while (pp_concat_match(next, m[i].mask_tail)) {
4836 len += next->len;
4837 next = next->next;
4838 }
4839
4840 /* No match or no text to process */
4841 if (len == tok->len)
4842 break;
4843
4844 p = buf = nasm_malloc(len + 1);
4845 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004846 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004847 tok = delete_Token(tok);
4848 }
4849 *p = '\0';
4850 *prev_next = tok = t = tokenize(buf);
4851 nasm_free(buf);
4852
4853 /*
4854 * Connect pasted into original stream,
4855 * ie A -> new-tokens -> B
4856 */
4857 while (t->next)
4858 t = t->next;
4859 t->next = next;
4860 prev_next = prev_nonspace = &t->next;
4861 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004862 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004863 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004864
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004865 if (did_paste) {
4866 pasted = true;
4867 } else {
4868 prev_next = &tok->next;
4869 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4870 prev_nonspace = prev_next;
4871 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004872
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004873 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004874 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004875
4876 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004877}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004878
4879/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004880 * Computes the proper rotation of mmacro parameters
4881 */
4882static int mmac_rotate(const MMacro *mac, unsigned int n)
4883{
4884 if (--n < mac->nparam)
4885 n = (n + mac->rotate) % mac->nparam;
4886
4887 return n+1;
4888}
4889
4890/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004891 * expands to a list of tokens from %{x:y}
4892 */
H. Peter Anvin (Intel)c0d0f882020-06-30 17:33:39 -07004893static void expand_mmac_params_range(MMacro *mac, Token *tline, Token ***tail)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004894{
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004895 Token *t;
4896 const char *arg = tok_text(tline) + 1;
4897 int fst, lst, incr, n;
4898 int parsed;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004899
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004900 parsed = sscanf(arg, "%d:%d", &fst, &lst);
4901 nasm_assert(parsed == 2);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004902
4903 /*
4904 * only macros params are accounted so
4905 * if someone passes %0 -- we reject such
4906 * value(s)
4907 */
4908 if (lst == 0 || fst == 0)
4909 goto err;
4910
4911 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004912 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4913 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004914 goto err;
4915
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004916 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4917 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004918
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004919 /*
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004920 * It will be at least one parameter, as we can loop
4921 * in either direction.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004922 */
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004923 incr = (fst < lst) ? 1 : -1;
4924
4925 while (true) {
4926 n = mmac_rotate(mac, fst);
4927 dup_tlistn(mac->params[n], mac->paramlen[n], tail);
4928 if (fst == lst)
4929 break;
4930 t = make_tok_char(NULL, ',');
4931 **tail = t;
4932 *tail = &t->next;
4933 fst += incr;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004934 }
4935
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004936 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004937
4938err:
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004939 nasm_nonfatal("`%%{%s}': macro parameters out of range", arg);
4940 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004941}
4942
H. Peter Anvin76690a12002-04-30 20:52:49 +00004943/*
4944 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004945 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004946 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004947 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004948static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004949{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004950 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004951 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004952 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004953
4954 tail = &thead;
4955 thead = NULL;
4956
H. Peter Anvine2c80182005-01-15 22:15:51 +00004957 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004958 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004959 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004960 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004961 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004962 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004963
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004964 tline = tline->next;
4965 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004966
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004967 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004968 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004969 change = true;
4970
4971 if (!mac) {
4972 err_not_mac = true;
4973 break;
4974 }
4975
H. Peter Anvin8571f062019-09-23 16:40:03 -07004976 type = TOK_ID;
4977 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004978 break;
4979 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004980 {
4981 Token *tt = NULL;
4982
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004983 change = true;
4984
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004985 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004986 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004987 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004988 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004989
4990 if (strchr(text, ':')) {
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004991 /* It is a range */
4992 expand_mmac_params_range(mac, t, &tail);
4993 text = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004994 break;
4995 }
4996
4997 switch (text[1]) {
4998 /*
4999 * We have to make a substitution of one of the
5000 * forms %1, %-1, %+1, %%foo, %0, %00.
5001 */
5002 case '0':
5003 if (!text[2]) {
5004 type = TOK_NUMBER;
5005 text = nasm_asprintf("%d", mac->nparam);
5006 break;
5007 }
5008 if (text[2] != '0' || text[3])
5009 goto invalid;
5010 /* a possible captured label == mac->params[0] */
5011 /* fall through */
5012 default:
5013 {
5014 unsigned long n;
5015 char *ep;
5016
5017 n = strtoul(text + 1, &ep, 10);
5018 if (unlikely(*ep))
5019 goto invalid;
5020
5021 if (n <= mac->nparam) {
5022 n = mmac_rotate(mac, n);
5023 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
5024 }
5025 text = NULL;
5026 break;
5027 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005028 case '-':
5029 case '+':
5030 {
5031 int cc;
5032 unsigned long n;
5033 char *ep;
5034
H. Peter Anvin8571f062019-09-23 16:40:03 -07005035 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005036 if (unlikely(*ep))
5037 goto invalid;
5038
Chang S. Bae057b8322020-04-18 23:11:21 +00005039 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005040 n = mmac_rotate(mac, n);
5041 tt = mac->params[n];
5042 }
5043 cc = find_cc(tt);
5044 if (cc == -1) {
5045 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005046 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005047 text = NULL;
5048 break;
5049 }
5050
5051 type = TOK_ID;
5052 if (text[1] == '-') {
5053 int ncc = inverse_ccs[cc];
5054 if (unlikely(ncc == -1)) {
5055 nasm_nonfatal("condition code `%s' is not invertible",
5056 conditions[cc]);
5057 break;
5058 }
5059 cc = ncc;
5060 }
5061 text = nasm_strdup(conditions[cc]);
5062 break;
5063 }
5064
5065 invalid:
5066 nasm_nonfatal("invalid macro parameter: `%s'", text);
5067 text = NULL;
5068 break;
5069 }
5070 break;
5071 }
5072
5073 case TOK_PREPROC_Q:
5074 if (mac) {
5075 type = TOK_ID;
5076 text = nasm_strdup(mac->iname);
5077 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005078 } else {
5079 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005080 }
5081 break;
5082
5083 case TOK_PREPROC_QQ:
5084 if (mac) {
5085 type = TOK_ID;
5086 text = nasm_strdup(mac->name);
5087 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005088 } else {
5089 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005090 }
5091 break;
5092
5093 case TOK_INDIRECT:
5094 {
5095 Token *tt;
5096
H. Peter Anvin8571f062019-09-23 16:40:03 -07005097 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005098 tt = expand_mmac_params(tt);
5099 tt = expand_smacro(tt);
5100 /* Why dup_tlist() here? We should own tt... */
5101 dup_tlist(tt, &tail);
5102 text = NULL;
5103 change = true;
5104 break;
5105 }
5106
5107 default:
5108 change = false;
5109 break;
5110 }
5111
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07005112 if (err_not_mac) {
5113 nasm_nonfatal("`%s': not in a macro call", text);
5114 text = NULL;
5115 change = true;
5116 }
5117
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005118 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119 if (!text) {
5120 delete_Token(t);
5121 } else {
5122 *tail = t;
5123 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005124 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005125 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005127 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005128 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005129 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005130 tail = &t->next;
5131 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005132 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005133
H. Peter Anvineba20a72002-04-30 20:53:55 +00005134 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07005135
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005136 if (changed) {
5137 const struct tokseq_match t[] = {
5138 {
5139 PP_CONCAT_MASK(TOK_ID) |
5140 PP_CONCAT_MASK(TOK_FLOAT), /* head */
5141 PP_CONCAT_MASK(TOK_ID) |
5142 PP_CONCAT_MASK(TOK_NUMBER) |
5143 PP_CONCAT_MASK(TOK_FLOAT) |
5144 PP_CONCAT_MASK(TOK_OTHER) /* tail */
5145 },
5146 {
5147 PP_CONCAT_MASK(TOK_NUMBER), /* head */
5148 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5149 }
5150 };
5151 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
5152 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07005153
H. Peter Anvin76690a12002-04-30 20:52:49 +00005154 return thead;
5155}
5156
H. Peter Anvin322bee02019-08-10 01:38:06 -07005157static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005158
H. Peter Anvin76690a12002-04-30 20:52:49 +00005159/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005160 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005161 * a macro at all, it is simply copied to the output and the pointer
5162 * advanced. tpp should be a pointer to a pointer (usually the next
5163 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005164 * to point to the first token of the expansion, and *tpp updated to
5165 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005166 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005167 * If the expansion is empty, *tpp will be unchanged but **tpp will
5168 * be advanced past the macro call.
5169 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07005170 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005171 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005172static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005173{
5174 Token **params = NULL;
5175 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005176 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005177 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005178 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005179 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005180 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005181 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005182 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005183
5184 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005185 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005186
H. Peter Anvin8571f062019-09-23 16:40:03 -07005187 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005188
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005189 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005190 smacro_deadman.levels--;
5191
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005192 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005193 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005194 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005195 smacro_deadman.triggered = true;
5196 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005197 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005198 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005199 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005200 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005201 Context *ctx = get_ctx(mname, &mname);
5202 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5203 } else {
5204 goto not_a_macro;
5205 }
5206
5207 /*
5208 * We've hit an identifier of some sort. First check whether the
5209 * identifier is a single-line macro at all, then think about
5210 * checking for parameters if necessary.
5211 */
5212 list_for_each(m, head) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005213 if (unlikely(m->alias && ppopt.noaliases))
H. Peter Anvind2354082019-08-27 16:38:48 -07005214 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005215 if (!mstrcmp(m->name, mname, m->casesense))
5216 break;
5217 }
5218
5219 if (!m) {
5220 goto not_a_macro;
5221 }
5222
5223 /* Parse parameters, if applicable */
5224
5225 params = NULL;
5226 nparam = 0;
5227
5228 if (m->nparam == 0) {
5229 /*
5230 * Simple case: the macro is parameterless.
5231 * Nothing to parse; the expansion code will
5232 * drop the macro name token.
5233 */
5234 } else {
5235 /*
5236 * Complicated case: at least one macro with this name
5237 * exists and takes parameters. We must find the
5238 * parameters in the call, count them, find the SMacro
5239 * that corresponds to that form of the macro call, and
5240 * substitute for the parameters when we expand. What a
5241 * pain.
5242 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005243 Token *t;
5244 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005245
5246 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005247 tline = skip_white(tline);
5248 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005249 /*
5250 * This macro wasn't called with parameters: ignore
5251 * the call. (Behaviour borrowed from gnu cpp.)
5252 */
5253 goto not_a_macro;
5254 }
5255
5256 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005257 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005258 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005259 t = tline; /* tline points to leading ( */
5260
5261 while (paren) {
5262 t = t->next;
5263
5264 if (!t) {
5265 nasm_nonfatal("macro call expects terminating `)'");
5266 goto not_a_macro;
5267 }
5268
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005269 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005270 continue;
5271
H. Peter Anvin8571f062019-09-23 16:40:03 -07005272 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005273 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005274 if (!brackets && paren == 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005275 nparam++;
5276 break;
5277
5278 case '{':
5279 brackets++;
5280 break;
5281
5282 case '}':
5283 if (brackets > 0)
5284 brackets--;
5285 break;
5286
5287 case '(':
5288 if (!brackets)
5289 paren++;
5290 break;
5291
5292 case ')':
5293 if (!brackets)
5294 paren--;
5295 break;
5296
5297 default:
5298 break; /* Normal token */
5299 }
5300 }
5301
5302 /*
5303 * Look for a macro matching in both name and parameter count.
5304 * We already know any matches cannot be anywhere before the
5305 * current position of "m", so there is no reason to
5306 * backtrack.
5307 */
5308 while (1) {
5309 if (!m) {
5310 /*!
5311 *!macro-params-single [on] single-line macro calls with wrong parameter count
5312 *! warns about \i{single-line macros} being invoked
5313 *! with the wrong number of parameters.
5314 */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005315 nasm_warn(WARN_MACRO_PARAMS_SINGLE|ERR_HOLD,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005316 "single-line macro `%s' exists, "
5317 "but not taking %d parameter%s",
5318 mname, nparam, (nparam == 1) ? "" : "s");
5319 goto not_a_macro;
5320 }
5321
5322 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005323 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005324 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005325 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005326 break; /* Also good */
5327 }
5328 m = m->next;
5329 }
5330 }
5331
5332 if (m->in_progress)
5333 goto not_a_macro;
5334
5335 /* Expand the macro */
5336 m->in_progress = true;
5337
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005338 if (nparam) {
5339 /* Extract parameters */
5340 Token **phead, **pep;
5341 int white = 0;
5342 int brackets = 0;
5343 int paren;
5344 bool bracketed = false;
5345 bool bad_bracket = false;
5346 enum sparmflags flags;
5347
5348 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005349 paren = 1;
5350 nasm_newn(params, nparam);
5351 i = 0;
5352 flags = m->params[i].flags;
5353 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005354 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005355
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005356 while (paren) {
5357 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005358 char ch;
5359
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005360 tline = tline->next;
5361
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005362 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005363 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005364
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005365 ch = 0;
5366 skip = false;
5367
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005368
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005369 switch (tline->type) {
5370 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005371 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005372 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005373 break;
5374
5375 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005376 if (!(flags & SPARM_NOSTRIP)) {
5377 if (brackets || *phead)
5378 white++; /* Keep interior whitespace */
5379 skip = true;
5380 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005381 break;
5382
5383 default:
5384 break;
5385 }
5386
5387 switch (ch) {
5388 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005389 if (!brackets && paren == 1 && !(flags & SPARM_GREEDY)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005390 i++;
5391 nasm_assert(i < nparam);
5392 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005393 *pep = NULL;
5394 bracketed = false;
5395 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005396 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005397 }
5398 break;
5399
5400 case '{':
5401 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005402 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5403 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005404 }
5405 brackets++;
5406 break;
5407
5408 case '}':
5409 if (brackets > 0) {
5410 if (!--brackets)
5411 skip = bracketed;
5412 }
5413 break;
5414
5415 case '(':
5416 if (!brackets)
5417 paren++;
5418 break;
5419
5420 case ')':
5421 if (!brackets) {
5422 paren--;
5423 if (!paren) {
5424 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005425 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005426 }
5427 }
5428 break;
5429
5430 default:
5431 break; /* Normal token */
5432 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005433
5434 if (!skip) {
5435 Token *t;
5436
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005437 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005438
5439 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005440 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005441 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005442 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005443 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005444 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005445 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005446 }
5447 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005448
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005449 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005450 * Possible further processing of parameters. Note that the
5451 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005452 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005453 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005454 enum sparmflags flags = m->params[i].flags;
5455
5456 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005457 /* Evaluate this parameter as a number */
5458 struct ppscan pps;
5459 struct tokenval tokval;
5460 expr *evalresult;
5461 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005462
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005463 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5464 pps.ntokens = -1;
5465 tokval.t_type = TOKEN_INVALID;
5466 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005467
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005468 free_tlist(eval_param);
5469 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005470
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005471 if (!evalresult) {
5472 /* Nothing meaningful to do */
5473 } else if (tokval.t_type) {
5474 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5475 } else if (!is_simple(evalresult)) {
5476 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5477 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005478 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005479 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005480 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005481
5482 if (flags & SPARM_STR) {
5483 /* Convert expansion to a quoted string */
5484 char *arg;
5485 Token *qs;
5486
5487 qs = expand_smacro_noreset(params[i]);
5488 arg = detoken(qs, false);
5489 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005490 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005491 nasm_free(arg);
5492 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005493 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005494 }
5495
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005496 /* Note: we own the expansion this returns. */
5497 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005498
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005499 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005500 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005501 tline = tafter;
5502
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005503 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005504 cond_comma = false;
5505
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005506 while (t) {
5507 enum pp_token_type type = t->type;
5508 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005509
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005510 switch (type) {
5511 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005512 delete_Token(t);
5513 t = dup_Token(tline, mstart);
5514 break;
5515
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005516 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005517 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005518 size_t mlen = strlen(m->name);
5519 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005520 char *p;
5521
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005522 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005523 if (t->type == TOK_LOCAL_MACRO) {
5524 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005525 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005526 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005527
H. Peter Anvin8571f062019-09-23 16:40:03 -07005528 psp = tok_text(mstart);
5529 get_ctx(psp, &pep);
5530 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005531
H. Peter Anvin8571f062019-09-23 16:40:03 -07005532 len = mlen + plen;
5533 p = nasm_malloc(len + 1);
5534 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005535 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005536 len = mlen;
5537 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005538 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005539 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005540 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005541 set_text_free(t, p, len);
5542
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005543 t->next = tline;
5544 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005545 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005546
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005547 case TOK_COND_COMMA:
5548 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005549 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005550 break;
5551
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005552 case TOK_ID:
5553 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005554 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005555 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005556 /*
5557 * Chain this into the target line *before* expanding,
5558 * that way we pick up any arguments to the new macro call,
5559 * if applicable.
5560 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005561 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005562 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005563 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005564 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005565 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005566 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005567 default:
5568 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005569 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005570 nasm_assert(!tup && param < nparam);
5571 delete_Token(t);
5572 t = NULL;
5573 tup = tnext;
5574 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005575 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005576 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005577 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005578 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005579 }
5580
5581 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005582 Token *endt = tline;
5583
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005584 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005585 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005586 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005587 t = t->next;
5588 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005589 }
5590
5591 if (tnext) {
5592 t = tnext;
5593 } else {
5594 t = tup;
5595 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005596 }
5597 }
5598
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005599 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005600 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005601 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005602
5603 m->in_progress = false;
5604
5605 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005606 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005607 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005608
5609 /*
5610 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005611 * and then advance to the next input token. Note that this is
5612 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005613 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005614not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005615 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005616 m = NULL;
5617done:
5618 smacro_deadman.levels++;
5619 if (unlikely(params))
5620 free_tlist_array(params, nparam);
5621 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005622}
5623
5624/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005625 * Expand all single-line macro calls made in the given line.
5626 * Return the expanded version of the line. The original is deemed
5627 * to be destroyed in the process. (In reality we'll just move
5628 * Tokens from input to output a lot of the time, rather than
5629 * actually bothering to destroy and replicate.)
5630 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005631static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005632{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005633 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005634 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5635 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005636 return expand_smacro_noreset(tline);
5637}
5638
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005639static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005640{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005641 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005642 bool expanded;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005643 errhold errhold; /* Hold warning/errors during expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005644
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005645 if (!org_tline)
5646 return NULL; /* Empty input */
5647
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005648 /*
5649 * Trick: we should avoid changing the start token pointer since it can
5650 * be contained in "next" field of other token. Because of this
5651 * we allocate a copy of first token and work with it; at the end of
5652 * routine we copy it back
5653 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005654 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005655
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005656 /*
5657 * Pretend that we always end up doing expansion on the first pass;
5658 * that way %+ get processed. However, if we process %+ before the
5659 * first pass, we end up with things like MACRO %+ TAIL trying to
5660 * look up the macro "MACROTAIL", which we don't want.
5661 */
5662 expanded = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005663
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005664 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005665 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005666 {
5667 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005668 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5669 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005670 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5671 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005672 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5673 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005674 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5675 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5676 }
5677 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005678 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005679
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005680 /*
5681 * We hold warnings/errors until we are done this this loop. It is
5682 * possible for nuisance warnings to appear that disappear on later
5683 * passes.
5684 */
5685 errhold = nasm_error_hold_push();
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005686
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005687 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005688 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005689
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005690 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005691 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005692
5693 /*
5694 * Now scan the entire line and look for successive TOK_IDs
5695 * that resulted after expansion (they can't be produced by
5696 * tokenize()). The successive TOK_IDs should be concatenated.
5697 * Also we look for %+ tokens and concatenate the tokens
5698 * before and after them (without white spaces in between).
5699 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005700 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5701 break; /* Done again! */
5702
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005703 nasm_error_hold_pop(errhold, false);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005704 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005705 }
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005706 nasm_error_hold_pop(errhold, true);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005707
H. Peter Anvin8571f062019-09-23 16:40:03 -07005708 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005709 /*
5710 * The expression expanded to empty line;
5711 * we can't return NULL because of the "trick" above.
5712 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005713 */
5714
5715 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005716 }
5717
H. Peter Anvin8571f062019-09-23 16:40:03 -07005718 steal_Token(org_tline, tline);
5719 org_tline->next = tline->next;
5720 delete_Token(tline);
5721
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005722 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005723}
5724
5725/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005726 * Similar to expand_smacro but used exclusively with macro identifiers
5727 * right before they are fetched in. The reason is that there can be
5728 * identifiers consisting of several subparts. We consider that if there
5729 * are more than one element forming the name, user wants a expansion,
5730 * otherwise it will be left as-is. Example:
5731 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005732 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005733 *
5734 * the identifier %$abc will be left as-is so that the handler for %define
5735 * will suck it and define the corresponding value. Other case:
5736 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005737 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005738 *
5739 * In this case user wants name to be expanded *before* %define starts
5740 * working, so we'll expand %$abc into something (if it has a value;
5741 * otherwise it will be left as-is) then concatenate all successive
5742 * PP_IDs into one.
5743 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005744static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005745{
5746 Token *cur, *oldnext = NULL;
5747
H. Peter Anvin734b1882002-04-30 21:01:08 +00005748 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005749 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005750
5751 cur = tline;
5752 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005753 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5754 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005755 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005756
5757 /* If identifier consists of just one token, don't expand */
5758 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005759 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005760
H. Peter Anvine2c80182005-01-15 22:15:51 +00005761 if (cur) {
5762 oldnext = cur->next; /* Detach the tail past identifier */
5763 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005764 }
5765
H. Peter Anvin734b1882002-04-30 21:01:08 +00005766 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005767
H. Peter Anvine2c80182005-01-15 22:15:51 +00005768 if (cur) {
5769 /* expand_smacro possibly changhed tline; re-scan for EOL */
5770 cur = tline;
5771 while (cur && cur->next)
5772 cur = cur->next;
5773 if (cur)
5774 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005775 }
5776
5777 return tline;
5778}
5779
5780/*
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005781 * This is called from find_mmacro_in_list() after finding a suitable macro.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005782 */
5783static MMacro *use_mmacro(MMacro *m, int *nparamp, Token ***paramsp)
5784{
5785 int nparam = *nparamp;
5786 Token **params = *paramsp;
5787
5788 /*
5789 * This one is right. Just check if cycle removal
5790 * prohibits us using it before we actually celebrate...
5791 */
5792 if (m->in_progress > m->max_depth) {
5793 if (m->max_depth > 0) {
5794 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
5795 m->max_depth);
5796 }
5797 nasm_free(params);
5798 *nparamp = 0;
5799 *paramsp = NULL;
5800 return NULL;
5801 }
5802
5803 /*
5804 * It's right, and we can use it. Add its default
5805 * parameters to the end of our list if necessary.
5806 */
5807 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
5808 int newnparam = m->nparam_min + m->ndefs;
5809 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5810 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5811 (newnparam - nparam) * sizeof(*params));
5812 nparam = newnparam;
5813 }
5814 /*
5815 * If we've gone over the maximum parameter count (and
5816 * we're in Plus mode), ignore parameters beyond
5817 * nparam_max.
5818 */
5819 if (m->plus && nparam > m->nparam_max)
5820 nparam = m->nparam_max;
5821
5822 /*
5823 * If nparam was adjusted above, make sure the list is still
5824 * NULL-terminated.
5825 */
5826 params[nparam+1] = NULL;
5827
5828 /* Done! */
5829 *paramsp = params;
5830 *nparamp = nparam;
5831 return m;
5832}
5833
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005834/*
5835 * Search a macro list and try to find a match. If matching, call
5836 * use_mmacro() to set up the macro call. m points to the list of
5837 * search, which is_mmacro() sets to the first *possible* match.
5838 */
5839static MMacro *
5840find_mmacro_in_list(MMacro *m, const char *finding,
5841 int *nparamp, Token ***paramsp)
5842{
5843 int nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005844
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005845 while (m) {
5846 if (m->nparam_min <= nparam
5847 && (m->plus || nparam <= m->nparam_max)) {
5848 /*
5849 * This one matches, use it.
5850 */
5851 return use_mmacro(m, nparamp, paramsp);
5852 }
5853
5854 /*
5855 * Otherwise search for the next one with a name match.
5856 */
5857 list_for_each(m, m->next) {
5858 if (!mstrcmp(m->name, finding, m->casesense))
5859 break;
5860 }
5861 }
5862
5863 return NULL;
5864}
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005865
5866/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005867 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005868 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005869 * to check for an initial label - that's taken care of in
5870 * expand_mmacro - but must check numbers of parameters. Guaranteed
5871 * to be called with tline->type == TOK_ID, so the putative macro
5872 * name is easy to find.
5873 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005874static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005875{
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005876 MMacro *head, *m, *found;
5877 Token **params, **comma;
5878 int raw_nparam, nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005879 const char *finding = tok_text(tline);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005880 bool empty_args = !tline->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005881
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005882 *nparamp = 0;
5883 *paramsp = NULL;
5884
H. Peter Anvin8571f062019-09-23 16:40:03 -07005885 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005886
5887 /*
5888 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005889 * name which isn't already excluded by macro cycle removal.
5890 * (The cycle removal test here helps optimize the case of wrapping
5891 * instructions, and is cheap to do here.)
5892 *
5893 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005894 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005895 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005896 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005897 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005898 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005899 (m->in_progress != 1 || m->max_depth > 0))
5900 break; /* Found something that needs consideration */
5901 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005902 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005903 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005904
5905 /*
5906 * OK, we have a potential macro. Count and demarcate the
5907 * parameters.
5908 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005909 comma = count_mmac_params(tline->next, nparamp, paramsp);
5910 raw_nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005911
5912 /*
5913 * Search for an exact match. This cannot come *before* the m
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005914 * found in the list search before, so we can start there.
5915 *
5916 * If found is NULL and *paramsp has been cleared, then we
5917 * encountered an error for which we have already issued a
5918 * diagnostic, so we should not proceed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005919 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005920 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5921 if (!*paramsp)
5922 return NULL;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005923
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005924 nparam = *nparamp;
5925 params = *paramsp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005926
5927 /*
5928 * Special weirdness: in NASM < 2.15, an expansion of
5929 * *only* whitespace, as can happen during macro expansion under
5930 * certain circumstances, is counted as zero arguments for the
5931 * purpose of %0, but one argument for the purpose of macro
5932 * matching! In particular, this affects:
5933 *
5934 * foobar %1
5935 *
5936 * ... with %1 being empty; this would call the one-argument
5937 * version of "foobar" with an empty argument, equivalent to
5938 *
5939 * foobar {%1}
5940 *
5941 * ... except that %0 would be set to 0 inside foobar, even if
5942 * foobar is declared with "%macro foobar 1" or equivalent!
5943 *
5944 * The proper way to do that is to define "%macro foobar 0-1".
5945 *
5946 * To be compatible without doing something too stupid, try to
5947 * match a zero-argument macro first, but if that fails, try
5948 * for a one-argument macro with the above behavior.
5949 *
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005950 * Furthermore, NASM < 2.15 will match stripping a tailing empty
5951 * argument, but in that case %0 *does* reflect that this argument
5952 * have been stripped; this is handled in count_mmac_params().
5953 *
5954 * To disable these insane legacy behaviors, use:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005955 *
5956 * %pragma preproc sane_empty_expansion yes
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005957 *
5958 *!macro-params-legacy [on] improperly calling multi-line macro for legacy support
5959 *! warns about \i{multi-line macros} being invoked
5960 *! with the wrong number of parameters, but for bug-compatibility
5961 *! with NASM versions older than 2.15, NASM tried to fix up the
5962 *! parameters to match the legacy behavior and call the macro anyway.
5963 *! This can happen in certain cases where there are empty arguments
5964 *! without braces, sometimes as a result of macro expansion.
5965 *!-
5966 *! The legacy behavior is quite strange and highly context-dependent,
5967 *! and can be disabled with:
5968 *!-
H. Peter Anvin (Intel)de8817d2020-06-27 22:30:50 -07005969 *! \c %pragma preproc sane_empty_expansion true
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005970 *!-
5971 *! It is highly recommended to use this option in new code.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005972 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005973 if (!ppopt.sane_empty_expansion) {
5974 if (!found) {
5975 if (raw_nparam == 0 && !empty_args) {
5976 /*
5977 * A single all-whitespace parameter as the only thing?
5978 * Look for a one-argument macro, but don't adjust
5979 * *nparamp.
5980 */
5981 int bogus_nparam = 1;
5982 params[2] = NULL;
5983 found = find_mmacro_in_list(m, finding, &bogus_nparam, paramsp);
5984 } else if (raw_nparam > 1 && comma) {
5985 Token *comma_tail = *comma;
5986
5987 /*
5988 * Drop the terminal argument and try again.
5989 * If we fail, we need to restore the comma to
5990 * preserve tlist.
5991 */
5992 *comma = NULL;
5993 *nparamp = raw_nparam - 1;
5994 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5995 if (found)
5996 free_tlist(comma_tail);
5997 else
5998 *comma = comma_tail;
5999 }
6000
6001 if (!*paramsp)
6002 return NULL;
6003 } else if (comma) {
6004 free_tlist(*comma);
6005 *comma = NULL;
6006 if (raw_nparam > found->nparam_min &&
6007 raw_nparam <= found->nparam_min + found->ndefs) {
6008 /* Replace empty argument with default parameter */
6009 params[raw_nparam] =
6010 found->defaults[raw_nparam - found->nparam_min];
6011 } else if (raw_nparam > found->nparam_max && found->plus) {
6012 /* Just drop the comma, don't adjust argument count */
6013 } else {
6014 /* Drop argument. This may cause nparam < nparam_min. */
6015 params[raw_nparam] = NULL;
6016 *nparamp = nparam = raw_nparam - 1;
6017 }
6018 }
6019
6020 if (found) {
6021 if (raw_nparam < found->nparam_min ||
6022 (raw_nparam > found->nparam_max && !found->plus)) {
6023 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
6024 "improperly calling multi-line macro `%s' with %d parameters",
6025 found->name, raw_nparam);
6026 } else if (comma) {
6027 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
6028 "dropping trailing empty parameter in call to multi-line macro `%s'", found->name);
6029 }
6030 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006031 }
6032
6033 /*
6034 * After all that, we didn't find one with the right number of
6035 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006036 *!
6037 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
6038 *! warns about \i{multi-line macros} being invoked
6039 *! with the wrong number of parameters. See \k{mlmacover} for an
6040 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006041 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006042 if (found)
6043 return found;
6044
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006045 nasm_warn(WARN_MACRO_PARAMS_MULTI,
6046 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006047 finding, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006048 nasm_free(*paramsp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006049 return NULL;
6050}
6051
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006052
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006053#if 0
6054
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006055/*
6056 * Save MMacro invocation specific fields in
6057 * preparation for a recursive macro expansion
6058 */
6059static void push_mmacro(MMacro *m)
6060{
6061 MMacroInvocation *i;
6062
6063 i = nasm_malloc(sizeof(MMacroInvocation));
6064 i->prev = m->prev;
6065 i->params = m->params;
6066 i->iline = m->iline;
6067 i->nparam = m->nparam;
6068 i->rotate = m->rotate;
6069 i->paramlen = m->paramlen;
6070 i->unique = m->unique;
6071 i->condcnt = m->condcnt;
6072 m->prev = i;
6073}
6074
6075
6076/*
6077 * Restore MMacro invocation specific fields that were
6078 * saved during a previous recursive macro expansion
6079 */
6080static void pop_mmacro(MMacro *m)
6081{
6082 MMacroInvocation *i;
6083
6084 if (m->prev) {
6085 i = m->prev;
6086 m->prev = i->prev;
6087 m->params = i->params;
6088 m->iline = i->iline;
6089 m->nparam = i->nparam;
6090 m->rotate = i->rotate;
6091 m->paramlen = i->paramlen;
6092 m->unique = i->unique;
6093 m->condcnt = i->condcnt;
6094 nasm_free(i);
6095 }
6096}
6097
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006098#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006099
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006100/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07006101 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006102 */
6103static void list_mmacro_call(const MMacro *m)
6104{
6105 const char prefix[] = " ;;; [macro] ";
6106 size_t namelen, size;
6107 char *buf, *p;
6108 unsigned int i;
6109 const Token *t;
6110
6111 namelen = strlen(m->iname);
6112 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
6113
6114 for (i = 1; i <= m->nparam; i++) {
6115 int j = 0;
6116 size += 3; /* Braces and space/comma */
6117 list_for_each(t, m->params[i]) {
6118 if (j++ >= m->paramlen[i])
6119 break;
6120 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
6121 }
6122 }
6123
6124 buf = p = nasm_malloc(size);
6125 p = mempcpy(p, prefix, sizeof(prefix) - 1);
6126 p = mempcpy(p, m->iname, namelen);
6127 *p++ = ' ';
6128
6129 for (i = 1; i <= m->nparam; i++) {
6130 int j = 0;
6131 *p++ = '{';
6132 list_for_each(t, m->params[i]) {
6133 if (j++ >= m->paramlen[i])
6134 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006135 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006136 }
6137 *p++ = '}';
6138 *p++ = ',';
6139 }
6140
6141 *--p = '\0'; /* Replace last delimeter with null */
6142 lfmt->line(LIST_MACRO, -1, buf);
6143 nasm_free(buf);
6144}
6145
6146/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006147 * Expand the multi-line macro call made by the given line, if
6148 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006149 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006150 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006151static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006152{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006153 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006154 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006155 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006156 Token **params, *t, *tt;
6157 MMacro *m;
6158 Line *l, *ll;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006159 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07006160 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006161 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006162
6163 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006164 t = skip_white(t);
6165 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07006166 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006167 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006168 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006169 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006170 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07006171 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006172 Token *last;
6173 /*
6174 * We have an id which isn't a macro call. We'll assume
6175 * it might be a label; we'll also check to see if a
6176 * colon follows it. Then, if there's another id after
6177 * that lot, we'll check it again for macro-hood.
6178 */
6179 label = last = t;
6180 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006181 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006182 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006183 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006184 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006185 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006186 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006187 last = t, t = t->next;
6188 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006189 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006190 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006191 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006192 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006193 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006194 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006195
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006196 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
6197 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
6198 if (!mmacro_deadman.triggered) {
6199 nasm_nonfatal("interminable multiline macro recursion");
6200 mmacro_deadman.triggered = true;
6201 }
6202 return 0;
6203 }
6204
6205 mmacro_deadman.total++;
6206 mmacro_deadman.levels++;
6207
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006208 /*
6209 * Fix up the parameters: this involves stripping leading and
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006210 * trailing whitespace and stripping braces if they are present.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006211 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006212 nasm_newn(paramlen, nparam+1);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006213
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006214 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006215 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08006216 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006217 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006218 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006219
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006220 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006221 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006222 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006223 brace = 1;
6224 braced = true;
6225 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006226 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006227
6228 params[i] = t;
6229 for (; t; t = t->next) {
6230 if (tok_white(t)) {
6231 white++;
6232 continue;
6233 }
6234
6235 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006236 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006237 case ',':
6238 if (comma && !brace)
6239 goto endparam;
6240 break;
6241
6242 case '{':
6243 brace++;
6244 break;
6245
6246 case '}':
6247 brace--;
6248 if (braced && !brace) {
6249 paramlen[i] += white;
6250 goto endparam;
6251 }
6252 break;
6253
6254 default:
6255 break;
6256 }
6257 }
6258
6259 paramlen[i] += white + 1;
6260 white = 0;
6261 }
6262 endparam:
6263 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006264 }
6265
6266 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006267 * OK, we have a MMacro structure together with a set of
6268 * parameters. We must now go through the expansion and push
6269 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00006270 * parameter tokens and macro-local tokens doesn't get done
6271 * until the single-line macro substitution process; this is
6272 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006273 * later through %rotate and give the right semantics for
6274 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006275 *
6276 * First, push an end marker on to istk->expansion, mark this
6277 * macro as in progress, and set up its invocation-specific
6278 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006279 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006280 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006281 ll->next = istk->expansion;
6282 ll->finishes = m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006283 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006284 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006285
6286 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006287 * Save the previous MMacro expansion in the case of
6288 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006289 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006290#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006291 if (m->max_depth && m->in_progress)
6292 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006293#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006294
6295 m->in_progress ++;
6296 m->params = params;
6297 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006298 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006299 m->nparam = nparam;
6300 m->rotate = 0;
6301 m->paramlen = paramlen;
6302 m->unique = unique++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006303 m->condcnt = 0;
6304
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006305 m->mstk = istk->mstk;
6306 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006307
6308 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006309 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006310 ll->next = istk->expansion;
6311 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006312 ll->first = dup_tlist(l->first, NULL);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006313 ll->where = l->where;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006314 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006315
6316 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006317 * If we had a label, and this macro definition does not include
6318 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00006319 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006320 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006321 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006322 /*
6323 * We had a label. If this macro contains an %00 parameter,
6324 * save the value as a special parameter (which is what it
6325 * is), otherwise push it as the first line of the macro
6326 * expansion.
6327 */
6328 if (m->capture_label) {
6329 params[0] = dup_Token(NULL, label);
6330 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006331 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006332 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006333 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006334 ll->finishes = NULL;
6335 ll->next = istk->expansion;
6336 istk->expansion = ll;
6337 ll->first = startline;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006338 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006339 if (!dont_prepend) {
6340 while (label->next)
6341 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006342 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006343 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006344 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006345 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006346
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006347 istk->nolist += !!(m->nolist & NL_LIST);
6348 istk->noline += !!(m->nolist & NL_LINE);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006349
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006350 if (!istk->nolist) {
6351 lfmt->uplevel(LIST_MACRO, 0);
6352
6353 if (list_option('m'))
6354 list_mmacro_call(m);
6355 }
6356
6357 if (!istk->noline)
6358 src_macro_push(m, istk->where);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006359
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006360 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006361}
6362
H. Peter Anvin130736c2016-02-17 20:27:41 -08006363/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006364 * This function decides if an error message should be suppressed.
6365 * It will never be called with a severity level of ERR_FATAL or
6366 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006367 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006368static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006369{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006370 /*
6371 * If we're in a dead branch of IF or something like it, ignore the error.
6372 * However, because %else etc are evaluated in the state context
6373 * of the previous branch, errors might get lost:
6374 * %if 0 ... %else trailing garbage ... %endif
6375 * So %else etc should set the ERR_PP_PRECOND flag.
6376 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006377 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006378 ((severity & ERR_PP_PRECOND) ?
6379 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006380 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006381 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006382
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006383 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006384}
6385
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006386static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006387stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006388{
6389 (void)s;
6390 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006391 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006392
H. Peter Anvin8571f062019-09-23 16:40:03 -07006393 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006394}
6395
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006396static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006397stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006398{
6399 (void)s;
6400 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006401 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006402
H. Peter Anvin8571f062019-09-23 16:40:03 -07006403 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006404}
6405
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006406static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006407stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006408{
6409 (void)s;
6410 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006411 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006412
H. Peter Anvin8571f062019-09-23 16:40:03 -07006413 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006414}
6415
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006416static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006417stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006418{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006419 (void)s;
6420 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006421 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006422
6423 switch (globalbits) {
6424 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006425 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006426 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006427 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006428 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006429 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006430 default:
6431 panic();
6432 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006433}
6434
H. Peter Anvin8b262472019-02-26 14:00:54 -08006435/* Add magic standard macros */
6436struct magic_macros {
6437 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006438 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006439 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006440};
6441static const struct magic_macros magic_macros[] =
6442{
H. Peter Anvind2354082019-08-27 16:38:48 -07006443 { "__?FILE?__", 0, stdmac_file },
6444 { "__?LINE?__", 0, stdmac_line },
6445 { "__?BITS?__", 0, stdmac_bits },
6446 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006447 { NULL, 0, NULL }
6448};
6449
6450static void pp_add_magic_stdmac(void)
6451{
6452 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006453 SMacro tmpl;
6454
6455 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006456
6457 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006458 tmpl.nparam = m->nparam;
6459 tmpl.expand = m->func;
6460 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006461 }
6462}
6463
H. Peter Anvin734b1882002-04-30 21:01:08 +00006464static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006465pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006466{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006467 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006468 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006469
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006470 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006471 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006472 nested_mac_count = 0;
6473 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006474 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006475 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006476 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006477 pp_mode = mode;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006478
6479 /* Reset options to default */
6480 nasm_zero(ppopt);
H. Peter Anvinf7606612016-07-13 14:23:48 -07006481
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006482 if (!use_loaded)
6483 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6484 memset(use_loaded, 0, use_package_count * sizeof(bool));
6485
H. Peter Anvin6686de22019-08-10 05:33:14 -07006486 /* First set up the top level input file */
6487 nasm_new(istk);
6488 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006489 if (!istk->fp) {
6490 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'%s%s",
6491 file, errno ? " " : "", errno ? strerror(errno) : "");
6492 }
H. Peter Anvin6686de22019-08-10 05:33:14 -07006493 src_set(0, file);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006494 istk->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006495 istk->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006496
6497 strlist_add(deplist, file);
6498
6499 /*
6500 * Set up the stdmac packages as a virtual include file,
6501 * indicated by a null file pointer.
6502 */
6503 nasm_new(inc);
6504 inc->next = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006505 src_set(0, NULL);
6506 inc->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006507 inc->nolist = !list_option('b');
6508 istk = inc;
6509 lfmt->uplevel(LIST_INCLUDE, 0);
6510
H. Peter Anvin8b262472019-02-26 14:00:54 -08006511 pp_add_magic_stdmac();
6512
H. Peter Anvinf7606612016-07-13 14:23:48 -07006513 if (tasm_compatible_mode)
6514 pp_add_stdmac(nasm_stdmac_tasm);
6515
6516 pp_add_stdmac(nasm_stdmac_nasm);
6517 pp_add_stdmac(nasm_stdmac_version);
6518
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006519 if (extrastdmac)
6520 pp_add_stdmac(extrastdmac);
6521
H. Peter Anvinf7606612016-07-13 14:23:48 -07006522 stdmacpos = stdmacros[0];
6523 stdmacnext = &stdmacros[1];
6524
H. Peter Anvind2456592008-06-19 15:04:18 -07006525 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006526
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006527 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006528 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006529 * other builtins, because it is special -- it varies between
6530 * passes -- but there is really no particular reason to make it
6531 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006532 *
6533 * 0 = dependencies only
6534 * 1 = preparatory passes
6535 * 2 = final pass
6536 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006537 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006538 switch (mode) {
6539 case PP_NORMAL:
6540 apass = pass_final() ? 2 : 1;
6541 break;
6542 case PP_DEPS:
6543 apass = 0;
6544 break;
6545 case PP_PREPROC:
6546 apass = 3;
6547 break;
6548 default:
6549 panic();
6550 }
6551
H. Peter Anvin8571f062019-09-23 16:40:03 -07006552 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006553}
6554
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006555static void pp_init(void)
6556{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006557}
6558
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006559/*
6560 * Get a line of tokens. If we popped the macro expansion/include stack,
6561 * we return a pointer to the dummy token tok_pop; at that point if
6562 * istk is NULL then we have reached end of input;
6563 */
6564static Token tok_pop; /* Dummy token placeholder */
6565
6566static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006567{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006568 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006569 Line *l = istk->expansion;
6570 Token *tline = NULL;
6571 Token *dtline;
6572
H. Peter Anvine2c80182005-01-15 22:15:51 +00006573 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006574 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006575 * buffer or from the input file.
6576 */
6577 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006578 while (l && l->finishes) {
6579 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006580
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006581 nasm_assert(fm == istk->mstk.mstk);
6582
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006583 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006584 /*
6585 * This is a macro-end marker for a macro with no
6586 * name, which means it's not really a macro at all
6587 * but a %rep block, and the `in_progress' field is
6588 * more than 1, meaning that we still need to
6589 * repeat. (1 means the natural last repetition; 0
6590 * means termination by %exitrep.) We have
6591 * therefore expanded up to the %endrep, and must
6592 * push the whole block on to the expansion buffer
6593 * again. We don't bother to remove the macro-end
6594 * marker: we'd only have to generate another one
6595 * if we did.
6596 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006597 fm->in_progress--;
6598 list_for_each(l, fm->expansion) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006599 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006600
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006601 nasm_new(ll);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006602 ll->next = istk->expansion;
6603 ll->first = dup_tlist(l->first, NULL);
6604 ll->where = l->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006605 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006606 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006607 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006608 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006609 MMacro *m = istk->mstk.mstk;
6610
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006611 /*
6612 * Check whether a `%rep' was started and not ended
6613 * within this macro expansion. This can happen and
6614 * should be detected. It's a fatal error because
6615 * I'm too confused to work out how to recover
6616 * sensibly from it.
6617 */
6618 if (defining) {
6619 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006620 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006621 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006622 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006623 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006624 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006625
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006626 /*
6627 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006628 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006629 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006630 istk->mstk = m->mstk;
6631 if (m->name) {
6632 /*
6633 * This was a real macro call, not a %rep, and
6634 * therefore the parameter information needs to
6635 * be freed and the iteration count/nesting
6636 * depth adjusted.
6637 */
6638
6639 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006640 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006641 * If all mmacro processing done,
6642 * clear all counters and the deadman
6643 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006644 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006645 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006646 }
6647
Adam Majer91e72402017-07-25 10:42:01 +02006648#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006649 if (m->prev) {
6650 pop_mmacro(m);
6651 fm->in_progress --;
6652 } else
Adam Majer91e72402017-07-25 10:42:01 +02006653#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006654 {
6655 nasm_free(m->params);
6656 free_tlist(m->iline);
6657 nasm_free(m->paramlen);
6658 fm->in_progress = 0;
6659 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006660 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006661
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006662 if (fm->nolist & NL_LIST) {
6663 istk->nolist--;
6664 } else if (!istk->nolist) {
6665 lfmt->downlevel(LIST_MACRO);
6666 }
6667
6668 if (fm->nolist & NL_LINE) {
6669 istk->noline--;
6670 } else if (!istk->noline) {
6671 if (fm == src_macro_current())
6672 src_macro_pop();
6673 src_update(l->where);
6674 }
6675
6676 istk->where = l->where;
6677
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006678 /*
6679 * FIXME It is incorrect to always free_mmacro here.
6680 * It leads to usage-after-free.
6681 *
6682 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6683 */
6684#if 0
6685 else
6686 free_mmacro(m);
6687#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006688 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006689 istk->expansion = l->next;
6690 nasm_free(l);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006691
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006692 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006693 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006694
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006695 do { /* until we get a line we can use */
6696 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006697
6698 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006699 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006700
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006701 istk->expansion = l->next;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006702 istk->where = l->where;
6703 tline = l->first;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006704 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006705
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006706 if (!istk->noline)
6707 src_update(istk->where);
6708
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006709 if (!istk->nolist) {
6710 line = detoken(tline, false);
6711 lfmt->line(LIST_MACRO, istk->where.lineno, line);
6712 nasm_free(line);
6713 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006714 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006715 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006716 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006717 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006718 } else {
6719 /*
6720 * The current file has ended; work down the istk
6721 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006722 Include *i = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006723 Include *is;
6724
H. Peter Anvin6686de22019-08-10 05:33:14 -07006725 if (i->fp)
6726 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006727 if (i->conds) {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006728 /* nasm_fatal can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006729 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006730 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006731
6732 list_for_each(is, i->next) {
6733 if (is->fp) {
6734 lfmt->downlevel(LIST_INCLUDE);
6735 src_update(is->where);
6736 break;
6737 }
6738 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006739 istk = i->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006740 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006741 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006742 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006743 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006744
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006745 /*
6746 * We must expand MMacro parameters and MMacro-local labels
6747 * _before_ we plunge into directive processing, to cope
6748 * with things like `%define something %1' such as STRUC
6749 * uses. Unless we're _defining_ a MMacro, in which case
6750 * those tokens should be left alone to go into the
6751 * definition; and unless we're in a non-emitting
6752 * condition, in which case we don't want to meddle with
6753 * anything.
6754 */
H. Peter Anvin (Intel)bacf04a2020-06-08 13:29:06 -07006755 if (!defining &&
6756 !(istk->conds && !emitting(istk->conds->state)) &&
6757 !(istk->mstk.mmac && !istk->mstk.mmac->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006758 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006759 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006760
H. Peter Anvine2c80182005-01-15 22:15:51 +00006761 /*
6762 * Check the line to see if it's a preprocessor directive.
6763 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006764 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6765 if (dtline)
6766 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006767 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006768 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006769 * We're defining a multi-line macro. We emit nothing
6770 * at all, and just
6771 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006772 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006773 MMacro *mmac = defining->dstk.mmac;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006774 Line *l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006775
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006776 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006777 l->next = defining->expansion;
6778 l->first = tline;
6779 l->finishes = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006780 l->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006781 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006782
6783 /*
6784 * Remember if this mmacro expansion contains %00:
6785 * if it does, we will have to handle leading labels
6786 * specially.
6787 */
6788 if (mmac) {
6789 const Token *t;
6790 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006791 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006792 mmac->capture_label = true;
6793 }
6794 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006795 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006796 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006797 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006798 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006799 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006800 * directive so we keep our place correctly.
6801 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006802 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006803 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006804 /*
6805 * We're in a %rep block which has been terminated, so
6806 * we're walking through to the %endrep without
6807 * emitting anything. Emit nothing at all, not even a
6808 * blank line: when we emerge from the %rep block we'll
6809 * give a line-number directive so we keep our place
6810 * correctly.
6811 */
6812 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006813 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006814 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006815 if (!expand_mmacro(tline))
6816 return tline;
6817 }
6818 }
6819}
6820
6821static char *pp_getline(void)
6822{
6823 char *line = NULL;
6824 Token *tline;
6825
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006826 while (true) {
6827 tline = pp_tokline();
6828 if (tline == &tok_pop) {
6829 /*
6830 * We popped the macro/include stack. If istk is empty,
6831 * we are at end of input, otherwise just loop back.
6832 */
6833 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006834 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006835 } else {
6836 /*
6837 * De-tokenize the line and emit it.
6838 */
6839 line = detoken(tline, true);
6840 free_tlist(tline);
6841 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006842 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006843 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006844
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006845 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006846 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006847 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006848 nasm_free(buf);
6849 }
6850
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006851 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006852}
6853
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006854static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006855{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006856 if (defining) {
6857 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006858 nasm_nonfatal("end of file while still defining macro `%s'",
6859 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006860 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006861 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006862 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006863
6864 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006865 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006866 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006867
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006868 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006869 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006870 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006871 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006872 Include *i = istk;
6873 istk = istk->next;
6874 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006875 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006876 }
6877 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006878 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006879 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006880}
6881
6882static void pp_cleanup_session(void)
6883{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006884 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006885 free_llist(predef);
6886 predef = NULL;
6887 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006888 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006889}
6890
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006891static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006892{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006893 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006894}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006895
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006896static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006897{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006898 Token *inc, *space, *name;
6899 Line *l;
6900
H. Peter Anvin734b1882002-04-30 21:01:08 +00006901 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006902 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006903 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006904
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006905 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006906 l->next = predef;
6907 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006908 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006909 predef = l;
6910}
6911
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006912static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006913{
6914 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006915 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006916 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006917
6918 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006919 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006920 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006921 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006922 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006923 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006924 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006925 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006926
H. Peter Anvin8571f062019-09-23 16:40:03 -07006927 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006928 if (space->next->type != TOK_PREPROC_ID &&
6929 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006930 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006931
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006932 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006933 l->next = predef;
6934 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006935 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006936 predef = l;
6937}
6938
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006939static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006940{
6941 Token *def, *space;
6942 Line *l;
6943
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006944 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006945 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006946 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006947
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006948 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006949 l->next = predef;
6950 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006951 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006952 predef = l;
6953}
6954
H. Peter Anvin05990342018-06-11 13:32:42 -07006955/* Insert an early preprocessor command that doesn't need special handling */
6956static void pp_pre_command(const char *what, char *string)
6957{
6958 char *cmd;
6959 Token *def, *space;
6960 Line *l;
6961
6962 def = tokenize(string);
6963 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006964 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006965 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6966 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6967 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006968 }
6969
6970 l = nasm_malloc(sizeof(Line));
6971 l->next = predef;
6972 l->first = def;
6973 l->finishes = NULL;
6974 predef = l;
6975}
6976
H. Peter Anvinf7606612016-07-13 14:23:48 -07006977static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006978{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006979 macros_t **mp;
6980
6981 /* Find the end of the list and avoid duplicates */
6982 for (mp = stdmacros; *mp; mp++) {
6983 if (*mp == macros)
6984 return; /* Nothing to do */
6985 }
6986
6987 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6988
6989 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006990}
6991
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006992static void pp_extra_stdmac(macros_t *macros)
6993{
6994 extrastdmac = macros;
6995}
6996
H. Peter Anvin8571f062019-09-23 16:40:03 -07006997/* Create a numeric token */
6998static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006999{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04007000 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08007001 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07007002 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08007003}
7004
H. Peter Anvin8571f062019-09-23 16:40:03 -07007005/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07007006static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08007007{
H. Peter Anvin8571f062019-09-23 16:40:03 -07007008 char *p = nasm_quote(str, &len);
7009 return new_Token_free(next, TOK_STRING, p, len);
7010}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07007011static Token *make_tok_qstr(Token *next, const char *str)
7012{
7013 return make_tok_qstr_len(next, str, strlen(str));
7014}
H. Peter Anvin8571f062019-09-23 16:40:03 -07007015
7016/* Create a single-character operator token */
7017static Token *make_tok_char(Token *next, char op)
7018{
7019 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
7020 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08007021 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00007022}
7023
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07007024/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007025 * Descent the macro hierarchy and display the expansion after
7026 * encountering an error message.
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07007027 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08007028static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007029{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007030 const MMacro *m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07007031
H. Peter Anvinddb29062018-12-11 00:06:29 -08007032 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007033
7034 while ((m = src_error_down())) {
7035 nasm_error(severity, "... from macro `%s' defined", m->name);
7036 }
7037
7038 src_error_reset();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007039}
7040
H. Peter Anvine7469712016-02-18 02:20:59 -08007041const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07007042 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007043 pp_reset,
7044 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08007045 pp_cleanup_pass,
7046 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03007047 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04007048 pp_pre_define,
7049 pp_pre_undefine,
7050 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07007051 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007052 pp_include_path,
7053 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07007054 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007055};