blob: 8230f16f2960a4d4b87a30cc7cab99139e6fe770 [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 */
H. Peter Anvin (Intel)d9ea17f2020-07-10 18:40:05 -0700948static inline char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000949{
H. Peter Anvin (Intel)d9ea17f2020-07-10 18:40:05 -0700950 if (unlikely(tasm_compatible_mode))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 return check_tasm_directive(line);
H. Peter Anvin (Intel)d9ea17f2020-07-10 18:40:05 -0700952 else
953 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000954}
955
956/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000957 * Free a linked list of tokens.
958 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000960{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400961 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000963}
964
965/*
966 * Free a linked list of lines.
967 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000969{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400970 Line *l, *tmp;
971 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 free_tlist(l->first);
973 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000974 }
975}
976
977/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700978 * Free an array of linked lists of tokens
979 */
980static void free_tlist_array(Token **array, size_t nlists)
981{
982 Token **listp = array;
983
984 while (nlists--)
985 free_tlist(*listp++);
986
987 nasm_free(array);
988}
989
990/*
991 * Duplicate a linked list of tokens.
992 */
993static Token *dup_tlist(const Token *list, Token ***tailp)
994{
995 Token *newlist = NULL;
996 Token **tailpp = &newlist;
997 const Token *t;
998
999 list_for_each(t, list) {
1000 Token *nt;
1001 *tailpp = nt = dup_Token(NULL, t);
1002 tailpp = &nt->next;
1003 }
1004
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001005 if (tailp) {
1006 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001007 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001008 }
1009
1010 return newlist;
1011}
1012
1013/*
1014 * Duplicate a linked list of tokens with a maximum count
1015 */
1016static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
1017{
1018 Token *newlist = NULL;
1019 Token **tailpp = &newlist;
1020 const Token *t;
1021
1022 list_for_each(t, list) {
1023 Token *nt;
1024 if (!cnt--)
1025 break;
1026 *tailpp = nt = dup_Token(NULL, t);
1027 tailpp = &nt->next;
1028 }
1029
1030 if (tailp) {
1031 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001032 if (newlist)
1033 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001034 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001035
1036 return newlist;
1037}
1038
1039/*
1040 * Duplicate a linked list of tokens in reverse order
1041 */
1042static Token *dup_tlist_reverse(const Token *list, Token *tail)
1043{
1044 const Token *t;
1045
1046 list_for_each(t, list)
1047 tail = dup_Token(tail, t);
1048
1049 return tail;
1050}
1051
1052/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001053 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001054 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001055static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001056{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001057 nasm_free(m->name);
1058 free_tlist(m->dlist);
1059 nasm_free(m->defaults);
1060 free_llist(m->expansion);
1061 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001062}
1063
1064/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001065 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001066 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001067static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001068{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001069 if (s->params) {
1070 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001071 for (i = 0; i < s->nparam; i++) {
1072 if (s->params[i].name.len > INLINE_TEXT)
1073 nasm_free(s->params[i].name.text.p.ptr);
1074 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001075 nasm_free(s->params);
1076 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001077 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001078 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001079}
1080
1081static void clear_smacro(SMacro *s)
1082{
1083 free_smacro_members(s);
1084 /* Wipe everything except the next pointer */
1085 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1086}
1087
1088/*
1089 * Free an SMacro
1090 */
1091static void free_smacro(SMacro *s)
1092{
1093 free_smacro_members(s);
1094 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001095}
1096
1097/*
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001098 * Free all currently defined macros, and free the hash tables if empty
H. Peter Anvin97a23472007-09-16 17:57:25 -07001099 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001100enum clear_what {
1101 CLEAR_NONE = 0,
1102 CLEAR_DEFINE = 1, /* Clear smacros */
1103 CLEAR_DEFALIAS = 2, /* Clear smacro aliases */
1104 CLEAR_ALLDEFINE = CLEAR_DEFINE|CLEAR_DEFALIAS,
1105 CLEAR_MMACRO = 4,
1106 CLEAR_ALL = CLEAR_ALLDEFINE|CLEAR_MMACRO
1107};
1108
1109static void clear_smacro_table(struct hash_table *smt, enum clear_what what)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001110{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001111 struct hash_iterator it;
1112 const struct hash_node *np;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001113 bool empty = true;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001114
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001115 /*
1116 * Walk the hash table and clear out anything we don't want
1117 */
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001118 hash_for_each(smt, it, np) {
1119 SMacro *tmp;
1120 SMacro *s = np->data;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001121 SMacro **head = (SMacro **)&np->data;
1122
1123 list_for_each_safe(s, tmp, s) {
1124 if (what & ((enum clear_what)s->alias + 1)) {
1125 *head = s->next;
1126 free_smacro(s);
1127 } else {
1128 empty = false;
1129 }
1130 }
H. Peter Anvin97a23472007-09-16 17:57:25 -07001131 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07001132
1133 /*
1134 * Free the hash table and keys if and only if it is now empty.
1135 * Note: we cannot free keys even for an empty list above, as that
1136 * mucks up the hash algorithm.
1137 */
1138 if (empty)
1139 hash_free_all(smt, true);
1140}
1141
1142static void free_smacro_table(struct hash_table *smt)
1143{
1144 clear_smacro_table(smt, CLEAR_ALLDEFINE);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001145}
1146
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001147static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001148{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001149 struct hash_iterator it;
1150 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001151
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001152 hash_for_each(mmt, it, np) {
1153 MMacro *tmp;
1154 MMacro *m = np->data;
1155 nasm_free((void *)np->key);
1156 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001157 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001158 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001159 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001160}
1161
1162static void free_macros(void)
1163{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001164 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001165 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001166}
1167
1168/*
1169 * Initialize the hash tables
1170 */
1171static void init_macros(void)
1172{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001173}
1174
1175/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001176 * Pop the context stack.
1177 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001179{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001180 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001181
1182 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001183 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001184 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001185 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001186}
1187
H. Peter Anvin072771e2008-05-22 13:17:51 -07001188/*
1189 * Search for a key in the hash index; adding it if necessary
1190 * (in which case we initialize the data pointer to NULL.)
1191 */
1192static void **
1193hash_findi_add(struct hash_table *hash, const char *str)
1194{
1195 struct hash_insert hi;
1196 void **r;
1197 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001198 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001199
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001200 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001201 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001202 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001203
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001204 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1205 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001206 return hash_add(&hi, strx, NULL);
1207}
1208
1209/*
1210 * Like hash_findi, but returns the data element rather than a pointer
1211 * to it. Used only when not adding a new element, hence no third
1212 * argument.
1213 */
1214static void *
1215hash_findix(struct hash_table *hash, const char *str)
1216{
1217 void **p;
1218
1219 p = hash_findi(hash, str, NULL);
1220 return p ? *p : NULL;
1221}
1222
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001223/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001224 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001225 * if there no more left -- return NULL
1226 */
1227static char *line_from_stdmac(void)
1228{
1229 unsigned char c;
1230 const unsigned char *p = stdmacpos;
1231 char *line, *q;
1232 size_t len = 0;
1233
1234 if (!stdmacpos)
1235 return NULL;
1236
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001237 /*
1238 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1239 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1240 */
1241 while ((c = *p++) != 127) {
1242 uint8_t ndir = c - 128;
1243 if (ndir < 256-96)
1244 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001245 else
1246 len++;
1247 }
1248
1249 line = nasm_malloc(len + 1);
1250 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001251
1252 while ((c = *stdmacpos++) != 127) {
1253 uint8_t ndir = c - 128;
1254 if (ndir < 256-96) {
1255 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1256 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001257 *q++ = ' ';
1258 } else {
1259 *q++ = c;
1260 }
1261 }
1262 stdmacpos = p;
1263 *q = '\0';
1264
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001265 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001266 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001267 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001268 if (*stdmacnext) {
1269 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001270 } else if (do_predef) {
1271 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001272
1273 /*
1274 * Nasty hack: here we push the contents of
1275 * `predef' on to the top-level expansion stack,
1276 * since this is the most convenient way to
1277 * implement the pre-include and pre-define
1278 * features.
1279 */
1280 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001281 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001282 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001283 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001284 l->finishes = NULL;
1285
1286 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001287 }
1288 do_predef = false;
1289 }
1290 }
1291
1292 return line;
1293}
1294
H. Peter Anvin6686de22019-08-10 05:33:14 -07001295/*
1296 * Read a line from a file. Return NULL on end of file.
1297 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001298static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001299{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001300 int c;
1301 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001302 const unsigned int delta = 512;
1303 const unsigned int pad = 8;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001304 bool cont = false;
1305 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001306
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001307 istk->where.lineno += istk->lineskip + istk->lineinc;
1308 src_set_linnum(istk->where.lineno);
1309 istk->lineskip = 0;
1310
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001311 size = delta;
1312 p = buffer = nasm_malloc(size);
1313
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001314 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001315 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001316
1317 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001318 case EOF:
1319 if (p == buffer) {
1320 nasm_free(buffer);
1321 return NULL;
1322 }
1323 c = 0;
1324 break;
1325
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001326 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001327 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001328 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001329 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001330 if (cont) {
1331 cont = false;
1332 continue;
1333 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001334 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001335 break;
1336
1337 case '\n':
1338 if (cont) {
1339 cont = false;
1340 continue;
1341 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001342 c = 0;
1343 break;
1344
1345 case 032: /* ^Z = legacy MS-DOS end of file mark */
1346 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001347 break;
1348
1349 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001350 next = fgetc(f);
1351 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001352 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001353 cont = true;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001354 istk->lineskip += istk->lineinc;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001355 continue;
1356 }
1357 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001359
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001360 if (p >= (buffer + size - pad)) {
1361 buffer = nasm_realloc(buffer, size + delta);
1362 p = buffer + size - pad;
1363 size += delta;
1364 }
1365
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001366 *p++ = c;
1367 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001368
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001369 return buffer;
1370}
1371
1372/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001373 * Common read routine regardless of source
1374 */
1375static char *read_line(void)
1376{
1377 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001378 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001379
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001380 if (f)
1381 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001382 else
1383 line = line_from_stdmac();
1384
1385 if (!line)
1386 return NULL;
1387
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07001388 if (!istk->nolist)
1389 lfmt->line(LIST_READ, istk->where.lineno, line);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001390
1391 return line;
1392}
1393
1394/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001395 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001396 * don't need to parse the value out of e.g. numeric tokens: we
1397 * simply split one string into many.
1398 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001399static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001400{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001401 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001402 Token *list = NULL;
1403 Token *t, **tail = &list;
1404
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001406 const char *p = line;
1407 const char *ep = NULL; /* End of token, for trimming the end */
1408 size_t toklen;
1409 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001410
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001412 /*
1413 * Preprocessor construct; find the end of the token.
1414 * Classification is handled later, because %{...} can be
1415 * used to create any preprocessor token.
1416 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001418 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001419 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001420 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001421 } else if (nasm_isdigit(*p) ||
1422 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 do {
1424 p++;
1425 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001426 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001427 } else if (*p == '{' || *p == '[') {
1428 /* %{...} or %[...] */
1429 char firstchar = *p;
1430 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001431 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001432 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1433 while (lvl) {
1434 if (*p == firstchar) {
1435 lvl++;
1436 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001437 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001438 } else if (nasm_isquote(*p)) {
1439 p = nasm_skip_string(p);
1440 }
1441
1442 /*
1443 * *p can have been advanced to a null character by
1444 * nasm_skip_string()
1445 */
1446 if (!*p) {
1447 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1448 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001449 break;
1450 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001451 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001452 }
1453 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001454 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001455 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001456 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001457 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001458 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001459 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001460 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001461 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001462 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001463 do {
1464 p++;
1465 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001466 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001467 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001468 p = nasm_skip_string(p);
1469 if (*p)
1470 p++;
1471 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001472 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001473 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001474 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001475 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001476 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001477 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001478 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001479 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001480 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1481 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 do {
1483 p++;
1484 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001485 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001486 } else if (*p == '%') {
1487 /* %% operator */
1488 p++;
1489 }
1490
1491 if (!ep)
1492 ep = p;
1493 toklen = ep - line;
1494
1495 /* Classify here, to handle %{...} correctly */
1496 if (toklen < 2) {
1497 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001499 char c0 = line[1];
1500
1501 switch (c0) {
1502 case '+':
1503 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1504 break;
1505
1506 case '-':
1507 type = TOK_MMACRO_PARAM;
1508 break;
1509
1510 case '?':
1511 if (toklen == 2)
1512 type = TOK_PREPROC_Q;
1513 else if (toklen == 3 && line[2] == '?')
1514 type = TOK_PREPROC_QQ;
1515 else
1516 type = TOK_PREPROC_ID;
1517 break;
1518
1519 case '!':
1520 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1521 break;
1522
1523 case '%':
1524 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1525 break;
1526
1527 case '$':
1528 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1529 break;
1530
1531 case '[':
1532 line += 2; /* Skip %[ */
1533 firstchar = *line; /* Don't clobber */
1534 toklen -= 2;
1535 type = TOK_INDIRECT;
1536 break;
1537
1538 case ',':
1539 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1540 break;
1541
1542 case '\'':
1543 case '\"':
1544 case '`':
1545 /* %{'string'} */
1546 type = TOK_PREPROC_ID;
1547 break;
1548
1549 case ':':
1550 type = TOK_MMACRO_PARAM; /* %{:..} */
1551 break;
1552
1553 default:
1554 if (nasm_isdigit(c0))
1555 type = TOK_MMACRO_PARAM;
1556 else if (nasm_isidchar(c0) || toklen > 2)
1557 type = TOK_PREPROC_ID;
1558 else
1559 type = TOK_OTHER;
1560 break;
1561 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001563 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001564 /*
1565 * An identifier. This includes the ? operator, which is
1566 * treated as a keyword, not as a special character
1567 * operator
1568 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001570 while (nasm_isidchar(*++p))
1571 ;
1572 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 /*
1574 * A string token.
1575 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001577 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001578
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 if (*p) {
1580 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001581 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001582 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 /* Handling unterminated strings by UNV */
1584 /* type = -1; */
1585 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001586 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001587 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001588 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001589 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001590 bool is_hex = false;
1591 bool is_float = false;
1592 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001593 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001594
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001596 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001598
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001599 if (*p == '$') {
1600 p++;
1601 is_hex = true;
1602 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001603
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001604 for (;;) {
1605 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001606
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001607 if (!is_hex && (c == 'e' || c == 'E')) {
1608 has_e = true;
1609 if (*p == '+' || *p == '-') {
1610 /*
1611 * e can only be followed by +/- if it is either a
1612 * prefixed hex number or a floating-point number
1613 */
1614 p++;
1615 is_float = true;
1616 }
1617 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1618 is_hex = true;
1619 } else if (c == 'P' || c == 'p') {
1620 is_float = true;
1621 if (*p == '+' || *p == '-')
1622 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001623 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001624 ; /* just advance */
1625 else if (c == '.') {
1626 /*
1627 * we need to deal with consequences of the legacy
1628 * parser, like "1.nolist" being two tokens
1629 * (TOK_NUMBER, TOK_ID) here; at least give it
1630 * a shot for now. In the future, we probably need
1631 * a flex-based scanner with proper pattern matching
1632 * to do it as well as it can be done. Nothing in
1633 * the world is going to help the person who wants
1634 * 0x123.p16 interpreted as two tokens, though.
1635 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001636 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001637 while (*r == '_')
1638 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001639
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001640 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1641 (!is_hex && (*r == 'e' || *r == 'E')) ||
1642 (*r == 'p' || *r == 'P')) {
1643 p = r;
1644 is_float = true;
1645 } else
1646 break; /* Terminate the token */
1647 } else
1648 break;
1649 }
1650 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001651
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001652 if (p == line+1 && *line == '$') {
1653 type = TOK_OTHER; /* TOKEN_HERE */
1654 } else {
1655 if (has_e && !is_hex) {
1656 /* 1e13 is floating-point, but 1e13h is not */
1657 is_float = true;
1658 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001659
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001660 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1661 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001662 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001664 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 /*
1666 * Whitespace just before end-of-line is discarded by
1667 * pretending it's a comment; whitespace just before a
1668 * comment gets lumped into the comment.
1669 */
1670 if (!*p || *p == ';') {
1671 type = TOK_COMMENT;
1672 while (*p)
1673 p++;
1674 }
1675 } else if (*p == ';') {
1676 type = TOK_COMMENT;
1677 while (*p)
1678 p++;
1679 } else {
1680 /*
1681 * Anything else is an operator of some kind. We check
1682 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001683 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1684 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001685 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 */
1687 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001688 switch (*p++) {
1689 case '>':
1690 if (*p == '>') {
1691 p++;
1692 if (*p == '>')
1693 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001694 } else if (*p == '=') {
1695 p++;
1696 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001697 break;
1698
1699 case '<':
1700 if (*p == '<') {
1701 p++;
1702 if (*p == '<')
1703 p++;
1704 } else if (*p == '=') {
1705 p++;
1706 if (*p == '>')
1707 p++;
1708 } else if (*p == '>') {
1709 p++;
1710 }
1711 break;
1712
1713 case '!':
1714 if (*p == '=')
1715 p++;
1716 break;
1717
1718 case '/':
1719 case '=':
1720 case '&':
1721 case '|':
1722 case '^':
1723 /* These operators can be doubled but nothing else */
1724 if (*p == p[-1])
1725 p++;
1726 break;
1727
1728 default:
1729 break;
1730 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001732
H. Peter Anvin8571f062019-09-23 16:40:03 -07001733 if (type == TOK_WHITESPACE) {
1734 *tail = t = new_White(NULL);
1735 tail = &t->next;
1736 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001737 if (!ep)
1738 ep = p;
1739 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001740 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 tail = &t->next;
1742 }
1743 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001744 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001745 return list;
1746}
1747
H. Peter Anvin8571f062019-09-23 16:40:03 -07001748/*
1749 * Tokens are allocated in blocks to improve speed. Set the blocksize
1750 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1751 *
1752 * alloc_Token() returns a zero-initialized token structure.
1753 */
1754#define TOKEN_BLOCKSIZE 4096
1755
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001756#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001757
1758static Token *freeTokens = NULL;
1759static Token *tokenblocks = NULL;
1760
1761static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001762{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001763 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764
H. Peter Anvin8571f062019-09-23 16:40:03 -07001765 if (unlikely(!t)) {
1766 Token *block;
1767 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768
H. Peter Anvin8571f062019-09-23 16:40:03 -07001769 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001770
H. Peter Anvin8571f062019-09-23 16:40:03 -07001771 /*
1772 * The first entry in each array are a linked list of
1773 * block allocations and is not used for data.
1774 */
1775 block[0].next = tokenblocks;
1776 block[0].type = TOK_BLOCK;
1777 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001778
H. Peter Anvin8571f062019-09-23 16:40:03 -07001779 /*
1780 * Add the rest to the free list
1781 */
1782 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1783 block[i].next = &block[i+1];
1784
1785 freeTokens = &block[2];
1786
1787 /*
1788 * Return the topmost usable token
1789 */
1790 return &block[1];
1791 }
1792
1793 freeTokens = t->next;
1794 t->next = NULL;
1795 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001796}
1797
H. Peter Anvin8571f062019-09-23 16:40:03 -07001798static Token *delete_Token(Token *t)
1799{
1800 Token *next = t->next;
1801
1802 nasm_zero(*t);
1803 t->next = freeTokens;
1804 freeTokens = t;
1805
1806 return next;
1807}
1808
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001810{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001811 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001812
H. Peter Anvin8571f062019-09-23 16:40:03 -07001813 list_for_each_safe(block, blocktmp, tokenblocks)
1814 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001815
H. Peter Anvin8571f062019-09-23 16:40:03 -07001816 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001818
H. Peter Anvin8571f062019-09-23 16:40:03 -07001819#else
1820
1821static inline Token *alloc_Token(void)
1822{
1823 Token *t;
1824 nasm_new(*t);
1825 return t;
1826}
1827
1828static Token *delete_Token(Token *t)
1829{
1830 Token *next = t->next;
1831 nasm_free(t);
1832 return next;
1833}
1834
1835static inline void delete_Blocks(void)
1836{
1837 /* Nothing to do */
1838}
1839
1840#endif
1841
H. Peter Anvin734b1882002-04-30 21:01:08 +00001842/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001843 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001844 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001845 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001846static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001847 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001848{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001849 Token *t = alloc_Token();
1850 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001851
H. Peter Anvin734b1882002-04-30 21:01:08 +00001852 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001853 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001854 if (type == TOK_WHITESPACE) {
1855 t->len = 1;
1856 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001858 if (text && text[0] && !txtlen)
1859 txtlen = tok_strlen(text);
1860
1861 t->len = tok_check_len(txtlen);
1862
1863 if (text) {
1864 textp = (txtlen > INLINE_TEXT)
1865 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1866 memcpy(textp, text, txtlen);
1867 textp[txtlen] = '\0'; /* In case we needed malloc() */
1868 } else {
1869 /*
1870 * Allocate a buffer but do not fill it. The caller
1871 * can fill in text, but must not change the length.
1872 * The filled in text must be exactly txtlen once
1873 * the buffer is filled and before the token is added
1874 * to any line lists.
1875 */
1876 if (txtlen > INLINE_TEXT)
1877 t->text.p.ptr = nasm_zalloc(txtlen+1);
1878 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001879 }
1880 return t;
1881}
1882
H. Peter Anvin8571f062019-09-23 16:40:03 -07001883/*
1884 * Same as new_Token(), but text belongs to the new token and is
1885 * either taken over or freed. This function MUST be called
1886 * with valid txt and txtlen, unlike new_Token().
1887 */
1888static Token *new_Token_free(Token * next, enum pp_token_type type,
1889 char *text, size_t txtlen)
1890{
1891 Token *t = alloc_Token();
1892
1893 t->next = next;
1894 t->type = type;
1895 t->len = tok_check_len(txtlen);
1896
1897 if (txtlen <= INLINE_TEXT) {
1898 memcpy(t->text.a, text, txtlen);
1899 free(text);
1900 } else {
1901 t->text.p.ptr = text;
1902 }
1903
1904 return t;
1905}
1906
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001907static Token *dup_Token(Token *next, const Token *src)
1908{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001909 Token *t = alloc_Token();
1910
1911 memcpy(t, src, sizeof *src);
1912 t->next = next;
1913
1914 if (t->len > INLINE_TEXT) {
1915 t->text.p.ptr = nasm_malloc(t->len + 1);
1916 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1917 }
1918
1919 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001920}
1921
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001922static Token *new_White(Token *next)
1923{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001924 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001925
H. Peter Anvin8571f062019-09-23 16:40:03 -07001926 t->next = next;
1927 t->type = TOK_WHITESPACE;
1928 t->len = 1;
1929 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001930
H. Peter Anvin8571f062019-09-23 16:40:03 -07001931 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001932}
1933
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001934/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001935 * This *transfers* the content from one token to another, leaving the
1936 * next pointer of the latter intact. Unlike dup_Token(), the old
1937 * token is destroyed, except for its next pointer, and the text
1938 * pointer allocation, if any, is simply transferred.
1939 */
1940static Token *steal_Token(Token *dst, Token *src)
1941{
1942 /* Overwrite everything except the next pointers */
1943 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1944 sizeof(Token) - sizeof(Token *));
1945
1946 /* Clear the donor token */
1947 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1948
1949 return dst;
1950}
1951
1952/*
1953 * Convert a line of tokens back into text. This modifies the list
1954 * by expanding environment variables.
1955 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001956 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001957 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001958 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001959static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001960{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001961 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001962 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001963 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001964
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001965 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001966 switch (t->type) {
1967 case TOK_ENVIRON:
1968 {
1969 const char *v = pp_getenv(t, true);
1970 set_text(t, v, tok_strlen(v));
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07001971 t->type = TOK_NAKED_STRING;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001972 break;
1973 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001974
H. Peter Anvin8571f062019-09-23 16:40:03 -07001975 case TOK_LOCAL_MACRO:
1976 case TOK_LOCAL_SYMBOL:
1977 if (expand_locals) {
1978 const char *q;
1979 char *p;
1980 Context *ctx = get_ctx(tok_text(t), &q);
1981 if (ctx) {
1982 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1983 set_text_free(t, p, nasm_last_string_len());
1984 t->type = TOK_ID;
1985 }
1986 }
1987 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001988
H. Peter Anvin (Intel)fcd3cb82020-07-10 17:22:47 -07001989 case TOK_INDIRECT:
1990 /*
1991 * This won't happen in when emitting to the assembler,
1992 * but can happen when emitting output for some of the
1993 * list options. The token string doesn't actually include
1994 * the brackets in this case.
1995 */
1996 len += 3; /* %[] */
1997 break;
1998
H. Peter Anvin8571f062019-09-23 16:40:03 -07001999 default:
2000 break; /* No modifications */
2001 }
2002
2003 if (debug_level(2)) {
2004 unsigned int t_len = t->len;
2005 unsigned int s_len = tok_strlen(tok_text(t));
2006 if (t_len != s_len) {
2007 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
2008 tok_text(t), t->type, s_len, t_len);
2009 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002010 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07002012
H. Peter Anvin8571f062019-09-23 16:40:03 -07002013 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002014 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002015
H. Peter Anvin734b1882002-04-30 21:01:08 +00002016 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002017
H. Peter Anvin (Intel)fcd3cb82020-07-10 17:22:47 -07002018 list_for_each(t, tlist) {
2019 switch (t->type) {
2020 case TOK_INDIRECT:
2021 *p++ = '%';
2022 *p++ = '[';
2023 p = mempcpy(p, tok_text(t), t->len);
2024 *p++ = ']';
2025 break;
2026
2027 default:
2028 p = mempcpy(p, tok_text(t), t->len);
2029 }
2030 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002031 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04002032
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002033 return line;
2034}
2035
2036/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002037 * A scanner, suitable for use by the expression evaluator, which
2038 * operates on a line of Tokens. Expects a pointer to a pointer to
2039 * the first token in the line to be passed in as its private_data
2040 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002041 *
2042 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00002043 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002044struct ppscan {
2045 Token *tptr;
2046 int ntokens;
2047};
2048
H. Peter Anvine2c80182005-01-15 22:15:51 +00002049static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002050{
H. Peter Anvin8b262472019-02-26 14:00:54 -08002051 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002052 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002053 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002054
H. Peter Anvine2c80182005-01-15 22:15:51 +00002055 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08002056 if (pps->ntokens && (tline = pps->tptr)) {
2057 pps->ntokens--;
2058 pps->tptr = tline->next;
2059 } else {
2060 pps->tptr = NULL;
2061 pps->ntokens = 0;
2062 return tokval->t_type = TOKEN_EOS;
2063 }
2064 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002065
H. Peter Anvin8571f062019-09-23 16:40:03 -07002066 txt = tok_text(tline);
2067 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002068
H. Peter Anvin8571f062019-09-23 16:40:03 -07002069 if (txt[0] == '$') {
2070 if (!txt[1]) {
2071 return tokval->t_type = TOKEN_HERE;
2072 } else if (txt[1] == '$' && !txt[2]) {
2073 return tokval->t_type = TOKEN_BASE;
2074 } else if (tline->type == TOK_ID) {
2075 tokval->t_charptr++;
2076 return tokval->t_type = TOKEN_ID;
2077 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002078 }
2079
H. Peter Anvin8571f062019-09-23 16:40:03 -07002080 switch (tline->type) {
2081 default:
2082 if (tline->len == 1)
2083 return tokval->t_type = txt[0];
2084 /* fall through */
2085 case TOK_ID:
2086 return nasm_token_hash(txt, tokval);
2087
2088 case TOK_NUMBER:
2089 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002091 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 if (rn_error)
2093 return tokval->t_type = TOKEN_ERRNUM;
2094 else
2095 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002096 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002097
H. Peter Anvin8571f062019-09-23 16:40:03 -07002098 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002100
2101 case TOK_STRING:
2102 tokval->t_charptr = (char *)unquote_token(tline);
2103 tokval->t_inttwo = tline->len;
2104 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002105 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002106}
2107
2108/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002109 * 1. An expression (true if nonzero 0)
2110 * 2. The keywords true, on, yes for true
2111 * 3. The keywords false, off, no for false
2112 * 4. An empty line, for true
2113 *
2114 * On error, return defval (usually the previous value)
2115 */
2116static bool pp_get_boolean_option(Token *tline, bool defval)
2117{
2118 static const char * const noyes[] = {
2119 "no", "yes",
2120 "false", "true",
2121 "off", "on"
2122 };
2123 struct ppscan pps;
2124 struct tokenval tokval;
2125 expr *evalresult;
2126
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002127 tline = skip_white(tline);
2128 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002129 return true;
2130
2131 if (tline->type == TOK_ID) {
2132 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002133 const char *txt = tok_text(tline);
2134
H. Peter Anvind2354082019-08-27 16:38:48 -07002135 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002136 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002137 return i & 1;
2138 }
2139
2140 pps.tptr = NULL;
2141 pps.tptr = tline;
2142 pps.ntokens = -1;
2143 tokval.t_type = TOKEN_INVALID;
2144 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2145
2146 if (!evalresult)
2147 return true;
2148
2149 if (tokval.t_type)
2150 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2151 if (!is_really_simple(evalresult)) {
2152 nasm_nonfatal("boolean flag expression must be a constant");
2153 return defval;
2154 }
2155
2156 return reloc_value(evalresult) != 0;
2157}
2158
2159/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002160 * Compare a string to the name of an existing macro; this is a
2161 * simple wrapper which calls either strcmp or nasm_stricmp
2162 * depending on the value of the `casesense' parameter.
2163 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002164static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002165{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002166 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002167}
2168
2169/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002170 * Compare a string to the name of an existing macro; this is a
2171 * simple wrapper which calls either strcmp or nasm_stricmp
2172 * depending on the value of the `casesense' parameter.
2173 */
2174static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2175{
2176 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2177}
2178
2179/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002180 * Return the Context structure associated with a %$ token. Return
2181 * NULL, having _already_ reported an error condition, if the
2182 * context stack isn't deep enough for the supplied number of $
2183 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002184 *
2185 * If "namep" is non-NULL, set it to the pointer to the macro name
2186 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002187 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002188static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002189{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002190 Context *ctx;
2191 int i;
2192
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002193 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002195
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002196 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002197 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002198
H. Peter Anvine2c80182005-01-15 22:15:51 +00002199 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002200 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002201 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002202 }
2203
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002204 name += 2;
2205 ctx = cstk;
2206 i = 0;
2207 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002208 name++;
2209 i++;
2210 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002211 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002213 nasm_nonfatal("`%s': context stack is only"
2214 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002215 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002216 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002217
2218 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002219 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002220
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002221 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002222}
2223
2224/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002225 * Open an include file. This routine must always return a valid
2226 * file pointer if it returns - it's responsible for throwing an
2227 * ERR_FATAL and bombing out completely if not. It should also try
2228 * the include path one by one until it finds the file or reaches
2229 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002230 *
2231 * Note: for INC_PROBE the function returns NULL at all times;
2232 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002233 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002234enum incopen_mode {
2235 INC_NEEDED, /* File must exist */
2236 INC_OPTIONAL, /* Missing is OK */
2237 INC_PROBE /* Only an existence probe */
2238};
2239
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002240/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002241static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002242 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002243{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002244 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002245 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002246 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002247 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002248 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002249
H. Peter Anvine2c80182005-01-15 22:15:51 +00002250 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002251 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002252 if (omode == INC_PROBE) {
2253 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002254 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002255 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002256 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002257 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002258 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002259 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002260 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002261 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002262 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002263
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002264 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002265
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002266 if (!ip) {
2267 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002268 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002269 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002270
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002271 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002272 ip = ip->next;
2273 }
2274}
2275
2276/*
2277 * Open a file, or test for the presence of one (depending on omode),
2278 * considering the include path.
2279 */
2280static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002281 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002282 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002283 enum incopen_mode omode,
2284 enum file_flags fmode)
2285{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002286 struct hash_insert hi;
2287 void **hp;
2288 char *path;
2289 FILE *fp = NULL;
2290
2291 hp = hash_find(&FileHash, file, &hi);
2292 if (hp) {
2293 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002294 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002295 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002296 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002297 } else {
2298 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002299 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002300
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002301 /* Positive or negative result */
2302 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002303
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002304 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002305 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002306 */
2307 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002308 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002309 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002310
H. Peter Anvin (Intel)5d68f982020-06-01 12:32:35 -07002311 if (path && !fp && omode != INC_PROBE)
2312 fp = nasm_open_read(path, fmode);
2313
2314 if (omode == INC_NEEDED && !fp) {
2315 if (!path)
2316 errno = ENOENT;
2317
2318 nasm_nonfatal("unable to open include file `%s': %s",
2319 file, strerror(errno));
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002320 }
2321
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002322 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002323 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002324
2325 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002326}
2327
2328/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002329 * Opens an include or input file. Public version, for use by modules
2330 * that get a file:lineno pair and need to look at the file again
2331 * (e.g. the CodeView debug backend). Returns NULL on failure.
2332 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002333FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002334{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002335 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002336}
2337
2338/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002339 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002340 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002341 * return true if _any_ single-line macro of that name is defined.
2342 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002343 * `nparam' or no parameters is defined.
2344 *
2345 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002346 * defined, or nparam is -1, the address of the definition structure
2347 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002348 * is NULL, no action will be taken regarding its contents, and no
2349 * error will occur.
2350 *
2351 * Note that this is also called with nparam zero to resolve
2352 * `ifdef'.
2353 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002354static bool
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002355smacro_defined(Context *ctx, const char *name, int nparam, SMacro **defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002356 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002357{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002358 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002359 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002360
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002361 smtbl = ctx ? &ctx->localmac : &smacros;
H. Peter Anvind2354082019-08-27 16:38:48 -07002362
2363restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002364 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002365
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 while (m) {
2367 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002368 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002369 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002370 if (m->alias && !find_alias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002371 if (!ppopt.noaliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002372 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002373 goto restart;
2374 } else {
2375 continue;
2376 }
2377 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002379 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002381 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 }
2383 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002384 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002385
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002386 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002387}
2388
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002389/* param should be a natural number [0; INT_MAX] */
2390static int read_param_count(const char *str)
2391{
2392 int result;
2393 bool err;
2394
2395 result = readnum(str, &err);
2396 if (result < 0 || result > INT_MAX) {
2397 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002398 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2399 str, 0, INT_MAX);
2400 } else if (err)
2401 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002402 return result;
2403}
2404
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002405/*
2406 * Count and mark off the parameters in a multi-line macro call.
2407 * This is called both from within the multi-line macro expansion
2408 * code, and also to mark off the default parameters when provided
2409 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002410 *
2411 * Note that we need space in the params array for parameter 0 being
2412 * a possible captured label as well as the final NULL.
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002413 *
2414 * Returns a pointer to the pointer to a terminal comma if present;
2415 * used to drop an empty terminal argument for legacy reasons.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002416 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002417static Token **count_mmac_params(Token *tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002418{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002419 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002420 int nparam = 0;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002421 Token *t;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002422 Token **comma = NULL, **maybe_comma = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002423 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002424
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002425 paramsize = PARAM_DELTA;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002426 nasm_newn(params, paramsize);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002427
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002428 t = skip_white(tline);
2429 if (t) {
2430 while (true) {
2431 /* Need two slots for captured label and NULL */
2432 if (unlikely(nparam+2 >= paramsize)) {
2433 paramsize += PARAM_DELTA;
2434 params = nasm_realloc(params, sizeof(*params) * paramsize);
2435 }
2436 params[++nparam] = t;
2437 if (tok_is(t, '{')) {
2438 int brace = 1;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002439
2440 comma = NULL; /* Non-empty parameter */
2441
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002442 while (brace && (t = t->next)) {
2443 brace += tok_is(t, '{');
2444 brace -= tok_is(t, '}');
2445 }
H. Peter Anvin (Intel)f8639bd2020-06-04 16:29:53 -07002446
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002447 if (t) {
2448 /*
2449 * Now we've found the closing brace, look further
2450 * for the comma.
2451 */
2452 t = skip_white(t->next);
2453 if (tok_isnt(t, ','))
2454 nasm_nonfatal("braces do not enclose all of macro parameter");
2455 } else {
2456 nasm_nonfatal("expecting closing brace in macro parameter");
2457 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002458 }
2459
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002460 /* Advance to the next comma */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002461 maybe_comma = &t->next;
2462 while (tok_isnt(t, ',')) {
2463 if (!tok_white(t))
2464 comma = NULL; /* Non-empty parameter */
2465 maybe_comma = &t->next;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002466 t = t->next;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002467 }
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002468
2469 if (!t)
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002470 break; /* End of string, no comma */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002471
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002472 comma = maybe_comma; /* Point to comma pointer */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07002473 t = skip_white(t->next); /* Eat the comma and whitespace */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002474 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002475 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002476
2477 params[nparam+1] = NULL;
2478 *paramsp = params;
2479 *nparamp = nparam;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07002480
2481 return comma;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002482}
2483
2484/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002485 * Determine whether one of the various `if' conditions is true or
2486 * not.
2487 *
2488 * We must free the tline we get passed.
2489 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002490static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002491{
H. Peter Anvin70055962007-10-11 00:05:31 -07002492 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002493 Token *t, *tt, *origline;
2494 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002495 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002496 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002497 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002498 const char *dname = pp_directives[ct];
2499 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002500 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002501
2502 origline = tline;
2503
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002504 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002505 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002506 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002507 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002508 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002509 if (!tline)
2510 break;
2511 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002512 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002513 dname);
2514 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002515 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002516 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002517 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 tline = tline->next;
2519 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002520 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002521
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002522 case PP_IFDEF:
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002523 case PP_IFDEFALIAS:
2524 {
2525 bool alias = cond == PP_IFDEFALIAS;
2526 SMacro *smac;
2527 Context *ctx;
2528 const char *mname;
2529
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002530 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002531 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002532 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002534 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002535 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002536 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002539
2540 mname = tok_text(tline);
2541 ctx = get_ctx(mname, &mname);
H. Peter Anvin (Intel)b91e7732020-06-05 12:22:26 -07002542 if (smacro_defined(ctx, mname, -1, &smac, true, alias) && smac
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002543 && smac->alias == alias) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002544 j = true;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002545 break;
2546 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002548 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 break;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07002550 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002551
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002552 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002553 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002554 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002555 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002556 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002557 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002558 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002559 tline->type != TOK_INTERNAL_STRING &&
2560 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002561 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002562 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002563 goto fail;
2564 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002565
2566 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002567 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002568 }
2569 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002570
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002571 case PP_IFIDNI:
2572 casesense = false;
2573 /* fall through */
2574 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 tline = expand_smacro(tline);
2576 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002577 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 tt = tt->next;
2579 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002580 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002581 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 }
2584 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002585 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002586 while (tok_isnt(t, ',') && tt) {
2587 unsigned int l1, l2;
2588 const char *t1, *t2;
2589
2590 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002591 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002592 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 }
2595 if (t->type == TOK_WHITESPACE) {
2596 t = t->next;
2597 continue;
2598 }
2599 if (tt->type == TOK_WHITESPACE) {
2600 tt = tt->next;
2601 continue;
2602 }
2603 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002604 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 break;
2606 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002607
H. Peter Anvin8571f062019-09-23 16:40:03 -07002608 t1 = unquote_token(t);
2609 t2 = unquote_token(tt);
2610 l1 = t->len;
2611 l2 = tt->len;
2612
2613 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2614 j = false;
2615 break;
2616 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002617
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 t = t->next;
2619 tt = tt->next;
2620 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002621 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002622 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002624
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002625 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002626 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002628 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002629
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002630 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002632 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002633 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 goto fail;
2635 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002636 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002637 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002639 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 searching.nparam_max = INT_MAX;
2641 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002642 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002644 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002645 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002646 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002647 } else {
2648 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002649 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002651 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002653 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002655 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002656 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002657 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002658 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002659 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002660 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002661 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002662 searching.nparam_max = searching.nparam_min;
2663 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 }
2665 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002666 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 tline = tline->next;
2668 searching.plus = true;
2669 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002670 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2671 while (mmac) {
2672 if (!strcmp(mmac->name, searching.name) &&
2673 (mmac->nparam_min <= searching.nparam_max
2674 || searching.plus)
2675 && (searching.nparam_min <= mmac->nparam_max
2676 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 found = true;
2678 break;
2679 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002680 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 }
2682 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002683 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 nasm_free(searching.name);
2685 j = found;
2686 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002687 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002688
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002689 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 needtype = TOK_ID;
2691 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002692 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 needtype = TOK_NUMBER;
2694 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002695 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002696 needtype = TOK_STRING;
2697 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002698
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699iftype:
2700 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002701
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002702 while (tok_white(t) ||
2703 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002705
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002706 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002708
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002709 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002710 tline = expand_smacro(tline);
2711 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002712
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 j = false;
2714 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002715 t = skip_white(t->next); /* Skip the actual token + whitespace */
2716 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 }
2718 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002719
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002720 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002721 tline = expand_smacro(tline);
2722 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 j = !t; /* Should be empty */
2724 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002725
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002726 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002727 pps.tptr = tline = expand_smacro(tline);
2728 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002730 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 if (!evalresult)
2732 return -1;
2733 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002734 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002736 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002737 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002740 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002742
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002743 case PP_IFUSING:
2744 case PP_IFUSABLE:
2745 {
2746 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002747 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002748
H. Peter Anvin8571f062019-09-23 16:40:03 -07002749 pkg = get_use_pkg(tline, dname, &name);
2750 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002751 goto fail;
2752
2753 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2754 break;
2755 }
2756
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002758 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002760 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002761
2762 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002763 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002764
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002765fail:
2766 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002767 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002768}
2769
2770/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002771 * Default smacro expansion routine: just returns a copy of the
2772 * expansion list.
2773 */
2774static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002775smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002776{
2777 (void)params;
2778 (void)nparams;
2779
2780 return dup_tlist(s->expansion, NULL);
2781}
2782
2783/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002784 * Emit a macro defintion or undef to the listing file, if
2785 * desired. This is similar to detoken(), but it handles the reverse
2786 * expansion list, does not expand %! or local variable tokens, and
2787 * does some special handling for macro parameters.
2788 */
2789static void
2790list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2791{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002792 Token *t;
2793 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002794 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002795 char *context_prefix = NULL;
2796 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002797
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002798 namelen = strlen(m->name);
2799 size = namelen + 2; /* Include room for space after name + NUL */
2800
2801 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002802 int context_depth = cstk->depth - ctx->depth + 1;
2803 context_prefix =
2804 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2805 ctx->name ? ctx->name : "",
2806 ctx->number, context_depth, "");
2807
2808 context_len = nasm_last_string_len();
2809 memset(context_prefix + context_len - context_depth,
2810 '$', context_depth);
2811 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002812 }
2813
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002814 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002815 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002816
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002817 if (m->nparam) {
2818 /*
2819 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002820 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002821 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002822 int i;
2823
2824 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002825 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002826 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002827 }
2828
2829 def = nasm_malloc(size);
2830 p = def+size;
2831 *--p = '\0';
2832
2833 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002834 p -= t->len;
2835 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002836 }
2837
2838 *--p = ' ';
2839
2840 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002841 int i;
2842
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002843 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002844 for (i = m->nparam-1; i >= 0; i--) {
2845 enum sparmflags flags = m->params[i].flags;
2846 if (flags & SPARM_GREEDY)
2847 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002848 p -= m->params[i].name.len;
2849 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2850
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002851 if (flags & SPARM_NOSTRIP)
2852 *--p = '!';
2853 if (flags & SPARM_STR)
2854 *--p = '&';
2855 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002856 *--p = '=';
2857 *--p = ',';
2858 }
2859 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002860 }
2861
2862 p -= namelen;
2863 memcpy(p, m->name, namelen);
2864
H. Peter Anvin6686de22019-08-10 05:33:14 -07002865 if (context_prefix) {
2866 p -= context_len;
2867 memcpy(p, context_prefix, context_len);
2868 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002869 }
2870
2871 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002872 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002873}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002874
2875/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002876 * Parse smacro arguments, return argument count. If the tmpl argument
2877 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002878 * *tpp is updated to point to the pointer to the first token after the
2879 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002880 *
2881 * The text values from any argument tokens are "stolen" and the
2882 * corresponding text fields set to NULL.
2883 */
2884static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2885{
2886 int nparam = 0;
2887 enum sparmflags flags;
2888 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002889 bool err, done;
2890 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002891 Token **tn = *tpp;
2892 Token *t = *tn;
2893 Token *name;
2894
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002895 /*
2896 * DO NOT skip whitespace here, or we won't be able to distinguish:
2897 *
2898 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2899 * %define bar(a,b) ; two arguments, empty expansion
2900 *
2901 * This ambiguity was inherited from C.
2902 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002903
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002904 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002905 goto finish;
2906
2907 if (tmpl) {
2908 Token *tx = t;
2909 Token **txpp = &tx;
2910 int sparam;
2911
2912 /* Count parameters first */
2913 sparam = parse_smacro_template(&txpp, NULL);
2914 if (!sparam)
2915 goto finish; /* No parameters, we're done */
2916 nasm_newn(params, sparam);
2917 }
2918
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002919 /* Skip leading paren */
2920 tn = &t->next;
2921 t = *tn;
2922
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002923 name = NULL;
2924 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002925 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002926
2927 while (!done) {
2928 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002929 if (name || flags)
2930 nasm_nonfatal("`)' expected to terminate macro template");
2931 else
2932 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002933 break;
2934 }
2935
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002936 switch (t->type) {
2937 case TOK_ID:
2938 if (name)
2939 goto bad;
2940 name = t;
2941 break;
2942
2943 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002944 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002945 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002946 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002947 case '=':
2948 flags |= SPARM_EVAL;
2949 break;
2950 case '&':
2951 flags |= SPARM_STR;
2952 break;
2953 case '!':
2954 flags |= SPARM_NOSTRIP;
2955 break;
2956 case '+':
2957 flags |= SPARM_GREEDY;
2958 greedy = true;
2959 break;
2960 case ',':
2961 if (greedy)
2962 nasm_nonfatal("greedy parameter must be last");
2963 /* fall through */
2964 case ')':
2965 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002966 if (name)
2967 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002968 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002969 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002970 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002971 name = NULL;
2972 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002973 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002974 break;
2975 default:
2976 goto bad;
2977 }
2978 break;
2979
2980 case TOK_WHITESPACE:
2981 break;
2982
2983 default:
2984 bad:
2985 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002986 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002987 err = true;
2988 }
2989 break;
2990 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002991
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002992 tn = &t->next;
2993 t = *tn;
2994 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002995
2996finish:
2997 while (t && t->type == TOK_WHITESPACE) {
2998 tn = &t->next;
2999 t = t->next;
3000 }
3001 *tpp = tn;
3002 if (tmpl) {
3003 tmpl->nparam = nparam;
3004 tmpl->greedy = greedy;
3005 tmpl->params = params;
3006 }
3007 return nparam;
3008}
3009
3010/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003011 * Common code for defining an smacro. The tmpl argument, if not NULL,
3012 * contains any macro parameters that aren't explicit arguments;
3013 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003014 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003015static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003016 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003017{
3018 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003019 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003020 Context *ctx;
3021 bool defining_alias = false;
3022 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07003023
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003024 if (tmpl) {
3025 defining_alias = tmpl->alias;
3026 nparam = tmpl->nparam;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003027 if (nparam && !defining_alias)
3028 mark_smac_params(expansion, tmpl, 0);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003029 }
3030
3031 while (1) {
3032 ctx = get_ctx(mname, &mname);
3033
H. Peter Anvind2354082019-08-27 16:38:48 -07003034 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003035 /* Create a new macro */
3036 smtbl = ctx ? &ctx->localmac : &smacros;
3037 smhead = (SMacro **) hash_findi_add(smtbl, mname);
3038 nasm_new(smac);
3039 smac->next = *smhead;
3040 *smhead = smac;
3041 break;
3042 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003043 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003044 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003045 /*
3046 * Some instances of the old code considered this a failure,
3047 * some others didn't. What is the right thing to do here?
3048 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003049 goto fail;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003050 } else if (!smac->alias || ppopt.noaliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003051 /*
3052 * We're redefining, so we have to take over an
3053 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08003054 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003055 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003056 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003057 break;
3058 } else if (smac->in_progress) {
3059 nasm_nonfatal("macro alias loop");
3060 goto fail;
3061 } else {
3062 /* It is an alias macro; follow the alias link */
3063 SMacro *s;
3064
3065 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003066 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003067 expansion, tmpl);
3068 smac->in_progress = false;
3069 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003070 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003071 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003072
3073 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003074 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003075 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003076 smac->expand = smacro_expand_default;
3077 if (tmpl) {
3078 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003079 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003080 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003081 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003082 if (tmpl->expand)
3083 smac->expand = tmpl->expand;
3084 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003085 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07003086 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
3087 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003088 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003089 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003090
3091fail:
3092 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003093 if (tmpl)
3094 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003095 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003096}
3097
3098/*
3099 * Undefine an smacro
3100 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003101static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003102{
3103 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003104 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003105 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003106
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003107 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003108 smtbl = ctx ? &ctx->localmac : &smacros;
3109 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003110
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003111 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003112 /*
3113 * We now have a macro name... go hunt for it.
3114 */
3115 sp = smhead;
3116 while ((s = *sp) != NULL) {
3117 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003118 if (s->alias && !undefalias) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003119 if (!ppopt.noaliases) {
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003120 if (s->in_progress) {
3121 nasm_nonfatal("macro alias loop");
3122 } else {
3123 s->in_progress = true;
3124 undef_smacro(tok_text(s->expansion), false);
3125 s->in_progress = false;
3126 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003127 }
3128 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003129 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003130 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3131 ctx, s);
3132 *sp = s->next;
3133 free_smacro(s);
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003134 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003135 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003136 }
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003137 sp = &s->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003139 }
3140}
3141
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003142/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003143 * Parse a mmacro specification.
3144 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003145static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003146{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003147 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003148 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003149 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003150 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003151 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003152 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003153 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003154
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003155#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003156 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003157#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003158 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003159 def->plus = false;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003160 def->nolist = 0;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003161 def->nparam_min = 0;
3162 def->nparam_max = 0;
3163
H. Peter Anvina26433d2008-07-16 14:40:01 -07003164 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003165 tline = skip_white(tline);
3166 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003167 nasm_nonfatal("`%s' expects a parameter count", directive);
3168 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003169 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003170 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003172 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003173 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003174 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003175 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003177 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003179 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003180 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 }
3182 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003183 }
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;
3186 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003187 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003188 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003189 tline->next->len == 7 &&
3190 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003191 tline = tline->next;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003192 if (!list_option('f'))
3193 def->nolist |= NL_LIST|NL_LINE;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003194 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003195
H. Peter Anvina26433d2008-07-16 14:40:01 -07003196 /*
3197 * Handle default parameters.
3198 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003199 def->ndefs = 0;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003200 if (tline && tline->next) {
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003201 Token **comma;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003202 def->dlist = tline->next;
3203 tline->next = NULL;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07003204 comma = count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
3205 if (!ppopt.sane_empty_expansion && comma) {
3206 *comma = NULL;
3207 def->ndefs--;
3208 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
3209 "dropping trailing empty default parameter in defintion of multi-line macro `%s'",
3210 def->name);
3211 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003212 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003213 def->dlist = NULL;
3214 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003215 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003216 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003217
H. Peter Anvin89cee572009-07-15 09:16:54 -04003218 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003219 !def->plus) {
3220 /*
3221 *!macro-defaults [on] macros with more default than optional parameters
3222 *! warns when a macro has more default parameters than optional parameters.
3223 *! See \k{mlmacdef} for why might want to disable this warning.
3224 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003225 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003226 "too many default macro parameters in macro `%s'", def->name);
3227 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003228
H. Peter Anvina26433d2008-07-16 14:40:01 -07003229 return true;
3230}
3231
3232
3233/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003234 * Decode a size directive
3235 */
3236static int parse_size(const char *str) {
3237 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003238 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003239 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003240 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003241 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003242}
3243
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003244/*
3245 * Process a preprocessor %pragma directive. Currently there are none.
3246 * Gets passed the token list starting with the "preproc" token from
3247 * "%pragma preproc".
3248 */
3249static void do_pragma_preproc(Token *tline)
3250{
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003251 const char *txt;
3252
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003253 /* Skip to the real stuff */
3254 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003255 tline = skip_white(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003256
3257 if (!tok_type(tline, TOK_ID))
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003258 return;
3259
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003260 txt = tok_text(tline);
3261 if (!nasm_stricmp(txt, "sane_empty_expansion")) {
3262 tline = skip_white(tline->next);
3263 ppopt.sane_empty_expansion =
3264 pp_get_boolean_option(tline, ppopt.sane_empty_expansion);
3265 } else {
3266 /* Unknown pragma, ignore for now */
3267 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003268}
3269
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003270static bool is_macro_id(const Token *t)
3271{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003272 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003273}
3274
H. Peter Anvin8571f062019-09-23 16:40:03 -07003275static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003276{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003277 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003278 Token *t = *tp;
3279
3280 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003281 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003282 t = expand_id(t);
3283
3284 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003285 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003286 return NULL;
3287 }
3288
H. Peter Anvin8571f062019-09-23 16:40:03 -07003289 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003290 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003291 *tp = t;
3292 return id;
3293}
3294
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003295/* Parse a %use package name and find the package. Set *err on syntax error. */
3296static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003297get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003298{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003299 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003300
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003301 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003302 t = expand_smacro(t);
3303
H. Peter Anvin8571f062019-09-23 16:40:03 -07003304 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003305
H. Peter Anvin8571f062019-09-23 16:40:03 -07003306 if (!t) {
3307 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3308 return NULL;
3309 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3310 nasm_nonfatal("`%s' expects a package name, got `%s'",
3311 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003312 return NULL;
3313 }
3314
H. Peter Anvin8571f062019-09-23 16:40:03 -07003315 *name = id = unquote_token(t);
3316
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003317 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003318 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003319 if (t)
3320 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3321
3322 return nasm_find_use_package(id);
3323}
3324
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003325/*
3326 * Mark parameter tokens in an smacro definition. If the type argument
3327 * is 0, create smac param tokens, otherwise use the type specified;
3328 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3329 * parameter tokens during expansion during %xdefine.
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07003330 *
3331 * tmpl may not be NULL here.
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003332 */
3333static void mark_smac_params(Token *tline, const SMacro *tmpl,
3334 enum pp_token_type type)
3335{
3336 const struct smac_param *params = tmpl->params;
3337 int nparam = tmpl->nparam;
3338 Token *t;
3339 int i;
3340
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003341 list_for_each(t, tline) {
3342 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3343 continue;
3344
3345 for (i = 0; i < nparam; i++) {
3346 if (tok_text_match(t, &params[i].name))
3347 t->type = type ? type : tok_smac_param(i);
3348 }
3349 }
3350}
3351
Ed Beroset3ab3f412002-06-11 03:31:49 +00003352/**
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003353 * %clear selected macro sets either globally or in contexts
3354 */
3355static void do_clear(enum clear_what what, bool context)
3356{
3357 if (context) {
3358 if (what & CLEAR_ALLDEFINE) {
3359 Context *ctx;
3360 list_for_each(ctx, cstk)
3361 clear_smacro_table(&ctx->localmac, what);
3362 }
3363 /* Nothing else can be context-local */
3364 } else {
3365 if (what & CLEAR_ALLDEFINE)
3366 clear_smacro_table(&smacros, what);
3367 if (what & CLEAR_MMACRO)
3368 free_mmacro_table(&mmacros);
3369 }
3370}
3371
3372/**
Ed Beroset3ab3f412002-06-11 03:31:49 +00003373 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003374 * Find out if a line contains a preprocessor directive, and deal
3375 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003376 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003377 * If a directive _is_ found, it is the responsibility of this routine
3378 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003379 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003380 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003381 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003382 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003383 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003384 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003385static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003386{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003387 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003388 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003389 bool err;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003390 enum nolist_flags nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003391 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003392 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003393 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003394 const char *p;
3395 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003396 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003397 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003398 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003399 Include *inc;
3400 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003401 Cond *cond;
3402 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003403 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003404 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003405 struct tokenval tokval;
3406 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003407 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003408 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003409 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003410 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003411
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003412 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003413 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003414
H. Peter Anvin (Intel)d9ea17f2020-07-10 18:40:05 -07003415 if (tok_is(tline, '#')) {
3416 /* cpp-style line directive */
3417 if (!tok_white(tline->next))
3418 return NO_DIRECTIVE_FOUND;
3419 dname = tok_text(tline);
3420 goto pp_line;
3421 }
3422
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003423 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003424 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3425 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003426
H. Peter Anvin8571f062019-09-23 16:40:03 -07003427 dname = tok_text(tline);
3428 if (dname[1] == '%')
3429 return NO_DIRECTIVE_FOUND;
3430
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003431 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003432
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003433 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003434 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003435 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003436 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003437 }
3438
3439 /*
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003440 * %line directives are always processed immediately and
3441 * unconditionally, as they are intended to reflect position
3442 * in externally preprocessed sources.
3443 */
3444 if (op == PP_LINE) {
H. Peter Anvin (Intel)d9ea17f2020-07-10 18:40:05 -07003445 pp_line:
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003446 /*
3447 * Syntax is `%line nnn[+mmm] [filename]'
3448 */
3449 if (pp_noline || istk->mstk.mstk)
3450 goto done;
3451
3452 tline = tline->next;
3453 tline = skip_white(tline);
3454 if (!tok_type(tline, TOK_NUMBER)) {
3455 nasm_nonfatal("`%s' expects line number", dname);
3456 goto done;
3457 }
3458 k = readnum(tok_text(tline), &err);
3459 m = 1;
3460 tline = tline->next;
3461 if (tok_is(tline, '+') || tok_is(tline, '-')) {
3462 bool minus = tok_is(tline, '-');
3463 tline = tline->next;
3464 if (!tok_type(tline, TOK_NUMBER)) {
3465 nasm_nonfatal("`%s' expects line increment", dname);
3466 goto done;
3467 }
3468 m = readnum(tok_text(tline), &err);
3469 if (minus)
3470 m = -m;
3471 tline = tline->next;
3472 }
3473 tline = skip_white(tline);
3474 if (tline) {
3475 if (tline->type == TOK_STRING) {
H. Peter Anvin (Intel)d9ea17f2020-07-10 18:40:05 -07003476 if (dname[0] == '#') {
3477 /* cpp version: treat double quotes like NASM backquotes */
3478 char *txt = tok_text_buf(tline);
3479 if (txt[0] == '"') {
3480 txt[0] = '`';
3481 txt[tline->len - 1] = '`';
3482 }
3483 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003484 src_set_fname(unquote_token(tline));
H. Peter Anvin (Intel)d9ea17f2020-07-10 18:40:05 -07003485 /*
3486 * Anything after the string is ignored by design (for cpp
3487 * compatibility and future extensions.)
3488 */
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003489 } else {
3490 char *fname = detoken(tline, false);
3491 src_set_fname(fname);
3492 nasm_free(fname);
3493 }
3494 }
3495 src_set_linnum(k);
3496
3497 istk->where = src_where();
3498 istk->lineinc = m;
3499 goto done;
3500 }
3501
3502 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003503 * If we're in a non-emitting branch of a condition construct,
3504 * or walking to the end of an already terminated %rep block,
3505 * we should ignore all directives except for condition
3506 * directives.
3507 */
3508 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003509 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003510 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003511 return NO_DIRECTIVE_FOUND;
3512 }
3513
3514 /*
3515 * If we're defining a macro or reading a %rep block, we should
3516 * ignore all directives except for %macro/%imacro (which nest),
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003517 * %endm/%endmacro, %line and (only if we're in a %rep block) %endrep.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003518 * If we're in a %rep block, another %rep nests, so should be let through.
3519 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003520 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3521 op != PP_ENDMACRO && op != PP_ENDM &&
3522 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003523 return NO_DIRECTIVE_FOUND;
3524 }
3525
3526 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003527 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003528 nested_mac_count++;
3529 return NO_DIRECTIVE_FOUND;
3530 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003531 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003532 nested_mac_count--;
3533 return NO_DIRECTIVE_FOUND;
3534 }
3535 }
3536 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003537 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003538 nested_rep_count++;
3539 return NO_DIRECTIVE_FOUND;
3540 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003541 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003542 nested_rep_count--;
3543 return NO_DIRECTIVE_FOUND;
3544 }
3545 }
3546 }
3547 }
3548
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003549 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003550 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003551 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003553
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003554 case PP_PRAGMA:
3555 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003556 * %pragma namespace options...
3557 *
3558 * The namespace "preproc" is reserved for the preprocessor;
3559 * all other namespaces generate a [pragma] assembly directive.
3560 *
3561 * Invalid %pragmas are ignored and may have different
3562 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003563 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003564 t = tline;
3565 tline = tline->next;
3566 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003567 tline = zap_white(expand_smacro(tline));
3568 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003569 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003570 /* Preprocessor pragma */
3571 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003572 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003573 } else {
3574 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003575
3576 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003577 for (t = tline; t->next; t = t->next)
3578 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003579 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003580
3581 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003582 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003583 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003584 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003585 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003586 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003587 }
3588 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003589 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003590
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 case PP_STACKSIZE:
3592 /* Directive to tell NASM what the default stack size is. The
3593 * default is for a 16-bit stack, and this can be overriden with
3594 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003596 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003598 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003600 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 /* All subsequent ARG directives are for a 32-bit stack */
3602 StackSize = 4;
3603 StackPointer = "ebp";
3604 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003605 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003606 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003607 /* All subsequent ARG directives are for a 64-bit stack */
3608 StackSize = 8;
3609 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003610 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003611 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003612 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 /* All subsequent ARG directives are for a 16-bit stack,
3614 * far function call.
3615 */
3616 StackSize = 2;
3617 StackPointer = "bp";
3618 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003619 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003620 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 /* All subsequent ARG directives are for a 16-bit stack,
3622 * far function call. We don't support near functions.
3623 */
3624 StackSize = 2;
3625 StackPointer = "bp";
3626 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003627 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003629 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003631 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003632
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 case PP_ARG:
3634 /* TASM like ARG directive to define arguments to functions, in
3635 * the following form:
3636 *
3637 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3638 */
3639 offset = ArgOffset;
3640 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003641 const char *arg;
3642 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003644
H. Peter Anvine2c80182005-01-15 22:15:51 +00003645 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003646 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003648 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003649 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003651 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003652
H. Peter Anvine2c80182005-01-15 22:15:51 +00003653 /* Find the argument size type */
3654 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003655 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003656 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003657 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 }
3659 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003660 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003661 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003662 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003664
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003666 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003667 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003668 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003669 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003670 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003672 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003673 }
3674 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003675
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 /* Round up to even stack slots */
3677 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003678
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 /* Now define the macro for the argument */
3680 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3681 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003682 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003684
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003686 tline = skip_white(tline->next);
3687 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003688 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003689 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003690
H. Peter Anvine2c80182005-01-15 22:15:51 +00003691 case PP_LOCAL:
3692 /* TASM like LOCAL directive to define local variables for a
3693 * function, in the following form:
3694 *
3695 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3696 *
3697 * The '= LocalSize' at the end is ignored by NASM, but is
3698 * required by TASM to define the local parameter size (and used
3699 * by the TASM macro package).
3700 */
3701 offset = LocalOffset;
3702 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003703 const char *local;
3704 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003705 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003706
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003708 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003710 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003711 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003713 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003714
H. Peter Anvine2c80182005-01-15 22:15:51 +00003715 /* Find the argument size type */
3716 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003717 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003718 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003719 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 }
3721 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003722 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003723 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003724 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003726
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003728 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003730 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003731 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003732 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003734 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003735 }
3736 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003737
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003738 /* Round up to even stack slots */
3739 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003740
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003741 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003742
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003743 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3745 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003746 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003747
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748 /* Now define the assign to setup the enter_c macro correctly */
3749 snprintf(directive, sizeof(directive),
3750 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003751 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003752
H. Peter Anvine2c80182005-01-15 22:15:51 +00003753 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003754 tline = skip_white(tline->next);
3755 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003756 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003757 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003758
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 case PP_CLEAR:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003760 {
3761 bool context = false;
3762
3763 t = tline->next = expand_smacro(tline->next);
3764 t = skip_white(t);
3765 if (!t) {
3766 /* Emulate legacy behavior */
3767 do_clear(CLEAR_DEFINE|CLEAR_MMACRO, false);
3768 } else {
3769 while ((t = skip_white(t)) && t->type == TOK_ID) {
3770 const char *txt = tok_text(t);
3771 if (!nasm_stricmp(txt, "all")) {
3772 do_clear(CLEAR_ALL, context);
3773 } else if (!nasm_stricmp(txt, "define") ||
3774 !nasm_stricmp(txt, "def") ||
3775 !nasm_stricmp(txt, "smacro")) {
3776 do_clear(CLEAR_DEFINE, context);
3777 } else if (!nasm_stricmp(txt, "defalias") ||
3778 !nasm_stricmp(txt, "alias") ||
3779 !nasm_stricmp(txt, "salias")) {
3780 do_clear(CLEAR_DEFALIAS, context);
3781 } else if (!nasm_stricmp(txt, "alldef") ||
3782 !nasm_stricmp(txt, "alldefine")) {
3783 do_clear(CLEAR_ALLDEFINE, context);
3784 } else if (!nasm_stricmp(txt, "macro") ||
3785 !nasm_stricmp(txt, "mmacro")) {
3786 do_clear(CLEAR_MMACRO, context);
3787 } else if (!nasm_stricmp(txt, "context") ||
3788 !nasm_stricmp(txt, "ctx")) {
3789 context = true;
3790 } else if (!nasm_stricmp(txt, "global")) {
3791 context = false;
3792 } else if (!nasm_stricmp(txt, "nothing") ||
3793 !nasm_stricmp(txt, "none") ||
3794 !nasm_stricmp(txt, "ignore") ||
3795 !nasm_stricmp(txt, "-") ||
3796 !nasm_stricmp(txt, "--")) {
3797 /* Do nothing */
3798 } else {
3799 nasm_nonfatal("invalid option to %s: %s", dname, txt);
3800 t = NULL;
3801 }
3802 }
3803 }
3804
3805 t = skip_white(t);
3806 if (t)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003807 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003808 break;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07003809 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003810
H. Peter Anvin418ca702008-05-30 10:42:30 -07003811 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003812 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003813 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003814 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003815 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003816 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003817 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003818 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003819 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003820 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003821
3822 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003823 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003824
3825 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003826 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003827 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003828
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003829 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003830 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003831 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003832 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003833 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003834 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003835 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003836 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003837 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003838 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003839 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003840 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003841 (pp_mode == PP_DEPS)
3842 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003843 if (!inc->fp) {
3844 /* -MG given but file not found */
3845 nasm_free(inc);
3846 } else {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07003847 inc->where = src_where();
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003848 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003849 inc->nolist = istk->nolist;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003850 inc->noline = istk->noline;
3851 if (!inc->noline)
3852 src_set(0, found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003853 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003854 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003855 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003856 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003857
H. Peter Anvind2456592008-06-19 15:04:18 -07003858 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003859 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003860 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003861 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003862
H. Peter Anvin8571f062019-09-23 16:40:03 -07003863 pkg = get_use_pkg(tline->next, dname, &name);
3864 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003865 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003866 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003867 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003868 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003869 /*
3870 * Not already included, go ahead and include it.
3871 * Treat it as an include file for the purpose of
3872 * producing a listing.
3873 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003874 use_loaded[pkg->index] = true;
3875 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003876 nasm_new(inc);
3877 inc->next = istk;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07003878 inc->nolist = istk->nolist + !list_option('b');
3879 inc->noline = istk->noline;
3880 if (!inc->noline)
3881 src_set(0, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003882 istk = inc;
3883 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003884 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003885 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003886 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003887 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003888 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003889 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003890 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003891 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003893 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003894 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003895 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003896 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003897 }
3898 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003899 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003900 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003901 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003902 } else {
3903 p = NULL; /* Anonymous */
3904 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003905
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003906 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003907 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003908 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003909 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003910 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003911 ctx->number = unique++;
3912 cstk = ctx;
3913 } else {
3914 /* %pop or %repl */
3915 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003916 nasm_nonfatal("`%s': context stack is empty", dname);
3917 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003918 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003919 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003920 "expected %s",
3921 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003922 else
3923 ctx_pop();
3924 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003925 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003926 nasm_free((char *)cstk->name);
3927 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003928 p = NULL;
3929 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003930 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003931 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003932 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003933 severity = ERR_FATAL;
3934 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003935 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003936 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003937 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003938 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003939 /*!
3940 *!user [on] %warning directives
3941 *! controls output of \c{%warning} directives (see \k{pperror}).
3942 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003943 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003944 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003945
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003946issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003947 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003948 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003949 tline->next = expand_smacro(tline->next);
3950 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003951 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003952 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003953 t = skip_white(t);
3954 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003955 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003956 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003957 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003958 } else {
3959 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003960 q = detoken(tline, false);
3961 nasm_error(severity, "%s", q);
3962 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003963 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003964 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003965 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003966
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003967 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003968 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003969 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003970 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003971 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003972 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003973 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003974 cond = nasm_malloc(sizeof(Cond));
3975 cond->next = istk->conds;
3976 cond->state = j;
3977 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003978 if(istk->mstk.mstk)
3979 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003980 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003981
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003982 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003983 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003984 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003985 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003986 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003987 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003988 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003989
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003990 case COND_DONE:
3991 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003992 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003993
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003994 case COND_ELSE_TRUE:
3995 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003996 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003997 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003998 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003999 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004000
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004001 case COND_IF_FALSE:
4002 /*
4003 * IMPORTANT: In the case of %if, we will already have
4004 * called expand_mmac_params(); however, if we're
4005 * processing an %elif we must have been in a
4006 * non-emitting mode, which would have inhibited
4007 * the normal invocation of expand_mmac_params().
4008 * Therefore, we have to do it explicitly here.
4009 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004010 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004011 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07004012 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004013 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004014 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07004015 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004016
H. Peter Anvine2c80182005-01-15 22:15:51 +00004017 case PP_ELSE:
4018 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004019 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004020 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004021 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07004022 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004023 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004024 case COND_IF_TRUE:
4025 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004026 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004027 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004028
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004029 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004030 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004031
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004032 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004033 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004034 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004035
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004036 case COND_ELSE_TRUE:
4037 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004038 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004039 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004040 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004041 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004042 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004043 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004044
H. Peter Anvine2c80182005-01-15 22:15:51 +00004045 case PP_ENDIF:
4046 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004047 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004048 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004049 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004050 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004051 cond = istk->conds;
4052 istk->conds = cond->next;
4053 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004054 if(istk->mstk.mstk)
4055 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004056 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004057
H. Peter Anvin8b262472019-02-26 14:00:54 -08004058 case PP_RMACRO:
4059 case PP_MACRO:
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004060 {
4061 MMacro *def;
4062
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004063 nasm_assert(!defining);
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004064 nasm_new(def);
4065 def->casesense = casesense;
H. Peter Anvin (Intel)23abe9f2020-07-10 01:52:49 -07004066 /*
4067 * dstk.mstk points to the previous definition bracket,
4068 * whereas dstk.mmac points to the topmost mmacro, which
4069 * in this case is the one we are just starting to create.
4070 */
4071 def->dstk.mstk = defining;
4072 def->dstk.mmac = def;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004073 if (op == PP_RMACRO)
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004074 def->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
4075 if (!parse_mmacro_spec(tline, def, dname)) {
4076 nasm_free(def);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004077 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004078 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07004079
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004080 defining = def;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004081 defining->where = istk->where;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07004082
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004083 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
4084 while (mmac) {
4085 if (!strcmp(mmac->name, defining->name) &&
4086 (mmac->nparam_min <= defining->nparam_max
4087 || defining->plus)
4088 && (defining->nparam_min <= mmac->nparam_max
4089 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004090 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004091 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004092 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004093 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004094 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004095 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004096 break;
H. Peter Anvin (Intel)7cfd0182020-06-01 12:04:35 -07004097 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004098
H. Peter Anvine2c80182005-01-15 22:15:51 +00004099 case PP_ENDM:
4100 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004101 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004102 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004103 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004104 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004105 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
4106 defining->next = *mmhead;
4107 *mmhead = defining;
4108 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004109 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004110
H. Peter Anvin89cee572009-07-15 09:16:54 -04004111 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004112 /*
4113 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004114 * macro-end marker for a macro with a name. Then we
4115 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004116 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004117 list_for_each(l, istk->expansion)
4118 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004119 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004120
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004121 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004122 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004123 * Remove all conditional entries relative to this
4124 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004125 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004126 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
4127 cond = istk->conds;
4128 istk->conds = cond->next;
4129 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004130 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004131 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004132 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004133 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004134 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004135 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05004136
H. Peter Anvina26433d2008-07-16 14:40:01 -07004137 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004138 casesense = false;
4139 /* fall through */
4140 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07004141 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004142 MMacro **mmac_p;
4143 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004144
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004145 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004146 spec.casesense = casesense;
4147 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004148 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004149 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004150 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
4151 while (mmac_p && *mmac_p) {
4152 mmac = *mmac_p;
4153 if (mmac->casesense == spec.casesense &&
4154 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
4155 mmac->nparam_min == spec.nparam_min &&
4156 mmac->nparam_max == spec.nparam_max &&
4157 mmac->plus == spec.plus) {
4158 *mmac_p = mmac->next;
4159 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004160 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004161 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004162 }
4163 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004164 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004165 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07004166 }
4167
H. Peter Anvine2c80182005-01-15 22:15:51 +00004168 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004169 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004170 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04004171 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004172 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004173 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004174 return DIRECTIVE_FOUND;
4175 }
4176 t = expand_smacro(tline->next);
4177 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004178 pps.tptr = tline = t;
4179 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 tokval.t_type = TOKEN_INVALID;
4181 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004182 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004183 free_tlist(tline);
4184 if (!evalresult)
4185 return DIRECTIVE_FOUND;
4186 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004187 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004188 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004189 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 return DIRECTIVE_FOUND;
4191 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004192 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004193 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004194 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004195 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004196 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004197 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004198 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004199
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004200 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004201 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004202 rotate += mmac->nparam;
4203
4204 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004205 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004206 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004207
H. Peter Anvine2c80182005-01-15 22:15:51 +00004208 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004209 {
4210 MMacro *tmp_defining;
4211
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004212 nolist = 0;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004213 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004214 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
4215 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004216 if (!list_option('f'))
4217 nolist |= NL_LIST; /* ... but update line numbers */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004218 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00004220
H. Peter Anvine2c80182005-01-15 22:15:51 +00004221 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08004222 pps.tptr = expand_smacro(tline);
4223 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004224 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004225 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004226 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004227 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004228 if (!evalresult)
4229 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004230 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004231 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00004232 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004233 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004234 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004236 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004237 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004238 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
4239 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004240 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004241 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08004242 /*!
4243 *!negative-rep [on] regative %rep count
4244 *! warns about negative counts given to the \c{%rep}
4245 *! preprocessor directive.
4246 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004247 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004248 "negative `%%rep' count: %"PRId64, count);
4249 count = 0;
4250 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04004251 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07004252 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004253 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004254 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07004255 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004256 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004257 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004258 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004259 defining->nolist = nolist;
4260 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004261 defining->mstk = istk->mstk;
4262 defining->dstk.mstk = tmp_defining;
4263 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004264 defining->where = istk->where;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004265 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004266 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004267
H. Peter Anvine2c80182005-01-15 22:15:51 +00004268 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004269 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004270 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004271 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004273
H. Peter Anvine2c80182005-01-15 22:15:51 +00004274 /*
4275 * Now we have a "macro" defined - although it has no name
4276 * and we won't be entering it in the hash tables - we must
4277 * push a macro-end marker for it on to istk->expansion.
4278 * After that, it will take care of propagating itself (a
4279 * macro-end marker line for a macro which is really a %rep
4280 * block will cause the macro to be re-expanded, complete
4281 * with another macro-end marker to ensure the process
4282 * continues) until the whole expansion is forcibly removed
4283 * from istk->expansion by a %exitrep.
4284 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004285 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004286 l->next = istk->expansion;
4287 l->finishes = defining;
4288 l->first = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004289 l->where = src_where();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004290 istk->expansion = l;
4291
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004292 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004293
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07004294 /* A loop does not change istk->noline */
4295 istk->nolist += !!(defining->nolist & NL_LIST);
4296 if (!istk->nolist)
4297 lfmt->uplevel(LIST_MACRO, 0);
4298
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004299 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004300 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004301
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004303 /*
4304 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004305 * macro-end marker for a macro with no name. Then we set
4306 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004307 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004308 list_for_each(l, istk->expansion)
4309 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004310 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004311
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004312 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004313 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004314 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004315 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004316 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004317
H. Peter Anvin8b262472019-02-26 14:00:54 -08004318 case PP_DEFINE:
4319 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004320 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004321 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004322 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004323 Token **lastp;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004324 int nparam;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004325
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004326 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004327 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004328
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004329 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004330 lastp = &tline->next;
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004331 nparam = parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004332 tline = *lastp;
4333 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004334
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004335 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004336 macro_start = tline;
4337 if (!is_macro_id(macro_start)) {
4338 nasm_nonfatal("`%s' expects a macro identifier to alias",
4339 dname);
4340 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004342 tt = macro_start->next;
4343 macro_start->next = NULL;
4344 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004345 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004346 if (tline && tline->type) {
4347 nasm_warn(WARN_OTHER,
4348 "trailing garbage after aliasing identifier ignored");
4349 }
4350 free_tlist(tt);
4351 tmpl.alias = true;
4352 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004353 if (op == PP_XDEFINE) {
4354 /* Protect macro parameter tokens */
H. Peter Anvin (Intel)e91f5cc2019-10-23 12:59:06 -07004355 if (nparam)
4356 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004357 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004358 }
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004359 /* NB: Does this still make sense? */
4360 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004361 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004362
H. Peter Anvine2c80182005-01-15 22:15:51 +00004363 /*
4364 * Good. We now have a macro name, a parameter count, and a
4365 * token list (in reverse order) for an expansion. We ought
4366 * to be OK just to create an SMacro, store it, and let
4367 * free_tlist have the rest of the line (which we have
4368 * carefully re-terminated after chopping off the expansion
4369 * from the end).
4370 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004371 define_smacro(mname, casesense, macro_start, &tmpl);
4372 break;
4373 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004374
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004376 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004377 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004378 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004379 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004380 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004381
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004382 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004383 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004384
H. Peter Anvin8b262472019-02-26 14:00:54 -08004385 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004386 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004387 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004388
H. Peter Anvin9e200162008-06-04 17:23:14 -07004389 last = tline;
4390 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004391 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004392
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004393 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004394 q = detoken(tline, false);
4395 macro_start = make_tok_qstr(NULL, q);
4396 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004397
4398 /*
4399 * We now have a macro name, an implicit parameter count of
4400 * zero, and a string token to use as an expansion. Create
4401 * and store an SMacro.
4402 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004403 define_smacro(mname, casesense, macro_start, NULL);
4404 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004405
H. Peter Anvin8b262472019-02-26 14:00:54 -08004406 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004407 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004408 goto done;
4409
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004410 last = tline;
4411 tline = expand_smacro(tline->next);
4412 last->next = NULL;
4413
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004414 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004415 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004416 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004417 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004418 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004419 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004420 }
4421
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004422 /*
4423 * Convert the string to a token stream. Note that smacros
4424 * are stored with the token stream reversed, so we have to
4425 * reverse the output of tokenize().
4426 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004427 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004428
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004429 /*
4430 * We now have a macro name, an implicit parameter count of
4431 * zero, and a numeric token to use as an expansion. Create
4432 * and store an SMacro.
4433 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004434 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004435 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004436 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004437
H. Peter Anvin418ca702008-05-30 10:42:30 -07004438 case PP_PATHSEARCH:
4439 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004440 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004441
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004442 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004443 goto done;
4444
H. Peter Anvin418ca702008-05-30 10:42:30 -07004445 last = tline;
4446 tline = expand_smacro(tline->next);
4447 last->next = NULL;
4448
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004449 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004450 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004451 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004452 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004453 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004454 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004455 }
4456 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004457 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004458
4459 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004460
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004461 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004462 if (!found_path)
4463 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004464 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004465
4466 /*
4467 * We now have a macro name, an implicit parameter count of
4468 * zero, and a string token to use as an expansion. Create
4469 * and store an SMacro.
4470 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004471 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004472 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004473 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004474 }
4475
H. Peter Anvine2c80182005-01-15 22:15:51 +00004476 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004477 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004478 goto done;
4479
H. Peter Anvine2c80182005-01-15 22:15:51 +00004480 last = tline;
4481 tline = expand_smacro(tline->next);
4482 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004483
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004484 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004485 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004486 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004487 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004488 free_tlist(tline);
4489 free_tlist(origline);
4490 return DIRECTIVE_FOUND;
4491 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004492
H. Peter Anvin8571f062019-09-23 16:40:03 -07004493 unquote_token(t);
4494 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004495
H. Peter Anvine2c80182005-01-15 22:15:51 +00004496 /*
4497 * We now have a macro name, an implicit parameter count of
4498 * zero, and a numeric token to use as an expansion. Create
4499 * and store an SMacro.
4500 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004501 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004502 free_tlist(tline);
4503 free_tlist(origline);
4504 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004505
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004506 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004507 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004508 goto done;
4509
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004510 last = tline;
4511 tline = expand_smacro(tline->next);
4512 last->next = NULL;
4513
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004514 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004515 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004516 switch (t->type) {
4517 case TOK_WHITESPACE:
4518 break;
4519 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004520 unquote_token(t);
4521 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004522 break;
4523 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004524 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004525 break;
4526 /* else fall through */
4527 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004528 nasm_nonfatal("non-string passed to `%s': %s", dname,
4529 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004530 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004531 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004532 }
4533 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004534
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004535 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004536 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004537 if (t->type == TOK_INTERNAL_STRING)
4538 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004539 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004540 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004541
4542 /*
4543 * We now have a macro name, an implicit parameter count of
4544 * zero, and a numeric token to use as an expansion. Create
4545 * and store an SMacro.
4546 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004547 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004548 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004549 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004550 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004551 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004552
H. Peter Anvine2c80182005-01-15 22:15:51 +00004553 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004554 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004555 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004556 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004557 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004558
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004559 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004560 goto done;
4561
H. Peter Anvine2c80182005-01-15 22:15:51 +00004562 last = tline;
4563 tline = expand_smacro(tline->next);
4564 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004565
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004566 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004567 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004568
4569 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004570
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004572 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004573 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004574 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004575 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004577
H. Peter Anvin8b262472019-02-26 14:00:54 -08004578 pps.tptr = t->next;
4579 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004580 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004581 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004582 if (!evalresult) {
4583 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004584 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004585 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004586 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004587 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004588 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004590 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004591
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004592 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004593 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004594 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004595 } else {
4596 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004597 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004598 if (!evalresult) {
4599 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004600 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004601 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004602 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004603 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004604 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004605 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004606 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004607 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004608
H. Peter Anvin8571f062019-09-23 16:40:03 -07004609 unquote_token(t);
4610 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004611
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004612 /* make start and count being in range */
4613 if (start < 0)
4614 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004615 if (count < 0)
4616 count = len + count + 1 - start;
4617 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004618 count = len - start;
4619 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004620 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004621
H. Peter Anvin8571f062019-09-23 16:40:03 -07004622 txt = (start < 0) ? "" : tok_text(t) + start;
4623 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004624 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004625
H. Peter Anvine2c80182005-01-15 22:15:51 +00004626 /*
4627 * We now have a macro name, an implicit parameter count of
4628 * zero, and a numeric token to use as an expansion. Create
4629 * and store an SMacro.
4630 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004631 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004632 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004633 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004634 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004635
H. Peter Anvin8b262472019-02-26 14:00:54 -08004636 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004637 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004638 goto done;
4639
H. Peter Anvine2c80182005-01-15 22:15:51 +00004640 last = tline;
4641 tline = expand_smacro(tline->next);
4642 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004643
H. Peter Anvin8b262472019-02-26 14:00:54 -08004644 pps.tptr = tline;
4645 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004646 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004647 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004648 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004649 if (!evalresult)
4650 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004651
H. Peter Anvine2c80182005-01-15 22:15:51 +00004652 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004653 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004654
H. Peter Anvine2c80182005-01-15 22:15:51 +00004655 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004656 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004657 free_tlist(origline);
4658 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004659 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004660
H. Peter Anvin8571f062019-09-23 16:40:03 -07004661 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004662
H. Peter Anvine2c80182005-01-15 22:15:51 +00004663 /*
4664 * We now have a macro name, an implicit parameter count of
4665 * zero, and a numeric token to use as an expansion. Create
4666 * and store an SMacro.
4667 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004668 define_smacro(mname, casesense, macro_start, NULL);
4669 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004670
H. Peter Anvind2354082019-08-27 16:38:48 -07004671 case PP_ALIASES:
4672 tline = tline->next;
4673 tline = expand_smacro(tline);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07004674 ppopt.noaliases = !pp_get_boolean_option(tline, !ppopt.noaliases);
H. Peter Anvind2354082019-08-27 16:38:48 -07004675 break;
4676
H. Peter Anvine2c80182005-01-15 22:15:51 +00004677 case PP_LINE:
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07004678 nasm_panic("`%s' directive not preprocessed early", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004679 break;
H. Peter Anvin (Intel)a79a7002020-07-10 02:55:50 -07004680
4681 case PP_NULL:
4682 /* Goes nowhere, does nothing... */
4683 break;
4684
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004685 }
4686
4687done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004688 free_tlist(origline);
4689 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004690}
4691
4692/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004693 * Ensure that a macro parameter contains a condition code and
4694 * nothing else. Return the condition code index if so, or -1
4695 * otherwise.
4696 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004697static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004698{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004699 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004700
H. Peter Anvin25a99342007-09-22 17:45:45 -07004701 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004702 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004703
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004704 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004705 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004706 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004707 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004708 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004709 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004710 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004711
H. Peter Anvin8571f062019-09-23 16:40:03 -07004712 return bsii(tok_text(t), (const char **)conditions,
4713 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004714}
4715
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004716static inline bool pp_concat_match(const Token *t, unsigned int mask)
4717{
4718 return t && (PP_CONCAT_MASK(t->type) & mask);
4719}
4720
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004721/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004722 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004723 * pasting, if @handle_explicit passed then explicit pasting
4724 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004725 * The @m array can contain a series of token types which are
4726 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004727 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004728static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004729 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004730{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004731 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004732 bool pasted = false;
4733 char *buf, *p;
4734 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004735
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004736 /*
4737 * The last token before pasting. We need it
4738 * to be able to connect new handled tokens.
4739 * In other words if there were a tokens stream
4740 *
4741 * A -> B -> C -> D
4742 *
4743 * and we've joined tokens B and C, the resulting
4744 * stream should be
4745 *
4746 * A -> BC -> D
4747 */
4748 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004749 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004750
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004751 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004752 prev_nonspace = NULL;
4753
4754 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004755 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004756
4757 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004758 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004759 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004760 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004761 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004762
4763 case TOK_PASTE:
4764 /* Explicit pasting */
4765 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004766 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004767
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004768 /* Left pasting token is start of line, just drop %+ */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004769 if (!prev_nonspace) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004770 tok = delete_Token(tok);
4771 break;
4772 }
4773
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004774 did_paste = true;
4775
4776 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004777 t = *prev_nonspace;
4778
4779 /* Delete leading whitespace */
4780 next = zap_white(t->next);
4781
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004782 /*
4783 * Delete the %+ token itself, followed by any whitespace.
4784 * In a sequence of %+ ... %+ ... %+ pasting sequences where
4785 * some expansions in the middle have ended up empty,
4786 * we can end up having multiple %+ tokens in a row;
4787 * just drop whem in that case.
4788 */
4789 while (next) {
4790 if (next->type == TOK_PASTE || next->type == TOK_WHITESPACE)
4791 next = delete_Token(next);
4792 else
4793 break;
4794 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004795
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004796 /*
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004797 * Nothing after? Just leave the existing token.
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004798 */
4799 if (!next) {
H. Peter Anvin (Intel)122c5fb2020-07-05 03:39:04 -07004800 t->next = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004801 break;
4802 }
4803
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004804 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004805 p = mempcpy(p, tok_text(t), t->len);
4806 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004807 *p = '\0';
4808 delete_Token(t);
4809 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004810 nasm_free(buf);
4811
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004812 if (unlikely(!t)) {
4813 /*
4814 * No output at all? Replace with a single whitespace.
4815 * This should never happen.
4816 */
4817 t = new_White(NULL);
4818 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004819
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004820 *prev_nonspace = tok = t;
4821 while (t->next)
4822 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004823
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004824 /* Delete the second token and attach to the end of the list */
4825 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004826
4827 /* We want to restart from the head of the pasted token */
4828 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004829 break;
4830
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004831 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004832 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004833 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004834 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004835 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004836 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004837
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004838 if (i >= mnum)
4839 break;
4840
4841 len = tok->len;
4842 while (pp_concat_match(next, m[i].mask_tail)) {
4843 len += next->len;
4844 next = next->next;
4845 }
4846
4847 /* No match or no text to process */
4848 if (len == tok->len)
4849 break;
4850
4851 p = buf = nasm_malloc(len + 1);
4852 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004853 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004854 tok = delete_Token(tok);
4855 }
4856 *p = '\0';
4857 *prev_next = tok = t = tokenize(buf);
4858 nasm_free(buf);
4859
4860 /*
4861 * Connect pasted into original stream,
4862 * ie A -> new-tokens -> B
4863 */
4864 while (t->next)
4865 t = t->next;
4866 t->next = next;
4867 prev_next = prev_nonspace = &t->next;
4868 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004869 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004870 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004871
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004872 if (did_paste) {
4873 pasted = true;
4874 } else {
4875 prev_next = &tok->next;
4876 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4877 prev_nonspace = prev_next;
4878 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004879
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004880 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004881 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004882
4883 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004884}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004885
4886/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004887 * Computes the proper rotation of mmacro parameters
4888 */
4889static int mmac_rotate(const MMacro *mac, unsigned int n)
4890{
4891 if (--n < mac->nparam)
4892 n = (n + mac->rotate) % mac->nparam;
4893
4894 return n+1;
4895}
4896
4897/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004898 * expands to a list of tokens from %{x:y}
4899 */
H. Peter Anvin (Intel)c0d0f882020-06-30 17:33:39 -07004900static void expand_mmac_params_range(MMacro *mac, Token *tline, Token ***tail)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004901{
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004902 Token *t;
4903 const char *arg = tok_text(tline) + 1;
4904 int fst, lst, incr, n;
4905 int parsed;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004906
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004907 parsed = sscanf(arg, "%d:%d", &fst, &lst);
4908 nasm_assert(parsed == 2);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004909
4910 /*
4911 * only macros params are accounted so
4912 * if someone passes %0 -- we reject such
4913 * value(s)
4914 */
4915 if (lst == 0 || fst == 0)
4916 goto err;
4917
4918 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004919 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4920 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004921 goto err;
4922
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004923 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4924 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004925
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004926 /*
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004927 * It will be at least one parameter, as we can loop
4928 * in either direction.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004929 */
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004930 incr = (fst < lst) ? 1 : -1;
4931
4932 while (true) {
4933 n = mmac_rotate(mac, fst);
4934 dup_tlistn(mac->params[n], mac->paramlen[n], tail);
4935 if (fst == lst)
4936 break;
4937 t = make_tok_char(NULL, ',');
4938 **tail = t;
4939 *tail = &t->next;
4940 fst += incr;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004941 }
4942
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004943 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004944
4945err:
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004946 nasm_nonfatal("`%%{%s}': macro parameters out of range", arg);
4947 return;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004948}
4949
H. Peter Anvin76690a12002-04-30 20:52:49 +00004950/*
4951 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004952 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004953 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004954 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004955static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004956{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004957 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004958 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004959 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004960
4961 tail = &thead;
4962 thead = NULL;
4963
H. Peter Anvine2c80182005-01-15 22:15:51 +00004964 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004965 bool change;
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004966 bool err_not_mac = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004967 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004968 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004969 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004970
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004971 tline = tline->next;
4972 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004973
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004974 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004975 case TOK_LOCAL_SYMBOL:
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004976 change = true;
4977
4978 if (!mac) {
4979 err_not_mac = true;
4980 break;
4981 }
4982
H. Peter Anvin8571f062019-09-23 16:40:03 -07004983 type = TOK_ID;
4984 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004985 break;
4986 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004987 {
4988 Token *tt = NULL;
4989
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004990 change = true;
4991
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004992 if (!mac) {
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07004993 err_not_mac = true;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004994 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004995 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004996
4997 if (strchr(text, ':')) {
H. Peter Anvin (Intel)e99a9462020-06-30 11:51:41 -07004998 /* It is a range */
4999 expand_mmac_params_range(mac, t, &tail);
5000 text = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005001 break;
5002 }
5003
5004 switch (text[1]) {
5005 /*
5006 * We have to make a substitution of one of the
5007 * forms %1, %-1, %+1, %%foo, %0, %00.
5008 */
5009 case '0':
5010 if (!text[2]) {
5011 type = TOK_NUMBER;
5012 text = nasm_asprintf("%d", mac->nparam);
5013 break;
5014 }
5015 if (text[2] != '0' || text[3])
5016 goto invalid;
5017 /* a possible captured label == mac->params[0] */
5018 /* fall through */
5019 default:
5020 {
5021 unsigned long n;
5022 char *ep;
5023
5024 n = strtoul(text + 1, &ep, 10);
5025 if (unlikely(*ep))
5026 goto invalid;
5027
5028 if (n <= mac->nparam) {
5029 n = mmac_rotate(mac, n);
5030 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
5031 }
5032 text = NULL;
5033 break;
5034 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005035 case '-':
5036 case '+':
5037 {
5038 int cc;
5039 unsigned long n;
5040 char *ep;
5041
H. Peter Anvin8571f062019-09-23 16:40:03 -07005042 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005043 if (unlikely(*ep))
5044 goto invalid;
5045
Chang S. Bae057b8322020-04-18 23:11:21 +00005046 if (n && n <= mac->nparam) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005047 n = mmac_rotate(mac, n);
5048 tt = mac->params[n];
5049 }
5050 cc = find_cc(tt);
5051 if (cc == -1) {
5052 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005053 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005054 text = NULL;
5055 break;
5056 }
5057
5058 type = TOK_ID;
5059 if (text[1] == '-') {
5060 int ncc = inverse_ccs[cc];
5061 if (unlikely(ncc == -1)) {
5062 nasm_nonfatal("condition code `%s' is not invertible",
5063 conditions[cc]);
5064 break;
5065 }
5066 cc = ncc;
5067 }
5068 text = nasm_strdup(conditions[cc]);
5069 break;
5070 }
5071
5072 invalid:
5073 nasm_nonfatal("invalid macro parameter: `%s'", text);
5074 text = NULL;
5075 break;
5076 }
5077 break;
5078 }
5079
5080 case TOK_PREPROC_Q:
5081 if (mac) {
5082 type = TOK_ID;
5083 text = nasm_strdup(mac->iname);
5084 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005085 } else {
5086 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005087 }
5088 break;
5089
5090 case TOK_PREPROC_QQ:
5091 if (mac) {
5092 type = TOK_ID;
5093 text = nasm_strdup(mac->name);
5094 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07005095 } else {
5096 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005097 }
5098 break;
5099
5100 case TOK_INDIRECT:
5101 {
5102 Token *tt;
5103
H. Peter Anvin8571f062019-09-23 16:40:03 -07005104 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005105 tt = expand_mmac_params(tt);
5106 tt = expand_smacro(tt);
5107 /* Why dup_tlist() here? We should own tt... */
5108 dup_tlist(tt, &tail);
5109 text = NULL;
5110 change = true;
5111 break;
5112 }
5113
5114 default:
5115 change = false;
5116 break;
5117 }
5118
H. Peter Anvin (Intel)a762cd42020-06-01 11:49:08 -07005119 if (err_not_mac) {
5120 nasm_nonfatal("`%s': not in a macro call", text);
5121 text = NULL;
5122 change = true;
5123 }
5124
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005125 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 if (!text) {
5127 delete_Token(t);
5128 } else {
5129 *tail = t;
5130 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005131 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005132 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005133 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005134 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005135 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005136 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005137 tail = &t->next;
5138 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00005139 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005140
H. Peter Anvineba20a72002-04-30 20:53:55 +00005141 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07005142
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005143 if (changed) {
5144 const struct tokseq_match t[] = {
5145 {
5146 PP_CONCAT_MASK(TOK_ID) |
5147 PP_CONCAT_MASK(TOK_FLOAT), /* head */
5148 PP_CONCAT_MASK(TOK_ID) |
5149 PP_CONCAT_MASK(TOK_NUMBER) |
5150 PP_CONCAT_MASK(TOK_FLOAT) |
5151 PP_CONCAT_MASK(TOK_OTHER) /* tail */
5152 },
5153 {
5154 PP_CONCAT_MASK(TOK_NUMBER), /* head */
5155 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5156 }
5157 };
5158 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
5159 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07005160
H. Peter Anvin76690a12002-04-30 20:52:49 +00005161 return thead;
5162}
5163
H. Peter Anvin322bee02019-08-10 01:38:06 -07005164static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005165
H. Peter Anvin76690a12002-04-30 20:52:49 +00005166/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005167 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005168 * a macro at all, it is simply copied to the output and the pointer
5169 * advanced. tpp should be a pointer to a pointer (usually the next
5170 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005171 * to point to the first token of the expansion, and *tpp updated to
5172 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005173 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005174 * If the expansion is empty, *tpp will be unchanged but **tpp will
5175 * be advanced past the macro call.
5176 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07005177 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005178 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005179static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005180{
5181 Token **params = NULL;
5182 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005183 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005184 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005185 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005186 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005187 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005188 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005189 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005190
5191 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005192 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005193
H. Peter Anvin8571f062019-09-23 16:40:03 -07005194 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005195
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005196 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005197 smacro_deadman.levels--;
5198
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005199 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07005200 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005201 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07005202 smacro_deadman.triggered = true;
5203 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005204 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005205 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005206 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005207 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005208 Context *ctx = get_ctx(mname, &mname);
5209 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
5210 } else {
5211 goto not_a_macro;
5212 }
5213
5214 /*
5215 * We've hit an identifier of some sort. First check whether the
5216 * identifier is a single-line macro at all, then think about
5217 * checking for parameters if necessary.
5218 */
5219 list_for_each(m, head) {
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005220 if (unlikely(m->alias && ppopt.noaliases))
H. Peter Anvind2354082019-08-27 16:38:48 -07005221 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005222 if (!mstrcmp(m->name, mname, m->casesense))
5223 break;
5224 }
5225
5226 if (!m) {
5227 goto not_a_macro;
5228 }
5229
5230 /* Parse parameters, if applicable */
5231
5232 params = NULL;
5233 nparam = 0;
5234
5235 if (m->nparam == 0) {
5236 /*
5237 * Simple case: the macro is parameterless.
5238 * Nothing to parse; the expansion code will
5239 * drop the macro name token.
5240 */
5241 } else {
5242 /*
5243 * Complicated case: at least one macro with this name
5244 * exists and takes parameters. We must find the
5245 * parameters in the call, count them, find the SMacro
5246 * that corresponds to that form of the macro call, and
5247 * substitute for the parameters when we expand. What a
5248 * pain.
5249 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005250 Token *t;
5251 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005252
5253 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005254 tline = skip_white(tline);
5255 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005256 /*
5257 * This macro wasn't called with parameters: ignore
5258 * the call. (Behaviour borrowed from gnu cpp.)
5259 */
5260 goto not_a_macro;
5261 }
5262
5263 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005264 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005265 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005266 t = tline; /* tline points to leading ( */
5267
5268 while (paren) {
5269 t = t->next;
5270
5271 if (!t) {
5272 nasm_nonfatal("macro call expects terminating `)'");
5273 goto not_a_macro;
5274 }
5275
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005276 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005277 continue;
5278
H. Peter Anvin8571f062019-09-23 16:40:03 -07005279 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005280 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005281 if (!brackets && paren == 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005282 nparam++;
5283 break;
5284
5285 case '{':
5286 brackets++;
5287 break;
5288
5289 case '}':
5290 if (brackets > 0)
5291 brackets--;
5292 break;
5293
5294 case '(':
5295 if (!brackets)
5296 paren++;
5297 break;
5298
5299 case ')':
5300 if (!brackets)
5301 paren--;
5302 break;
5303
5304 default:
5305 break; /* Normal token */
5306 }
5307 }
5308
5309 /*
5310 * Look for a macro matching in both name and parameter count.
5311 * We already know any matches cannot be anywhere before the
5312 * current position of "m", so there is no reason to
5313 * backtrack.
5314 */
5315 while (1) {
5316 if (!m) {
5317 /*!
5318 *!macro-params-single [on] single-line macro calls with wrong parameter count
5319 *! warns about \i{single-line macros} being invoked
5320 *! with the wrong number of parameters.
5321 */
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005322 nasm_warn(WARN_MACRO_PARAMS_SINGLE|ERR_HOLD,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005323 "single-line macro `%s' exists, "
5324 "but not taking %d parameter%s",
5325 mname, nparam, (nparam == 1) ? "" : "s");
5326 goto not_a_macro;
5327 }
5328
5329 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005330 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005331 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005332 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005333 break; /* Also good */
5334 }
5335 m = m->next;
5336 }
5337 }
5338
5339 if (m->in_progress)
5340 goto not_a_macro;
5341
5342 /* Expand the macro */
5343 m->in_progress = true;
5344
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005345 if (nparam) {
5346 /* Extract parameters */
5347 Token **phead, **pep;
5348 int white = 0;
5349 int brackets = 0;
5350 int paren;
5351 bool bracketed = false;
5352 bool bad_bracket = false;
5353 enum sparmflags flags;
5354
5355 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005356 paren = 1;
5357 nasm_newn(params, nparam);
5358 i = 0;
5359 flags = m->params[i].flags;
5360 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005361 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005362
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005363 while (paren) {
5364 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005365 char ch;
5366
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005367 tline = tline->next;
5368
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005369 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005370 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005371
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005372 ch = 0;
5373 skip = false;
5374
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005375
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005376 switch (tline->type) {
5377 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005378 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005379 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005380 break;
5381
5382 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005383 if (!(flags & SPARM_NOSTRIP)) {
5384 if (brackets || *phead)
5385 white++; /* Keep interior whitespace */
5386 skip = true;
5387 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005388 break;
5389
5390 default:
5391 break;
5392 }
5393
5394 switch (ch) {
5395 case ',':
H. Peter Anvinbd00f252020-06-04 21:05:01 -07005396 if (!brackets && paren == 1 && !(flags & SPARM_GREEDY)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005397 i++;
5398 nasm_assert(i < nparam);
5399 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005400 *pep = NULL;
5401 bracketed = false;
5402 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005403 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005404 }
5405 break;
5406
5407 case '{':
5408 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005409 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5410 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005411 }
5412 brackets++;
5413 break;
5414
5415 case '}':
5416 if (brackets > 0) {
5417 if (!--brackets)
5418 skip = bracketed;
5419 }
5420 break;
5421
5422 case '(':
5423 if (!brackets)
5424 paren++;
5425 break;
5426
5427 case ')':
5428 if (!brackets) {
5429 paren--;
5430 if (!paren) {
5431 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005432 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005433 }
5434 }
5435 break;
5436
5437 default:
5438 break; /* Normal token */
5439 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005440
5441 if (!skip) {
5442 Token *t;
5443
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005444 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005445
5446 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005447 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005448 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005449 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005450 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005451 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005452 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005453 }
5454 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005455
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005456 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005457 * Possible further processing of parameters. Note that the
5458 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005459 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005460 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005461 enum sparmflags flags = m->params[i].flags;
5462
5463 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005464 /* Evaluate this parameter as a number */
5465 struct ppscan pps;
5466 struct tokenval tokval;
5467 expr *evalresult;
5468 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005469
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005470 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5471 pps.ntokens = -1;
5472 tokval.t_type = TOKEN_INVALID;
5473 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005474
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005475 free_tlist(eval_param);
5476 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005477
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005478 if (!evalresult) {
5479 /* Nothing meaningful to do */
5480 } else if (tokval.t_type) {
5481 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5482 } else if (!is_simple(evalresult)) {
5483 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5484 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005485 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005486 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005487 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005488
5489 if (flags & SPARM_STR) {
5490 /* Convert expansion to a quoted string */
5491 char *arg;
5492 Token *qs;
5493
5494 qs = expand_smacro_noreset(params[i]);
5495 arg = detoken(qs, false);
5496 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005497 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005498 nasm_free(arg);
5499 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005500 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005501 }
5502
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005503 /* Note: we own the expansion this returns. */
5504 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005505
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005506 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005507 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005508 tline = tafter;
5509
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005510 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005511 cond_comma = false;
5512
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005513 while (t) {
5514 enum pp_token_type type = t->type;
5515 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005516
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005517 switch (type) {
5518 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005519 delete_Token(t);
5520 t = dup_Token(tline, mstart);
5521 break;
5522
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005523 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005524 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005525 size_t mlen = strlen(m->name);
5526 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005527 char *p;
5528
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005529 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005530 if (t->type == TOK_LOCAL_MACRO) {
5531 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005532 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005533 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005534
H. Peter Anvin8571f062019-09-23 16:40:03 -07005535 psp = tok_text(mstart);
5536 get_ctx(psp, &pep);
5537 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005538
H. Peter Anvin8571f062019-09-23 16:40:03 -07005539 len = mlen + plen;
5540 p = nasm_malloc(len + 1);
5541 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005542 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005543 len = mlen;
5544 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005545 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005546 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005547 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005548 set_text_free(t, p, len);
5549
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005550 t->next = tline;
5551 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005552 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005553
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005554 case TOK_COND_COMMA:
5555 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005556 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005557 break;
5558
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005559 case TOK_ID:
5560 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005561 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005562 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005563 /*
5564 * Chain this into the target line *before* expanding,
5565 * that way we pick up any arguments to the new macro call,
5566 * if applicable.
5567 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005568 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005569 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005570 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005571 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005572 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005573 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005574 default:
5575 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005576 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005577 nasm_assert(!tup && param < nparam);
5578 delete_Token(t);
5579 t = NULL;
5580 tup = tnext;
5581 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005582 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005583 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005584 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005585 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005586 }
5587
5588 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005589 Token *endt = tline;
5590
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005591 tline = t;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005592 while (!cond_comma && t && t != endt) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005593 cond_comma = t->type != TOK_WHITESPACE;
Chang S. Bae95e54a92020-02-06 14:39:22 -08005594 t = t->next;
5595 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005596 }
5597
5598 if (tnext) {
5599 t = tnext;
5600 } else {
5601 t = tup;
5602 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005603 }
5604 }
5605
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005606 **tpp = tline;
H. Peter Anvin (Intel)6e714962020-06-01 12:21:10 -07005607 for (t = tline; t && t != tafter; t = t->next)
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005608 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005609
5610 m->in_progress = false;
5611
5612 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005613 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005614 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005615
5616 /*
5617 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005618 * and then advance to the next input token. Note that this is
5619 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005620 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005621not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005622 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005623 m = NULL;
5624done:
5625 smacro_deadman.levels++;
5626 if (unlikely(params))
5627 free_tlist_array(params, nparam);
5628 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005629}
5630
5631/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005632 * Expand all single-line macro calls made in the given line.
5633 * Return the expanded version of the line. The original is deemed
5634 * to be destroyed in the process. (In reality we'll just move
5635 * Tokens from input to output a lot of the time, rather than
5636 * actually bothering to destroy and replicate.)
5637 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005638static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005639{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005640 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005641 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5642 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005643 return expand_smacro_noreset(tline);
5644}
5645
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005646static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005647{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005648 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005649 bool expanded;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005650 errhold errhold; /* Hold warning/errors during expansion */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005651
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005652 if (!org_tline)
5653 return NULL; /* Empty input */
5654
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005655 /*
5656 * Trick: we should avoid changing the start token pointer since it can
5657 * be contained in "next" field of other token. Because of this
5658 * we allocate a copy of first token and work with it; at the end of
5659 * routine we copy it back
5660 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005661 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005662
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005663 /*
5664 * Pretend that we always end up doing expansion on the first pass;
5665 * that way %+ get processed. However, if we process %+ before the
5666 * first pass, we end up with things like MACRO %+ TAIL trying to
5667 * look up the macro "MACROTAIL", which we don't want.
5668 */
5669 expanded = true;
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005670
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005671 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005672 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005673 {
5674 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005675 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5676 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005677 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5678 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005679 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5680 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005681 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5682 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5683 }
5684 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005685 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005686
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005687 /*
5688 * We hold warnings/errors until we are done this this loop. It is
5689 * possible for nuisance warnings to appear that disappear on later
5690 * passes.
5691 */
5692 errhold = nasm_error_hold_push();
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005693
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005694 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005695 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005696
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005697 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005698 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005699
5700 /*
5701 * Now scan the entire line and look for successive TOK_IDs
5702 * that resulted after expansion (they can't be produced by
5703 * tokenize()). The successive TOK_IDs should be concatenated.
5704 * Also we look for %+ tokens and concatenate the tokens
5705 * before and after them (without white spaces in between).
5706 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005707 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5708 break; /* Done again! */
5709
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005710 nasm_error_hold_pop(errhold, false);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005711 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005712 }
H. Peter Anvin (Intel)4964d802020-06-04 15:53:31 -07005713 nasm_error_hold_pop(errhold, true);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005714
H. Peter Anvin8571f062019-09-23 16:40:03 -07005715 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005716 /*
5717 * The expression expanded to empty line;
5718 * we can't return NULL because of the "trick" above.
5719 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005720 */
5721
5722 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005723 }
5724
H. Peter Anvin8571f062019-09-23 16:40:03 -07005725 steal_Token(org_tline, tline);
5726 org_tline->next = tline->next;
5727 delete_Token(tline);
5728
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005729 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005730}
5731
5732/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005733 * Similar to expand_smacro but used exclusively with macro identifiers
5734 * right before they are fetched in. The reason is that there can be
5735 * identifiers consisting of several subparts. We consider that if there
5736 * are more than one element forming the name, user wants a expansion,
5737 * otherwise it will be left as-is. Example:
5738 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005739 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005740 *
5741 * the identifier %$abc will be left as-is so that the handler for %define
5742 * will suck it and define the corresponding value. Other case:
5743 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005744 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005745 *
5746 * In this case user wants name to be expanded *before* %define starts
5747 * working, so we'll expand %$abc into something (if it has a value;
5748 * otherwise it will be left as-is) then concatenate all successive
5749 * PP_IDs into one.
5750 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005751static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005752{
5753 Token *cur, *oldnext = NULL;
5754
H. Peter Anvin734b1882002-04-30 21:01:08 +00005755 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005756 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005757
5758 cur = tline;
5759 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005760 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5761 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005762 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005763
5764 /* If identifier consists of just one token, don't expand */
5765 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005766 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005767
H. Peter Anvine2c80182005-01-15 22:15:51 +00005768 if (cur) {
5769 oldnext = cur->next; /* Detach the tail past identifier */
5770 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005771 }
5772
H. Peter Anvin734b1882002-04-30 21:01:08 +00005773 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005774
H. Peter Anvine2c80182005-01-15 22:15:51 +00005775 if (cur) {
5776 /* expand_smacro possibly changhed tline; re-scan for EOL */
5777 cur = tline;
5778 while (cur && cur->next)
5779 cur = cur->next;
5780 if (cur)
5781 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005782 }
5783
5784 return tline;
5785}
5786
5787/*
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005788 * This is called from find_mmacro_in_list() after finding a suitable macro.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005789 */
5790static MMacro *use_mmacro(MMacro *m, int *nparamp, Token ***paramsp)
5791{
5792 int nparam = *nparamp;
5793 Token **params = *paramsp;
5794
5795 /*
5796 * This one is right. Just check if cycle removal
5797 * prohibits us using it before we actually celebrate...
5798 */
5799 if (m->in_progress > m->max_depth) {
5800 if (m->max_depth > 0) {
5801 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
5802 m->max_depth);
5803 }
5804 nasm_free(params);
5805 *nparamp = 0;
5806 *paramsp = NULL;
5807 return NULL;
5808 }
5809
5810 /*
5811 * It's right, and we can use it. Add its default
5812 * parameters to the end of our list if necessary.
5813 */
5814 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
5815 int newnparam = m->nparam_min + m->ndefs;
5816 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5817 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5818 (newnparam - nparam) * sizeof(*params));
5819 nparam = newnparam;
5820 }
5821 /*
5822 * If we've gone over the maximum parameter count (and
5823 * we're in Plus mode), ignore parameters beyond
5824 * nparam_max.
5825 */
5826 if (m->plus && nparam > m->nparam_max)
5827 nparam = m->nparam_max;
5828
5829 /*
5830 * If nparam was adjusted above, make sure the list is still
5831 * NULL-terminated.
5832 */
5833 params[nparam+1] = NULL;
5834
5835 /* Done! */
5836 *paramsp = params;
5837 *nparamp = nparam;
5838 return m;
5839}
5840
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005841/*
5842 * Search a macro list and try to find a match. If matching, call
5843 * use_mmacro() to set up the macro call. m points to the list of
5844 * search, which is_mmacro() sets to the first *possible* match.
5845 */
5846static MMacro *
5847find_mmacro_in_list(MMacro *m, const char *finding,
5848 int *nparamp, Token ***paramsp)
5849{
5850 int nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005851
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005852 while (m) {
5853 if (m->nparam_min <= nparam
5854 && (m->plus || nparam <= m->nparam_max)) {
5855 /*
5856 * This one matches, use it.
5857 */
5858 return use_mmacro(m, nparamp, paramsp);
5859 }
5860
5861 /*
5862 * Otherwise search for the next one with a name match.
5863 */
5864 list_for_each(m, m->next) {
5865 if (!mstrcmp(m->name, finding, m->casesense))
5866 break;
5867 }
5868 }
5869
5870 return NULL;
5871}
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005872
5873/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005874 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005875 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005876 * to check for an initial label - that's taken care of in
5877 * expand_mmacro - but must check numbers of parameters. Guaranteed
5878 * to be called with tline->type == TOK_ID, so the putative macro
5879 * name is easy to find.
5880 */
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005881static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005882{
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005883 MMacro *head, *m, *found;
5884 Token **params, **comma;
5885 int raw_nparam, nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005886 const char *finding = tok_text(tline);
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005887 bool empty_args = !tline->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005888
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005889 *nparamp = 0;
5890 *paramsp = NULL;
5891
H. Peter Anvin8571f062019-09-23 16:40:03 -07005892 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005893
5894 /*
5895 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005896 * name which isn't already excluded by macro cycle removal.
5897 * (The cycle removal test here helps optimize the case of wrapping
5898 * instructions, and is cheap to do here.)
5899 *
5900 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005901 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005902 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005903 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005904 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005905 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005906 (m->in_progress != 1 || m->max_depth > 0))
5907 break; /* Found something that needs consideration */
5908 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005909 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005910 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005911
5912 /*
5913 * OK, we have a potential macro. Count and demarcate the
5914 * parameters.
5915 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005916 comma = count_mmac_params(tline->next, nparamp, paramsp);
5917 raw_nparam = *nparamp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005918
5919 /*
5920 * Search for an exact match. This cannot come *before* the m
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005921 * found in the list search before, so we can start there.
5922 *
5923 * If found is NULL and *paramsp has been cleared, then we
5924 * encountered an error for which we have already issued a
5925 * diagnostic, so we should not proceed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005926 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005927 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
5928 if (!*paramsp)
5929 return NULL;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005930
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005931 nparam = *nparamp;
5932 params = *paramsp;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005933
5934 /*
5935 * Special weirdness: in NASM < 2.15, an expansion of
5936 * *only* whitespace, as can happen during macro expansion under
5937 * certain circumstances, is counted as zero arguments for the
5938 * purpose of %0, but one argument for the purpose of macro
5939 * matching! In particular, this affects:
5940 *
5941 * foobar %1
5942 *
5943 * ... with %1 being empty; this would call the one-argument
5944 * version of "foobar" with an empty argument, equivalent to
5945 *
5946 * foobar {%1}
5947 *
5948 * ... except that %0 would be set to 0 inside foobar, even if
5949 * foobar is declared with "%macro foobar 1" or equivalent!
5950 *
5951 * The proper way to do that is to define "%macro foobar 0-1".
5952 *
5953 * To be compatible without doing something too stupid, try to
5954 * match a zero-argument macro first, but if that fails, try
5955 * for a one-argument macro with the above behavior.
5956 *
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005957 * Furthermore, NASM < 2.15 will match stripping a tailing empty
5958 * argument, but in that case %0 *does* reflect that this argument
5959 * have been stripped; this is handled in count_mmac_params().
5960 *
5961 * To disable these insane legacy behaviors, use:
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005962 *
5963 * %pragma preproc sane_empty_expansion yes
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005964 *
5965 *!macro-params-legacy [on] improperly calling multi-line macro for legacy support
5966 *! warns about \i{multi-line macros} being invoked
5967 *! with the wrong number of parameters, but for bug-compatibility
5968 *! with NASM versions older than 2.15, NASM tried to fix up the
5969 *! parameters to match the legacy behavior and call the macro anyway.
5970 *! This can happen in certain cases where there are empty arguments
5971 *! without braces, sometimes as a result of macro expansion.
5972 *!-
5973 *! The legacy behavior is quite strange and highly context-dependent,
5974 *! and can be disabled with:
5975 *!-
H. Peter Anvin (Intel)de8817d2020-06-27 22:30:50 -07005976 *! \c %pragma preproc sane_empty_expansion true
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005977 *!-
5978 *! It is highly recommended to use this option in new code.
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07005979 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07005980 if (!ppopt.sane_empty_expansion) {
5981 if (!found) {
5982 if (raw_nparam == 0 && !empty_args) {
5983 /*
5984 * A single all-whitespace parameter as the only thing?
5985 * Look for a one-argument macro, but don't adjust
5986 * *nparamp.
5987 */
5988 int bogus_nparam = 1;
5989 params[2] = NULL;
5990 found = find_mmacro_in_list(m, finding, &bogus_nparam, paramsp);
5991 } else if (raw_nparam > 1 && comma) {
5992 Token *comma_tail = *comma;
5993
5994 /*
5995 * Drop the terminal argument and try again.
5996 * If we fail, we need to restore the comma to
5997 * preserve tlist.
5998 */
5999 *comma = NULL;
6000 *nparamp = raw_nparam - 1;
6001 found = find_mmacro_in_list(m, finding, nparamp, paramsp);
6002 if (found)
6003 free_tlist(comma_tail);
6004 else
6005 *comma = comma_tail;
6006 }
6007
6008 if (!*paramsp)
6009 return NULL;
6010 } else if (comma) {
6011 free_tlist(*comma);
6012 *comma = NULL;
6013 if (raw_nparam > found->nparam_min &&
6014 raw_nparam <= found->nparam_min + found->ndefs) {
6015 /* Replace empty argument with default parameter */
6016 params[raw_nparam] =
6017 found->defaults[raw_nparam - found->nparam_min];
6018 } else if (raw_nparam > found->nparam_max && found->plus) {
6019 /* Just drop the comma, don't adjust argument count */
6020 } else {
6021 /* Drop argument. This may cause nparam < nparam_min. */
6022 params[raw_nparam] = NULL;
6023 *nparamp = nparam = raw_nparam - 1;
6024 }
6025 }
6026
6027 if (found) {
6028 if (raw_nparam < found->nparam_min ||
6029 (raw_nparam > found->nparam_max && !found->plus)) {
6030 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
6031 "improperly calling multi-line macro `%s' with %d parameters",
6032 found->name, raw_nparam);
6033 } else if (comma) {
6034 nasm_warn(WARN_MACRO_PARAMS_LEGACY,
6035 "dropping trailing empty parameter in call to multi-line macro `%s'", found->name);
6036 }
6037 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006038 }
6039
6040 /*
6041 * After all that, we didn't find one with the right number of
6042 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006043 *!
6044 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
6045 *! warns about \i{multi-line macros} being invoked
6046 *! with the wrong number of parameters. See \k{mlmacover} for an
6047 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006048 */
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006049 if (found)
6050 return found;
6051
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006052 nasm_warn(WARN_MACRO_PARAMS_MULTI,
6053 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006054 finding, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006055 nasm_free(*paramsp);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006056 return NULL;
6057}
6058
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006059
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006060#if 0
6061
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006062/*
6063 * Save MMacro invocation specific fields in
6064 * preparation for a recursive macro expansion
6065 */
6066static void push_mmacro(MMacro *m)
6067{
6068 MMacroInvocation *i;
6069
6070 i = nasm_malloc(sizeof(MMacroInvocation));
6071 i->prev = m->prev;
6072 i->params = m->params;
6073 i->iline = m->iline;
6074 i->nparam = m->nparam;
6075 i->rotate = m->rotate;
6076 i->paramlen = m->paramlen;
6077 i->unique = m->unique;
6078 i->condcnt = m->condcnt;
6079 m->prev = i;
6080}
6081
6082
6083/*
6084 * Restore MMacro invocation specific fields that were
6085 * saved during a previous recursive macro expansion
6086 */
6087static void pop_mmacro(MMacro *m)
6088{
6089 MMacroInvocation *i;
6090
6091 if (m->prev) {
6092 i = m->prev;
6093 m->prev = i->prev;
6094 m->params = i->params;
6095 m->iline = i->iline;
6096 m->nparam = i->nparam;
6097 m->rotate = i->rotate;
6098 m->paramlen = i->paramlen;
6099 m->unique = i->unique;
6100 m->condcnt = i->condcnt;
6101 nasm_free(i);
6102 }
6103}
6104
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006105#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006106
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006107/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07006108 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006109 */
6110static void list_mmacro_call(const MMacro *m)
6111{
6112 const char prefix[] = " ;;; [macro] ";
6113 size_t namelen, size;
6114 char *buf, *p;
6115 unsigned int i;
6116 const Token *t;
6117
6118 namelen = strlen(m->iname);
6119 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
6120
6121 for (i = 1; i <= m->nparam; i++) {
6122 int j = 0;
6123 size += 3; /* Braces and space/comma */
6124 list_for_each(t, m->params[i]) {
6125 if (j++ >= m->paramlen[i])
6126 break;
6127 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
6128 }
6129 }
6130
6131 buf = p = nasm_malloc(size);
6132 p = mempcpy(p, prefix, sizeof(prefix) - 1);
6133 p = mempcpy(p, m->iname, namelen);
6134 *p++ = ' ';
6135
6136 for (i = 1; i <= m->nparam; i++) {
6137 int j = 0;
6138 *p++ = '{';
6139 list_for_each(t, m->params[i]) {
6140 if (j++ >= m->paramlen[i])
6141 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006142 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006143 }
6144 *p++ = '}';
6145 *p++ = ',';
6146 }
6147
6148 *--p = '\0'; /* Replace last delimeter with null */
6149 lfmt->line(LIST_MACRO, -1, buf);
6150 nasm_free(buf);
6151}
6152
6153/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006154 * Expand the multi-line macro call made by the given line, if
6155 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006156 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006157 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006158static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006159{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006160 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006161 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006162 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006163 Token **params, *t, *tt;
6164 MMacro *m;
6165 Line *l, *ll;
H. Peter Anvin (Intel)42894382020-06-14 19:42:22 -07006166 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07006167 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006168 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006169
6170 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006171 t = skip_white(t);
6172 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07006173 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006174 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006175 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006176 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006177 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07006178 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006179 Token *last;
6180 /*
6181 * We have an id which isn't a macro call. We'll assume
6182 * it might be a label; we'll also check to see if a
6183 * colon follows it. Then, if there's another id after
6184 * that lot, we'll check it again for macro-hood.
6185 */
6186 label = last = t;
6187 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006188 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006189 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006190 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006191 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006192 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006193 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00006194 last = t, t = t->next;
6195 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006196 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006197 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006198 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006199 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006200 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006201 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006202
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006203 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
6204 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
6205 if (!mmacro_deadman.triggered) {
6206 nasm_nonfatal("interminable multiline macro recursion");
6207 mmacro_deadman.triggered = true;
6208 }
6209 return 0;
6210 }
6211
6212 mmacro_deadman.total++;
6213 mmacro_deadman.levels++;
6214
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006215 /*
6216 * Fix up the parameters: this involves stripping leading and
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006217 * trailing whitespace and stripping braces if they are present.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006218 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006219 nasm_newn(paramlen, nparam+1);
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006220
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006221 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006222 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08006223 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006224 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006225 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006226
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006227 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006228 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006229 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006230 brace = 1;
6231 braced = true;
6232 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006233 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006234
6235 params[i] = t;
6236 for (; t; t = t->next) {
6237 if (tok_white(t)) {
6238 white++;
6239 continue;
6240 }
6241
6242 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006243 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07006244 case ',':
6245 if (comma && !brace)
6246 goto endparam;
6247 break;
6248
6249 case '{':
6250 brace++;
6251 break;
6252
6253 case '}':
6254 brace--;
6255 if (braced && !brace) {
6256 paramlen[i] += white;
6257 goto endparam;
6258 }
6259 break;
6260
6261 default:
6262 break;
6263 }
6264 }
6265
6266 paramlen[i] += white + 1;
6267 white = 0;
6268 }
6269 endparam:
6270 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006271 }
6272
6273 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006274 * OK, we have a MMacro structure together with a set of
6275 * parameters. We must now go through the expansion and push
6276 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00006277 * parameter tokens and macro-local tokens doesn't get done
6278 * until the single-line macro substitution process; this is
6279 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006280 * later through %rotate and give the right semantics for
6281 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006282 *
6283 * First, push an end marker on to istk->expansion, mark this
6284 * macro as in progress, and set up its invocation-specific
6285 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006286 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006287 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006288 ll->next = istk->expansion;
6289 ll->finishes = m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006290 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006291 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006292
6293 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006294 * Save the previous MMacro expansion in the case of
6295 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006296 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006297#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006298 if (m->max_depth && m->in_progress)
6299 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006300#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006301
6302 m->in_progress ++;
6303 m->params = params;
6304 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006305 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006306 m->nparam = nparam;
6307 m->rotate = 0;
6308 m->paramlen = paramlen;
6309 m->unique = unique++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006310 m->condcnt = 0;
6311
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006312 m->mstk = istk->mstk;
6313 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006314
6315 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07006316 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006317 ll->next = istk->expansion;
6318 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006319 ll->first = dup_tlist(l->first, NULL);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006320 ll->where = l->where;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006321 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006322
6323 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006324 * If we had a label, and this macro definition does not include
6325 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00006326 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006327 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006328 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006329 /*
6330 * We had a label. If this macro contains an %00 parameter,
6331 * save the value as a special parameter (which is what it
6332 * is), otherwise push it as the first line of the macro
6333 * expansion.
6334 */
6335 if (m->capture_label) {
6336 params[0] = dup_Token(NULL, label);
6337 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006338 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006339 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006340 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006341 ll->finishes = NULL;
6342 ll->next = istk->expansion;
6343 istk->expansion = ll;
6344 ll->first = startline;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006345 ll->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006346 if (!dont_prepend) {
6347 while (label->next)
6348 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07006349 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006350 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006351 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006352 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006353
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006354 istk->nolist += !!(m->nolist & NL_LIST);
6355 istk->noline += !!(m->nolist & NL_LINE);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006356
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006357 if (!istk->nolist) {
6358 lfmt->uplevel(LIST_MACRO, 0);
6359
6360 if (list_option('m'))
6361 list_mmacro_call(m);
6362 }
6363
6364 if (!istk->noline)
6365 src_macro_push(m, istk->where);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07006366
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006367 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006368}
6369
H. Peter Anvin130736c2016-02-17 20:27:41 -08006370/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006371 * This function decides if an error message should be suppressed.
6372 * It will never be called with a severity level of ERR_FATAL or
6373 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08006374 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006375static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006376{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006377 /*
6378 * If we're in a dead branch of IF or something like it, ignore the error.
6379 * However, because %else etc are evaluated in the state context
6380 * of the previous branch, errors might get lost:
6381 * %if 0 ... %else trailing garbage ... %endif
6382 * So %else etc should set the ERR_PP_PRECOND flag.
6383 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006384 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08006385 ((severity & ERR_PP_PRECOND) ?
6386 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07006387 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006388 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006389
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006390 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006391}
6392
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006393static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006394stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006395{
6396 (void)s;
6397 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006398 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006399
H. Peter Anvin8571f062019-09-23 16:40:03 -07006400 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006401}
6402
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006403static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006404stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006405{
6406 (void)s;
6407 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006408 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006409
H. Peter Anvin8571f062019-09-23 16:40:03 -07006410 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006411}
6412
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006413static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006414stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006415{
6416 (void)s;
6417 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006418 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006419
H. Peter Anvin8571f062019-09-23 16:40:03 -07006420 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006421}
6422
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006423static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006424stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006425{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006426 (void)s;
6427 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006428 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006429
6430 switch (globalbits) {
6431 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006432 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006433 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006434 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006435 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006436 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006437 default:
6438 panic();
6439 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006440}
6441
H. Peter Anvin8b262472019-02-26 14:00:54 -08006442/* Add magic standard macros */
6443struct magic_macros {
6444 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006445 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006446 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006447};
6448static const struct magic_macros magic_macros[] =
6449{
H. Peter Anvind2354082019-08-27 16:38:48 -07006450 { "__?FILE?__", 0, stdmac_file },
6451 { "__?LINE?__", 0, stdmac_line },
6452 { "__?BITS?__", 0, stdmac_bits },
6453 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006454 { NULL, 0, NULL }
6455};
6456
6457static void pp_add_magic_stdmac(void)
6458{
6459 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006460 SMacro tmpl;
6461
6462 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006463
6464 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006465 tmpl.nparam = m->nparam;
6466 tmpl.expand = m->func;
6467 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006468 }
6469}
6470
H. Peter Anvin734b1882002-04-30 21:01:08 +00006471static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006472pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006473{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006474 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006475 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006476
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006477 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006478 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006479 nested_mac_count = 0;
6480 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006481 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006482 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006483 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006484 pp_mode = mode;
H. Peter Anvin (Intel)ee0e3ec2020-06-08 19:01:48 -07006485
6486 /* Reset options to default */
6487 nasm_zero(ppopt);
H. Peter Anvinf7606612016-07-13 14:23:48 -07006488
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006489 if (!use_loaded)
6490 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6491 memset(use_loaded, 0, use_package_count * sizeof(bool));
6492
H. Peter Anvin6686de22019-08-10 05:33:14 -07006493 /* First set up the top level input file */
6494 nasm_new(istk);
6495 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006496 if (!istk->fp) {
6497 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'%s%s",
6498 file, errno ? " " : "", errno ? strerror(errno) : "");
6499 }
H. Peter Anvin6686de22019-08-10 05:33:14 -07006500 src_set(0, file);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006501 istk->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006502 istk->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006503
6504 strlist_add(deplist, file);
6505
6506 /*
6507 * Set up the stdmac packages as a virtual include file,
6508 * indicated by a null file pointer.
6509 */
6510 nasm_new(inc);
6511 inc->next = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006512 src_set(0, NULL);
6513 inc->where = src_where();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006514 inc->nolist = !list_option('b');
6515 istk = inc;
6516 lfmt->uplevel(LIST_INCLUDE, 0);
6517
H. Peter Anvin8b262472019-02-26 14:00:54 -08006518 pp_add_magic_stdmac();
6519
H. Peter Anvinf7606612016-07-13 14:23:48 -07006520 if (tasm_compatible_mode)
6521 pp_add_stdmac(nasm_stdmac_tasm);
6522
6523 pp_add_stdmac(nasm_stdmac_nasm);
6524 pp_add_stdmac(nasm_stdmac_version);
6525
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006526 if (extrastdmac)
6527 pp_add_stdmac(extrastdmac);
6528
H. Peter Anvinf7606612016-07-13 14:23:48 -07006529 stdmacpos = stdmacros[0];
6530 stdmacnext = &stdmacros[1];
6531
H. Peter Anvind2456592008-06-19 15:04:18 -07006532 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006533
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006534 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006535 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006536 * other builtins, because it is special -- it varies between
6537 * passes -- but there is really no particular reason to make it
6538 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006539 *
6540 * 0 = dependencies only
6541 * 1 = preparatory passes
6542 * 2 = final pass
6543 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006544 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006545 switch (mode) {
6546 case PP_NORMAL:
6547 apass = pass_final() ? 2 : 1;
6548 break;
6549 case PP_DEPS:
6550 apass = 0;
6551 break;
6552 case PP_PREPROC:
6553 apass = 3;
6554 break;
6555 default:
6556 panic();
6557 }
6558
H. Peter Anvin8571f062019-09-23 16:40:03 -07006559 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006560}
6561
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006562static void pp_init(void)
6563{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006564}
6565
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006566/*
6567 * Get a line of tokens. If we popped the macro expansion/include stack,
6568 * we return a pointer to the dummy token tok_pop; at that point if
6569 * istk is NULL then we have reached end of input;
6570 */
6571static Token tok_pop; /* Dummy token placeholder */
6572
6573static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006574{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006575 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006576 Line *l = istk->expansion;
6577 Token *tline = NULL;
6578 Token *dtline;
6579
H. Peter Anvine2c80182005-01-15 22:15:51 +00006580 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006581 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006582 * buffer or from the input file.
6583 */
6584 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006585 while (l && l->finishes) {
6586 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006587
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006588 nasm_assert(fm == istk->mstk.mstk);
6589
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006590 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006591 /*
6592 * This is a macro-end marker for a macro with no
6593 * name, which means it's not really a macro at all
6594 * but a %rep block, and the `in_progress' field is
6595 * more than 1, meaning that we still need to
6596 * repeat. (1 means the natural last repetition; 0
6597 * means termination by %exitrep.) We have
6598 * therefore expanded up to the %endrep, and must
6599 * push the whole block on to the expansion buffer
6600 * again. We don't bother to remove the macro-end
6601 * marker: we'd only have to generate another one
6602 * if we did.
6603 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006604 fm->in_progress--;
6605 list_for_each(l, fm->expansion) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006606 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006607
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006608 nasm_new(ll);
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006609 ll->next = istk->expansion;
6610 ll->first = dup_tlist(l->first, NULL);
6611 ll->where = l->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006612 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006613 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006614 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006615 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006616 MMacro *m = istk->mstk.mstk;
6617
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006618 /*
6619 * Check whether a `%rep' was started and not ended
6620 * within this macro expansion. This can happen and
6621 * should be detected. It's a fatal error because
6622 * I'm too confused to work out how to recover
6623 * sensibly from it.
6624 */
6625 if (defining) {
6626 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006627 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006628 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006629 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006630 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006631 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006632
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006633 /*
6634 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006635 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006636 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006637 istk->mstk = m->mstk;
6638 if (m->name) {
6639 /*
6640 * This was a real macro call, not a %rep, and
6641 * therefore the parameter information needs to
6642 * be freed and the iteration count/nesting
6643 * depth adjusted.
6644 */
6645
6646 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006647 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006648 * If all mmacro processing done,
6649 * clear all counters and the deadman
6650 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006651 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006652 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006653 }
6654
Adam Majer91e72402017-07-25 10:42:01 +02006655#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006656 if (m->prev) {
6657 pop_mmacro(m);
6658 fm->in_progress --;
6659 } else
Adam Majer91e72402017-07-25 10:42:01 +02006660#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006661 {
6662 nasm_free(m->params);
6663 free_tlist(m->iline);
6664 nasm_free(m->paramlen);
6665 fm->in_progress = 0;
6666 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006667 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006668
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006669 if (fm->nolist & NL_LIST) {
6670 istk->nolist--;
6671 } else if (!istk->nolist) {
6672 lfmt->downlevel(LIST_MACRO);
6673 }
6674
6675 if (fm->nolist & NL_LINE) {
6676 istk->noline--;
6677 } else if (!istk->noline) {
6678 if (fm == src_macro_current())
6679 src_macro_pop();
6680 src_update(l->where);
6681 }
6682
6683 istk->where = l->where;
6684
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006685 /*
6686 * FIXME It is incorrect to always free_mmacro here.
6687 * It leads to usage-after-free.
6688 *
6689 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6690 */
6691#if 0
6692 else
6693 free_mmacro(m);
6694#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006695 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006696 istk->expansion = l->next;
6697 nasm_free(l);
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006698
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006699 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006700 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006701
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006702 do { /* until we get a line we can use */
6703 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006704
6705 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006706 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006707
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006708 istk->expansion = l->next;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006709 istk->where = l->where;
6710 tline = l->first;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006711 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006712
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07006713 if (!istk->noline)
6714 src_update(istk->where);
6715
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006716 if (!istk->nolist) {
6717 line = detoken(tline, false);
6718 lfmt->line(LIST_MACRO, istk->where.lineno, line);
6719 nasm_free(line);
6720 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006721 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006722 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006723 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006724 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006725 } else {
6726 /*
6727 * The current file has ended; work down the istk
6728 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006729 Include *i = istk;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006730 Include *is;
6731
H. Peter Anvin6686de22019-08-10 05:33:14 -07006732 if (i->fp)
6733 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006734 if (i->conds) {
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006735 /* nasm_fatal can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006736 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006737 }
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006738
6739 list_for_each(is, i->next) {
6740 if (is->fp) {
6741 lfmt->downlevel(LIST_INCLUDE);
6742 src_update(is->where);
6743 break;
6744 }
6745 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006746 istk = i->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006747 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006748 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006749 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006750 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006751
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006752 /*
6753 * We must expand MMacro parameters and MMacro-local labels
6754 * _before_ we plunge into directive processing, to cope
6755 * with things like `%define something %1' such as STRUC
6756 * uses. Unless we're _defining_ a MMacro, in which case
6757 * those tokens should be left alone to go into the
6758 * definition; and unless we're in a non-emitting
6759 * condition, in which case we don't want to meddle with
6760 * anything.
6761 */
H. Peter Anvin (Intel)bacf04a2020-06-08 13:29:06 -07006762 if (!defining &&
6763 !(istk->conds && !emitting(istk->conds->state)) &&
6764 !(istk->mstk.mmac && !istk->mstk.mmac->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006765 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006766 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006767
H. Peter Anvine2c80182005-01-15 22:15:51 +00006768 /*
6769 * Check the line to see if it's a preprocessor directive.
6770 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006771 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6772 if (dtline)
6773 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006774 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006775 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006776 * We're defining a multi-line macro. We emit nothing
6777 * at all, and just
6778 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006779 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006780 MMacro *mmac = defining->dstk.mmac;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006781 Line *l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006782
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006783 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006784 l->next = defining->expansion;
6785 l->first = tline;
6786 l->finishes = NULL;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07006787 l->where = istk->where;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006788 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006789
6790 /*
6791 * Remember if this mmacro expansion contains %00:
6792 * if it does, we will have to handle leading labels
6793 * specially.
6794 */
6795 if (mmac) {
6796 const Token *t;
6797 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006798 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006799 mmac->capture_label = true;
6800 }
6801 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006802 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006803 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006804 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006805 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006806 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006807 * directive so we keep our place correctly.
6808 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006809 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006810 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006811 /*
6812 * We're in a %rep block which has been terminated, so
6813 * we're walking through to the %endrep without
6814 * emitting anything. Emit nothing at all, not even a
6815 * blank line: when we emerge from the %rep block we'll
6816 * give a line-number directive so we keep our place
6817 * correctly.
6818 */
6819 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006820 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006821 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006822 if (!expand_mmacro(tline))
6823 return tline;
6824 }
6825 }
6826}
6827
6828static char *pp_getline(void)
6829{
6830 char *line = NULL;
6831 Token *tline;
6832
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006833 while (true) {
6834 tline = pp_tokline();
6835 if (tline == &tok_pop) {
6836 /*
6837 * We popped the macro/include stack. If istk is empty,
6838 * we are at end of input, otherwise just loop back.
6839 */
6840 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006841 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006842 } else {
6843 /*
6844 * De-tokenize the line and emit it.
6845 */
6846 line = detoken(tline, true);
6847 free_tlist(tline);
6848 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006849 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006850 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006851
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006852 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006853 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006854 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006855 nasm_free(buf);
6856 }
6857
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006858 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006859}
6860
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006861static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006862{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006863 if (defining) {
6864 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006865 nasm_nonfatal("end of file while still defining macro `%s'",
6866 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006867 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006868 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006869 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006870
6871 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006872 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006873 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006874
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006875 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006876 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006877 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006878 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006879 Include *i = istk;
6880 istk = istk->next;
6881 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006882 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006883 }
6884 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006885 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006886 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006887}
6888
6889static void pp_cleanup_session(void)
6890{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006891 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006892 free_llist(predef);
6893 predef = NULL;
6894 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006895 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006896}
6897
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006898static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006899{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006900 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006901}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006902
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006903static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006904{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006905 Token *inc, *space, *name;
6906 Line *l;
6907
H. Peter Anvin734b1882002-04-30 21:01:08 +00006908 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006909 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006910 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006911
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006912 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006913 l->next = predef;
6914 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006915 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006916 predef = l;
6917}
6918
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006919static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006920{
6921 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006922 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006923 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006924
6925 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006926 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006927 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006928 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006929 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006930 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006931 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006932 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006933
H. Peter Anvin8571f062019-09-23 16:40:03 -07006934 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006935 if (space->next->type != TOK_PREPROC_ID &&
6936 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006937 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006938
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006939 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006940 l->next = predef;
6941 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006942 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006943 predef = l;
6944}
6945
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006946static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006947{
6948 Token *def, *space;
6949 Line *l;
6950
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006951 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006952 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006953 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006954
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006955 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006956 l->next = predef;
6957 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006958 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006959 predef = l;
6960}
6961
H. Peter Anvin05990342018-06-11 13:32:42 -07006962/* Insert an early preprocessor command that doesn't need special handling */
6963static void pp_pre_command(const char *what, char *string)
6964{
6965 char *cmd;
6966 Token *def, *space;
6967 Line *l;
6968
6969 def = tokenize(string);
6970 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006971 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006972 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6973 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6974 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006975 }
6976
6977 l = nasm_malloc(sizeof(Line));
6978 l->next = predef;
6979 l->first = def;
6980 l->finishes = NULL;
6981 predef = l;
6982}
6983
H. Peter Anvinf7606612016-07-13 14:23:48 -07006984static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006985{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006986 macros_t **mp;
6987
6988 /* Find the end of the list and avoid duplicates */
6989 for (mp = stdmacros; *mp; mp++) {
6990 if (*mp == macros)
6991 return; /* Nothing to do */
6992 }
6993
6994 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6995
6996 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006997}
6998
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006999static void pp_extra_stdmac(macros_t *macros)
7000{
7001 extrastdmac = macros;
7002}
7003
H. Peter Anvin8571f062019-09-23 16:40:03 -07007004/* Create a numeric token */
7005static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00007006{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04007007 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08007008 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07007009 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08007010}
7011
H. Peter Anvin8571f062019-09-23 16:40:03 -07007012/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07007013static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08007014{
H. Peter Anvin8571f062019-09-23 16:40:03 -07007015 char *p = nasm_quote(str, &len);
7016 return new_Token_free(next, TOK_STRING, p, len);
7017}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07007018static Token *make_tok_qstr(Token *next, const char *str)
7019{
7020 return make_tok_qstr_len(next, str, strlen(str));
7021}
H. Peter Anvin8571f062019-09-23 16:40:03 -07007022
7023/* Create a single-character operator token */
7024static Token *make_tok_char(Token *next, char op)
7025{
7026 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
7027 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08007028 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00007029}
7030
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07007031/*
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007032 * Descent the macro hierarchy and display the expansion after
7033 * encountering an error message.
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07007034 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08007035static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007036{
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007037 const MMacro *m;
H. Peter Anvin (Intel)b2927482020-06-14 20:09:11 -07007038
H. Peter Anvinddb29062018-12-11 00:06:29 -08007039 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
H. Peter Anvin (Intel)5b7369d2020-07-05 02:16:13 -07007040
7041 while ((m = src_error_down())) {
7042 nasm_error(severity, "... from macro `%s' defined", m->name);
7043 }
7044
7045 src_error_reset();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007046}
7047
H. Peter Anvine7469712016-02-18 02:20:59 -08007048const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07007049 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007050 pp_reset,
7051 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08007052 pp_cleanup_pass,
7053 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03007054 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04007055 pp_pre_define,
7056 pp_pre_undefine,
7057 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07007058 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07007059 pp_include_path,
7060 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07007061 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00007062};