blob: 790cf73c37b42108d9179c1e845e8083090a3cec [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003 * Copyright 1996-2019 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
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
84typedef struct Line Line;
85typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087
88/*
H. Peter Anvin8571f062019-09-23 16:40:03 -070089 * This is the internal form which we break input lines up into.
90 * Typically stored in linked lists.
91 *
92 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
93 * not necessarily used as-is, but is also used to encode the number
94 * and expansion type of substituted parameter. So in the definition
95 *
96 * %define a(x,=y) ( (x) & ~(y) )
97 *
98 * the token representing `x' will have its type changed to
99 * tok_smac_param(0) but the one representing `y' will be
100 * tok_smac_param(1); see the accessor functions below.
101 *
102 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
103 * which doesn't need quotes around it. Used in the pre-include
104 * mechanism as an alternative to trying to find a sensible type of
105 * quote to use on the filename we were passed.
106 */
107enum pp_token_type {
108 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT,
109 TOK_CORRUPT, /* Token text modified in an unsafe manner, now bogus */
110 TOK_BLOCK, /* Storage block pointer, not a real token */
111 TOK_ID,
112 TOK_PREPROC_ID, TOK_MMACRO_PARAM, TOK_LOCAL_SYMBOL,
113 TOK_LOCAL_MACRO, TOK_ENVIRON, TOK_STRING,
114 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
115 TOK_INTERNAL_STRING,
116 TOK_PREPROC_Q, TOK_PREPROC_QQ,
117 TOK_PASTE, /* %+ */
118 TOK_COND_COMMA, /* %, */
119 TOK_INDIRECT, /* %[...] */
120 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
121 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
122};
123
124static inline enum pp_token_type tok_smac_param(int param)
125{
126 return TOK_SMAC_START_PARAMS + param;
127}
128static int smac_nparam(enum pp_token_type toktype)
129{
130 return toktype - TOK_SMAC_START_PARAMS;
131}
132static bool is_smac_param(enum pp_token_type toktype)
133{
134 return toktype >= TOK_SMAC_START_PARAMS;
135}
136
137#define PP_CONCAT_MASK(x) (1U << (x))
138
139struct tokseq_match {
140 int mask_head;
141 int mask_tail;
142};
143
144/*
145 * This is tuned so struct Token should be 64 bytes on 64-bit
146 * systems and 32 bytes on 32-bit systems. It enables them
147 * to be nicely cache aligned, and the text to still be kept
148 * inline for nearly all tokens.
149 *
150 * We prohibit tokens of length > MAX_TEXT even though
151 * length here is an unsigned int; this avoids problems
152 * if the length is passed through an interface with type "int",
153 * and is absurdly large anyway.
154 *
155 * For the text mode, in pointer mode the pointer is stored at the end
156 * of the union and the pad field is cleared. This allows short tokens
157 * to be unconditionally tested for by only looking at the first text
158 * bytes and not examining the type or len fields.
159 */
160#define INLINE_TEXT (7*sizeof(char *)-sizeof(enum pp_token_type)-sizeof(unsigned int)-1)
161#define MAX_TEXT (INT_MAX-2)
162
163struct Token {
164 Token *next;
165 enum pp_token_type type;
166 unsigned int len;
167 union {
168 char a[INLINE_TEXT+1];
169 struct {
170 char pad[INLINE_TEXT+1 - sizeof(char *)];
171 char *ptr;
172 } p;
173 } text;
174};
175
176/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700177 * Note on the storage of both SMacro and MMacros: the hash table
178 * indexes them case-insensitively, and we then have to go through a
179 * linked list of potential case aliases (and, for MMacros, parameter
180 * ranges); this is to preserve the matching semantics of the earlier
181 * code. If the number of case aliases for a specific macro is a
182 * performance issue, you may want to reconsider your coding style.
183 */
184
185/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700186 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700187 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700188typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700189
190/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 * Store the definition of a single-line macro.
192 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700193enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700194 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700195 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
196 SPARM_STR = 2, /* Convert to quoted string ($) */
197 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
198 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
199};
200
201struct smac_param {
H. Peter Anvin8571f062019-09-23 16:40:03 -0700202 Token name;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700203 enum sparmflags flags;
204};
205
H. Peter Anvine2c80182005-01-15 22:15:51 +0000206struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800207 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800208 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700209 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700210 ExpandSMacro expand;
211 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700212 struct smac_param *params;
213 int nparam;
214 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800215 bool casesense;
216 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700217 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000218};
219
220/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800221 * Store the definition of a multi-line macro. This is also used to
222 * store the interiors of `%rep...%endrep' blocks, which are
223 * effectively self-re-invoking multi-line macros which simply
224 * don't have a name or bother to appear in the hash tables. %rep
225 * blocks are signified by having a NULL `name' field.
226 *
227 * In a MMacro describing a `%rep' block, the `in_progress' field
228 * isn't merely boolean, but gives the number of repeats left to
229 * run.
230 *
231 * The `next' field is used for storing MMacros in hash tables; the
232 * `next_active' field is for stacking them on istk entries.
233 *
234 * When a MMacro is being expanded, `params', `iline', `nparam',
235 * `paramlen', `rotate' and `unique' are local to the invocation.
236 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700237
238/*
239 * Expansion stack. Note that .mmac can point back to the macro itself,
240 * whereas .mstk cannot.
241 */
242struct mstk {
243 MMacro *mstk; /* Any expansion, real macro or not */
244 MMacro *mmac; /* Highest level actual mmacro */
245};
246
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800247struct MMacro {
248 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700249#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800250 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700251#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800252 char *name;
253 int nparam_min, nparam_max;
254 bool casesense;
255 bool plus; /* is the last parameter greedy? */
256 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700257 bool capture_label; /* macro definition has %00; capture label */
258 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800259 int32_t max_depth; /* maximum number of recursive expansions allowed */
260 Token *dlist; /* All defaults as one list */
261 Token **defaults; /* Parameter default pointers */
262 int ndefs; /* number of default parameters */
263 Line *expansion;
264
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700265 struct mstk mstk; /* Macro expansion stack */
266 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800267 Token **params; /* actual parameters */
268 Token *iline; /* invocation line */
269 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700270 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800271 int *paramlen;
272 uint64_t unique;
273 int lineno; /* Current line number on expansion */
274 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700275
H. Peter Anvin274cda82016-05-10 02:56:29 -0700276 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700277 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800278};
279
280
281/* Store the definition of a multi-line macro, as defined in a
282 * previous recursive macro expansion.
283 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700284#if 0
285
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800286struct MMacroInvocation {
287 MMacroInvocation *prev; /* previous invocation */
288 Token **params; /* actual parameters */
289 Token *iline; /* invocation line */
290 unsigned int nparam, rotate;
291 int *paramlen;
292 uint64_t unique;
293 uint64_t condcnt;
294};
295
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700296#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800297
298/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000299 * The context stack is composed of a linked list of these.
300 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000301struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800302 Context *next;
H. Peter Anvin8571f062019-09-23 16:40:03 -0700303 const char *name;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800304 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700305 uint64_t number;
306 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000307};
308
H. Peter Anvin8571f062019-09-23 16:40:03 -0700309
310static inline const char *tok_text(const struct Token *t)
311{
312 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
313}
314
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000315/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700316 * Returns a mutable pointer to the text buffer. The text can be changed,
317 * but the length MUST NOT CHANGE, in either direction; nor is it permitted
318 * to pad with null characters to create an artificially shorter string.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000319 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700320static inline char *tok_text_buf(struct Token *t)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800321{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700322 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800323}
324
H. Peter Anvin8571f062019-09-23 16:40:03 -0700325static inline unsigned int tok_check_len(size_t len)
326{
327 if (unlikely(len > MAX_TEXT))
328 nasm_fatal("impossibly large token");
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400329
H. Peter Anvin8571f062019-09-23 16:40:03 -0700330 return len;
331}
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400332
H. Peter Anvin8571f062019-09-23 16:40:03 -0700333/* strlen() variant useful for set_text() and its variants */
334static size_t tok_strlen(const char *str)
335{
336 return strnlen(str, MAX_TEXT+1);
337}
338
339/*
340 * Set the text field to a copy of the given string; the length if
341 * not given should be obtained with tok_strlen().
342 */
343static Token *set_text(struct Token *t, const char *text, size_t len)
344{
345 char *textp;
346
347 if (t->len > INLINE_TEXT)
348 nasm_free(t->text.p.ptr);
349
350 nasm_zero(t->text.a);
351
352 t->len = tok_check_len(len);
353 textp = (len > INLINE_TEXT)
354 ? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
355 memcpy(textp, text, len+1);
356 return t;
357}
358
359/*
360 * Set the text field to the existing pre-allocated string, either
361 * taking over or freeing the allocation in the process.
362 */
363static Token *set_text_free(struct Token *t, char *text, unsigned int len)
364{
365 if (t->len > INLINE_TEXT)
366 nasm_free(t->text.p.ptr);
367
368 nasm_zero(t->text.a);
369
370 t->len = tok_check_len(len);
371 if (len > INLINE_TEXT) {
372 t->text.p.ptr = text;
373 } else {
374 memcpy(t->text.a, text, len+1);
375 nasm_free(text);
376 }
377
378 return t;
379}
380
381/*
382 * Allocate a new buffer containing a copy of the text field
383 * of the token.
384 */
385static char *dup_text(const struct Token *t)
386{
387 size_t size = t->len + 1;
388 char *p = nasm_malloc(size);
389
390 return memcpy(p, tok_text(t), size);
391}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392
393/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800394 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000395 * these, which is essentially a container to allow several linked
396 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700397 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398 * Note that in this module, linked lists are treated as stacks
399 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800400 * `expansion' field in MMacro structures, so that the linked list,
401 * if walked, would give the macro lines in reverse order; this
402 * means that we can walk the list when expanding a macro, and thus
403 * push the lines on to the `expansion' field in _istk_ in reverse
404 * order (so that when popped back off they are in the right
405 * order). It may seem cockeyed, and it relies on my design having
406 * an even number of steps in, but it works...
407 *
408 * Some of these structures, rather than being actual lines, are
409 * markers delimiting the end of the expansion of a given macro.
410 * This is for use in the cycle-tracking and %rep-handling code.
411 * Such structures have `finishes' non-NULL, and `first' NULL. All
412 * others have `finishes' NULL, but `first' may still be NULL if
413 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800416 Line *next;
417 MMacro *finishes;
418 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500419};
420
421/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000422 * To handle an arbitrary level of file inclusion, we maintain a
423 * stack (ie linked list) of these things.
424 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426 Include *next;
427 FILE *fp;
428 Cond *conds;
429 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700430 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700431 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700432 int lineno, lineinc;
433 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434};
435
436/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700437 * File real name hash, so we don't have to re-search the include
438 * path for every pass (and potentially more than that if a file
439 * is used more than once.)
440 */
441struct hash_table FileHash;
442
443/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700444 * Counters to trap on insane macro recursion or processing.
445 * Note: for smacros these count *down*, for mmacros they count *up*.
446 */
447struct deadman {
448 int64_t total; /* Total number of macros/tokens */
449 int64_t levels; /* Descent depth across all macros */
450 bool triggered; /* Already triggered, no need for error msg */
451};
452
453static struct deadman smacro_deadman, mmacro_deadman;
454
455/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000456 * Conditional assembly: we maintain a separate stack of these for
457 * each level of file inclusion. (The only reason we keep the
458 * stacks separate is to ensure that a stray `%endif' in a file
459 * included from within the true branch of a `%if' won't terminate
460 * it and cause confusion: instead, rightly, it'll cause an error.)
461 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700462enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000463 /*
464 * These states are for use just after %if or %elif: IF_TRUE
465 * means the condition has evaluated to truth so we are
466 * currently emitting, whereas IF_FALSE means we are not
467 * currently emitting but will start doing so if a %else comes
468 * up. In these states, all directives are admissible: %elif,
469 * %else and %endif. (And of course %if.)
470 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800471 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000472 /*
473 * These states come up after a %else: ELSE_TRUE means we're
474 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
475 * any %elif or %else will cause an error.
476 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800477 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000478 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200479 * These states mean that we're not emitting now, and also that
480 * nothing until %endif will be emitted at all. COND_DONE is
481 * used when we've had our moment of emission
482 * and have now started seeing %elifs. COND_NEVER is used when
483 * the condition construct in question is contained within a
484 * non-emitting branch of a larger condition construct,
485 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000486 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800487 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000488};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700489struct Cond {
490 Cond *next;
491 enum cond_state state;
492};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800493#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000494
H. Peter Anvin70653092007-10-19 14:42:29 -0700495/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000496 * These defines are used as the possible return values for do_directive
497 */
498#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300499#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000500
Keith Kanios852f1ee2009-07-12 00:19:55 -0500501/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000502 * Condition codes. Note that we use c_ prefix not C_ because C_ is
503 * used in nasm.h for the "real" condition codes. At _this_ level,
504 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
505 * ones, so we need a different enum...
506 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700507static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000508 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
509 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000510 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000511};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700512enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000513 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
514 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 -0700515 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
516 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000517};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700518static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000519 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
520 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 +0000521 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000522};
523
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800524/*
525 * Directive names.
526 */
527/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
528static int is_condition(enum preproc_token arg)
529{
530 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
531}
532
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000533/* For TASM compatibility we need to be able to recognise TASM compatible
534 * conditional compilation directives. Using the NASM pre-processor does
535 * not work, so we look for them specifically from the following list and
536 * then jam in the equivalent NASM directive into the input stream.
537 */
538
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000540 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
541 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
542};
543
H. Peter Anvin476d2862007-10-02 22:04:15 -0700544static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000545 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
546 "ifndef", "include", "local"
547};
548
549static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700550static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000551static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800552static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000553
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000554static Context *cstk;
555static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300556static const struct strlist *ipath_list;
H. Peter Anvind2354082019-08-27 16:38:48 -0700557static bool do_aliases;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000558
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300559static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300561static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000562
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800563static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700564static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800565static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000566
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800568 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800570static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000571
572/*
573 * The current set of single-line macros we have defined.
574 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700575static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000576
577/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800578 * The multi-line macro we are currently defining, or the %rep
579 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800581static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582
Charles Crayned4200be2008-07-12 16:42:33 -0700583static uint64_t nested_mac_count;
584static uint64_t nested_rep_count;
585
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586/*
587 * The number of macro parameters to allocate space for at a time.
588 */
589#define PARAM_DELTA 16
590
591/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700592 * The standard macro set: defined in macros.c in a set of arrays.
593 * This gives our position in any macro set, while we are processing it.
594 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700596static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700597static macros_t **stdmacnext;
598static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300599static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000600
601/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700602 * Map of which %use packages have been loaded
603 */
604static bool *use_loaded;
605
606/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000607 * Forward declarations.
608 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700609static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000610static Token *expand_mmac_params(Token * tline);
611static Token *expand_smacro(Token * tline);
612static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400613static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700614static Token *make_tok_num(Token *next, int64_t val);
615static Token *make_tok_qstr(Token *next, const char *str);
616static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700617static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700618 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700619static Token *new_Token_free(Token * next, enum pp_token_type type,
620 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700621static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700622static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700623static Token *delete_Token(Token *t);
624static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700625static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700626get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700628/* Safe test for token type, false on x == NULL */
629static inline bool tok_type(const Token *x, enum pp_token_type t)
630{
631 return x && x->type == t;
632}
633
634/* Whitespace token? */
635static inline bool tok_white(const Token *x)
636{
637 return tok_type(x, TOK_WHITESPACE);
638}
639
640/* Skip past any whitespace */
641static inline Token *skip_white(Token *x)
642{
643 while (tok_white(x))
644 x = x->next;
645
646 return x;
647}
648
649/* Delete any whitespace */
650static Token *zap_white(Token *x)
651{
652 while (tok_white(x))
653 x = delete_Token(x);
654
655 return x;
656}
657
H. Peter Anvin8571f062019-09-23 16:40:03 -0700658/*
659 * Single special character tests. The use of & rather than && is intentional; it
660 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
661 * a smart compiler should turn it into a 16-bit memory reference.
662 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700663static inline bool tok_is(const Token *x, char c)
664{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700665 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700666}
667
668/* True if any other kind of token that "c", but not NULL */
669static inline bool tok_isnt(const Token *x, char c)
670{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700671 return x && !((x->text.a[0] == c) & !x->text.a[1]);
672}
673
674/*
675 * Unquote a token if it is a string, and set its type to
676 * TOK_INTERNAL_STRING.
677 */
678static const char *unquote_token(Token *t)
679{
680 if (t->type != TOK_STRING)
681 return tok_text(t);
682
683 t->type = TOK_INTERNAL_STRING;
684
685 if (t->len > INLINE_TEXT) {
686 char *p = t->text.p.ptr;
687
688 t->len = nasm_unquote(p, NULL);
689
690 if (t->len <= INLINE_TEXT) {
691 nasm_zero(t->text.a);
692 memcpy(t->text.a, p, t->len);
693 nasm_free(p);
694 return t->text.a;
695 } else {
696 return p;
697 }
698 } else {
699 t->len = nasm_unquote(t->text.a, NULL);
700 return t->text.a;
701 }
702}
703
704static const char *unquote_token_cstr(Token *t)
705{
706 if (t->type != TOK_STRING)
707 return tok_text(t);
708
709 t->type = TOK_INTERNAL_STRING;
710
711 if (t->len > INLINE_TEXT) {
712 char *p = t->text.p.ptr;
713
714 t->len = nasm_unquote_cstr(p, NULL);
715
716 if (t->len <= INLINE_TEXT) {
717 nasm_zero(t->text.a);
718 memcpy(t->text.a, p, t->len);
719 nasm_free(p);
720 return t->text.a;
721 } else {
722 return p;
723 }
724 } else {
725 t->len = nasm_unquote_cstr(t->text.a, NULL);
726 return t->text.a;
727 }
728}
729
730static Token *quote_internal_string_token(Token *t)
731{
732 size_t len;
733 char *p;
734
735 p = nasm_quote(tok_text(t), &len);
736 t->type = TOK_STRING;
737 return set_text_free(t, p, len);
738}
739
740static inline Token *quote_token(Token *t)
741{
742 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
743 return t;
744
745 return quote_internal_string_token(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700746}
H. Peter Anvin76690a12002-04-30 20:52:49 +0000747
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400748/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700749 * In-place reverse a list of tokens.
750 */
751static Token *reverse_tokens(Token *t)
752{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800753 Token *prev = NULL;
754 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700755
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800756 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400757 next = t->next;
758 t->next = prev;
759 prev = t;
760 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800761 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700762
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800763 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700764}
765
766/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700767 * getenv() variant operating on an input token
768 */
769static const char *pp_getenv(const Token *t, bool warn)
770{
771 const char *txt = tok_text(t);
772 const char *v;
773 char *buf = NULL;
774 bool is_string = false;
775
776 if (!t)
777 return NULL;
778
779 switch (t->type) {
780 case TOK_ENVIRON:
781 txt += 2; /* Skip leading %! */
782 is_string = nasm_isquote(*txt);
783 break;
784
785 case TOK_STRING:
786 is_string = true;
787 break;
788
789 case TOK_INTERNAL_STRING:
790 case TOK_ID:
791 is_string = false;
792 break;
793
794 default:
795 return NULL;
796 }
797
798 if (is_string) {
799 buf = nasm_strdup(txt);
800 nasm_unquote_cstr(buf, NULL);
801 txt = buf;
802 }
803
804 v = getenv(txt);
805 if (warn && !v) {
806 /*!
807 *!environment [on] nonexistent environment variable
808 *! warns if a nonexistent environment variable
809 *! is accessed using the \c{%!} preprocessor
810 *! construct (see \k{getenv}.) Such environment
811 *! variables are treated as empty (with this
812 *! warning issued) starting in NASM 2.15;
813 *! earlier versions of NASM would treat this as
814 *! an error.
815 */
816 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
817 v = "";
818 }
819
820 if (buf)
821 nasm_free(buf);
822
823 return v;
824}
825
826/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300827 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000828 * front of them. We do it here because I could not find any other
829 * place to do it for the moment, and it is a hack (ideally it would
830 * be nice to be able to use the NASM pre-processor to do it).
831 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000832static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000833{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000834 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400835 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000836
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400837 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000838
839 /* Binary search for the directive name */
840 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400841 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400842 q = nasm_skip_word(p);
843 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 if (len) {
845 oldchar = p[len];
846 p[len] = 0;
847 while (j - i > 1) {
848 k = (j + i) / 2;
849 m = nasm_stricmp(p, tasm_directives[k]);
850 if (m == 0) {
851 /* We have found a directive, so jam a % in front of it
852 * so that NASM will then recognise it as one if it's own.
853 */
854 p[len] = oldchar;
855 len = strlen(p);
856 oldline = line;
857 line = nasm_malloc(len + 2);
858 line[0] = '%';
859 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700860 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300861 * NASM does not recognise IFDIFI, so we convert
862 * it to %if 0. This is not used in NASM
863 * compatible code, but does need to parse for the
864 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700866 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 } else {
868 memcpy(line + 1, p, len + 1);
869 }
870 nasm_free(oldline);
871 return line;
872 } else if (m < 0) {
873 j = k;
874 } else
875 i = k;
876 }
877 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000878 }
879 return line;
880}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000881
H. Peter Anvin76690a12002-04-30 20:52:49 +0000882/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000883 * The pre-preprocessing stage... This function translates line
884 * number indications as they emerge from GNU cpp (`# lineno "file"
885 * flags') into NASM preprocessor line number indications (`%line
886 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000887 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000888static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000889{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000890 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000891 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 if (line[0] == '#' && line[1] == ' ') {
894 oldline = line;
895 fname = oldline + 2;
896 lineno = atoi(fname);
897 fname += strspn(fname, "0123456789 ");
898 if (*fname == '"')
899 fname++;
900 fnlen = strcspn(fname, "\"");
901 line = nasm_malloc(20 + fnlen);
902 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
903 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000904 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000905 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000907 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000908}
909
910/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000911 * Free a linked list of tokens.
912 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000914{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400915 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000917}
918
919/*
920 * Free a linked list of lines.
921 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000923{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400924 Line *l, *tmp;
925 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 free_tlist(l->first);
927 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000928 }
929}
930
931/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700932 * Free an array of linked lists of tokens
933 */
934static void free_tlist_array(Token **array, size_t nlists)
935{
936 Token **listp = array;
937
938 while (nlists--)
939 free_tlist(*listp++);
940
941 nasm_free(array);
942}
943
944/*
945 * Duplicate a linked list of tokens.
946 */
947static Token *dup_tlist(const Token *list, Token ***tailp)
948{
949 Token *newlist = NULL;
950 Token **tailpp = &newlist;
951 const Token *t;
952
953 list_for_each(t, list) {
954 Token *nt;
955 *tailpp = nt = dup_Token(NULL, t);
956 tailpp = &nt->next;
957 }
958
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700959 if (tailp) {
960 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700961 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700962 }
963
964 return newlist;
965}
966
967/*
968 * Duplicate a linked list of tokens with a maximum count
969 */
970static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
971{
972 Token *newlist = NULL;
973 Token **tailpp = &newlist;
974 const Token *t;
975
976 list_for_each(t, list) {
977 Token *nt;
978 if (!cnt--)
979 break;
980 *tailpp = nt = dup_Token(NULL, t);
981 tailpp = &nt->next;
982 }
983
984 if (tailp) {
985 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -0700986 if (newlist)
987 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700988 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700989
990 return newlist;
991}
992
993/*
994 * Duplicate a linked list of tokens in reverse order
995 */
996static Token *dup_tlist_reverse(const Token *list, Token *tail)
997{
998 const Token *t;
999
1000 list_for_each(t, list)
1001 tail = dup_Token(tail, t);
1002
1003 return tail;
1004}
1005
1006/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001007 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001008 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001009static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001010{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001011 nasm_free(m->name);
1012 free_tlist(m->dlist);
1013 nasm_free(m->defaults);
1014 free_llist(m->expansion);
1015 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001016}
1017
1018/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001019 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001020 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001021static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001022{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001023 if (s->params) {
1024 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001025 for (i = 0; i < s->nparam; i++) {
1026 if (s->params[i].name.len > INLINE_TEXT)
1027 nasm_free(s->params[i].name.text.p.ptr);
1028 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001029 nasm_free(s->params);
1030 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001031 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001032 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001033}
1034
1035static void clear_smacro(SMacro *s)
1036{
1037 free_smacro_members(s);
1038 /* Wipe everything except the next pointer */
1039 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1040}
1041
1042/*
1043 * Free an SMacro
1044 */
1045static void free_smacro(SMacro *s)
1046{
1047 free_smacro_members(s);
1048 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001049}
1050
1051/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001052 * Free all currently defined macros, and free the hash tables
1053 */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001054static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001055{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001056 struct hash_iterator it;
1057 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001058
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001059 hash_for_each(smt, it, np) {
1060 SMacro *tmp;
1061 SMacro *s = np->data;
1062 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001063 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001064 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001065 }
H. Peter Anvin072771e2008-05-22 13:17:51 -07001066 hash_free(smt);
1067}
1068
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001069static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001070{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001071 struct hash_iterator it;
1072 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001073
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001074 hash_for_each(mmt, it, np) {
1075 MMacro *tmp;
1076 MMacro *m = np->data;
1077 nasm_free((void *)np->key);
1078 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001079 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001080 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001081 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001082}
1083
1084static void free_macros(void)
1085{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001086 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001087 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001088}
1089
1090/*
1091 * Initialize the hash tables
1092 */
1093static void init_macros(void)
1094{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001095}
1096
1097/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001098 * Pop the context stack.
1099 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001101{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001102 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001103
1104 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001105 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001106 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001107 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001108}
1109
H. Peter Anvin072771e2008-05-22 13:17:51 -07001110/*
1111 * Search for a key in the hash index; adding it if necessary
1112 * (in which case we initialize the data pointer to NULL.)
1113 */
1114static void **
1115hash_findi_add(struct hash_table *hash, const char *str)
1116{
1117 struct hash_insert hi;
1118 void **r;
1119 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001120 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001121
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001122 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001123 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001124 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001125
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001126 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1127 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001128 return hash_add(&hi, strx, NULL);
1129}
1130
1131/*
1132 * Like hash_findi, but returns the data element rather than a pointer
1133 * to it. Used only when not adding a new element, hence no third
1134 * argument.
1135 */
1136static void *
1137hash_findix(struct hash_table *hash, const char *str)
1138{
1139 void **p;
1140
1141 p = hash_findi(hash, str, NULL);
1142 return p ? *p : NULL;
1143}
1144
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001145/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001146 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001147 * if there no more left -- return NULL
1148 */
1149static char *line_from_stdmac(void)
1150{
1151 unsigned char c;
1152 const unsigned char *p = stdmacpos;
1153 char *line, *q;
1154 size_t len = 0;
1155
1156 if (!stdmacpos)
1157 return NULL;
1158
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001159 /*
1160 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1161 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1162 */
1163 while ((c = *p++) != 127) {
1164 uint8_t ndir = c - 128;
1165 if (ndir < 256-96)
1166 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001167 else
1168 len++;
1169 }
1170
1171 line = nasm_malloc(len + 1);
1172 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001173
1174 while ((c = *stdmacpos++) != 127) {
1175 uint8_t ndir = c - 128;
1176 if (ndir < 256-96) {
1177 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1178 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001179 *q++ = ' ';
1180 } else {
1181 *q++ = c;
1182 }
1183 }
1184 stdmacpos = p;
1185 *q = '\0';
1186
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001187 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001188 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001189 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001190 if (*stdmacnext) {
1191 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001192 } else if (do_predef) {
1193 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001194
1195 /*
1196 * Nasty hack: here we push the contents of
1197 * `predef' on to the top-level expansion stack,
1198 * since this is the most convenient way to
1199 * implement the pre-include and pre-define
1200 * features.
1201 */
1202 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001203 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001204 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001205 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001206 l->finishes = NULL;
1207
1208 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001209 }
1210 do_predef = false;
1211 }
1212 }
1213
1214 return line;
1215}
1216
H. Peter Anvin6686de22019-08-10 05:33:14 -07001217/*
1218 * Read a line from a file. Return NULL on end of file.
1219 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001220static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001221{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001222 int c;
1223 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001224 const unsigned int delta = 512;
1225 const unsigned int pad = 8;
1226 unsigned int nr_cont = 0;
1227 bool cont = false;
1228 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001229 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001231 size = delta;
1232 p = buffer = nasm_malloc(size);
1233
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001234 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001235 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001236
1237 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001238 case EOF:
1239 if (p == buffer) {
1240 nasm_free(buffer);
1241 return NULL;
1242 }
1243 c = 0;
1244 break;
1245
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001246 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001247 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001248 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001249 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001250 if (cont) {
1251 cont = false;
1252 continue;
1253 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001254 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001255 break;
1256
1257 case '\n':
1258 if (cont) {
1259 cont = false;
1260 continue;
1261 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001262 c = 0;
1263 break;
1264
1265 case 032: /* ^Z = legacy MS-DOS end of file mark */
1266 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001267 break;
1268
1269 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001270 next = fgetc(f);
1271 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001272 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001273 cont = true;
1274 nr_cont++;
1275 continue;
1276 }
1277 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001279
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001280 if (p >= (buffer + size - pad)) {
1281 buffer = nasm_realloc(buffer, size + delta);
1282 p = buffer + size - pad;
1283 size += delta;
1284 }
1285
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001286 *p++ = c;
1287 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001288
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001289 lineno = src_get_linnum() + istk->lineinc +
1290 (nr_cont * istk->lineinc);
1291 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001292
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001293 return buffer;
1294}
1295
1296/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001297 * Common read routine regardless of source
1298 */
1299static char *read_line(void)
1300{
1301 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001302 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001303
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001304 if (f)
1305 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001306 else
1307 line = line_from_stdmac();
1308
1309 if (!line)
1310 return NULL;
1311
1312 if (!istk->nolist)
1313 lfmt->line(LIST_READ, src_get_linnum(), line);
1314
1315 return line;
1316}
1317
1318/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001319 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001320 * don't need to parse the value out of e.g. numeric tokens: we
1321 * simply split one string into many.
1322 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001323static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001324{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001325 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001326 Token *list = NULL;
1327 Token *t, **tail = &list;
1328
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001330 const char *p = line;
1331 const char *ep = NULL; /* End of token, for trimming the end */
1332 size_t toklen;
1333 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001334
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001336 /*
1337 * Preprocessor construct; find the end of the token.
1338 * Classification is handled later, because %{...} can be
1339 * used to create any preprocessor token.
1340 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001342 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001343 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001344 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001345 } else if (nasm_isdigit(*p) ||
1346 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 do {
1348 p++;
1349 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001350 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001351 } else if (*p == '{' || *p == '[') {
1352 /* %{...} or %[...] */
1353 char firstchar = *p;
1354 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001356 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1357 while (lvl) {
1358 if (*p == firstchar) {
1359 lvl++;
1360 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001361 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001362 } else if (nasm_isquote(*p)) {
1363 p = nasm_skip_string(p);
1364 }
1365
1366 /*
1367 * *p can have been advanced to a null character by
1368 * nasm_skip_string()
1369 */
1370 if (!*p) {
1371 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1372 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 break;
1374 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001375 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001376 }
1377 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001378 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001379 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001381 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001382 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001383 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001384 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001385 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001386 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001387 do {
1388 p++;
1389 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001390 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001391 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001392 p = nasm_skip_string(p);
1393 if (*p)
1394 p++;
1395 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001396 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001397 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001398 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001399 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001400 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001401 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001402 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001403 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001404 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1405 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 do {
1407 p++;
1408 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001409 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001410 } else if (*p == '%') {
1411 /* %% operator */
1412 p++;
1413 }
1414
1415 if (!ep)
1416 ep = p;
1417 toklen = ep - line;
1418
1419 /* Classify here, to handle %{...} correctly */
1420 if (toklen < 2) {
1421 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001423 char c0 = line[1];
1424
1425 switch (c0) {
1426 case '+':
1427 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1428 break;
1429
1430 case '-':
1431 type = TOK_MMACRO_PARAM;
1432 break;
1433
1434 case '?':
1435 if (toklen == 2)
1436 type = TOK_PREPROC_Q;
1437 else if (toklen == 3 && line[2] == '?')
1438 type = TOK_PREPROC_QQ;
1439 else
1440 type = TOK_PREPROC_ID;
1441 break;
1442
1443 case '!':
1444 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1445 break;
1446
1447 case '%':
1448 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1449 break;
1450
1451 case '$':
1452 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1453 break;
1454
1455 case '[':
1456 line += 2; /* Skip %[ */
1457 firstchar = *line; /* Don't clobber */
1458 toklen -= 2;
1459 type = TOK_INDIRECT;
1460 break;
1461
1462 case ',':
1463 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1464 break;
1465
1466 case '\'':
1467 case '\"':
1468 case '`':
1469 /* %{'string'} */
1470 type = TOK_PREPROC_ID;
1471 break;
1472
1473 case ':':
1474 type = TOK_MMACRO_PARAM; /* %{:..} */
1475 break;
1476
1477 default:
1478 if (nasm_isdigit(c0))
1479 type = TOK_MMACRO_PARAM;
1480 else if (nasm_isidchar(c0) || toklen > 2)
1481 type = TOK_PREPROC_ID;
1482 else
1483 type = TOK_OTHER;
1484 break;
1485 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001487 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001488 /*
1489 * An identifier. This includes the ? operator, which is
1490 * treated as a keyword, not as a special character
1491 * operator
1492 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001494 while (nasm_isidchar(*++p))
1495 ;
1496 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 /*
1498 * A string token.
1499 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001501 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001502
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 if (*p) {
1504 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001505 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001506 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 /* Handling unterminated strings by UNV */
1508 /* type = -1; */
1509 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001510 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001511 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001512 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001513 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001514 bool is_hex = false;
1515 bool is_float = false;
1516 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001517 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001518
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001520 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001522
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001523 if (*p == '$') {
1524 p++;
1525 is_hex = true;
1526 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001527
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 for (;;) {
1529 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001530
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001531 if (!is_hex && (c == 'e' || c == 'E')) {
1532 has_e = true;
1533 if (*p == '+' || *p == '-') {
1534 /*
1535 * e can only be followed by +/- if it is either a
1536 * prefixed hex number or a floating-point number
1537 */
1538 p++;
1539 is_float = true;
1540 }
1541 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1542 is_hex = true;
1543 } else if (c == 'P' || c == 'p') {
1544 is_float = true;
1545 if (*p == '+' || *p == '-')
1546 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001547 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 ; /* just advance */
1549 else if (c == '.') {
1550 /*
1551 * we need to deal with consequences of the legacy
1552 * parser, like "1.nolist" being two tokens
1553 * (TOK_NUMBER, TOK_ID) here; at least give it
1554 * a shot for now. In the future, we probably need
1555 * a flex-based scanner with proper pattern matching
1556 * to do it as well as it can be done. Nothing in
1557 * the world is going to help the person who wants
1558 * 0x123.p16 interpreted as two tokens, though.
1559 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001560 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001561 while (*r == '_')
1562 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001563
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001564 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1565 (!is_hex && (*r == 'e' || *r == 'E')) ||
1566 (*r == 'p' || *r == 'P')) {
1567 p = r;
1568 is_float = true;
1569 } else
1570 break; /* Terminate the token */
1571 } else
1572 break;
1573 }
1574 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001575
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001576 if (p == line+1 && *line == '$') {
1577 type = TOK_OTHER; /* TOKEN_HERE */
1578 } else {
1579 if (has_e && !is_hex) {
1580 /* 1e13 is floating-point, but 1e13h is not */
1581 is_float = true;
1582 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001583
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001584 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1585 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001586 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001588 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 /*
1590 * Whitespace just before end-of-line is discarded by
1591 * pretending it's a comment; whitespace just before a
1592 * comment gets lumped into the comment.
1593 */
1594 if (!*p || *p == ';') {
1595 type = TOK_COMMENT;
1596 while (*p)
1597 p++;
1598 }
1599 } else if (*p == ';') {
1600 type = TOK_COMMENT;
1601 while (*p)
1602 p++;
1603 } else {
1604 /*
1605 * Anything else is an operator of some kind. We check
1606 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001607 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1608 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001609 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 */
1611 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001612 switch (*p++) {
1613 case '>':
1614 if (*p == '>') {
1615 p++;
1616 if (*p == '>')
1617 p++;
1618 }
1619 break;
1620
1621 case '<':
1622 if (*p == '<') {
1623 p++;
1624 if (*p == '<')
1625 p++;
1626 } else if (*p == '=') {
1627 p++;
1628 if (*p == '>')
1629 p++;
1630 } else if (*p == '>') {
1631 p++;
1632 }
1633 break;
1634
1635 case '!':
1636 if (*p == '=')
1637 p++;
1638 break;
1639
1640 case '/':
1641 case '=':
1642 case '&':
1643 case '|':
1644 case '^':
1645 /* These operators can be doubled but nothing else */
1646 if (*p == p[-1])
1647 p++;
1648 break;
1649
1650 default:
1651 break;
1652 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001654
H. Peter Anvin8571f062019-09-23 16:40:03 -07001655 if (type == TOK_WHITESPACE) {
1656 *tail = t = new_White(NULL);
1657 tail = &t->next;
1658 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001659 if (!ep)
1660 ep = p;
1661 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001662 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 tail = &t->next;
1664 }
1665 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001666 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001667 return list;
1668}
1669
H. Peter Anvin8571f062019-09-23 16:40:03 -07001670/*
1671 * Tokens are allocated in blocks to improve speed. Set the blocksize
1672 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1673 *
1674 * alloc_Token() returns a zero-initialized token structure.
1675 */
1676#define TOKEN_BLOCKSIZE 4096
1677
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001678#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001679
1680static Token *freeTokens = NULL;
1681static Token *tokenblocks = NULL;
1682
1683static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001684{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001685 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686
H. Peter Anvin8571f062019-09-23 16:40:03 -07001687 if (unlikely(!t)) {
1688 Token *block;
1689 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690
H. Peter Anvin8571f062019-09-23 16:40:03 -07001691 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001692
H. Peter Anvin8571f062019-09-23 16:40:03 -07001693 /*
1694 * The first entry in each array are a linked list of
1695 * block allocations and is not used for data.
1696 */
1697 block[0].next = tokenblocks;
1698 block[0].type = TOK_BLOCK;
1699 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001700
H. Peter Anvin8571f062019-09-23 16:40:03 -07001701 /*
1702 * Add the rest to the free list
1703 */
1704 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1705 block[i].next = &block[i+1];
1706
1707 freeTokens = &block[2];
1708
1709 /*
1710 * Return the topmost usable token
1711 */
1712 return &block[1];
1713 }
1714
1715 freeTokens = t->next;
1716 t->next = NULL;
1717 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001718}
1719
H. Peter Anvin8571f062019-09-23 16:40:03 -07001720static Token *delete_Token(Token *t)
1721{
1722 Token *next = t->next;
1723
1724 nasm_zero(*t);
1725 t->next = freeTokens;
1726 freeTokens = t;
1727
1728 return next;
1729}
1730
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001732{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001733 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001734
H. Peter Anvin8571f062019-09-23 16:40:03 -07001735 list_for_each_safe(block, blocktmp, tokenblocks)
1736 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001737
H. Peter Anvin8571f062019-09-23 16:40:03 -07001738 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001740
H. Peter Anvin8571f062019-09-23 16:40:03 -07001741#else
1742
1743static inline Token *alloc_Token(void)
1744{
1745 Token *t;
1746 nasm_new(*t);
1747 return t;
1748}
1749
1750static Token *delete_Token(Token *t)
1751{
1752 Token *next = t->next;
1753 nasm_free(t);
1754 return next;
1755}
1756
1757static inline void delete_Blocks(void)
1758{
1759 /* Nothing to do */
1760}
1761
1762#endif
1763
H. Peter Anvin734b1882002-04-30 21:01:08 +00001764/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001765 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001766 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001767 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001768static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001769 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001770{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001771 Token *t = alloc_Token();
1772 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001773
H. Peter Anvin734b1882002-04-30 21:01:08 +00001774 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001775 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001776 if (type == TOK_WHITESPACE) {
1777 t->len = 1;
1778 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001780 if (text && text[0] && !txtlen)
1781 txtlen = tok_strlen(text);
1782
1783 t->len = tok_check_len(txtlen);
1784
1785 if (text) {
1786 textp = (txtlen > INLINE_TEXT)
1787 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1788 memcpy(textp, text, txtlen);
1789 textp[txtlen] = '\0'; /* In case we needed malloc() */
1790 } else {
1791 /*
1792 * Allocate a buffer but do not fill it. The caller
1793 * can fill in text, but must not change the length.
1794 * The filled in text must be exactly txtlen once
1795 * the buffer is filled and before the token is added
1796 * to any line lists.
1797 */
1798 if (txtlen > INLINE_TEXT)
1799 t->text.p.ptr = nasm_zalloc(txtlen+1);
1800 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001801 }
1802 return t;
1803}
1804
H. Peter Anvin8571f062019-09-23 16:40:03 -07001805/*
1806 * Same as new_Token(), but text belongs to the new token and is
1807 * either taken over or freed. This function MUST be called
1808 * with valid txt and txtlen, unlike new_Token().
1809 */
1810static Token *new_Token_free(Token * next, enum pp_token_type type,
1811 char *text, size_t txtlen)
1812{
1813 Token *t = alloc_Token();
1814
1815 t->next = next;
1816 t->type = type;
1817 t->len = tok_check_len(txtlen);
1818
1819 if (txtlen <= INLINE_TEXT) {
1820 memcpy(t->text.a, text, txtlen);
1821 free(text);
1822 } else {
1823 t->text.p.ptr = text;
1824 }
1825
1826 return t;
1827}
1828
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001829static Token *dup_Token(Token *next, const Token *src)
1830{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001831 Token *t = alloc_Token();
1832
1833 memcpy(t, src, sizeof *src);
1834 t->next = next;
1835
1836 if (t->len > INLINE_TEXT) {
1837 t->text.p.ptr = nasm_malloc(t->len + 1);
1838 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1839 }
1840
1841 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001842}
1843
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001844static Token *new_White(Token *next)
1845{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001846 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001847
H. Peter Anvin8571f062019-09-23 16:40:03 -07001848 t->next = next;
1849 t->type = TOK_WHITESPACE;
1850 t->len = 1;
1851 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001852
H. Peter Anvin8571f062019-09-23 16:40:03 -07001853 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001854}
1855
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001856/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001857 * This *transfers* the content from one token to another, leaving the
1858 * next pointer of the latter intact. Unlike dup_Token(), the old
1859 * token is destroyed, except for its next pointer, and the text
1860 * pointer allocation, if any, is simply transferred.
1861 */
1862static Token *steal_Token(Token *dst, Token *src)
1863{
1864 /* Overwrite everything except the next pointers */
1865 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1866 sizeof(Token) - sizeof(Token *));
1867
1868 /* Clear the donor token */
1869 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1870
1871 return dst;
1872}
1873
1874/*
1875 * Convert a line of tokens back into text. This modifies the list
1876 * by expanding environment variables.
1877 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001878 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001879 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001880 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001881static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001882{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001883 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001884 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001885 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001886
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001887 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001888 switch (t->type) {
1889 case TOK_ENVIRON:
1890 {
1891 const char *v = pp_getenv(t, true);
1892 set_text(t, v, tok_strlen(v));
1893 t->type = TOK_INTERNAL_STRING;
1894 break;
1895 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001896
H. Peter Anvin8571f062019-09-23 16:40:03 -07001897 case TOK_LOCAL_MACRO:
1898 case TOK_LOCAL_SYMBOL:
1899 if (expand_locals) {
1900 const char *q;
1901 char *p;
1902 Context *ctx = get_ctx(tok_text(t), &q);
1903 if (ctx) {
1904 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1905 set_text_free(t, p, nasm_last_string_len());
1906 t->type = TOK_ID;
1907 }
1908 }
1909 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001910
H. Peter Anvin8571f062019-09-23 16:40:03 -07001911 default:
1912 break; /* No modifications */
1913 }
1914
1915 if (debug_level(2)) {
1916 unsigned int t_len = t->len;
1917 unsigned int s_len = tok_strlen(tok_text(t));
1918 if (t_len != s_len) {
1919 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
1920 tok_text(t), t->type, s_len, t_len);
1921 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001922 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001924
H. Peter Anvin8571f062019-09-23 16:40:03 -07001925 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001926 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001927
H. Peter Anvin734b1882002-04-30 21:01:08 +00001928 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001929
H. Peter Anvin8571f062019-09-23 16:40:03 -07001930 list_for_each(t, tlist)
1931 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001932 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001933
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001934 return line;
1935}
1936
1937/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001938 * A scanner, suitable for use by the expression evaluator, which
1939 * operates on a line of Tokens. Expects a pointer to a pointer to
1940 * the first token in the line to be passed in as its private_data
1941 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001942 *
1943 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001944 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001945struct ppscan {
1946 Token *tptr;
1947 int ntokens;
1948};
1949
H. Peter Anvine2c80182005-01-15 22:15:51 +00001950static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001951{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001952 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001953 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001954 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001955
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001957 if (pps->ntokens && (tline = pps->tptr)) {
1958 pps->ntokens--;
1959 pps->tptr = tline->next;
1960 } else {
1961 pps->tptr = NULL;
1962 pps->ntokens = 0;
1963 return tokval->t_type = TOKEN_EOS;
1964 }
1965 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001966
H. Peter Anvin8571f062019-09-23 16:40:03 -07001967 txt = tok_text(tline);
1968 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001969
H. Peter Anvin8571f062019-09-23 16:40:03 -07001970 if (txt[0] == '$') {
1971 if (!txt[1]) {
1972 return tokval->t_type = TOKEN_HERE;
1973 } else if (txt[1] == '$' && !txt[2]) {
1974 return tokval->t_type = TOKEN_BASE;
1975 } else if (tline->type == TOK_ID) {
1976 tokval->t_charptr++;
1977 return tokval->t_type = TOKEN_ID;
1978 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001979 }
1980
H. Peter Anvin8571f062019-09-23 16:40:03 -07001981 switch (tline->type) {
1982 default:
1983 if (tline->len == 1)
1984 return tokval->t_type = txt[0];
1985 /* fall through */
1986 case TOK_ID:
1987 return nasm_token_hash(txt, tokval);
1988
1989 case TOK_NUMBER:
1990 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001992 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 if (rn_error)
1994 return tokval->t_type = TOKEN_ERRNUM;
1995 else
1996 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001997 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001998
H. Peter Anvin8571f062019-09-23 16:40:03 -07001999 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002000 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002001
2002 case TOK_STRING:
2003 tokval->t_charptr = (char *)unquote_token(tline);
2004 tokval->t_inttwo = tline->len;
2005 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002006 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002007}
2008
2009/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002010 * 1. An expression (true if nonzero 0)
2011 * 2. The keywords true, on, yes for true
2012 * 3. The keywords false, off, no for false
2013 * 4. An empty line, for true
2014 *
2015 * On error, return defval (usually the previous value)
2016 */
2017static bool pp_get_boolean_option(Token *tline, bool defval)
2018{
2019 static const char * const noyes[] = {
2020 "no", "yes",
2021 "false", "true",
2022 "off", "on"
2023 };
2024 struct ppscan pps;
2025 struct tokenval tokval;
2026 expr *evalresult;
2027
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002028 tline = skip_white(tline);
2029 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002030 return true;
2031
2032 if (tline->type == TOK_ID) {
2033 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002034 const char *txt = tok_text(tline);
2035
H. Peter Anvind2354082019-08-27 16:38:48 -07002036 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002037 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002038 return i & 1;
2039 }
2040
2041 pps.tptr = NULL;
2042 pps.tptr = tline;
2043 pps.ntokens = -1;
2044 tokval.t_type = TOKEN_INVALID;
2045 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2046
2047 if (!evalresult)
2048 return true;
2049
2050 if (tokval.t_type)
2051 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2052 if (!is_really_simple(evalresult)) {
2053 nasm_nonfatal("boolean flag expression must be a constant");
2054 return defval;
2055 }
2056
2057 return reloc_value(evalresult) != 0;
2058}
2059
2060/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002061 * Compare a string to the name of an existing macro; this is a
2062 * simple wrapper which calls either strcmp or nasm_stricmp
2063 * depending on the value of the `casesense' parameter.
2064 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002065static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002066{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002067 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002068}
2069
2070/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002071 * Compare a string to the name of an existing macro; this is a
2072 * simple wrapper which calls either strcmp or nasm_stricmp
2073 * depending on the value of the `casesense' parameter.
2074 */
2075static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2076{
2077 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2078}
2079
2080/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002081 * Return the Context structure associated with a %$ token. Return
2082 * NULL, having _already_ reported an error condition, if the
2083 * context stack isn't deep enough for the supplied number of $
2084 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002085 *
2086 * If "namep" is non-NULL, set it to the pointer to the macro name
2087 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002088 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002089static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002090{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002091 Context *ctx;
2092 int i;
2093
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002094 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002095 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002096
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002097 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002098 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002099
H. Peter Anvine2c80182005-01-15 22:15:51 +00002100 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002101 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002102 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002103 }
2104
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002105 name += 2;
2106 ctx = cstk;
2107 i = 0;
2108 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 name++;
2110 i++;
2111 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002112 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002114 nasm_nonfatal("`%s': context stack is only"
2115 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002116 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002117 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002118
2119 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002121
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002122 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002123}
2124
2125/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002126 * Open an include file. This routine must always return a valid
2127 * file pointer if it returns - it's responsible for throwing an
2128 * ERR_FATAL and bombing out completely if not. It should also try
2129 * the include path one by one until it finds the file or reaches
2130 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002131 *
2132 * Note: for INC_PROBE the function returns NULL at all times;
2133 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002134 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002135enum incopen_mode {
2136 INC_NEEDED, /* File must exist */
2137 INC_OPTIONAL, /* Missing is OK */
2138 INC_PROBE /* Only an existence probe */
2139};
2140
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002141/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002142static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002143 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002144{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002145 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002146 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002147 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002148 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002149 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002150
H. Peter Anvine2c80182005-01-15 22:15:51 +00002151 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002152 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002153 if (omode == INC_PROBE) {
2154 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002155 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002156 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002157 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002158 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002159 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002160 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002161 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002162 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002163 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002164
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002165 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002166
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002167 if (!ip) {
2168 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002169 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002170 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002171
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002172 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002173 ip = ip->next;
2174 }
2175}
2176
2177/*
2178 * Open a file, or test for the presence of one (depending on omode),
2179 * considering the include path.
2180 */
2181static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002182 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002183 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002184 enum incopen_mode omode,
2185 enum file_flags fmode)
2186{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002187 struct hash_insert hi;
2188 void **hp;
2189 char *path;
2190 FILE *fp = NULL;
2191
2192 hp = hash_find(&FileHash, file, &hi);
2193 if (hp) {
2194 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002195 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002196 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002197 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002198 } else {
2199 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002200 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002201
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002202 /* Positive or negative result */
2203 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002204
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002205 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002206 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002207 */
2208 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002209 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002210 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002211
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002212 if (!path) {
2213 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002214 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002215 } else {
2216 if (!fp && omode != INC_PROBE)
2217 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002218 }
2219
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002220 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002221 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002222
2223 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002224}
2225
2226/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002227 * Opens an include or input file. Public version, for use by modules
2228 * that get a file:lineno pair and need to look at the file again
2229 * (e.g. the CodeView debug backend). Returns NULL on failure.
2230 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002231FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002232{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002233 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002234}
2235
2236/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002237 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002238 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002239 * return true if _any_ single-line macro of that name is defined.
2240 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002241 * `nparam' or no parameters is defined.
2242 *
2243 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002244 * defined, or nparam is -1, the address of the definition structure
2245 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002246 * is NULL, no action will be taken regarding its contents, and no
2247 * error will occur.
2248 *
2249 * Note that this is also called with nparam zero to resolve
2250 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002251 *
2252 * If you already know which context macro belongs to, you can pass
2253 * the context pointer as first parameter; if you won't but name begins
2254 * with %$ the context will be automatically computed. If all_contexts
2255 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002256 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002257static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002258smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002259 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002260{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002261 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002262 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002263
H. Peter Anvin97a23472007-09-16 17:57:25 -07002264 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002265 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002266 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002267 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002268 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002269 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002270 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002271 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002272 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002273 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002274 }
H. Peter Anvind2354082019-08-27 16:38:48 -07002275
2276restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002277 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002278
H. Peter Anvine2c80182005-01-15 22:15:51 +00002279 while (m) {
2280 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002281 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002282 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002283 if (m->alias && !find_alias) {
2284 if (do_aliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002285 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002286 goto restart;
2287 } else {
2288 continue;
2289 }
2290 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002291 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002292 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002293 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002294 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295 }
2296 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002297 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002298
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002299 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002300}
2301
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002302/* param should be a natural number [0; INT_MAX] */
2303static int read_param_count(const char *str)
2304{
2305 int result;
2306 bool err;
2307
2308 result = readnum(str, &err);
2309 if (result < 0 || result > INT_MAX) {
2310 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002311 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2312 str, 0, INT_MAX);
2313 } else if (err)
2314 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002315 return result;
2316}
2317
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002318/*
2319 * Count and mark off the parameters in a multi-line macro call.
2320 * This is called both from within the multi-line macro expansion
2321 * code, and also to mark off the default parameters when provided
2322 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002323 *
2324 * Note that we need space in the params array for parameter 0 being
2325 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002326 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002327static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002328{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002329 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002330 int nparam = 0;
2331 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002332
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002333 paramsize = PARAM_DELTA;
2334 params = nasm_malloc(paramsize * sizeof(*params));
2335 params[0] = NULL;
2336
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002337 while ((t = skip_white(t))) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002338 /* 2 slots for captured label and NULL */
2339 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002341 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002343 params[++nparam] = t;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002344 if (tok_is(t, '{')) {
2345 int brace = 1;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002346 while (brace && (t = t->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002347 brace += tok_is(t, '{');
2348 brace -= tok_is(t, '}');
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002349 }
2350
2351 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 /*
2353 * Now we've found the closing brace, look further
2354 * for the comma.
2355 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002356 t = skip_white(t->next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002357 if (tok_isnt(t, ','))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002358 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002359 } else {
2360 nasm_nonfatal("expecting closing brace in macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002362 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002363
2364 while (tok_isnt(t, ','))
2365 t = t->next;
2366
2367 if (t) /* got a comma */
2368 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002369 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002370
2371 params[nparam+1] = NULL;
2372 *paramsp = params;
2373 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002374}
2375
2376/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002377 * Determine whether one of the various `if' conditions is true or
2378 * not.
2379 *
2380 * We must free the tline we get passed.
2381 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002382static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002383{
H. Peter Anvin70055962007-10-11 00:05:31 -07002384 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002385 Token *t, *tt, *origline;
2386 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002387 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002388 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002389 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002390 const char *dname = pp_directives[ct];
2391 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002392 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002393
2394 origline = tline;
2395
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002396 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002397 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002398 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002399 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002400 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002401 if (!tline)
2402 break;
2403 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002404 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002405 dname);
2406 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002408 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002409 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 tline = tline->next;
2411 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002412 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002413
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002414 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002415 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002416 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002417 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002419 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002420 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002421 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002422 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002423 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002424 if (smacro_defined(NULL, tok_text(tline), 0, NULL, true, false))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002425 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002427 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002428 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002429
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002430 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002431 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002432 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002433 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002434 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002435 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002436 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002437 tline->type != TOK_INTERNAL_STRING &&
2438 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002439 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002440 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002441 goto fail;
2442 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002443
2444 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002445 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002446 }
2447 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002448
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002449 case PP_IFIDNI:
2450 casesense = false;
2451 /* fall through */
2452 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 tline = expand_smacro(tline);
2454 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002455 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 tt = tt->next;
2457 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002458 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002459 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002460 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002461 }
2462 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002463 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002464 while (tok_isnt(t, ',') && tt) {
2465 unsigned int l1, l2;
2466 const char *t1, *t2;
2467
2468 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002469 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002470 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002471 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002472 }
2473 if (t->type == TOK_WHITESPACE) {
2474 t = t->next;
2475 continue;
2476 }
2477 if (tt->type == TOK_WHITESPACE) {
2478 tt = tt->next;
2479 continue;
2480 }
2481 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002482 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 break;
2484 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002485
H. Peter Anvin8571f062019-09-23 16:40:03 -07002486 t1 = unquote_token(t);
2487 t2 = unquote_token(tt);
2488 l1 = t->len;
2489 l2 = tt->len;
2490
2491 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2492 j = false;
2493 break;
2494 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002495
H. Peter Anvine2c80182005-01-15 22:15:51 +00002496 t = t->next;
2497 tt = tt->next;
2498 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002499 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002500 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002502
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002503 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002504 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002506 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002507
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002508 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002509 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002510 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002511 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 goto fail;
2513 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002514 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002515 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002516 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002517 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002518 searching.nparam_max = INT_MAX;
2519 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002520 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002522 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002523 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002524 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002525 } else {
2526 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002527 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002529 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002531 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002533 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002534 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002535 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002536 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002537 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002538 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002539 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002540 searching.nparam_max = searching.nparam_min;
2541 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 }
2543 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002544 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 tline = tline->next;
2546 searching.plus = true;
2547 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002548 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2549 while (mmac) {
2550 if (!strcmp(mmac->name, searching.name) &&
2551 (mmac->nparam_min <= searching.nparam_max
2552 || searching.plus)
2553 && (searching.nparam_min <= mmac->nparam_max
2554 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 found = true;
2556 break;
2557 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002558 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 }
2560 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002561 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 nasm_free(searching.name);
2563 j = found;
2564 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002565 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002566
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002567 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 needtype = TOK_ID;
2569 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002570 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002571 needtype = TOK_NUMBER;
2572 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002573 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 needtype = TOK_STRING;
2575 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002576
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002577iftype:
2578 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002579
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002580 while (tok_white(t) ||
2581 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002582 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002583
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002584 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002586
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002587 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002588 tline = expand_smacro(tline);
2589 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002590
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 j = false;
2592 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002593 t = skip_white(t->next); /* Skip the actual token + whitespace */
2594 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 }
2596 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002597
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002598 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002599 tline = expand_smacro(tline);
2600 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 j = !t; /* Should be empty */
2602 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002603
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002604 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002605 pps.tptr = tline = expand_smacro(tline);
2606 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002607 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002608 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 if (!evalresult)
2610 return -1;
2611 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002612 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002613 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002614 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002615 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002617 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002618 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002620
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002621 case PP_IFUSING:
2622 case PP_IFUSABLE:
2623 {
2624 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002625 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002626
H. Peter Anvin8571f062019-09-23 16:40:03 -07002627 pkg = get_use_pkg(tline, dname, &name);
2628 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002629 goto fail;
2630
2631 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2632 break;
2633 }
2634
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002636 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002638 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002639
2640 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002641 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002642
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002643fail:
2644 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002645 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002646}
2647
2648/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002649 * Default smacro expansion routine: just returns a copy of the
2650 * expansion list.
2651 */
2652static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002653smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002654{
2655 (void)params;
2656 (void)nparams;
2657
2658 return dup_tlist(s->expansion, NULL);
2659}
2660
2661/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002662 * Emit a macro defintion or undef to the listing file, if
2663 * desired. This is similar to detoken(), but it handles the reverse
2664 * expansion list, does not expand %! or local variable tokens, and
2665 * does some special handling for macro parameters.
2666 */
2667static void
2668list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2669{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002670 Token *t;
2671 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002672 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002673 char *context_prefix = NULL;
2674 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002675
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002676 namelen = strlen(m->name);
2677 size = namelen + 2; /* Include room for space after name + NUL */
2678
2679 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002680 int context_depth = cstk->depth - ctx->depth + 1;
2681 context_prefix =
2682 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2683 ctx->name ? ctx->name : "",
2684 ctx->number, context_depth, "");
2685
2686 context_len = nasm_last_string_len();
2687 memset(context_prefix + context_len - context_depth,
2688 '$', context_depth);
2689 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002690 }
2691
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002692 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002693 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002694
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002695 if (m->nparam) {
2696 /*
2697 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002698 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002699 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002700 int i;
2701
2702 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002703 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002704 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002705 }
2706
2707 def = nasm_malloc(size);
2708 p = def+size;
2709 *--p = '\0';
2710
2711 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002712 p -= t->len;
2713 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002714 }
2715
2716 *--p = ' ';
2717
2718 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002719 int i;
2720
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002721 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002722 for (i = m->nparam-1; i >= 0; i--) {
2723 enum sparmflags flags = m->params[i].flags;
2724 if (flags & SPARM_GREEDY)
2725 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002726 p -= m->params[i].name.len;
2727 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2728
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002729 if (flags & SPARM_NOSTRIP)
2730 *--p = '!';
2731 if (flags & SPARM_STR)
2732 *--p = '&';
2733 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002734 *--p = '=';
2735 *--p = ',';
2736 }
2737 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002738 }
2739
2740 p -= namelen;
2741 memcpy(p, m->name, namelen);
2742
H. Peter Anvin6686de22019-08-10 05:33:14 -07002743 if (context_prefix) {
2744 p -= context_len;
2745 memcpy(p, context_prefix, context_len);
2746 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002747 }
2748
2749 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002750 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002751}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002752
2753/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002754 * Parse smacro arguments, return argument count. If the tmpl argument
2755 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002756 * *tpp is updated to point to the pointer to the first token after the
2757 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002758 *
2759 * The text values from any argument tokens are "stolen" and the
2760 * corresponding text fields set to NULL.
2761 */
2762static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2763{
2764 int nparam = 0;
2765 enum sparmflags flags;
2766 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002767 bool err, done;
2768 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002769 Token **tn = *tpp;
2770 Token *t = *tn;
2771 Token *name;
2772
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002773 /*
2774 * DO NOT skip whitespace here, or we won't be able to distinguish:
2775 *
2776 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2777 * %define bar(a,b) ; two arguments, empty expansion
2778 *
2779 * This ambiguity was inherited from C.
2780 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002781
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002782 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002783 goto finish;
2784
2785 if (tmpl) {
2786 Token *tx = t;
2787 Token **txpp = &tx;
2788 int sparam;
2789
2790 /* Count parameters first */
2791 sparam = parse_smacro_template(&txpp, NULL);
2792 if (!sparam)
2793 goto finish; /* No parameters, we're done */
2794 nasm_newn(params, sparam);
2795 }
2796
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002797 /* Skip leading paren */
2798 tn = &t->next;
2799 t = *tn;
2800
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002801 name = NULL;
2802 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002803 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002804
2805 while (!done) {
2806 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002807 if (name || flags)
2808 nasm_nonfatal("`)' expected to terminate macro template");
2809 else
2810 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002811 break;
2812 }
2813
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002814 switch (t->type) {
2815 case TOK_ID:
2816 if (name)
2817 goto bad;
2818 name = t;
2819 break;
2820
2821 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002822 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002823 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002824 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002825 case '=':
2826 flags |= SPARM_EVAL;
2827 break;
2828 case '&':
2829 flags |= SPARM_STR;
2830 break;
2831 case '!':
2832 flags |= SPARM_NOSTRIP;
2833 break;
2834 case '+':
2835 flags |= SPARM_GREEDY;
2836 greedy = true;
2837 break;
2838 case ',':
2839 if (greedy)
2840 nasm_nonfatal("greedy parameter must be last");
2841 /* fall through */
2842 case ')':
2843 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002844 if (name)
2845 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002846 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002847 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002848 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002849 name = NULL;
2850 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002851 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002852 break;
2853 default:
2854 goto bad;
2855 }
2856 break;
2857
2858 case TOK_WHITESPACE:
2859 break;
2860
2861 default:
2862 bad:
2863 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002864 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002865 err = true;
2866 }
2867 break;
2868 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002869
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002870 tn = &t->next;
2871 t = *tn;
2872 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002873
2874finish:
2875 while (t && t->type == TOK_WHITESPACE) {
2876 tn = &t->next;
2877 t = t->next;
2878 }
2879 *tpp = tn;
2880 if (tmpl) {
2881 tmpl->nparam = nparam;
2882 tmpl->greedy = greedy;
2883 tmpl->params = params;
2884 }
2885 return nparam;
2886}
2887
2888/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002889 * Common code for defining an smacro. The tmpl argument, if not NULL,
2890 * contains any macro parameters that aren't explicit arguments;
2891 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002892 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002893static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002894 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002895{
2896 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002897 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002898 Context *ctx;
2899 bool defining_alias = false;
2900 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002901
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002902 if (tmpl) {
2903 defining_alias = tmpl->alias;
2904 nparam = tmpl->nparam;
2905 }
2906
2907 while (1) {
2908 ctx = get_ctx(mname, &mname);
2909
H. Peter Anvind2354082019-08-27 16:38:48 -07002910 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002911 /* Create a new macro */
2912 smtbl = ctx ? &ctx->localmac : &smacros;
2913 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2914 nasm_new(smac);
2915 smac->next = *smhead;
2916 *smhead = smac;
2917 break;
2918 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002919 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002920 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002921 /*
2922 * Some instances of the old code considered this a failure,
2923 * some others didn't. What is the right thing to do here?
2924 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002925 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002926 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002927 /*
2928 * We're redefining, so we have to take over an
2929 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002930 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002931 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002932 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002933 break;
2934 } else if (smac->in_progress) {
2935 nasm_nonfatal("macro alias loop");
2936 goto fail;
2937 } else {
2938 /* It is an alias macro; follow the alias link */
2939 SMacro *s;
2940
2941 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002942 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002943 expansion, tmpl);
2944 smac->in_progress = false;
2945 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002946 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002947 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002948
2949 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002950 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002951 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002952 smac->expand = smacro_expand_default;
2953 if (tmpl) {
2954 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002955 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002956 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002957 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002958 if (tmpl->expand)
2959 smac->expand = tmpl->expand;
2960 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002961 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002962 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2963 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002964 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002965 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002966
2967fail:
2968 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002969 if (tmpl)
2970 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002971 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002972}
2973
2974/*
2975 * Undefine an smacro
2976 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002977static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002978{
2979 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002980 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002981 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002982
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002983 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002984 smtbl = ctx ? &ctx->localmac : &smacros;
2985 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002986
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002987 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002988 /*
2989 * We now have a macro name... go hunt for it.
2990 */
2991 sp = smhead;
2992 while ((s = *sp) != NULL) {
2993 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002994 if (s->alias && !undefalias) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002995 if (!do_aliases)
2996 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002997 if (s->in_progress) {
2998 nasm_nonfatal("macro alias loop");
2999 } else {
3000 s->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003001 undef_smacro(tok_text(s->expansion), false);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003002 s->in_progress = false;
3003 }
3004 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003005 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003006 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3007 ctx, s);
3008 *sp = s->next;
3009 free_smacro(s);
3010 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003011 } else {
3012 sp = &s->next;
3013 }
3014 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003015 }
3016}
3017
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003018/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003019 * Parse a mmacro specification.
3020 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003021static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003022{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003023 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003024 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003025 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003026 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003027 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003028 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003029 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003030
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003031#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003032 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003033#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003034 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003035 def->plus = false;
3036 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003037 def->nparam_min = 0;
3038 def->nparam_max = 0;
3039
H. Peter Anvina26433d2008-07-16 14:40:01 -07003040 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003041 tline = skip_white(tline);
3042 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003043 nasm_nonfatal("`%s' expects a parameter count", directive);
3044 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003045 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003046 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003047 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003048 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003049 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003050 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003051 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003052 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003053 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003054 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003055 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003056 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003057 }
3058 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003059 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003060 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003061 tline = tline->next;
3062 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003063 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003064 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003065 tline->next->len == 7 &&
3066 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003067 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003068 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003069 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003070
H. Peter Anvina26433d2008-07-16 14:40:01 -07003071 /*
3072 * Handle default parameters.
3073 */
3074 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003075 def->dlist = tline->next;
3076 tline->next = NULL;
3077 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003078 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003079 def->dlist = NULL;
3080 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003081 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003082 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003083
H. Peter Anvin89cee572009-07-15 09:16:54 -04003084 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003085 !def->plus) {
3086 /*
3087 *!macro-defaults [on] macros with more default than optional parameters
3088 *! warns when a macro has more default parameters than optional parameters.
3089 *! See \k{mlmacdef} for why might want to disable this warning.
3090 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003091 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003092 "too many default macro parameters in macro `%s'", def->name);
3093 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003094
H. Peter Anvina26433d2008-07-16 14:40:01 -07003095 return true;
3096}
3097
3098
3099/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003100 * Decode a size directive
3101 */
3102static int parse_size(const char *str) {
3103 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003104 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003105 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003106 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003107 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003108}
3109
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003110/*
3111 * Process a preprocessor %pragma directive. Currently there are none.
3112 * Gets passed the token list starting with the "preproc" token from
3113 * "%pragma preproc".
3114 */
3115static void do_pragma_preproc(Token *tline)
3116{
3117 /* Skip to the real stuff */
3118 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003119 tline = skip_white(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003120 if (!tline)
3121 return;
3122
3123 (void)tline; /* Nothing else to do at present */
3124}
3125
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003126static bool is_macro_id(const Token *t)
3127{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003128 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003129}
3130
H. Peter Anvin8571f062019-09-23 16:40:03 -07003131static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003132{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003133 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003134 Token *t = *tp;
3135
3136 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003137 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003138 t = expand_id(t);
3139
3140 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003141 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003142 return NULL;
3143 }
3144
H. Peter Anvin8571f062019-09-23 16:40:03 -07003145 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003146 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003147 *tp = t;
3148 return id;
3149}
3150
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003151/* Parse a %use package name and find the package. Set *err on syntax error. */
3152static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003153get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003154{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003155 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003156
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003157 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003158 t = expand_smacro(t);
3159
H. Peter Anvin8571f062019-09-23 16:40:03 -07003160 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003161
H. Peter Anvin8571f062019-09-23 16:40:03 -07003162 if (!t) {
3163 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3164 return NULL;
3165 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3166 nasm_nonfatal("`%s' expects a package name, got `%s'",
3167 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003168 return NULL;
3169 }
3170
H. Peter Anvin8571f062019-09-23 16:40:03 -07003171 *name = id = unquote_token(t);
3172
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003173 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003174 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003175 if (t)
3176 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3177
3178 return nasm_find_use_package(id);
3179}
3180
Ed Beroset3ab3f412002-06-11 03:31:49 +00003181/**
3182 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003183 * Find out if a line contains a preprocessor directive, and deal
3184 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003185 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003186 * If a directive _is_ found, it is the responsibility of this routine
3187 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003188 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003189 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003190 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003191 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003192 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003193 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003194static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003195{
H. Peter Anvin4169a472007-09-12 01:29:43 +00003196 enum preproc_token i;
3197 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003198 bool err;
3199 int nparam;
3200 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003201 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003202 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003203 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003204 const char *p;
3205 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003206 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003207 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003208 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003209 Include *inc;
3210 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003211 Cond *cond;
3212 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003213 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003214 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003215 struct tokenval tokval;
3216 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003217 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003218 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003219 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003220 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003221
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003222 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003223 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003224
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003225 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003226 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3227 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003228
H. Peter Anvin8571f062019-09-23 16:40:03 -07003229 dname = tok_text(tline);
3230 if (dname[1] == '%')
3231 return NO_DIRECTIVE_FOUND;
3232
3233 i = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003234
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003235 casesense = true;
3236 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
3237 casesense = false;
3238 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003239 }
3240
3241 /*
3242 * If we're in a non-emitting branch of a condition construct,
3243 * or walking to the end of an already terminated %rep block,
3244 * we should ignore all directives except for condition
3245 * directives.
3246 */
3247 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003248 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
3249 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003250 return NO_DIRECTIVE_FOUND;
3251 }
3252
3253 /*
3254 * If we're defining a macro or reading a %rep block, we should
3255 * ignore all directives except for %macro/%imacro (which nest),
3256 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
3257 * If we're in a %rep block, another %rep nests, so should be let through.
3258 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003259 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003260 i != PP_ENDMACRO && i != PP_ENDM &&
3261 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
3262 return NO_DIRECTIVE_FOUND;
3263 }
3264
3265 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003266 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003267 nested_mac_count++;
3268 return NO_DIRECTIVE_FOUND;
3269 } else if (nested_mac_count > 0) {
3270 if (i == PP_ENDMACRO) {
3271 nested_mac_count--;
3272 return NO_DIRECTIVE_FOUND;
3273 }
3274 }
3275 if (!defining->name) {
3276 if (i == PP_REP) {
3277 nested_rep_count++;
3278 return NO_DIRECTIVE_FOUND;
3279 } else if (nested_rep_count > 0) {
3280 if (i == PP_ENDREP) {
3281 nested_rep_count--;
3282 return NO_DIRECTIVE_FOUND;
3283 }
3284 }
3285 }
3286 }
3287
H. Peter Anvin4169a472007-09-12 01:29:43 +00003288 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003289 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003290 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003291 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003292
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003293 case PP_PRAGMA:
3294 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003295 * %pragma namespace options...
3296 *
3297 * The namespace "preproc" is reserved for the preprocessor;
3298 * all other namespaces generate a [pragma] assembly directive.
3299 *
3300 * Invalid %pragmas are ignored and may have different
3301 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003302 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003303 t = tline;
3304 tline = tline->next;
3305 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003306 tline = zap_white(expand_smacro(tline));
3307 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003308 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003309 /* Preprocessor pragma */
3310 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003311 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003312 } else {
3313 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003314
3315 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003316 for (t = tline; t->next; t = t->next)
3317 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003318 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003319
3320 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003321 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003322 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003323 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003324 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003325 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003326 }
3327 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003328 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003329
H. Peter Anvine2c80182005-01-15 22:15:51 +00003330 case PP_STACKSIZE:
3331 /* Directive to tell NASM what the default stack size is. The
3332 * default is for a 16-bit stack, and this can be overriden with
3333 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003335 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003337 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003339 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003340 /* All subsequent ARG directives are for a 32-bit stack */
3341 StackSize = 4;
3342 StackPointer = "ebp";
3343 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003344 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003345 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003346 /* All subsequent ARG directives are for a 64-bit stack */
3347 StackSize = 8;
3348 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003349 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003350 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003351 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 /* All subsequent ARG directives are for a 16-bit stack,
3353 * far function call.
3354 */
3355 StackSize = 2;
3356 StackPointer = "bp";
3357 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003358 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003359 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 /* All subsequent ARG directives are for a 16-bit stack,
3361 * far function call. We don't support near functions.
3362 */
3363 StackSize = 2;
3364 StackPointer = "bp";
3365 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003366 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003367 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003368 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003370 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003371
H. Peter Anvine2c80182005-01-15 22:15:51 +00003372 case PP_ARG:
3373 /* TASM like ARG directive to define arguments to functions, in
3374 * the following form:
3375 *
3376 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3377 */
3378 offset = ArgOffset;
3379 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003380 const char *arg;
3381 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003382 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003383
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003385 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003386 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003387 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003388 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003390 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 /* Find the argument size type */
3393 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003394 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003395 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003396 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 }
3398 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003399 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003400 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003401 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003402 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003403
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003405 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003407 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003408 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003409 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003411 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003412 }
3413 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003414
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003415 /* Round up to even stack slots */
3416 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003417
H. Peter Anvine2c80182005-01-15 22:15:51 +00003418 /* Now define the macro for the argument */
3419 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3420 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003421 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003423
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003425 tline = skip_white(tline->next);
3426 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003427 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003428 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003429
H. Peter Anvine2c80182005-01-15 22:15:51 +00003430 case PP_LOCAL:
3431 /* TASM like LOCAL directive to define local variables for a
3432 * function, in the following form:
3433 *
3434 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3435 *
3436 * The '= LocalSize' at the end is ignored by NASM, but is
3437 * required by TASM to define the local parameter size (and used
3438 * by the TASM macro package).
3439 */
3440 offset = LocalOffset;
3441 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003442 const char *local;
3443 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003445
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003447 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003449 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003450 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003452 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003453
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 /* Find the argument size type */
3455 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003456 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003457 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003458 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 }
3460 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003461 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003462 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003463 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003464 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003465
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003467 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003469 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003471 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003473 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 }
3475 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003476
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 /* Round up to even stack slots */
3478 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003479
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003481
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3484 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003485 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003486
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 /* Now define the assign to setup the enter_c macro correctly */
3488 snprintf(directive, sizeof(directive),
3489 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003490 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003491
H. Peter Anvine2c80182005-01-15 22:15:51 +00003492 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003493 tline = skip_white(tline->next);
3494 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003496 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003497
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 case PP_CLEAR:
3499 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003500 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003501 free_macros();
3502 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003503 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003504
H. Peter Anvin418ca702008-05-30 10:42:30 -07003505 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003506 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003507 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003508 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003509 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003510 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003511 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003512 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003513 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003514 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003515
3516 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003517 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003518
3519 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003520 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003521 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003522
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003523 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003524 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003525 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003526 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003528 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003529 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003530 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003531 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003532 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003533 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003534 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003535 (pp_mode == PP_DEPS)
3536 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003537 if (!inc->fp) {
3538 /* -MG given but file not found */
3539 nasm_free(inc);
3540 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003541 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003542 inc->lineno = src_set_linnum(0);
3543 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003544 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003545 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003546 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003547 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003548 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
H. Peter Anvind2456592008-06-19 15:04:18 -07003550 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003551 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003552 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003553 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003554
H. Peter Anvin8571f062019-09-23 16:40:03 -07003555 pkg = get_use_pkg(tline->next, dname, &name);
3556 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003557 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003558 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003559 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003560 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003561 /*
3562 * Not already included, go ahead and include it.
3563 * Treat it as an include file for the purpose of
3564 * producing a listing.
3565 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003566 use_loaded[pkg->index] = true;
3567 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003568 nasm_new(inc);
3569 inc->next = istk;
3570 inc->fname = src_set_fname(NULL);
3571 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003572 inc->nolist = !list_option('b') || istk->nolist;
3573 istk = inc;
3574 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003575 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003576 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003577 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003580 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003582 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003584 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003585 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003586 nasm_nonfatal("`%s' expects a context identifier",
3587 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003588 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003589 }
3590 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003591 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003592 pp_directives[i]);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003593 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003594 } else {
3595 p = NULL; /* Anonymous */
3596 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003597
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003598 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003599 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003600 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003601 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003602 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003603 ctx->number = unique++;
3604 cstk = ctx;
3605 } else {
3606 /* %pop or %repl */
3607 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003608 nasm_nonfatal("`%s': context stack is empty",
3609 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003610 } else if (i == PP_POP) {
3611 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003612 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003613 "expected %s",
3614 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003615 else
3616 ctx_pop();
3617 } else {
3618 /* i == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003619 nasm_free((char *)cstk->name);
3620 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003621 p = NULL;
3622 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003623 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003624 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003625 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003626 severity = ERR_FATAL;
3627 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003629 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003630 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003631 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003632 /*!
3633 *!user [on] %warning directives
3634 *! controls output of \c{%warning} directives (see \k{pperror}).
3635 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003636 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003637 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003638
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003639issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003640 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003641 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003642 tline->next = expand_smacro(tline->next);
3643 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003644 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003646 t = skip_white(t);
3647 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003648 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003649 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003650 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003651 } else {
3652 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003653 q = detoken(tline, false);
3654 nasm_error(severity, "%s", q);
3655 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003656 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003657 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003658 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003659
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003660 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003661 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003662 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003663 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003664 j = if_condition(tline->next, i);
3665 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003666 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003667 cond = nasm_malloc(sizeof(Cond));
3668 cond->next = istk->conds;
3669 cond->state = j;
3670 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003671 if(istk->mstk.mstk)
3672 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003673 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003674
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003675 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003676 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003677 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003678 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003679 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003680 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003681 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003682
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003683 case COND_DONE:
3684 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003685 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003686
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003687 case COND_ELSE_TRUE:
3688 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003689 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003690 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003691 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003692 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003693
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003694 case COND_IF_FALSE:
3695 /*
3696 * IMPORTANT: In the case of %if, we will already have
3697 * called expand_mmac_params(); however, if we're
3698 * processing an %elif we must have been in a
3699 * non-emitting mode, which would have inhibited
3700 * the normal invocation of expand_mmac_params().
3701 * Therefore, we have to do it explicitly here.
3702 */
3703 j = if_condition(expand_mmac_params(tline->next), i);
3704 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003705 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003706 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003708 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003709
H. Peter Anvine2c80182005-01-15 22:15:51 +00003710 case PP_ELSE:
3711 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003712 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003713 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003714 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003715 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003716 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003717 case COND_IF_TRUE:
3718 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003719 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003720 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003721
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003722 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003723 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003724
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003725 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003726 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003727 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003728
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003729 case COND_ELSE_TRUE:
3730 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003731 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003732 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003733 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003734 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003735 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003736 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003737
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 case PP_ENDIF:
3739 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003740 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003741 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003742 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003743 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003744 cond = istk->conds;
3745 istk->conds = cond->next;
3746 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003747 if(istk->mstk.mstk)
3748 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003749 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003750
H. Peter Anvin8b262472019-02-26 14:00:54 -08003751 case PP_RMACRO:
3752 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003753 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003754 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003755 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003756 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003757 if (i == PP_RMACRO)
3758 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003759 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003760 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003761 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003762 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003763
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003764 src_get(&defining->xline, &defining->fname);
3765
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003766 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3767 while (mmac) {
3768 if (!strcmp(mmac->name, defining->name) &&
3769 (mmac->nparam_min <= defining->nparam_max
3770 || defining->plus)
3771 && (defining->nparam_min <= mmac->nparam_max
3772 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003773 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003774 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003775 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003776 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003777 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003778 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003779 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003780
H. Peter Anvine2c80182005-01-15 22:15:51 +00003781 case PP_ENDM:
3782 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003783 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003784 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003785 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003786 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003787 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3788 defining->next = *mmhead;
3789 *mmhead = defining;
3790 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003791 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003792
H. Peter Anvin89cee572009-07-15 09:16:54 -04003793 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003794 /*
3795 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003796 * macro-end marker for a macro with a name. Then we
3797 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003798 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003799 list_for_each(l, istk->expansion)
3800 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003801 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003802
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003803 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003804 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003805 * Remove all conditional entries relative to this
3806 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003807 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003808 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3809 cond = istk->conds;
3810 istk->conds = cond->next;
3811 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003812 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003813 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003814 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003815 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003816 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003817 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003818
H. Peter Anvina26433d2008-07-16 14:40:01 -07003819 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003820 casesense = false;
3821 /* fall through */
3822 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003823 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003824 MMacro **mmac_p;
3825 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003826
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003827 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003828 spec.casesense = casesense;
3829 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003830 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003831 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003832 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3833 while (mmac_p && *mmac_p) {
3834 mmac = *mmac_p;
3835 if (mmac->casesense == spec.casesense &&
3836 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3837 mmac->nparam_min == spec.nparam_min &&
3838 mmac->nparam_max == spec.nparam_max &&
3839 mmac->plus == spec.plus) {
3840 *mmac_p = mmac->next;
3841 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003842 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003843 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003844 }
3845 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003846 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003847 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003848 }
3849
H. Peter Anvine2c80182005-01-15 22:15:51 +00003850 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003851 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003852 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003853 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003855 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003856 return DIRECTIVE_FOUND;
3857 }
3858 t = expand_smacro(tline->next);
3859 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003860 pps.tptr = tline = t;
3861 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003862 tokval.t_type = TOKEN_INVALID;
3863 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003864 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003865 free_tlist(tline);
3866 if (!evalresult)
3867 return DIRECTIVE_FOUND;
3868 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003869 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003871 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872 return DIRECTIVE_FOUND;
3873 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003874 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003875 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003876 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003877 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003878 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003879 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003880 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003881
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003882 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003883 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003884 rotate += mmac->nparam;
3885
3886 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003887 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003888 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003889
H. Peter Anvine2c80182005-01-15 22:15:51 +00003890 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003891 {
3892 MMacro *tmp_defining;
3893
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003894 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003895 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003896 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
3897 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003898 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003899 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003900 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003901
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003903 pps.tptr = expand_smacro(tline);
3904 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003905 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003906 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003908 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003909 if (!evalresult)
3910 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003912 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003914 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003915 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003916 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003917 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003918 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003919 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3920 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003921 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003922 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003923 /*!
3924 *!negative-rep [on] regative %rep count
3925 *! warns about negative counts given to the \c{%rep}
3926 *! preprocessor directive.
3927 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003928 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003929 "negative `%%rep' count: %"PRId64, count);
3930 count = 0;
3931 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003932 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003933 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003935 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003936 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003938 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003939 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003940 defining->nolist = nolist;
3941 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003942 defining->mstk = istk->mstk;
3943 defining->dstk.mstk = tmp_defining;
3944 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003945 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003946 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003947 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003948
H. Peter Anvine2c80182005-01-15 22:15:51 +00003949 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003950 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003951 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003952 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003954
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 /*
3956 * Now we have a "macro" defined - although it has no name
3957 * and we won't be entering it in the hash tables - we must
3958 * push a macro-end marker for it on to istk->expansion.
3959 * After that, it will take care of propagating itself (a
3960 * macro-end marker line for a macro which is really a %rep
3961 * block will cause the macro to be re-expanded, complete
3962 * with another macro-end marker to ensure the process
3963 * continues) until the whole expansion is forcibly removed
3964 * from istk->expansion by a %exitrep.
3965 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003966 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003967 l->next = istk->expansion;
3968 l->finishes = defining;
3969 l->first = NULL;
3970 istk->expansion = l;
3971
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003972 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003973
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003974 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003975 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003976 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003977
H. Peter Anvine2c80182005-01-15 22:15:51 +00003978 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003979 /*
3980 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003981 * macro-end marker for a macro with no name. Then we set
3982 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003983 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003984 list_for_each(l, istk->expansion)
3985 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003986 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003987
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003988 if (l)
3989 l->finishes->in_progress = 1;
3990 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003991 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003992 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003993
H. Peter Anvin8b262472019-02-26 14:00:54 -08003994 case PP_DEFINE:
3995 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003996 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003997 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003998 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003999 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004000
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004001 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004002 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004003
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004004 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004005 lastp = &tline->next;
4006 nparam = parse_smacro_template(&lastp, &tmpl);
4007 tline = *lastp;
4008 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004009
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07004010 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004011 macro_start = tline;
4012 if (!is_macro_id(macro_start)) {
4013 nasm_nonfatal("`%s' expects a macro identifier to alias",
4014 dname);
4015 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004016 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004017 tt = macro_start->next;
4018 macro_start->next = NULL;
4019 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004020 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004021 if (tline && tline->type) {
4022 nasm_warn(WARN_OTHER,
4023 "trailing garbage after aliasing identifier ignored");
4024 }
4025 free_tlist(tt);
4026 tmpl.alias = true;
4027 } else {
4028 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07004029 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004030 tline = expand_smacro(tline);
4031
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004032 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004033 macro_start = NULL;
4034 t = tline;
4035 while (t) {
4036 if (t->type == TOK_ID) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004037 const char *ttext = tok_text(t);
4038 size_t tlen = t->len;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004039 for (i = 0; i < nparam; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004040 if (tmpl.params[i].name.len == t->len &&
4041 !memcmp(ttext, tok_text(&tmpl.params[i].name), tlen)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004042 t->type = tok_smac_param(i);
4043 break;
4044 }
4045 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004046 }
4047 tt = t->next;
4048 t->next = macro_start;
4049 macro_start = t;
4050 t = tt;
4051 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004053
H. Peter Anvine2c80182005-01-15 22:15:51 +00004054 /*
4055 * Good. We now have a macro name, a parameter count, and a
4056 * token list (in reverse order) for an expansion. We ought
4057 * to be OK just to create an SMacro, store it, and let
4058 * free_tlist have the rest of the line (which we have
4059 * carefully re-terminated after chopping off the expansion
4060 * from the end).
4061 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004062 define_smacro(mname, casesense, macro_start, &tmpl);
4063 break;
4064 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004065
H. Peter Anvine2c80182005-01-15 22:15:51 +00004066 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004067 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004068 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004069 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004070 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004071 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004072
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004073 undef_smacro(mname, i == PP_UNDEFALIAS);
4074 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004075
H. Peter Anvin8b262472019-02-26 14:00:54 -08004076 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004077 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004078 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004079
H. Peter Anvin9e200162008-06-04 17:23:14 -07004080 last = tline;
4081 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004082 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004083
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004084 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004085 q = detoken(tline, false);
4086 macro_start = make_tok_qstr(NULL, q);
4087 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004088
4089 /*
4090 * We now have a macro name, an implicit parameter count of
4091 * zero, and a string token to use as an expansion. Create
4092 * and store an SMacro.
4093 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004094 define_smacro(mname, casesense, macro_start, NULL);
4095 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004096
H. Peter Anvin8b262472019-02-26 14:00:54 -08004097 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004098 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004099 goto done;
4100
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004101 last = tline;
4102 tline = expand_smacro(tline->next);
4103 last->next = NULL;
4104
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004105 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004106 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004107 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004108 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004109 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004110 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004111 }
4112
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004113 /*
4114 * Convert the string to a token stream. Note that smacros
4115 * are stored with the token stream reversed, so we have to
4116 * reverse the output of tokenize().
4117 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004118 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004119
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004120 /*
4121 * We now have a macro name, an implicit parameter count of
4122 * zero, and a numeric token to use as an expansion. Create
4123 * and store an SMacro.
4124 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004125 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004126 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004127 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004128
H. Peter Anvin418ca702008-05-30 10:42:30 -07004129 case PP_PATHSEARCH:
4130 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004131 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004132
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004133 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004134 goto done;
4135
H. Peter Anvin418ca702008-05-30 10:42:30 -07004136 last = tline;
4137 tline = expand_smacro(tline->next);
4138 last->next = NULL;
4139
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004140 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004141 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004142 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004143 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004144 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004145 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004146 }
4147 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004148 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004149
4150 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004151
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004152 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004153 if (!found_path)
4154 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004155 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004156
4157 /*
4158 * We now have a macro name, an implicit parameter count of
4159 * zero, and a string token to use as an expansion. Create
4160 * and store an SMacro.
4161 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004162 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004163 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004164 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004165 }
4166
H. Peter Anvine2c80182005-01-15 22:15:51 +00004167 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004168 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004169 goto done;
4170
H. Peter Anvine2c80182005-01-15 22:15:51 +00004171 last = tline;
4172 tline = expand_smacro(tline->next);
4173 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004174
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004175 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004176 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004177 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004178 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004179 free_tlist(tline);
4180 free_tlist(origline);
4181 return DIRECTIVE_FOUND;
4182 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004183
H. Peter Anvin8571f062019-09-23 16:40:03 -07004184 unquote_token(t);
4185 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004186
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 /*
4188 * We now have a macro name, an implicit parameter count of
4189 * zero, and a numeric token to use as an expansion. Create
4190 * and store an SMacro.
4191 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004192 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004193 free_tlist(tline);
4194 free_tlist(origline);
4195 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004196
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004197 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004198 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004199 goto done;
4200
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004201 last = tline;
4202 tline = expand_smacro(tline->next);
4203 last->next = NULL;
4204
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004205 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004206 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004207 switch (t->type) {
4208 case TOK_WHITESPACE:
4209 break;
4210 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004211 unquote_token(t);
4212 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004213 break;
4214 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004215 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004216 break;
4217 /* else fall through */
4218 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004219 nasm_nonfatal("non-string passed to `%s': %s", dname,
4220 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004221 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004222 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004223 }
4224 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004225
H. Peter Anvin8571f062019-09-23 16:40:03 -07004226 q = qbuf = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004227 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004228 if (t->type == TOK_INTERNAL_STRING)
4229 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004230 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004231
4232 /*
4233 * We now have a macro name, an implicit parameter count of
4234 * zero, and a numeric token to use as an expansion. Create
4235 * and store an SMacro.
4236 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004237 macro_start = make_tok_qstr(NULL, qbuf);
4238 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004239 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004240 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004241 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004242
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004244 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004245 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004246 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004247 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004248
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004249 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004250 goto done;
4251
H. Peter Anvine2c80182005-01-15 22:15:51 +00004252 last = tline;
4253 tline = expand_smacro(tline->next);
4254 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004255
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004256 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004257 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004258
4259 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004260
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004262 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004263 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004264 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004265 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004266 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004267
H. Peter Anvin8b262472019-02-26 14:00:54 -08004268 pps.tptr = t->next;
4269 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004270 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004271 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 if (!evalresult) {
4273 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004274 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004275 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004276 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004278 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004280 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004281
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004282 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004283 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004284 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004285 } else {
4286 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004287 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004288 if (!evalresult) {
4289 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004290 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004291 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004292 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004293 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004294 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004295 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004296 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004297 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004298
H. Peter Anvin8571f062019-09-23 16:40:03 -07004299 unquote_token(t);
4300 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004301
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004302 /* make start and count being in range */
4303 if (start < 0)
4304 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004305 if (count < 0)
4306 count = len + count + 1 - start;
4307 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004308 count = len - start;
4309 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004310 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004311
H. Peter Anvin8571f062019-09-23 16:40:03 -07004312 txt = (start < 0) ? "" : tok_text(t) + start;
4313 len = count;
4314 macro_start = make_tok_qstr(NULL, txt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004315
H. Peter Anvine2c80182005-01-15 22:15:51 +00004316 /*
4317 * We now have a macro name, an implicit parameter count of
4318 * zero, and a numeric token to use as an expansion. Create
4319 * and store an SMacro.
4320 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004321 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004322 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004323 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004324 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004325
H. Peter Anvin8b262472019-02-26 14:00:54 -08004326 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004327 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004328 goto done;
4329
H. Peter Anvine2c80182005-01-15 22:15:51 +00004330 last = tline;
4331 tline = expand_smacro(tline->next);
4332 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004333
H. Peter Anvin8b262472019-02-26 14:00:54 -08004334 pps.tptr = tline;
4335 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004336 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004337 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004339 if (!evalresult)
4340 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004341
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004343 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004344
H. Peter Anvine2c80182005-01-15 22:15:51 +00004345 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004346 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 free_tlist(origline);
4348 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004349 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004350
H. Peter Anvin8571f062019-09-23 16:40:03 -07004351 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004352
H. Peter Anvine2c80182005-01-15 22:15:51 +00004353 /*
4354 * We now have a macro name, an implicit parameter count of
4355 * zero, and a numeric token to use as an expansion. Create
4356 * and store an SMacro.
4357 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004358 define_smacro(mname, casesense, macro_start, NULL);
4359 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004360
H. Peter Anvind2354082019-08-27 16:38:48 -07004361 case PP_ALIASES:
4362 tline = tline->next;
4363 tline = expand_smacro(tline);
4364 do_aliases = pp_get_boolean_option(tline, do_aliases);
4365 break;
4366
H. Peter Anvine2c80182005-01-15 22:15:51 +00004367 case PP_LINE:
4368 /*
4369 * Syntax is `%line nnn[+mmm] [filename]'
4370 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004371 if (unlikely(pp_noline))
4372 goto done;
4373
H. Peter Anvine2c80182005-01-15 22:15:51 +00004374 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004375 tline = skip_white(tline);
4376 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004377 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004378 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004380 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004381 m = 1;
4382 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004383 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004385 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004386 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004387 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004389 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 tline = tline->next;
4391 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004392 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 src_set_linnum(k);
4394 istk->lineinc = m;
4395 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004396 char *fname = detoken(tline, false);
4397 src_set_fname(fname);
4398 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004399 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004400 break;
4401 }
4402
4403done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004404 free_tlist(origline);
4405 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004406}
4407
4408/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004409 * Ensure that a macro parameter contains a condition code and
4410 * nothing else. Return the condition code index if so, or -1
4411 * otherwise.
4412 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004413static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004414{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004415 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004416
H. Peter Anvin25a99342007-09-22 17:45:45 -07004417 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004418 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004419
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004420 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004421 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004422 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004423 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004424 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004425 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004426 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004427
H. Peter Anvin8571f062019-09-23 16:40:03 -07004428 return bsii(tok_text(t), (const char **)conditions,
4429 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004430}
4431
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004432static inline bool pp_concat_match(const Token *t, unsigned int mask)
4433{
4434 return t && (PP_CONCAT_MASK(t->type) & mask);
4435}
4436
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004437/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004438 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004439 * pasting, if @handle_explicit passed then explicit pasting
4440 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004441 * The @m array can contain a series of token types which are
4442 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004443 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004444static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004445 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004446{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004447 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004448 bool pasted = false;
4449 char *buf, *p;
4450 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004451
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004452 /*
4453 * The last token before pasting. We need it
4454 * to be able to connect new handled tokens.
4455 * In other words if there were a tokens stream
4456 *
4457 * A -> B -> C -> D
4458 *
4459 * and we've joined tokens B and C, the resulting
4460 * stream should be
4461 *
4462 * A -> BC -> D
4463 */
4464 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004465 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004466
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004467 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004468 prev_nonspace = NULL;
4469
4470 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004471 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004472
4473 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004474 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004475 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004476 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004477 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004478
4479 case TOK_PASTE:
4480 /* Explicit pasting */
4481 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004482 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004483
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004484 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004485 if (!prev_nonspace) {
4486 nasm_nonfatal("No lvalue found on pasting");
4487 tok = delete_Token(tok);
4488 break;
4489 }
4490
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004491 did_paste = true;
4492
4493 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004494 t = *prev_nonspace;
4495
4496 /* Delete leading whitespace */
4497 next = zap_white(t->next);
4498
4499 /* Delete the %+ token itself */
4500 nasm_assert(next == tok);
4501 next = delete_Token(next);
4502
4503 /* Delete trailing whitespace */
4504 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004505
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004506 /*
4507 * No ending token, this might happen in two
4508 * cases
4509 *
4510 * 1) There indeed no right token at all
4511 * 2) There is a bare "%define ID" statement,
4512 * and @ID does expand to whitespace.
4513 *
4514 * So technically we need to do a grammar analysis
4515 * in another stage of parsing, but for now lets don't
4516 * change the behaviour people used to. Simply allow
4517 * whitespace after paste token.
4518 */
4519 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004520 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004521 break;
4522 }
4523
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004524 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004525 p = mempcpy(p, tok_text(t), t->len);
4526 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004527 *p = '\0';
4528 delete_Token(t);
4529 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004530 nasm_free(buf);
4531
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004532 if (unlikely(!t)) {
4533 /*
4534 * No output at all? Replace with a single whitespace.
4535 * This should never happen.
4536 */
4537 t = new_White(NULL);
4538 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004539
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004540 *prev_nonspace = tok = t;
4541 while (t->next)
4542 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004543
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004544 /* Delete the second token and attach to the end of the list */
4545 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004546
4547 /* We want to restart from the head of the pasted token */
4548 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004549 break;
4550
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004551 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004552 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004553 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004554 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004555 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004556 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004557
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004558 if (i >= mnum)
4559 break;
4560
4561 len = tok->len;
4562 while (pp_concat_match(next, m[i].mask_tail)) {
4563 len += next->len;
4564 next = next->next;
4565 }
4566
4567 /* No match or no text to process */
4568 if (len == tok->len)
4569 break;
4570
4571 p = buf = nasm_malloc(len + 1);
4572 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004573 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004574 tok = delete_Token(tok);
4575 }
4576 *p = '\0';
4577 *prev_next = tok = t = tokenize(buf);
4578 nasm_free(buf);
4579
4580 /*
4581 * Connect pasted into original stream,
4582 * ie A -> new-tokens -> B
4583 */
4584 while (t->next)
4585 t = t->next;
4586 t->next = next;
4587 prev_next = prev_nonspace = &t->next;
4588 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004589 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004590 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004591
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004592 if (did_paste) {
4593 pasted = true;
4594 } else {
4595 prev_next = &tok->next;
4596 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4597 prev_nonspace = prev_next;
4598 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004599
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004600 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004601 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004602
4603 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004604}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004605
4606/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004607 * Computes the proper rotation of mmacro parameters
4608 */
4609static int mmac_rotate(const MMacro *mac, unsigned int n)
4610{
4611 if (--n < mac->nparam)
4612 n = (n + mac->rotate) % mac->nparam;
4613
4614 return n+1;
4615}
4616
4617/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004618 * expands to a list of tokens from %{x:y}
4619 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004620static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004621{
4622 Token *t = tline, **tt, *tm, *head;
4623 char *pos;
4624 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004625
H. Peter Anvin8571f062019-09-23 16:40:03 -07004626 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004627 nasm_assert(pos);
4628
4629 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004630 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004631
4632 /*
4633 * only macros params are accounted so
4634 * if someone passes %0 -- we reject such
4635 * value(s)
4636 */
4637 if (lst == 0 || fst == 0)
4638 goto err;
4639
4640 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004641 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4642 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004643 goto err;
4644
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004645 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4646 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004647
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004648 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004649 fst--, lst--;
4650
4651 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004652 * It will be at least one token. Note we
4653 * need to scan params until separator, otherwise
4654 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004655 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004656 j = (fst + mac->rotate) % mac->nparam;
4657 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004658 if (!tm)
4659 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004660 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004661 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004662 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004663 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004664 *tt = t, tt = &t->next, tm = tm->next;
4665 }
4666
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004667 if (fst < lst) {
4668 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004669 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004670 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004671 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004672 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004673 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004674 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004675 *tt = t, tt = &t->next, tm = tm->next;
4676 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004677 }
4678 } else {
4679 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004680 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004681 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004682 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004683 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004684 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004685 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004686 *tt = t, tt = &t->next, tm = tm->next;
4687 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004688 }
4689 }
4690
4691 *last = tt;
4692 return head;
4693
4694err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004695 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004696 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004697 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004698}
4699
H. Peter Anvin76690a12002-04-30 20:52:49 +00004700/*
4701 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004702 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004703 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004704 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004705static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004706{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004707 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004708 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004709 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004710
4711 tail = &thead;
4712 thead = NULL;
4713
H. Peter Anvine2c80182005-01-15 22:15:51 +00004714 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004715 bool change;
4716 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004717 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004718 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004719
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004720 tline = tline->next;
4721 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004722
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004723 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004724 case TOK_LOCAL_SYMBOL:
4725 type = TOK_ID;
4726 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4727 change = true;
4728 break;
4729 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004730 {
4731 Token *tt = NULL;
4732
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004733 change = true;
4734
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004735 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004736 nasm_nonfatal("`%s': not in a macro call", text);
4737 text = NULL;
4738 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004739 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004740
4741 if (strchr(text, ':')) {
4742 /*
4743 * seems we have a parameters range here
4744 */
4745 Token *head, **last;
4746 head = expand_mmac_params_range(mac, t, &last);
4747 if (head) {
4748 *tail = head;
4749 *last = tline;
4750 text = NULL;
4751 }
4752 break;
4753 }
4754
4755 switch (text[1]) {
4756 /*
4757 * We have to make a substitution of one of the
4758 * forms %1, %-1, %+1, %%foo, %0, %00.
4759 */
4760 case '0':
4761 if (!text[2]) {
4762 type = TOK_NUMBER;
4763 text = nasm_asprintf("%d", mac->nparam);
4764 break;
4765 }
4766 if (text[2] != '0' || text[3])
4767 goto invalid;
4768 /* a possible captured label == mac->params[0] */
4769 /* fall through */
4770 default:
4771 {
4772 unsigned long n;
4773 char *ep;
4774
4775 n = strtoul(text + 1, &ep, 10);
4776 if (unlikely(*ep))
4777 goto invalid;
4778
4779 if (n <= mac->nparam) {
4780 n = mmac_rotate(mac, n);
4781 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4782 }
4783 text = NULL;
4784 break;
4785 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004786 case '-':
4787 case '+':
4788 {
4789 int cc;
4790 unsigned long n;
4791 char *ep;
4792
4793 text = NULL;
4794
H. Peter Anvin8571f062019-09-23 16:40:03 -07004795 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004796 if (unlikely(*ep))
4797 goto invalid;
4798
4799 if (n && n < mac->nparam) {
4800 n = mmac_rotate(mac, n);
4801 tt = mac->params[n];
4802 }
4803 cc = find_cc(tt);
4804 if (cc == -1) {
4805 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004806 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004807 text = NULL;
4808 break;
4809 }
4810
4811 type = TOK_ID;
4812 if (text[1] == '-') {
4813 int ncc = inverse_ccs[cc];
4814 if (unlikely(ncc == -1)) {
4815 nasm_nonfatal("condition code `%s' is not invertible",
4816 conditions[cc]);
4817 break;
4818 }
4819 cc = ncc;
4820 }
4821 text = nasm_strdup(conditions[cc]);
4822 break;
4823 }
4824
4825 invalid:
4826 nasm_nonfatal("invalid macro parameter: `%s'", text);
4827 text = NULL;
4828 break;
4829 }
4830 break;
4831 }
4832
4833 case TOK_PREPROC_Q:
4834 if (mac) {
4835 type = TOK_ID;
4836 text = nasm_strdup(mac->iname);
4837 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004838 } else {
4839 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004840 }
4841 break;
4842
4843 case TOK_PREPROC_QQ:
4844 if (mac) {
4845 type = TOK_ID;
4846 text = nasm_strdup(mac->name);
4847 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004848 } else {
4849 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004850 }
4851 break;
4852
4853 case TOK_INDIRECT:
4854 {
4855 Token *tt;
4856
H. Peter Anvin8571f062019-09-23 16:40:03 -07004857 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004858 tt = expand_mmac_params(tt);
4859 tt = expand_smacro(tt);
4860 /* Why dup_tlist() here? We should own tt... */
4861 dup_tlist(tt, &tail);
4862 text = NULL;
4863 change = true;
4864 break;
4865 }
4866
4867 default:
4868 change = false;
4869 break;
4870 }
4871
4872 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004873 if (!text) {
4874 delete_Token(t);
4875 } else {
4876 *tail = t;
4877 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004878 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004879 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004881 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004882 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004883 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004884 tail = &t->next;
4885 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004886 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004887
H. Peter Anvineba20a72002-04-30 20:53:55 +00004888 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004889
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004890 if (changed) {
4891 const struct tokseq_match t[] = {
4892 {
4893 PP_CONCAT_MASK(TOK_ID) |
4894 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4895 PP_CONCAT_MASK(TOK_ID) |
4896 PP_CONCAT_MASK(TOK_NUMBER) |
4897 PP_CONCAT_MASK(TOK_FLOAT) |
4898 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4899 },
4900 {
4901 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4902 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4903 }
4904 };
4905 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4906 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004907
H. Peter Anvin76690a12002-04-30 20:52:49 +00004908 return thead;
4909}
4910
H. Peter Anvin322bee02019-08-10 01:38:06 -07004911static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004912
H. Peter Anvin76690a12002-04-30 20:52:49 +00004913/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004914 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004915 * a macro at all, it is simply copied to the output and the pointer
4916 * advanced. tpp should be a pointer to a pointer (usually the next
4917 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004918 * to point to the first token of the expansion, and *tpp updated to
4919 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004920 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004921 * If the expansion is empty, *tpp will be unchanged but **tpp will
4922 * be advanced past the macro call.
4923 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004924 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004925 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004926static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004927{
4928 Token **params = NULL;
4929 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004930 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004931 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004932 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004933 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004934 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004935 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004936 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004937
4938 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004939 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004940
H. Peter Anvin8571f062019-09-23 16:40:03 -07004941 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004942
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004943 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004944 smacro_deadman.levels--;
4945
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004946 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004947 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004948 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004949 smacro_deadman.triggered = true;
4950 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004951 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004952 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004953 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004954 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004955 Context *ctx = get_ctx(mname, &mname);
4956 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4957 } else {
4958 goto not_a_macro;
4959 }
4960
4961 /*
4962 * We've hit an identifier of some sort. First check whether the
4963 * identifier is a single-line macro at all, then think about
4964 * checking for parameters if necessary.
4965 */
4966 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004967 if (unlikely(m->alias && !do_aliases))
4968 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004969 if (!mstrcmp(m->name, mname, m->casesense))
4970 break;
4971 }
4972
4973 if (!m) {
4974 goto not_a_macro;
4975 }
4976
4977 /* Parse parameters, if applicable */
4978
4979 params = NULL;
4980 nparam = 0;
4981
4982 if (m->nparam == 0) {
4983 /*
4984 * Simple case: the macro is parameterless.
4985 * Nothing to parse; the expansion code will
4986 * drop the macro name token.
4987 */
4988 } else {
4989 /*
4990 * Complicated case: at least one macro with this name
4991 * exists and takes parameters. We must find the
4992 * parameters in the call, count them, find the SMacro
4993 * that corresponds to that form of the macro call, and
4994 * substitute for the parameters when we expand. What a
4995 * pain.
4996 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004997 Token *t;
4998 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004999
5000 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005001 tline = skip_white(tline);
5002 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005003 /*
5004 * This macro wasn't called with parameters: ignore
5005 * the call. (Behaviour borrowed from gnu cpp.)
5006 */
5007 goto not_a_macro;
5008 }
5009
5010 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005011 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005012 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005013 t = tline; /* tline points to leading ( */
5014
5015 while (paren) {
5016 t = t->next;
5017
5018 if (!t) {
5019 nasm_nonfatal("macro call expects terminating `)'");
5020 goto not_a_macro;
5021 }
5022
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005023 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005024 continue;
5025
H. Peter Anvin8571f062019-09-23 16:40:03 -07005026 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005027 case ',':
5028 if (!brackets)
5029 nparam++;
5030 break;
5031
5032 case '{':
5033 brackets++;
5034 break;
5035
5036 case '}':
5037 if (brackets > 0)
5038 brackets--;
5039 break;
5040
5041 case '(':
5042 if (!brackets)
5043 paren++;
5044 break;
5045
5046 case ')':
5047 if (!brackets)
5048 paren--;
5049 break;
5050
5051 default:
5052 break; /* Normal token */
5053 }
5054 }
5055
5056 /*
5057 * Look for a macro matching in both name and parameter count.
5058 * We already know any matches cannot be anywhere before the
5059 * current position of "m", so there is no reason to
5060 * backtrack.
5061 */
5062 while (1) {
5063 if (!m) {
5064 /*!
5065 *!macro-params-single [on] single-line macro calls with wrong parameter count
5066 *! warns about \i{single-line macros} being invoked
5067 *! with the wrong number of parameters.
5068 */
5069 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
5070 "single-line macro `%s' exists, "
5071 "but not taking %d parameter%s",
5072 mname, nparam, (nparam == 1) ? "" : "s");
5073 goto not_a_macro;
5074 }
5075
5076 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005077 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005078 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005079 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005080 break; /* Also good */
5081 }
5082 m = m->next;
5083 }
5084 }
5085
5086 if (m->in_progress)
5087 goto not_a_macro;
5088
5089 /* Expand the macro */
5090 m->in_progress = true;
5091
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005092 if (nparam) {
5093 /* Extract parameters */
5094 Token **phead, **pep;
5095 int white = 0;
5096 int brackets = 0;
5097 int paren;
5098 bool bracketed = false;
5099 bool bad_bracket = false;
5100 enum sparmflags flags;
5101
5102 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005103 paren = 1;
5104 nasm_newn(params, nparam);
5105 i = 0;
5106 flags = m->params[i].flags;
5107 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005108 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005109
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005110 while (paren) {
5111 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005112 char ch;
5113
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005114 tline = tline->next;
5115
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005116 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005117 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005118
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005119 ch = 0;
5120 skip = false;
5121
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005122
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005123 switch (tline->type) {
5124 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005125 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005126 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005127 break;
5128
5129 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005130 if (!(flags & SPARM_NOSTRIP)) {
5131 if (brackets || *phead)
5132 white++; /* Keep interior whitespace */
5133 skip = true;
5134 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005135 break;
5136
5137 default:
5138 break;
5139 }
5140
5141 switch (ch) {
5142 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005143 if (!brackets && !(flags & SPARM_GREEDY)) {
5144 i++;
5145 nasm_assert(i < nparam);
5146 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005147 *pep = NULL;
5148 bracketed = false;
5149 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005150 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005151 }
5152 break;
5153
5154 case '{':
5155 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005156 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5157 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005158 }
5159 brackets++;
5160 break;
5161
5162 case '}':
5163 if (brackets > 0) {
5164 if (!--brackets)
5165 skip = bracketed;
5166 }
5167 break;
5168
5169 case '(':
5170 if (!brackets)
5171 paren++;
5172 break;
5173
5174 case ')':
5175 if (!brackets) {
5176 paren--;
5177 if (!paren) {
5178 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005179 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005180 }
5181 }
5182 break;
5183
5184 default:
5185 break; /* Normal token */
5186 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005187
5188 if (!skip) {
5189 Token *t;
5190
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005191 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005192
5193 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005194 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005195 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005196 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005197 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005198 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005199 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005200 }
5201 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005202
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005203 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005204 * Possible further processing of parameters. Note that the
5205 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005206 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005207 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005208 enum sparmflags flags = m->params[i].flags;
5209
5210 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005211 /* Evaluate this parameter as a number */
5212 struct ppscan pps;
5213 struct tokenval tokval;
5214 expr *evalresult;
5215 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005216
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005217 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5218 pps.ntokens = -1;
5219 tokval.t_type = TOKEN_INVALID;
5220 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005221
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005222 free_tlist(eval_param);
5223 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005224
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005225 if (!evalresult) {
5226 /* Nothing meaningful to do */
5227 } else if (tokval.t_type) {
5228 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5229 } else if (!is_simple(evalresult)) {
5230 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5231 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005232 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005233 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005234 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005235
5236 if (flags & SPARM_STR) {
5237 /* Convert expansion to a quoted string */
5238 char *arg;
5239 Token *qs;
5240
5241 qs = expand_smacro_noreset(params[i]);
5242 arg = detoken(qs, false);
5243 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005244 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005245 nasm_free(arg);
5246 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005247 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005248 }
5249
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005250 /* Note: we own the expansion this returns. */
5251 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005252
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005253 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005254 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005255 tline = tafter;
5256
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005257 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005258 cond_comma = false;
5259
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005260 while (t) {
5261 enum pp_token_type type = t->type;
5262 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005263
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005264 switch (type) {
5265 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005266 delete_Token(t);
5267 t = dup_Token(tline, mstart);
5268 break;
5269
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005270 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005271 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005272 size_t mlen = strlen(m->name);
5273 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005274 char *p;
5275
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005276 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005277 if (t->type == TOK_LOCAL_MACRO) {
5278 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005279 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005280 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005281
H. Peter Anvin8571f062019-09-23 16:40:03 -07005282 psp = tok_text(mstart);
5283 get_ctx(psp, &pep);
5284 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005285
H. Peter Anvin8571f062019-09-23 16:40:03 -07005286 len = mlen + plen;
5287 p = nasm_malloc(len + 1);
5288 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005289 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005290 len = mlen;
5291 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005292 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005293 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005294 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005295 set_text_free(t, p, len);
5296
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005297 t->next = tline;
5298 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005299 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005300
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005301 case TOK_COND_COMMA:
5302 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005303 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005304 break;
5305
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005306 case TOK_ID:
5307 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005308 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005309 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005310 /*
5311 * Chain this into the target line *before* expanding,
5312 * that way we pick up any arguments to the new macro call,
5313 * if applicable.
5314 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005315 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005316 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005317 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005318 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005319 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005320 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005321 default:
5322 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005323 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005324 nasm_assert(!tup && param < nparam);
5325 delete_Token(t);
5326 t = NULL;
5327 tup = tnext;
5328 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005329 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005330 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005331 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005332 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005333 }
5334
5335 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005336 Token *endt = tline;
5337
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005338 tline = t;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005339 while (!cond_comma && t && t != endt)
5340 cond_comma = t->type != TOK_WHITESPACE;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005341 }
5342
5343 if (tnext) {
5344 t = tnext;
5345 } else {
5346 t = tup;
5347 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005348 }
5349 }
5350
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005351 **tpp = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005352 for (t = tline; t != tafter; t = t->next)
5353 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005354
5355 m->in_progress = false;
5356
5357 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005358 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005359 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005360
5361 /*
5362 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005363 * and then advance to the next input token. Note that this is
5364 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005365 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005366not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005367 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005368 m = NULL;
5369done:
5370 smacro_deadman.levels++;
5371 if (unlikely(params))
5372 free_tlist_array(params, nparam);
5373 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005374}
5375
5376/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005377 * Expand all single-line macro calls made in the given line.
5378 * Return the expanded version of the line. The original is deemed
5379 * to be destroyed in the process. (In reality we'll just move
5380 * Tokens from input to output a lot of the time, rather than
5381 * actually bothering to destroy and replicate.)
5382 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005383static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005384{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005385 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005386 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5387 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005388 return expand_smacro_noreset(tline);
5389}
5390
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005391static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005392{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005393 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005394 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005395
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005396 if (!org_tline)
5397 return NULL; /* Empty input */
5398
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005399 /*
5400 * Trick: we should avoid changing the start token pointer since it can
5401 * be contained in "next" field of other token. Because of this
5402 * we allocate a copy of first token and work with it; at the end of
5403 * routine we copy it back
5404 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005405 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005406
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005407 /*
5408 * Pretend that we always end up doing expansion on the first pass;
5409 * that way %+ get processed. However, if we process %+ before the
5410 * first pass, we end up with things like MACRO %+ TAIL trying to
5411 * look up the macro "MACROTAIL", which we don't want.
5412 */
5413 expanded = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005414 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005415 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005416 {
5417 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005418 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5419 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005420 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5421 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005422 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5423 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005424 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5425 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5426 }
5427 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005428 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005429
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005430 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005431 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005432
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005433 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005434 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005435
5436 /*
5437 * Now scan the entire line and look for successive TOK_IDs
5438 * that resulted after expansion (they can't be produced by
5439 * tokenize()). The successive TOK_IDs should be concatenated.
5440 * Also we look for %+ tokens and concatenate the tokens
5441 * before and after them (without white spaces in between).
5442 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005443 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5444 break; /* Done again! */
5445
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005446 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005447 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005448
H. Peter Anvin8571f062019-09-23 16:40:03 -07005449 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005450 /*
5451 * The expression expanded to empty line;
5452 * we can't return NULL because of the "trick" above.
5453 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005454 */
5455
5456 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005457 }
5458
H. Peter Anvin8571f062019-09-23 16:40:03 -07005459 steal_Token(org_tline, tline);
5460 org_tline->next = tline->next;
5461 delete_Token(tline);
5462
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005463 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005464}
5465
5466/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005467 * Similar to expand_smacro but used exclusively with macro identifiers
5468 * right before they are fetched in. The reason is that there can be
5469 * identifiers consisting of several subparts. We consider that if there
5470 * are more than one element forming the name, user wants a expansion,
5471 * otherwise it will be left as-is. Example:
5472 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005473 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005474 *
5475 * the identifier %$abc will be left as-is so that the handler for %define
5476 * will suck it and define the corresponding value. Other case:
5477 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005478 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005479 *
5480 * In this case user wants name to be expanded *before* %define starts
5481 * working, so we'll expand %$abc into something (if it has a value;
5482 * otherwise it will be left as-is) then concatenate all successive
5483 * PP_IDs into one.
5484 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005485static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005486{
5487 Token *cur, *oldnext = NULL;
5488
H. Peter Anvin734b1882002-04-30 21:01:08 +00005489 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005490 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005491
5492 cur = tline;
5493 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005494 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5495 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005496 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005497
5498 /* If identifier consists of just one token, don't expand */
5499 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005500 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005501
H. Peter Anvine2c80182005-01-15 22:15:51 +00005502 if (cur) {
5503 oldnext = cur->next; /* Detach the tail past identifier */
5504 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005505 }
5506
H. Peter Anvin734b1882002-04-30 21:01:08 +00005507 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005508
H. Peter Anvine2c80182005-01-15 22:15:51 +00005509 if (cur) {
5510 /* expand_smacro possibly changhed tline; re-scan for EOL */
5511 cur = tline;
5512 while (cur && cur->next)
5513 cur = cur->next;
5514 if (cur)
5515 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005516 }
5517
5518 return tline;
5519}
5520
5521/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005522 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005523 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005524 * to check for an initial label - that's taken care of in
5525 * expand_mmacro - but must check numbers of parameters. Guaranteed
5526 * to be called with tline->type == TOK_ID, so the putative macro
5527 * name is easy to find.
5528 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005529static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005530{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005531 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005532 Token **params;
5533 int nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005534 const char *finding = tok_text(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005535
H. Peter Anvin8571f062019-09-23 16:40:03 -07005536 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005537
5538 /*
5539 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005540 * name which isn't already excluded by macro cycle removal.
5541 * (The cycle removal test here helps optimize the case of wrapping
5542 * instructions, and is cheap to do here.)
5543 *
5544 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005545 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005546 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005547 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005548 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005549 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005550 (m->in_progress != 1 || m->max_depth > 0))
5551 break; /* Found something that needs consideration */
5552 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005553 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005554 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005555
5556 /*
5557 * OK, we have a potential macro. Count and demarcate the
5558 * parameters.
5559 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005560 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005561
5562 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005563 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005564 * structure that handles this number.
5565 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005566 while (m) {
5567 if (m->nparam_min <= nparam
5568 && (m->plus || nparam <= m->nparam_max)) {
5569 /*
5570 * This one is right. Just check if cycle removal
5571 * prohibits us using it before we actually celebrate...
5572 */
5573 if (m->in_progress > m->max_depth) {
5574 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005575 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005576 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005577 }
5578 nasm_free(params);
5579 return NULL;
5580 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005581 /*
5582 * It's right, and we can use it. Add its default
5583 * parameters to the end of our list if necessary.
5584 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005585 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005586 int newnparam = m->nparam_min + m->ndefs;
5587 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5588 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5589 (newnparam - nparam) * sizeof(*params));
5590 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005591 }
5592 /*
5593 * If we've gone over the maximum parameter count (and
5594 * we're in Plus mode), ignore parameters beyond
5595 * nparam_max.
5596 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005597 if (m->plus && nparam > m->nparam_max)
5598 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005599
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005600 /*
5601 * If nparam was adjusted above, make sure the list is still
5602 * NULL-terminated.
5603 */
5604 params[nparam+1] = NULL;
5605
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005606 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005607 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005608 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005609 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005610 }
5611 /*
5612 * This one wasn't right: look for the next one with the
5613 * same name.
5614 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005615 list_for_each(m, m->next)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005616 if (!mstrcmp(m->name, tok_text(tline), m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005617 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005618 }
5619
5620 /*
5621 * After all that, we didn't find one with the right number of
5622 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005623 *!
5624 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5625 *! warns about \i{multi-line macros} being invoked
5626 *! with the wrong number of parameters. See \k{mlmacover} for an
5627 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005628 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005629 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5630 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005631 tok_text(tline), nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005632 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005633 return NULL;
5634}
5635
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005636
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005637#if 0
5638
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005639/*
5640 * Save MMacro invocation specific fields in
5641 * preparation for a recursive macro expansion
5642 */
5643static void push_mmacro(MMacro *m)
5644{
5645 MMacroInvocation *i;
5646
5647 i = nasm_malloc(sizeof(MMacroInvocation));
5648 i->prev = m->prev;
5649 i->params = m->params;
5650 i->iline = m->iline;
5651 i->nparam = m->nparam;
5652 i->rotate = m->rotate;
5653 i->paramlen = m->paramlen;
5654 i->unique = m->unique;
5655 i->condcnt = m->condcnt;
5656 m->prev = i;
5657}
5658
5659
5660/*
5661 * Restore MMacro invocation specific fields that were
5662 * saved during a previous recursive macro expansion
5663 */
5664static void pop_mmacro(MMacro *m)
5665{
5666 MMacroInvocation *i;
5667
5668 if (m->prev) {
5669 i = m->prev;
5670 m->prev = i->prev;
5671 m->params = i->params;
5672 m->iline = i->iline;
5673 m->nparam = i->nparam;
5674 m->rotate = i->rotate;
5675 m->paramlen = i->paramlen;
5676 m->unique = i->unique;
5677 m->condcnt = i->condcnt;
5678 nasm_free(i);
5679 }
5680}
5681
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005682#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005683
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005684/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005685 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005686 */
5687static void list_mmacro_call(const MMacro *m)
5688{
5689 const char prefix[] = " ;;; [macro] ";
5690 size_t namelen, size;
5691 char *buf, *p;
5692 unsigned int i;
5693 const Token *t;
5694
5695 namelen = strlen(m->iname);
5696 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5697
5698 for (i = 1; i <= m->nparam; i++) {
5699 int j = 0;
5700 size += 3; /* Braces and space/comma */
5701 list_for_each(t, m->params[i]) {
5702 if (j++ >= m->paramlen[i])
5703 break;
5704 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5705 }
5706 }
5707
5708 buf = p = nasm_malloc(size);
5709 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5710 p = mempcpy(p, m->iname, namelen);
5711 *p++ = ' ';
5712
5713 for (i = 1; i <= m->nparam; i++) {
5714 int j = 0;
5715 *p++ = '{';
5716 list_for_each(t, m->params[i]) {
5717 if (j++ >= m->paramlen[i])
5718 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005719 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005720 }
5721 *p++ = '}';
5722 *p++ = ',';
5723 }
5724
5725 *--p = '\0'; /* Replace last delimeter with null */
5726 lfmt->line(LIST_MACRO, -1, buf);
5727 nasm_free(buf);
5728}
5729
5730/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005731 * Expand the multi-line macro call made by the given line, if
5732 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005733 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005734 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005735static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005736{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005737 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005738 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005739 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005740 Token **params, *t, *tt;
5741 MMacro *m;
5742 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005743 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005744 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005745 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005746
5747 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005748 t = skip_white(t);
5749 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005750 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005751 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005752 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005753 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005754 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07005755 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005756 Token *last;
5757 /*
5758 * We have an id which isn't a macro call. We'll assume
5759 * it might be a label; we'll also check to see if a
5760 * colon follows it. Then, if there's another id after
5761 * that lot, we'll check it again for macro-hood.
5762 */
5763 label = last = t;
5764 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005765 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005766 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005767 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005768 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005769 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005770 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005771 last = t, t = t->next;
5772 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005773 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005774 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005775 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005776 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005777 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005778 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005779
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005780 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5781 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5782 if (!mmacro_deadman.triggered) {
5783 nasm_nonfatal("interminable multiline macro recursion");
5784 mmacro_deadman.triggered = true;
5785 }
5786 return 0;
5787 }
5788
5789 mmacro_deadman.total++;
5790 mmacro_deadman.levels++;
5791
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005792 /*
5793 * Fix up the parameters: this involves stripping leading and
5794 * trailing whitespace, then stripping braces if they are
5795 * present.
5796 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005797 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005798
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005799 nasm_assert(params[nparam+1] == NULL);
5800
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005801 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005802 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005803 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005804 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005805 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005806
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005807 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005808 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005809 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005810 brace = 1;
5811 braced = true;
5812 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005813 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005814
5815 params[i] = t;
5816 for (; t; t = t->next) {
5817 if (tok_white(t)) {
5818 white++;
5819 continue;
5820 }
5821
5822 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005823 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005824 case ',':
5825 if (comma && !brace)
5826 goto endparam;
5827 break;
5828
5829 case '{':
5830 brace++;
5831 break;
5832
5833 case '}':
5834 brace--;
5835 if (braced && !brace) {
5836 paramlen[i] += white;
5837 goto endparam;
5838 }
5839 break;
5840
5841 default:
5842 break;
5843 }
5844 }
5845
5846 paramlen[i] += white + 1;
5847 white = 0;
5848 }
5849 endparam:
5850 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005851 }
5852
5853 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005854 * OK, we have a MMacro structure together with a set of
5855 * parameters. We must now go through the expansion and push
5856 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005857 * parameter tokens and macro-local tokens doesn't get done
5858 * until the single-line macro substitution process; this is
5859 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005860 * later through %rotate and give the right semantics for
5861 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005862 *
5863 * First, push an end marker on to istk->expansion, mark this
5864 * macro as in progress, and set up its invocation-specific
5865 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005866 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005867 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005868 ll->next = istk->expansion;
5869 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005870 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005871
5872 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005873 * Save the previous MMacro expansion in the case of
5874 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005875 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005876#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005877 if (m->max_depth && m->in_progress)
5878 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005879#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005880
5881 m->in_progress ++;
5882 m->params = params;
5883 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005884 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005885 m->nparam = nparam;
5886 m->rotate = 0;
5887 m->paramlen = paramlen;
5888 m->unique = unique++;
5889 m->lineno = 0;
5890 m->condcnt = 0;
5891
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005892 m->mstk = istk->mstk;
5893 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005894
5895 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005896 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005897 ll->next = istk->expansion;
5898 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005899 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005900 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005901
5902 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005903 * If we had a label, and this macro definition does not include
5904 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005905 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005906 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005907 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005908 /*
5909 * We had a label. If this macro contains an %00 parameter,
5910 * save the value as a special parameter (which is what it
5911 * is), otherwise push it as the first line of the macro
5912 * expansion.
5913 */
5914 if (m->capture_label) {
5915 params[0] = dup_Token(NULL, label);
5916 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005917 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005918 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005919 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005920 ll->finishes = NULL;
5921 ll->next = istk->expansion;
5922 istk->expansion = ll;
5923 ll->first = startline;
5924 if (!dont_prepend) {
5925 while (label->next)
5926 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005927 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005928 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005929 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005930 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005931
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005932 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005933
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005934 if (list_option('m') && !m->nolist)
5935 list_mmacro_call(m);
5936
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005937 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005938}
5939
H. Peter Anvin130736c2016-02-17 20:27:41 -08005940/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005941 * This function decides if an error message should be suppressed.
5942 * It will never be called with a severity level of ERR_FATAL or
5943 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005944 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005945static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005946{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005947 /*
5948 * If we're in a dead branch of IF or something like it, ignore the error.
5949 * However, because %else etc are evaluated in the state context
5950 * of the previous branch, errors might get lost:
5951 * %if 0 ... %else trailing garbage ... %endif
5952 * So %else etc should set the ERR_PP_PRECOND flag.
5953 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005954 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005955 ((severity & ERR_PP_PRECOND) ?
5956 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005957 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005958 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005959
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005960 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005961}
5962
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005963static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005964stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005965{
5966 (void)s;
5967 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005968 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005969
H. Peter Anvin8571f062019-09-23 16:40:03 -07005970 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08005971}
5972
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005973static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005974stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005975{
5976 (void)s;
5977 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005978 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005979
H. Peter Anvin8571f062019-09-23 16:40:03 -07005980 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08005981}
5982
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005983static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005984stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005985{
5986 (void)s;
5987 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005988 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005989
H. Peter Anvin8571f062019-09-23 16:40:03 -07005990 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005991}
5992
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005993static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005994stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005995{
H. Peter Anvin8b262472019-02-26 14:00:54 -08005996 (void)s;
5997 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005998 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005999
6000 switch (globalbits) {
6001 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006002 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006003 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006004 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006005 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006006 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006007 default:
6008 panic();
6009 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006010}
6011
H. Peter Anvin8b262472019-02-26 14:00:54 -08006012/* Add magic standard macros */
6013struct magic_macros {
6014 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006015 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006016 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006017};
6018static const struct magic_macros magic_macros[] =
6019{
H. Peter Anvind2354082019-08-27 16:38:48 -07006020 { "__?FILE?__", 0, stdmac_file },
6021 { "__?LINE?__", 0, stdmac_line },
6022 { "__?BITS?__", 0, stdmac_bits },
6023 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006024 { NULL, 0, NULL }
6025};
6026
6027static void pp_add_magic_stdmac(void)
6028{
6029 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006030 SMacro tmpl;
6031
6032 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006033
6034 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006035 tmpl.nparam = m->nparam;
6036 tmpl.expand = m->func;
6037 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006038 }
6039}
6040
H. Peter Anvin734b1882002-04-30 21:01:08 +00006041static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006042pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006043{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006044 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006045 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006046
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006047 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006048 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006049 nested_mac_count = 0;
6050 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006051 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006052 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006053 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006054 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07006055 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07006056
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006057 if (!use_loaded)
6058 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6059 memset(use_loaded, 0, use_package_count * sizeof(bool));
6060
H. Peter Anvin6686de22019-08-10 05:33:14 -07006061 /* First set up the top level input file */
6062 nasm_new(istk);
6063 istk->fp = nasm_open_read(file, NF_TEXT);
6064 src_set(0, file);
6065 istk->lineinc = 1;
6066 if (!istk->fp)
6067 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6068
6069 strlist_add(deplist, file);
6070
6071 /*
6072 * Set up the stdmac packages as a virtual include file,
6073 * indicated by a null file pointer.
6074 */
6075 nasm_new(inc);
6076 inc->next = istk;
6077 inc->fname = src_set_fname(NULL);
6078 inc->nolist = !list_option('b');
6079 istk = inc;
6080 lfmt->uplevel(LIST_INCLUDE, 0);
6081
H. Peter Anvin8b262472019-02-26 14:00:54 -08006082 pp_add_magic_stdmac();
6083
H. Peter Anvinf7606612016-07-13 14:23:48 -07006084 if (tasm_compatible_mode)
6085 pp_add_stdmac(nasm_stdmac_tasm);
6086
6087 pp_add_stdmac(nasm_stdmac_nasm);
6088 pp_add_stdmac(nasm_stdmac_version);
6089
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006090 if (extrastdmac)
6091 pp_add_stdmac(extrastdmac);
6092
H. Peter Anvinf7606612016-07-13 14:23:48 -07006093 stdmacpos = stdmacros[0];
6094 stdmacnext = &stdmacros[1];
6095
H. Peter Anvind2456592008-06-19 15:04:18 -07006096 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006097
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006098 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006099 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006100 * other builtins, because it is special -- it varies between
6101 * passes -- but there is really no particular reason to make it
6102 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006103 *
6104 * 0 = dependencies only
6105 * 1 = preparatory passes
6106 * 2 = final pass
6107 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006108 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006109 switch (mode) {
6110 case PP_NORMAL:
6111 apass = pass_final() ? 2 : 1;
6112 break;
6113 case PP_DEPS:
6114 apass = 0;
6115 break;
6116 case PP_PREPROC:
6117 apass = 3;
6118 break;
6119 default:
6120 panic();
6121 }
6122
H. Peter Anvin8571f062019-09-23 16:40:03 -07006123 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006124}
6125
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006126static void pp_init(void)
6127{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006128}
6129
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006130/*
6131 * Get a line of tokens. If we popped the macro expansion/include stack,
6132 * we return a pointer to the dummy token tok_pop; at that point if
6133 * istk is NULL then we have reached end of input;
6134 */
6135static Token tok_pop; /* Dummy token placeholder */
6136
6137static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006138{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006139 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006140 Line *l = istk->expansion;
6141 Token *tline = NULL;
6142 Token *dtline;
6143
H. Peter Anvine2c80182005-01-15 22:15:51 +00006144 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006145 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006146 * buffer or from the input file.
6147 */
6148 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006149 while (l && l->finishes) {
6150 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006151
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006152 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006153 /*
6154 * This is a macro-end marker for a macro with no
6155 * name, which means it's not really a macro at all
6156 * but a %rep block, and the `in_progress' field is
6157 * more than 1, meaning that we still need to
6158 * repeat. (1 means the natural last repetition; 0
6159 * means termination by %exitrep.) We have
6160 * therefore expanded up to the %endrep, and must
6161 * push the whole block on to the expansion buffer
6162 * again. We don't bother to remove the macro-end
6163 * marker: we'd only have to generate another one
6164 * if we did.
6165 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006166 fm->in_progress--;
6167 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006168 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006169 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006170
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006171 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006172 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006173 tail = &ll->first;
6174
6175 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006176 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006177 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006178 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006179 }
6180 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006181 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006182 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006183 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006184 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006185 MMacro *m = istk->mstk.mstk;
6186
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006187 /*
6188 * Check whether a `%rep' was started and not ended
6189 * within this macro expansion. This can happen and
6190 * should be detected. It's a fatal error because
6191 * I'm too confused to work out how to recover
6192 * sensibly from it.
6193 */
6194 if (defining) {
6195 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006196 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006197 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006198 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006199 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006200 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006201
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006202 /*
6203 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006204 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006205 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006206 istk->mstk = m->mstk;
6207 if (m->name) {
6208 /*
6209 * This was a real macro call, not a %rep, and
6210 * therefore the parameter information needs to
6211 * be freed and the iteration count/nesting
6212 * depth adjusted.
6213 */
6214
6215 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006216 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006217 * If all mmacro processing done,
6218 * clear all counters and the deadman
6219 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006220 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006221 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006222 }
6223
Adam Majer91e72402017-07-25 10:42:01 +02006224#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006225 if (m->prev) {
6226 pop_mmacro(m);
6227 fm->in_progress --;
6228 } else
Adam Majer91e72402017-07-25 10:42:01 +02006229#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006230 {
6231 nasm_free(m->params);
6232 free_tlist(m->iline);
6233 nasm_free(m->paramlen);
6234 fm->in_progress = 0;
6235 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006236 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006237
6238 /*
6239 * FIXME It is incorrect to always free_mmacro here.
6240 * It leads to usage-after-free.
6241 *
6242 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6243 */
6244#if 0
6245 else
6246 free_mmacro(m);
6247#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006248 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006249 istk->expansion = l->next;
6250 nasm_free(l);
6251 lfmt->downlevel(LIST_MACRO);
6252 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006253 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006254
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006255 do { /* until we get a line we can use */
6256 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006257
6258 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006259 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006260 int32_t lineno;
6261
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006262 if (istk->mstk.mstk) {
6263 istk->mstk.mstk->lineno++;
6264 if (istk->mstk.mstk->fname)
6265 lineno = istk->mstk.mstk->lineno +
6266 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006267 else
6268 lineno = 0; /* Defined at init time or builtin */
6269 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006270 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006271 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006272
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006273 tline = l->first;
6274 istk->expansion = l->next;
6275 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006276
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006277 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006278 if (!istk->nolist)
6279 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006280 nasm_free(line);
6281 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006282 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006283 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006284 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006285 } else {
6286 /*
6287 * The current file has ended; work down the istk
6288 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006289 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006290 if (i->fp)
6291 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006292 if (i->conds) {
6293 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006294 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006295 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006296 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006297 if (i->next)
6298 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006299 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006300 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006301 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006302 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006303 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006304 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006305
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006306 /*
6307 * We must expand MMacro parameters and MMacro-local labels
6308 * _before_ we plunge into directive processing, to cope
6309 * with things like `%define something %1' such as STRUC
6310 * uses. Unless we're _defining_ a MMacro, in which case
6311 * those tokens should be left alone to go into the
6312 * definition; and unless we're in a non-emitting
6313 * condition, in which case we don't want to meddle with
6314 * anything.
6315 */
6316 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006317 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006318 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006319 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006320
H. Peter Anvine2c80182005-01-15 22:15:51 +00006321 /*
6322 * Check the line to see if it's a preprocessor directive.
6323 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006324 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6325 if (dtline)
6326 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006327 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006328 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006329 * We're defining a multi-line macro. We emit nothing
6330 * at all, and just
6331 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006332 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006333 MMacro *mmac = defining->dstk.mmac;
6334
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006335 Line *l = nasm_malloc(sizeof(Line));
6336 l->next = defining->expansion;
6337 l->first = tline;
6338 l->finishes = NULL;
6339 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006340
6341 /*
6342 * Remember if this mmacro expansion contains %00:
6343 * if it does, we will have to handle leading labels
6344 * specially.
6345 */
6346 if (mmac) {
6347 const Token *t;
6348 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006349 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006350 mmac->capture_label = true;
6351 }
6352 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006353 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006354 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006355 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006356 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006357 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006358 * directive so we keep our place correctly.
6359 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006360 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006361 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006362 /*
6363 * We're in a %rep block which has been terminated, so
6364 * we're walking through to the %endrep without
6365 * emitting anything. Emit nothing at all, not even a
6366 * blank line: when we emerge from the %rep block we'll
6367 * give a line-number directive so we keep our place
6368 * correctly.
6369 */
6370 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006371 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006372 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006373 if (!expand_mmacro(tline))
6374 return tline;
6375 }
6376 }
6377}
6378
6379static char *pp_getline(void)
6380{
6381 char *line = NULL;
6382 Token *tline;
6383
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006384 while (true) {
6385 tline = pp_tokline();
6386 if (tline == &tok_pop) {
6387 /*
6388 * We popped the macro/include stack. If istk is empty,
6389 * we are at end of input, otherwise just loop back.
6390 */
6391 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006392 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006393 } else {
6394 /*
6395 * De-tokenize the line and emit it.
6396 */
6397 line = detoken(tline, true);
6398 free_tlist(tline);
6399 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006400 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006401 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006402
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006403 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006404 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006405 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006406 nasm_free(buf);
6407 }
6408
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006409 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006410}
6411
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006412static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006413{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006414 if (defining) {
6415 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006416 nasm_nonfatal("end of file while still defining macro `%s'",
6417 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006418 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006419 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006420 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006421
6422 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006423 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006424 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006425
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006426 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006427 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006428 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006429 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006430 Include *i = istk;
6431 istk = istk->next;
6432 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006433 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006434 }
6435 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006436 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006437 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006438}
6439
6440static void pp_cleanup_session(void)
6441{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006442 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006443 free_llist(predef);
6444 predef = NULL;
6445 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006446 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006447}
6448
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006449static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006450{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006451 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006452}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006453
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006454static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006455{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006456 Token *inc, *space, *name;
6457 Line *l;
6458
H. Peter Anvin734b1882002-04-30 21:01:08 +00006459 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006460 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006461 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006462
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006463 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006464 l->next = predef;
6465 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006466 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006467 predef = l;
6468}
6469
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006470static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006471{
6472 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006473 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006474 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006475
6476 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006477 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006478 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006479 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006480 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006481 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006482 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006483 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006484
H. Peter Anvin8571f062019-09-23 16:40:03 -07006485 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006486 if (space->next->type != TOK_PREPROC_ID &&
6487 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006488 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006489
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006490 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006491 l->next = predef;
6492 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006493 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006494 predef = l;
6495}
6496
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006497static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006498{
6499 Token *def, *space;
6500 Line *l;
6501
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006502 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006503 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006504 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006505
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006506 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006507 l->next = predef;
6508 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006509 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006510 predef = l;
6511}
6512
H. Peter Anvin05990342018-06-11 13:32:42 -07006513/* Insert an early preprocessor command that doesn't need special handling */
6514static void pp_pre_command(const char *what, char *string)
6515{
6516 char *cmd;
6517 Token *def, *space;
6518 Line *l;
6519
6520 def = tokenize(string);
6521 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006522 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006523 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6524 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6525 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006526 }
6527
6528 l = nasm_malloc(sizeof(Line));
6529 l->next = predef;
6530 l->first = def;
6531 l->finishes = NULL;
6532 predef = l;
6533}
6534
H. Peter Anvinf7606612016-07-13 14:23:48 -07006535static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006536{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006537 macros_t **mp;
6538
6539 /* Find the end of the list and avoid duplicates */
6540 for (mp = stdmacros; *mp; mp++) {
6541 if (*mp == macros)
6542 return; /* Nothing to do */
6543 }
6544
6545 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6546
6547 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006548}
6549
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006550static void pp_extra_stdmac(macros_t *macros)
6551{
6552 extrastdmac = macros;
6553}
6554
H. Peter Anvin8571f062019-09-23 16:40:03 -07006555/* Create a numeric token */
6556static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006557{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006558 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006559 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006560 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006561}
6562
H. Peter Anvin8571f062019-09-23 16:40:03 -07006563/* Create a quoted string token */
6564static Token *make_tok_qstr(Token *next, const char *str)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006565{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006566 size_t len = strlen(str);
6567 char *p = nasm_quote(str, &len);
6568 return new_Token_free(next, TOK_STRING, p, len);
6569}
6570
6571/* Create a single-character operator token */
6572static Token *make_tok_char(Token *next, char op)
6573{
6574 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6575 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006576 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006577}
6578
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006579static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006580{
6581 if (!m)
6582 return;
6583
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006584 /* We need to print the mstk.mmac list in reverse order */
6585 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006586
6587 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006588 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006589 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006590 }
6591}
6592
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006593static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006594{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006595 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006596
H. Peter Anvinddb29062018-12-11 00:06:29 -08006597 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6598 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006599
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006600 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006601 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006602
H. Peter Anvinddb29062018-12-11 00:06:29 -08006603 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006604}
6605
H. Peter Anvine7469712016-02-18 02:20:59 -08006606const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006607 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006608 pp_reset,
6609 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006610 pp_cleanup_pass,
6611 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006612 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006613 pp_pre_define,
6614 pp_pre_undefine,
6615 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006616 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006617 pp_include_path,
6618 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006619 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006620};