blob: c335395110fac762b2db65dcb56391a6ba5ca701 [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);
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -0700616static Token *make_tok_qstr_len(Token *next, const char *str, size_t len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700617static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700618static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700619 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700620static Token *new_Token_free(Token * next, enum pp_token_type type,
621 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700622static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700623static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700624static Token *delete_Token(Token *t);
625static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700626static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700627get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000628
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700629/* Safe test for token type, false on x == NULL */
630static inline bool tok_type(const Token *x, enum pp_token_type t)
631{
632 return x && x->type == t;
633}
634
635/* Whitespace token? */
636static inline bool tok_white(const Token *x)
637{
638 return tok_type(x, TOK_WHITESPACE);
639}
640
641/* Skip past any whitespace */
642static inline Token *skip_white(Token *x)
643{
644 while (tok_white(x))
645 x = x->next;
646
647 return x;
648}
649
650/* Delete any whitespace */
651static Token *zap_white(Token *x)
652{
653 while (tok_white(x))
654 x = delete_Token(x);
655
656 return x;
657}
658
H. Peter Anvin8571f062019-09-23 16:40:03 -0700659/*
660 * Single special character tests. The use of & rather than && is intentional; it
661 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
662 * a smart compiler should turn it into a 16-bit memory reference.
663 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700664static inline bool tok_is(const Token *x, char c)
665{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700666 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700667}
668
669/* True if any other kind of token that "c", but not NULL */
670static inline bool tok_isnt(const Token *x, char c)
671{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700672 return x && !((x->text.a[0] == c) & !x->text.a[1]);
673}
674
675/*
676 * Unquote a token if it is a string, and set its type to
677 * TOK_INTERNAL_STRING.
678 */
679static const char *unquote_token(Token *t)
680{
681 if (t->type != TOK_STRING)
682 return tok_text(t);
683
684 t->type = TOK_INTERNAL_STRING;
685
686 if (t->len > INLINE_TEXT) {
687 char *p = t->text.p.ptr;
688
689 t->len = nasm_unquote(p, NULL);
690
691 if (t->len <= INLINE_TEXT) {
692 nasm_zero(t->text.a);
693 memcpy(t->text.a, p, t->len);
694 nasm_free(p);
695 return t->text.a;
696 } else {
697 return p;
698 }
699 } else {
700 t->len = nasm_unquote(t->text.a, NULL);
701 return t->text.a;
702 }
703}
704
705static const char *unquote_token_cstr(Token *t)
706{
707 if (t->type != TOK_STRING)
708 return tok_text(t);
709
710 t->type = TOK_INTERNAL_STRING;
711
712 if (t->len > INLINE_TEXT) {
713 char *p = t->text.p.ptr;
714
715 t->len = nasm_unquote_cstr(p, NULL);
716
717 if (t->len <= INLINE_TEXT) {
718 nasm_zero(t->text.a);
719 memcpy(t->text.a, p, t->len);
720 nasm_free(p);
721 return t->text.a;
722 } else {
723 return p;
724 }
725 } else {
726 t->len = nasm_unquote_cstr(t->text.a, NULL);
727 return t->text.a;
728 }
729}
730
731static Token *quote_internal_string_token(Token *t)
732{
733 size_t len;
734 char *p;
735
736 p = nasm_quote(tok_text(t), &len);
737 t->type = TOK_STRING;
738 return set_text_free(t, p, len);
739}
740
741static inline Token *quote_token(Token *t)
742{
743 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
744 return t;
745
746 return quote_internal_string_token(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700747}
H. Peter Anvin76690a12002-04-30 20:52:49 +0000748
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400749/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700750 * In-place reverse a list of tokens.
751 */
752static Token *reverse_tokens(Token *t)
753{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800754 Token *prev = NULL;
755 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700756
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800757 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400758 next = t->next;
759 t->next = prev;
760 prev = t;
761 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800762 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700763
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800764 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700765}
766
767/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700768 * getenv() variant operating on an input token
769 */
770static const char *pp_getenv(const Token *t, bool warn)
771{
772 const char *txt = tok_text(t);
773 const char *v;
774 char *buf = NULL;
775 bool is_string = false;
776
777 if (!t)
778 return NULL;
779
780 switch (t->type) {
781 case TOK_ENVIRON:
782 txt += 2; /* Skip leading %! */
783 is_string = nasm_isquote(*txt);
784 break;
785
786 case TOK_STRING:
787 is_string = true;
788 break;
789
790 case TOK_INTERNAL_STRING:
791 case TOK_ID:
792 is_string = false;
793 break;
794
795 default:
796 return NULL;
797 }
798
799 if (is_string) {
800 buf = nasm_strdup(txt);
801 nasm_unquote_cstr(buf, NULL);
802 txt = buf;
803 }
804
805 v = getenv(txt);
806 if (warn && !v) {
807 /*!
808 *!environment [on] nonexistent environment variable
809 *! warns if a nonexistent environment variable
810 *! is accessed using the \c{%!} preprocessor
811 *! construct (see \k{getenv}.) Such environment
812 *! variables are treated as empty (with this
813 *! warning issued) starting in NASM 2.15;
814 *! earlier versions of NASM would treat this as
815 *! an error.
816 */
817 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
818 v = "";
819 }
820
821 if (buf)
822 nasm_free(buf);
823
824 return v;
825}
826
827/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300828 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000829 * front of them. We do it here because I could not find any other
830 * place to do it for the moment, and it is a hack (ideally it would
831 * be nice to be able to use the NASM pre-processor to do it).
832 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000833static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000834{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000835 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400836 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000837
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400838 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000839
840 /* Binary search for the directive name */
841 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400842 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400843 q = nasm_skip_word(p);
844 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 if (len) {
846 oldchar = p[len];
847 p[len] = 0;
848 while (j - i > 1) {
849 k = (j + i) / 2;
850 m = nasm_stricmp(p, tasm_directives[k]);
851 if (m == 0) {
852 /* We have found a directive, so jam a % in front of it
853 * so that NASM will then recognise it as one if it's own.
854 */
855 p[len] = oldchar;
856 len = strlen(p);
857 oldline = line;
858 line = nasm_malloc(len + 2);
859 line[0] = '%';
860 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700861 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300862 * NASM does not recognise IFDIFI, so we convert
863 * it to %if 0. This is not used in NASM
864 * compatible code, but does need to parse for the
865 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700867 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 } else {
869 memcpy(line + 1, p, len + 1);
870 }
871 nasm_free(oldline);
872 return line;
873 } else if (m < 0) {
874 j = k;
875 } else
876 i = k;
877 }
878 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000879 }
880 return line;
881}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000882
H. Peter Anvin76690a12002-04-30 20:52:49 +0000883/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000884 * The pre-preprocessing stage... This function translates line
885 * number indications as they emerge from GNU cpp (`# lineno "file"
886 * flags') into NASM preprocessor line number indications (`%line
887 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000888 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000889static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000890{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000891 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000892 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000893
H. Peter Anvine2c80182005-01-15 22:15:51 +0000894 if (line[0] == '#' && line[1] == ' ') {
895 oldline = line;
896 fname = oldline + 2;
897 lineno = atoi(fname);
898 fname += strspn(fname, "0123456789 ");
899 if (*fname == '"')
900 fname++;
901 fnlen = strcspn(fname, "\"");
902 line = nasm_malloc(20 + fnlen);
903 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
904 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000905 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000906 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000907 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000908 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000909}
910
911/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000912 * Free a linked list of tokens.
913 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000915{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400916 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000917 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000918}
919
920/*
921 * Free a linked list of lines.
922 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000924{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400925 Line *l, *tmp;
926 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 free_tlist(l->first);
928 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 }
930}
931
932/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700933 * Free an array of linked lists of tokens
934 */
935static void free_tlist_array(Token **array, size_t nlists)
936{
937 Token **listp = array;
938
939 while (nlists--)
940 free_tlist(*listp++);
941
942 nasm_free(array);
943}
944
945/*
946 * Duplicate a linked list of tokens.
947 */
948static Token *dup_tlist(const Token *list, Token ***tailp)
949{
950 Token *newlist = NULL;
951 Token **tailpp = &newlist;
952 const Token *t;
953
954 list_for_each(t, list) {
955 Token *nt;
956 *tailpp = nt = dup_Token(NULL, t);
957 tailpp = &nt->next;
958 }
959
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700960 if (tailp) {
961 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700962 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700963 }
964
965 return newlist;
966}
967
968/*
969 * Duplicate a linked list of tokens with a maximum count
970 */
971static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
972{
973 Token *newlist = NULL;
974 Token **tailpp = &newlist;
975 const Token *t;
976
977 list_for_each(t, list) {
978 Token *nt;
979 if (!cnt--)
980 break;
981 *tailpp = nt = dup_Token(NULL, t);
982 tailpp = &nt->next;
983 }
984
985 if (tailp) {
986 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -0700987 if (newlist)
988 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700989 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700990
991 return newlist;
992}
993
994/*
995 * Duplicate a linked list of tokens in reverse order
996 */
997static Token *dup_tlist_reverse(const Token *list, Token *tail)
998{
999 const Token *t;
1000
1001 list_for_each(t, list)
1002 tail = dup_Token(tail, t);
1003
1004 return tail;
1005}
1006
1007/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001008 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001009 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001010static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001011{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001012 nasm_free(m->name);
1013 free_tlist(m->dlist);
1014 nasm_free(m->defaults);
1015 free_llist(m->expansion);
1016 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001017}
1018
1019/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001020 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001021 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001022static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001023{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001024 if (s->params) {
1025 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001026 for (i = 0; i < s->nparam; i++) {
1027 if (s->params[i].name.len > INLINE_TEXT)
1028 nasm_free(s->params[i].name.text.p.ptr);
1029 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001030 nasm_free(s->params);
1031 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001032 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001033 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001034}
1035
1036static void clear_smacro(SMacro *s)
1037{
1038 free_smacro_members(s);
1039 /* Wipe everything except the next pointer */
1040 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1041}
1042
1043/*
1044 * Free an SMacro
1045 */
1046static void free_smacro(SMacro *s)
1047{
1048 free_smacro_members(s);
1049 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001050}
1051
1052/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001053 * Free all currently defined macros, and free the hash tables
1054 */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001055static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001056{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001057 struct hash_iterator it;
1058 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001059
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001060 hash_for_each(smt, it, np) {
1061 SMacro *tmp;
1062 SMacro *s = np->data;
1063 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001064 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001065 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001066 }
H. Peter Anvin072771e2008-05-22 13:17:51 -07001067 hash_free(smt);
1068}
1069
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001070static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001071{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001072 struct hash_iterator it;
1073 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001074
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001075 hash_for_each(mmt, it, np) {
1076 MMacro *tmp;
1077 MMacro *m = np->data;
1078 nasm_free((void *)np->key);
1079 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001080 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001081 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001082 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001083}
1084
1085static void free_macros(void)
1086{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001087 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001088 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001089}
1090
1091/*
1092 * Initialize the hash tables
1093 */
1094static void init_macros(void)
1095{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001096}
1097
1098/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001099 * Pop the context stack.
1100 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001102{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001103 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001104
1105 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001106 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001107 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001108 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001109}
1110
H. Peter Anvin072771e2008-05-22 13:17:51 -07001111/*
1112 * Search for a key in the hash index; adding it if necessary
1113 * (in which case we initialize the data pointer to NULL.)
1114 */
1115static void **
1116hash_findi_add(struct hash_table *hash, const char *str)
1117{
1118 struct hash_insert hi;
1119 void **r;
1120 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001121 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001122
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001123 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001124 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001125 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001126
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001127 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1128 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001129 return hash_add(&hi, strx, NULL);
1130}
1131
1132/*
1133 * Like hash_findi, but returns the data element rather than a pointer
1134 * to it. Used only when not adding a new element, hence no third
1135 * argument.
1136 */
1137static void *
1138hash_findix(struct hash_table *hash, const char *str)
1139{
1140 void **p;
1141
1142 p = hash_findi(hash, str, NULL);
1143 return p ? *p : NULL;
1144}
1145
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001146/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001147 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001148 * if there no more left -- return NULL
1149 */
1150static char *line_from_stdmac(void)
1151{
1152 unsigned char c;
1153 const unsigned char *p = stdmacpos;
1154 char *line, *q;
1155 size_t len = 0;
1156
1157 if (!stdmacpos)
1158 return NULL;
1159
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001160 /*
1161 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1162 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1163 */
1164 while ((c = *p++) != 127) {
1165 uint8_t ndir = c - 128;
1166 if (ndir < 256-96)
1167 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001168 else
1169 len++;
1170 }
1171
1172 line = nasm_malloc(len + 1);
1173 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001174
1175 while ((c = *stdmacpos++) != 127) {
1176 uint8_t ndir = c - 128;
1177 if (ndir < 256-96) {
1178 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1179 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001180 *q++ = ' ';
1181 } else {
1182 *q++ = c;
1183 }
1184 }
1185 stdmacpos = p;
1186 *q = '\0';
1187
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001188 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001189 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001190 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001191 if (*stdmacnext) {
1192 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001193 } else if (do_predef) {
1194 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001195
1196 /*
1197 * Nasty hack: here we push the contents of
1198 * `predef' on to the top-level expansion stack,
1199 * since this is the most convenient way to
1200 * implement the pre-include and pre-define
1201 * features.
1202 */
1203 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001204 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001205 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001206 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001207 l->finishes = NULL;
1208
1209 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001210 }
1211 do_predef = false;
1212 }
1213 }
1214
1215 return line;
1216}
1217
H. Peter Anvin6686de22019-08-10 05:33:14 -07001218/*
1219 * Read a line from a file. Return NULL on end of file.
1220 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001221static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001222{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001223 int c;
1224 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001225 const unsigned int delta = 512;
1226 const unsigned int pad = 8;
1227 unsigned int nr_cont = 0;
1228 bool cont = false;
1229 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001230 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001231
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001232 size = delta;
1233 p = buffer = nasm_malloc(size);
1234
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001235 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001236 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001237
1238 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001239 case EOF:
1240 if (p == buffer) {
1241 nasm_free(buffer);
1242 return NULL;
1243 }
1244 c = 0;
1245 break;
1246
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001247 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001248 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001249 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001250 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001251 if (cont) {
1252 cont = false;
1253 continue;
1254 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001255 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001256 break;
1257
1258 case '\n':
1259 if (cont) {
1260 cont = false;
1261 continue;
1262 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001263 c = 0;
1264 break;
1265
1266 case 032: /* ^Z = legacy MS-DOS end of file mark */
1267 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001268 break;
1269
1270 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001271 next = fgetc(f);
1272 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001273 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001274 cont = true;
1275 nr_cont++;
1276 continue;
1277 }
1278 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001280
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001281 if (p >= (buffer + size - pad)) {
1282 buffer = nasm_realloc(buffer, size + delta);
1283 p = buffer + size - pad;
1284 size += delta;
1285 }
1286
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001287 *p++ = c;
1288 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001289
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001290 lineno = src_get_linnum() + istk->lineinc +
1291 (nr_cont * istk->lineinc);
1292 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001293
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001294 return buffer;
1295}
1296
1297/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001298 * Common read routine regardless of source
1299 */
1300static char *read_line(void)
1301{
1302 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001303 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001304
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001305 if (f)
1306 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001307 else
1308 line = line_from_stdmac();
1309
1310 if (!line)
1311 return NULL;
1312
1313 if (!istk->nolist)
1314 lfmt->line(LIST_READ, src_get_linnum(), line);
1315
1316 return line;
1317}
1318
1319/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001320 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001321 * don't need to parse the value out of e.g. numeric tokens: we
1322 * simply split one string into many.
1323 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001324static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001325{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001326 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001327 Token *list = NULL;
1328 Token *t, **tail = &list;
1329
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001331 const char *p = line;
1332 const char *ep = NULL; /* End of token, for trimming the end */
1333 size_t toklen;
1334 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001335
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001337 /*
1338 * Preprocessor construct; find the end of the token.
1339 * Classification is handled later, because %{...} can be
1340 * used to create any preprocessor token.
1341 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001343 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001344 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001345 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001346 } else if (nasm_isdigit(*p) ||
1347 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001348 do {
1349 p++;
1350 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001351 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001352 } else if (*p == '{' || *p == '[') {
1353 /* %{...} or %[...] */
1354 char firstchar = *p;
1355 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001356 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001357 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1358 while (lvl) {
1359 if (*p == firstchar) {
1360 lvl++;
1361 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001362 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001363 } else if (nasm_isquote(*p)) {
1364 p = nasm_skip_string(p);
1365 }
1366
1367 /*
1368 * *p can have been advanced to a null character by
1369 * nasm_skip_string()
1370 */
1371 if (!*p) {
1372 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1373 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001374 break;
1375 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001376 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001377 }
1378 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001379 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001380 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001381 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001382 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001383 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001384 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001385 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001386 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001387 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001388 do {
1389 p++;
1390 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001391 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001392 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001393 p = nasm_skip_string(p);
1394 if (*p)
1395 p++;
1396 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001397 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001398 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001399 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001400 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001401 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001402 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001403 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001404 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001405 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1406 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 do {
1408 p++;
1409 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001410 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001411 } else if (*p == '%') {
1412 /* %% operator */
1413 p++;
1414 }
1415
1416 if (!ep)
1417 ep = p;
1418 toklen = ep - line;
1419
1420 /* Classify here, to handle %{...} correctly */
1421 if (toklen < 2) {
1422 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001424 char c0 = line[1];
1425
1426 switch (c0) {
1427 case '+':
1428 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1429 break;
1430
1431 case '-':
1432 type = TOK_MMACRO_PARAM;
1433 break;
1434
1435 case '?':
1436 if (toklen == 2)
1437 type = TOK_PREPROC_Q;
1438 else if (toklen == 3 && line[2] == '?')
1439 type = TOK_PREPROC_QQ;
1440 else
1441 type = TOK_PREPROC_ID;
1442 break;
1443
1444 case '!':
1445 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1446 break;
1447
1448 case '%':
1449 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1450 break;
1451
1452 case '$':
1453 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1454 break;
1455
1456 case '[':
1457 line += 2; /* Skip %[ */
1458 firstchar = *line; /* Don't clobber */
1459 toklen -= 2;
1460 type = TOK_INDIRECT;
1461 break;
1462
1463 case ',':
1464 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1465 break;
1466
1467 case '\'':
1468 case '\"':
1469 case '`':
1470 /* %{'string'} */
1471 type = TOK_PREPROC_ID;
1472 break;
1473
1474 case ':':
1475 type = TOK_MMACRO_PARAM; /* %{:..} */
1476 break;
1477
1478 default:
1479 if (nasm_isdigit(c0))
1480 type = TOK_MMACRO_PARAM;
1481 else if (nasm_isidchar(c0) || toklen > 2)
1482 type = TOK_PREPROC_ID;
1483 else
1484 type = TOK_OTHER;
1485 break;
1486 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001488 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001489 /*
1490 * An identifier. This includes the ? operator, which is
1491 * treated as a keyword, not as a special character
1492 * operator
1493 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001494 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001495 while (nasm_isidchar(*++p))
1496 ;
1497 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 /*
1499 * A string token.
1500 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001502 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001503
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 if (*p) {
1505 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001506 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001507 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 /* Handling unterminated strings by UNV */
1509 /* type = -1; */
1510 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001511 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001512 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001513 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001514 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001515 bool is_hex = false;
1516 bool is_float = false;
1517 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001518 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001519
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001521 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001523
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001524 if (*p == '$') {
1525 p++;
1526 is_hex = true;
1527 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001528
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 for (;;) {
1530 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001531
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001532 if (!is_hex && (c == 'e' || c == 'E')) {
1533 has_e = true;
1534 if (*p == '+' || *p == '-') {
1535 /*
1536 * e can only be followed by +/- if it is either a
1537 * prefixed hex number or a floating-point number
1538 */
1539 p++;
1540 is_float = true;
1541 }
1542 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1543 is_hex = true;
1544 } else if (c == 'P' || c == 'p') {
1545 is_float = true;
1546 if (*p == '+' || *p == '-')
1547 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001548 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001549 ; /* just advance */
1550 else if (c == '.') {
1551 /*
1552 * we need to deal with consequences of the legacy
1553 * parser, like "1.nolist" being two tokens
1554 * (TOK_NUMBER, TOK_ID) here; at least give it
1555 * a shot for now. In the future, we probably need
1556 * a flex-based scanner with proper pattern matching
1557 * to do it as well as it can be done. Nothing in
1558 * the world is going to help the person who wants
1559 * 0x123.p16 interpreted as two tokens, though.
1560 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001561 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001562 while (*r == '_')
1563 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001565 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1566 (!is_hex && (*r == 'e' || *r == 'E')) ||
1567 (*r == 'p' || *r == 'P')) {
1568 p = r;
1569 is_float = true;
1570 } else
1571 break; /* Terminate the token */
1572 } else
1573 break;
1574 }
1575 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001576
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001577 if (p == line+1 && *line == '$') {
1578 type = TOK_OTHER; /* TOKEN_HERE */
1579 } else {
1580 if (has_e && !is_hex) {
1581 /* 1e13 is floating-point, but 1e13h is not */
1582 is_float = true;
1583 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001584
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001585 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1586 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001587 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001589 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 /*
1591 * Whitespace just before end-of-line is discarded by
1592 * pretending it's a comment; whitespace just before a
1593 * comment gets lumped into the comment.
1594 */
1595 if (!*p || *p == ';') {
1596 type = TOK_COMMENT;
1597 while (*p)
1598 p++;
1599 }
1600 } else if (*p == ';') {
1601 type = TOK_COMMENT;
1602 while (*p)
1603 p++;
1604 } else {
1605 /*
1606 * Anything else is an operator of some kind. We check
1607 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001608 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1609 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001610 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 */
1612 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001613 switch (*p++) {
1614 case '>':
1615 if (*p == '>') {
1616 p++;
1617 if (*p == '>')
1618 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001619 } else if (*p == '=') {
1620 p++;
1621 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001622 break;
1623
1624 case '<':
1625 if (*p == '<') {
1626 p++;
1627 if (*p == '<')
1628 p++;
1629 } else if (*p == '=') {
1630 p++;
1631 if (*p == '>')
1632 p++;
1633 } else if (*p == '>') {
1634 p++;
1635 }
1636 break;
1637
1638 case '!':
1639 if (*p == '=')
1640 p++;
1641 break;
1642
1643 case '/':
1644 case '=':
1645 case '&':
1646 case '|':
1647 case '^':
1648 /* These operators can be doubled but nothing else */
1649 if (*p == p[-1])
1650 p++;
1651 break;
1652
1653 default:
1654 break;
1655 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001657
H. Peter Anvin8571f062019-09-23 16:40:03 -07001658 if (type == TOK_WHITESPACE) {
1659 *tail = t = new_White(NULL);
1660 tail = &t->next;
1661 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001662 if (!ep)
1663 ep = p;
1664 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001665 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 tail = &t->next;
1667 }
1668 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001669 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001670 return list;
1671}
1672
H. Peter Anvin8571f062019-09-23 16:40:03 -07001673/*
1674 * Tokens are allocated in blocks to improve speed. Set the blocksize
1675 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1676 *
1677 * alloc_Token() returns a zero-initialized token structure.
1678 */
1679#define TOKEN_BLOCKSIZE 4096
1680
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001681#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001682
1683static Token *freeTokens = NULL;
1684static Token *tokenblocks = NULL;
1685
1686static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001687{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001688 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001689
H. Peter Anvin8571f062019-09-23 16:40:03 -07001690 if (unlikely(!t)) {
1691 Token *block;
1692 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693
H. Peter Anvin8571f062019-09-23 16:40:03 -07001694 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001695
H. Peter Anvin8571f062019-09-23 16:40:03 -07001696 /*
1697 * The first entry in each array are a linked list of
1698 * block allocations and is not used for data.
1699 */
1700 block[0].next = tokenblocks;
1701 block[0].type = TOK_BLOCK;
1702 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001703
H. Peter Anvin8571f062019-09-23 16:40:03 -07001704 /*
1705 * Add the rest to the free list
1706 */
1707 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1708 block[i].next = &block[i+1];
1709
1710 freeTokens = &block[2];
1711
1712 /*
1713 * Return the topmost usable token
1714 */
1715 return &block[1];
1716 }
1717
1718 freeTokens = t->next;
1719 t->next = NULL;
1720 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001721}
1722
H. Peter Anvin8571f062019-09-23 16:40:03 -07001723static Token *delete_Token(Token *t)
1724{
1725 Token *next = t->next;
1726
1727 nasm_zero(*t);
1728 t->next = freeTokens;
1729 freeTokens = t;
1730
1731 return next;
1732}
1733
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001735{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001736 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001737
H. Peter Anvin8571f062019-09-23 16:40:03 -07001738 list_for_each_safe(block, blocktmp, tokenblocks)
1739 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001740
H. Peter Anvin8571f062019-09-23 16:40:03 -07001741 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001743
H. Peter Anvin8571f062019-09-23 16:40:03 -07001744#else
1745
1746static inline Token *alloc_Token(void)
1747{
1748 Token *t;
1749 nasm_new(*t);
1750 return t;
1751}
1752
1753static Token *delete_Token(Token *t)
1754{
1755 Token *next = t->next;
1756 nasm_free(t);
1757 return next;
1758}
1759
1760static inline void delete_Blocks(void)
1761{
1762 /* Nothing to do */
1763}
1764
1765#endif
1766
H. Peter Anvin734b1882002-04-30 21:01:08 +00001767/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001768 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001769 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001770 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001771static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001772 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001773{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001774 Token *t = alloc_Token();
1775 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001776
H. Peter Anvin734b1882002-04-30 21:01:08 +00001777 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001778 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001779 if (type == TOK_WHITESPACE) {
1780 t->len = 1;
1781 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001783 if (text && text[0] && !txtlen)
1784 txtlen = tok_strlen(text);
1785
1786 t->len = tok_check_len(txtlen);
1787
1788 if (text) {
1789 textp = (txtlen > INLINE_TEXT)
1790 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1791 memcpy(textp, text, txtlen);
1792 textp[txtlen] = '\0'; /* In case we needed malloc() */
1793 } else {
1794 /*
1795 * Allocate a buffer but do not fill it. The caller
1796 * can fill in text, but must not change the length.
1797 * The filled in text must be exactly txtlen once
1798 * the buffer is filled and before the token is added
1799 * to any line lists.
1800 */
1801 if (txtlen > INLINE_TEXT)
1802 t->text.p.ptr = nasm_zalloc(txtlen+1);
1803 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001804 }
1805 return t;
1806}
1807
H. Peter Anvin8571f062019-09-23 16:40:03 -07001808/*
1809 * Same as new_Token(), but text belongs to the new token and is
1810 * either taken over or freed. This function MUST be called
1811 * with valid txt and txtlen, unlike new_Token().
1812 */
1813static Token *new_Token_free(Token * next, enum pp_token_type type,
1814 char *text, size_t txtlen)
1815{
1816 Token *t = alloc_Token();
1817
1818 t->next = next;
1819 t->type = type;
1820 t->len = tok_check_len(txtlen);
1821
1822 if (txtlen <= INLINE_TEXT) {
1823 memcpy(t->text.a, text, txtlen);
1824 free(text);
1825 } else {
1826 t->text.p.ptr = text;
1827 }
1828
1829 return t;
1830}
1831
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001832static Token *dup_Token(Token *next, const Token *src)
1833{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001834 Token *t = alloc_Token();
1835
1836 memcpy(t, src, sizeof *src);
1837 t->next = next;
1838
1839 if (t->len > INLINE_TEXT) {
1840 t->text.p.ptr = nasm_malloc(t->len + 1);
1841 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1842 }
1843
1844 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001845}
1846
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001847static Token *new_White(Token *next)
1848{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001849 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001850
H. Peter Anvin8571f062019-09-23 16:40:03 -07001851 t->next = next;
1852 t->type = TOK_WHITESPACE;
1853 t->len = 1;
1854 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001855
H. Peter Anvin8571f062019-09-23 16:40:03 -07001856 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001857}
1858
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001859/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001860 * This *transfers* the content from one token to another, leaving the
1861 * next pointer of the latter intact. Unlike dup_Token(), the old
1862 * token is destroyed, except for its next pointer, and the text
1863 * pointer allocation, if any, is simply transferred.
1864 */
1865static Token *steal_Token(Token *dst, Token *src)
1866{
1867 /* Overwrite everything except the next pointers */
1868 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1869 sizeof(Token) - sizeof(Token *));
1870
1871 /* Clear the donor token */
1872 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1873
1874 return dst;
1875}
1876
1877/*
1878 * Convert a line of tokens back into text. This modifies the list
1879 * by expanding environment variables.
1880 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001881 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001882 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001883 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001884static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001885{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001886 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001887 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001888 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001889
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001890 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001891 switch (t->type) {
1892 case TOK_ENVIRON:
1893 {
1894 const char *v = pp_getenv(t, true);
1895 set_text(t, v, tok_strlen(v));
1896 t->type = TOK_INTERNAL_STRING;
1897 break;
1898 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001899
H. Peter Anvin8571f062019-09-23 16:40:03 -07001900 case TOK_LOCAL_MACRO:
1901 case TOK_LOCAL_SYMBOL:
1902 if (expand_locals) {
1903 const char *q;
1904 char *p;
1905 Context *ctx = get_ctx(tok_text(t), &q);
1906 if (ctx) {
1907 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1908 set_text_free(t, p, nasm_last_string_len());
1909 t->type = TOK_ID;
1910 }
1911 }
1912 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001913
H. Peter Anvin8571f062019-09-23 16:40:03 -07001914 default:
1915 break; /* No modifications */
1916 }
1917
1918 if (debug_level(2)) {
1919 unsigned int t_len = t->len;
1920 unsigned int s_len = tok_strlen(tok_text(t));
1921 if (t_len != s_len) {
1922 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
1923 tok_text(t), t->type, s_len, t_len);
1924 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001925 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001927
H. Peter Anvin8571f062019-09-23 16:40:03 -07001928 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001929 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001930
H. Peter Anvin734b1882002-04-30 21:01:08 +00001931 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001932
H. Peter Anvin8571f062019-09-23 16:40:03 -07001933 list_for_each(t, tlist)
1934 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001935 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001936
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001937 return line;
1938}
1939
1940/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001941 * A scanner, suitable for use by the expression evaluator, which
1942 * operates on a line of Tokens. Expects a pointer to a pointer to
1943 * the first token in the line to be passed in as its private_data
1944 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001945 *
1946 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001947 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001948struct ppscan {
1949 Token *tptr;
1950 int ntokens;
1951};
1952
H. Peter Anvine2c80182005-01-15 22:15:51 +00001953static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001954{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001955 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001956 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001957 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001958
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001960 if (pps->ntokens && (tline = pps->tptr)) {
1961 pps->ntokens--;
1962 pps->tptr = tline->next;
1963 } else {
1964 pps->tptr = NULL;
1965 pps->ntokens = 0;
1966 return tokval->t_type = TOKEN_EOS;
1967 }
1968 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001969
H. Peter Anvin8571f062019-09-23 16:40:03 -07001970 txt = tok_text(tline);
1971 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001972
H. Peter Anvin8571f062019-09-23 16:40:03 -07001973 if (txt[0] == '$') {
1974 if (!txt[1]) {
1975 return tokval->t_type = TOKEN_HERE;
1976 } else if (txt[1] == '$' && !txt[2]) {
1977 return tokval->t_type = TOKEN_BASE;
1978 } else if (tline->type == TOK_ID) {
1979 tokval->t_charptr++;
1980 return tokval->t_type = TOKEN_ID;
1981 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001982 }
1983
H. Peter Anvin8571f062019-09-23 16:40:03 -07001984 switch (tline->type) {
1985 default:
1986 if (tline->len == 1)
1987 return tokval->t_type = txt[0];
1988 /* fall through */
1989 case TOK_ID:
1990 return nasm_token_hash(txt, tokval);
1991
1992 case TOK_NUMBER:
1993 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001994 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001995 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 if (rn_error)
1997 return tokval->t_type = TOKEN_ERRNUM;
1998 else
1999 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002000 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002001
H. Peter Anvin8571f062019-09-23 16:40:03 -07002002 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002003 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002004
2005 case TOK_STRING:
2006 tokval->t_charptr = (char *)unquote_token(tline);
2007 tokval->t_inttwo = tline->len;
2008 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002009 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002010}
2011
2012/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002013 * 1. An expression (true if nonzero 0)
2014 * 2. The keywords true, on, yes for true
2015 * 3. The keywords false, off, no for false
2016 * 4. An empty line, for true
2017 *
2018 * On error, return defval (usually the previous value)
2019 */
2020static bool pp_get_boolean_option(Token *tline, bool defval)
2021{
2022 static const char * const noyes[] = {
2023 "no", "yes",
2024 "false", "true",
2025 "off", "on"
2026 };
2027 struct ppscan pps;
2028 struct tokenval tokval;
2029 expr *evalresult;
2030
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002031 tline = skip_white(tline);
2032 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002033 return true;
2034
2035 if (tline->type == TOK_ID) {
2036 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002037 const char *txt = tok_text(tline);
2038
H. Peter Anvind2354082019-08-27 16:38:48 -07002039 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002040 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002041 return i & 1;
2042 }
2043
2044 pps.tptr = NULL;
2045 pps.tptr = tline;
2046 pps.ntokens = -1;
2047 tokval.t_type = TOKEN_INVALID;
2048 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2049
2050 if (!evalresult)
2051 return true;
2052
2053 if (tokval.t_type)
2054 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2055 if (!is_really_simple(evalresult)) {
2056 nasm_nonfatal("boolean flag expression must be a constant");
2057 return defval;
2058 }
2059
2060 return reloc_value(evalresult) != 0;
2061}
2062
2063/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002064 * Compare a string to the name of an existing macro; this is a
2065 * simple wrapper which calls either strcmp or nasm_stricmp
2066 * depending on the value of the `casesense' parameter.
2067 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002068static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002069{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002070 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002071}
2072
2073/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002074 * Compare a string to the name of an existing macro; this is a
2075 * simple wrapper which calls either strcmp or nasm_stricmp
2076 * depending on the value of the `casesense' parameter.
2077 */
2078static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2079{
2080 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2081}
2082
2083/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002084 * Return the Context structure associated with a %$ token. Return
2085 * NULL, having _already_ reported an error condition, if the
2086 * context stack isn't deep enough for the supplied number of $
2087 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002088 *
2089 * If "namep" is non-NULL, set it to the pointer to the macro name
2090 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002091 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002092static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002093{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002094 Context *ctx;
2095 int i;
2096
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002097 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002098 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002099
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002100 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002102
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002104 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002105 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002106 }
2107
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002108 name += 2;
2109 ctx = cstk;
2110 i = 0;
2111 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 name++;
2113 i++;
2114 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002115 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002116 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002117 nasm_nonfatal("`%s': context stack is only"
2118 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002120 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002121
2122 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002123 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002124
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002125 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002126}
2127
2128/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002129 * Open an include file. This routine must always return a valid
2130 * file pointer if it returns - it's responsible for throwing an
2131 * ERR_FATAL and bombing out completely if not. It should also try
2132 * the include path one by one until it finds the file or reaches
2133 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002134 *
2135 * Note: for INC_PROBE the function returns NULL at all times;
2136 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002137 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002138enum incopen_mode {
2139 INC_NEEDED, /* File must exist */
2140 INC_OPTIONAL, /* Missing is OK */
2141 INC_PROBE /* Only an existence probe */
2142};
2143
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002144/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002145static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002146 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002147{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002148 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002149 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002150 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002151 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002152 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002153
H. Peter Anvine2c80182005-01-15 22:15:51 +00002154 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002155 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002156 if (omode == INC_PROBE) {
2157 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002158 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002159 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002160 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002161 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002162 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002163 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002164 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002165 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002166 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002167
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002168 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002169
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002170 if (!ip) {
2171 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002172 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002173 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002174
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002175 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002176 ip = ip->next;
2177 }
2178}
2179
2180/*
2181 * Open a file, or test for the presence of one (depending on omode),
2182 * considering the include path.
2183 */
2184static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002185 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002186 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002187 enum incopen_mode omode,
2188 enum file_flags fmode)
2189{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002190 struct hash_insert hi;
2191 void **hp;
2192 char *path;
2193 FILE *fp = NULL;
2194
2195 hp = hash_find(&FileHash, file, &hi);
2196 if (hp) {
2197 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002198 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002199 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002200 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002201 } else {
2202 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002203 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002204
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002205 /* Positive or negative result */
2206 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002207
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002208 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002209 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002210 */
2211 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002212 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002213 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002214
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002215 if (!path) {
2216 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002217 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002218 } else {
2219 if (!fp && omode != INC_PROBE)
2220 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002221 }
2222
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002223 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002224 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002225
2226 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002227}
2228
2229/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002230 * Opens an include or input file. Public version, for use by modules
2231 * that get a file:lineno pair and need to look at the file again
2232 * (e.g. the CodeView debug backend). Returns NULL on failure.
2233 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002234FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002235{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002236 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002237}
2238
2239/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002240 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002241 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002242 * return true if _any_ single-line macro of that name is defined.
2243 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002244 * `nparam' or no parameters is defined.
2245 *
2246 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002247 * defined, or nparam is -1, the address of the definition structure
2248 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002249 * is NULL, no action will be taken regarding its contents, and no
2250 * error will occur.
2251 *
2252 * Note that this is also called with nparam zero to resolve
2253 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002254 *
2255 * If you already know which context macro belongs to, you can pass
2256 * the context pointer as first parameter; if you won't but name begins
2257 * with %$ the context will be automatically computed. If all_contexts
2258 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002259 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002260static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002261smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002262 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002263{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002264 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002265 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002266
H. Peter Anvin97a23472007-09-16 17:57:25 -07002267 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002268 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002269 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002270 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002271 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002272 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002273 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002274 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002275 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002276 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002277 }
H. Peter Anvind2354082019-08-27 16:38:48 -07002278
2279restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002280 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002281
H. Peter Anvine2c80182005-01-15 22:15:51 +00002282 while (m) {
2283 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002284 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002285 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002286 if (m->alias && !find_alias) {
2287 if (do_aliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002288 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002289 goto restart;
2290 } else {
2291 continue;
2292 }
2293 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002294 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002295 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002296 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002297 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002298 }
2299 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002300 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002301
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002302 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002303}
2304
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002305/* param should be a natural number [0; INT_MAX] */
2306static int read_param_count(const char *str)
2307{
2308 int result;
2309 bool err;
2310
2311 result = readnum(str, &err);
2312 if (result < 0 || result > INT_MAX) {
2313 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002314 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2315 str, 0, INT_MAX);
2316 } else if (err)
2317 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002318 return result;
2319}
2320
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002321/*
2322 * Count and mark off the parameters in a multi-line macro call.
2323 * This is called both from within the multi-line macro expansion
2324 * code, and also to mark off the default parameters when provided
2325 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002326 *
2327 * Note that we need space in the params array for parameter 0 being
2328 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002329 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002330static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002331{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002332 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002333 int nparam = 0;
2334 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002335
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002336 paramsize = PARAM_DELTA;
2337 params = nasm_malloc(paramsize * sizeof(*params));
2338 params[0] = NULL;
2339
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002340 while ((t = skip_white(t))) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002341 /* 2 slots for captured label and NULL */
2342 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002344 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002346 params[++nparam] = t;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002347 if (tok_is(t, '{')) {
2348 int brace = 1;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002349 while (brace && (t = t->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002350 brace += tok_is(t, '{');
2351 brace -= tok_is(t, '}');
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002352 }
2353
2354 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 /*
2356 * Now we've found the closing brace, look further
2357 * for the comma.
2358 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002359 t = skip_white(t->next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002360 if (tok_isnt(t, ','))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002361 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002362 } else {
2363 nasm_nonfatal("expecting closing brace in macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002365 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002366
2367 while (tok_isnt(t, ','))
2368 t = t->next;
2369
2370 if (t) /* got a comma */
2371 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002372 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002373
2374 params[nparam+1] = NULL;
2375 *paramsp = params;
2376 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002377}
2378
2379/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002380 * Determine whether one of the various `if' conditions is true or
2381 * not.
2382 *
2383 * We must free the tline we get passed.
2384 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002385static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002386{
H. Peter Anvin70055962007-10-11 00:05:31 -07002387 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002388 Token *t, *tt, *origline;
2389 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002390 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002391 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002392 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002393 const char *dname = pp_directives[ct];
2394 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002395 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002396
2397 origline = tline;
2398
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002399 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002400 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002401 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002402 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002403 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002404 if (!tline)
2405 break;
2406 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002407 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002408 dname);
2409 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002411 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002412 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 tline = tline->next;
2414 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002415 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002416
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002417 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002418 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002419 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002420 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002422 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002423 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002424 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002425 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002427 if (smacro_defined(NULL, tok_text(tline), 0, NULL, true, false))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002428 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002430 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002431 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002432
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002433 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002434 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002435 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002436 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002437 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002438 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002439 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002440 tline->type != TOK_INTERNAL_STRING &&
2441 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002442 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002443 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002444 goto fail;
2445 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002446
2447 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002448 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002449 }
2450 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002451
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002452 case PP_IFIDNI:
2453 casesense = false;
2454 /* fall through */
2455 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 tline = expand_smacro(tline);
2457 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002458 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 tt = tt->next;
2460 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002461 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002462 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002463 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 }
2465 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002466 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002467 while (tok_isnt(t, ',') && tt) {
2468 unsigned int l1, l2;
2469 const char *t1, *t2;
2470
2471 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002472 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002473 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002474 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002475 }
2476 if (t->type == TOK_WHITESPACE) {
2477 t = t->next;
2478 continue;
2479 }
2480 if (tt->type == TOK_WHITESPACE) {
2481 tt = tt->next;
2482 continue;
2483 }
2484 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002485 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 break;
2487 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002488
H. Peter Anvin8571f062019-09-23 16:40:03 -07002489 t1 = unquote_token(t);
2490 t2 = unquote_token(tt);
2491 l1 = t->len;
2492 l2 = tt->len;
2493
2494 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2495 j = false;
2496 break;
2497 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002498
H. Peter Anvine2c80182005-01-15 22:15:51 +00002499 t = t->next;
2500 tt = tt->next;
2501 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002502 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002503 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002504 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002505
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002506 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002507 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002508 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002509 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002510
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002511 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002512 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002513 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002514 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 goto fail;
2516 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002517 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002518 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002520 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002521 searching.nparam_max = INT_MAX;
2522 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002523 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002524 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002525 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002526 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002527 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002528 } else {
2529 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002530 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002532 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002533 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002534 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002535 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002536 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002537 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002538 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002539 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002540 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002541 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002542 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002543 searching.nparam_max = searching.nparam_min;
2544 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002545 }
2546 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002547 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 tline = tline->next;
2549 searching.plus = true;
2550 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002551 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2552 while (mmac) {
2553 if (!strcmp(mmac->name, searching.name) &&
2554 (mmac->nparam_min <= searching.nparam_max
2555 || searching.plus)
2556 && (searching.nparam_min <= mmac->nparam_max
2557 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 found = true;
2559 break;
2560 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002561 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 }
2563 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002564 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002565 nasm_free(searching.name);
2566 j = found;
2567 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002568 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002569
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002570 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002571 needtype = TOK_ID;
2572 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002573 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 needtype = TOK_NUMBER;
2575 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002576 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002577 needtype = TOK_STRING;
2578 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002579
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002580iftype:
2581 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002582
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002583 while (tok_white(t) ||
2584 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002586
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002587 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002589
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002590 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002591 tline = expand_smacro(tline);
2592 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002593
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 j = false;
2595 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002596 t = skip_white(t->next); /* Skip the actual token + whitespace */
2597 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 }
2599 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002600
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002601 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002602 tline = expand_smacro(tline);
2603 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 j = !t; /* Should be empty */
2605 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002606
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002607 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002608 pps.tptr = tline = expand_smacro(tline);
2609 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002611 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 if (!evalresult)
2613 return -1;
2614 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002615 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002617 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002618 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002621 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002623
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002624 case PP_IFUSING:
2625 case PP_IFUSABLE:
2626 {
2627 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002628 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002629
H. Peter Anvin8571f062019-09-23 16:40:03 -07002630 pkg = get_use_pkg(tline, dname, &name);
2631 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002632 goto fail;
2633
2634 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2635 break;
2636 }
2637
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002639 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002641 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002642
2643 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002644 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002645
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002646fail:
2647 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002648 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002649}
2650
2651/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002652 * Default smacro expansion routine: just returns a copy of the
2653 * expansion list.
2654 */
2655static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002656smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002657{
2658 (void)params;
2659 (void)nparams;
2660
2661 return dup_tlist(s->expansion, NULL);
2662}
2663
2664/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002665 * Emit a macro defintion or undef to the listing file, if
2666 * desired. This is similar to detoken(), but it handles the reverse
2667 * expansion list, does not expand %! or local variable tokens, and
2668 * does some special handling for macro parameters.
2669 */
2670static void
2671list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2672{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002673 Token *t;
2674 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002675 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002676 char *context_prefix = NULL;
2677 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002678
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002679 namelen = strlen(m->name);
2680 size = namelen + 2; /* Include room for space after name + NUL */
2681
2682 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002683 int context_depth = cstk->depth - ctx->depth + 1;
2684 context_prefix =
2685 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2686 ctx->name ? ctx->name : "",
2687 ctx->number, context_depth, "");
2688
2689 context_len = nasm_last_string_len();
2690 memset(context_prefix + context_len - context_depth,
2691 '$', context_depth);
2692 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002693 }
2694
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002695 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002696 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002697
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002698 if (m->nparam) {
2699 /*
2700 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002701 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002702 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002703 int i;
2704
2705 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002706 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002707 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002708 }
2709
2710 def = nasm_malloc(size);
2711 p = def+size;
2712 *--p = '\0';
2713
2714 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002715 p -= t->len;
2716 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002717 }
2718
2719 *--p = ' ';
2720
2721 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002722 int i;
2723
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002724 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002725 for (i = m->nparam-1; i >= 0; i--) {
2726 enum sparmflags flags = m->params[i].flags;
2727 if (flags & SPARM_GREEDY)
2728 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002729 p -= m->params[i].name.len;
2730 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2731
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002732 if (flags & SPARM_NOSTRIP)
2733 *--p = '!';
2734 if (flags & SPARM_STR)
2735 *--p = '&';
2736 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002737 *--p = '=';
2738 *--p = ',';
2739 }
2740 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002741 }
2742
2743 p -= namelen;
2744 memcpy(p, m->name, namelen);
2745
H. Peter Anvin6686de22019-08-10 05:33:14 -07002746 if (context_prefix) {
2747 p -= context_len;
2748 memcpy(p, context_prefix, context_len);
2749 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002750 }
2751
2752 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002753 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002754}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002755
2756/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002757 * Parse smacro arguments, return argument count. If the tmpl argument
2758 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002759 * *tpp is updated to point to the pointer to the first token after the
2760 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002761 *
2762 * The text values from any argument tokens are "stolen" and the
2763 * corresponding text fields set to NULL.
2764 */
2765static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2766{
2767 int nparam = 0;
2768 enum sparmflags flags;
2769 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002770 bool err, done;
2771 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002772 Token **tn = *tpp;
2773 Token *t = *tn;
2774 Token *name;
2775
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002776 /*
2777 * DO NOT skip whitespace here, or we won't be able to distinguish:
2778 *
2779 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2780 * %define bar(a,b) ; two arguments, empty expansion
2781 *
2782 * This ambiguity was inherited from C.
2783 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002784
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002785 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002786 goto finish;
2787
2788 if (tmpl) {
2789 Token *tx = t;
2790 Token **txpp = &tx;
2791 int sparam;
2792
2793 /* Count parameters first */
2794 sparam = parse_smacro_template(&txpp, NULL);
2795 if (!sparam)
2796 goto finish; /* No parameters, we're done */
2797 nasm_newn(params, sparam);
2798 }
2799
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002800 /* Skip leading paren */
2801 tn = &t->next;
2802 t = *tn;
2803
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002804 name = NULL;
2805 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002806 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002807
2808 while (!done) {
2809 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002810 if (name || flags)
2811 nasm_nonfatal("`)' expected to terminate macro template");
2812 else
2813 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002814 break;
2815 }
2816
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002817 switch (t->type) {
2818 case TOK_ID:
2819 if (name)
2820 goto bad;
2821 name = t;
2822 break;
2823
2824 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002825 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002826 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002827 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002828 case '=':
2829 flags |= SPARM_EVAL;
2830 break;
2831 case '&':
2832 flags |= SPARM_STR;
2833 break;
2834 case '!':
2835 flags |= SPARM_NOSTRIP;
2836 break;
2837 case '+':
2838 flags |= SPARM_GREEDY;
2839 greedy = true;
2840 break;
2841 case ',':
2842 if (greedy)
2843 nasm_nonfatal("greedy parameter must be last");
2844 /* fall through */
2845 case ')':
2846 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002847 if (name)
2848 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002849 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002850 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002851 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002852 name = NULL;
2853 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002854 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002855 break;
2856 default:
2857 goto bad;
2858 }
2859 break;
2860
2861 case TOK_WHITESPACE:
2862 break;
2863
2864 default:
2865 bad:
2866 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002867 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002868 err = true;
2869 }
2870 break;
2871 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002872
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002873 tn = &t->next;
2874 t = *tn;
2875 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002876
2877finish:
2878 while (t && t->type == TOK_WHITESPACE) {
2879 tn = &t->next;
2880 t = t->next;
2881 }
2882 *tpp = tn;
2883 if (tmpl) {
2884 tmpl->nparam = nparam;
2885 tmpl->greedy = greedy;
2886 tmpl->params = params;
2887 }
2888 return nparam;
2889}
2890
2891/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002892 * Common code for defining an smacro. The tmpl argument, if not NULL,
2893 * contains any macro parameters that aren't explicit arguments;
2894 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002895 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002896static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002897 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002898{
2899 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002900 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002901 Context *ctx;
2902 bool defining_alias = false;
2903 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002904
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002905 if (tmpl) {
2906 defining_alias = tmpl->alias;
2907 nparam = tmpl->nparam;
2908 }
2909
2910 while (1) {
2911 ctx = get_ctx(mname, &mname);
2912
H. Peter Anvind2354082019-08-27 16:38:48 -07002913 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002914 /* Create a new macro */
2915 smtbl = ctx ? &ctx->localmac : &smacros;
2916 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2917 nasm_new(smac);
2918 smac->next = *smhead;
2919 *smhead = smac;
2920 break;
2921 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002922 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002923 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002924 /*
2925 * Some instances of the old code considered this a failure,
2926 * some others didn't. What is the right thing to do here?
2927 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002928 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002929 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002930 /*
2931 * We're redefining, so we have to take over an
2932 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002933 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002934 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002935 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002936 break;
2937 } else if (smac->in_progress) {
2938 nasm_nonfatal("macro alias loop");
2939 goto fail;
2940 } else {
2941 /* It is an alias macro; follow the alias link */
2942 SMacro *s;
2943
2944 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002945 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002946 expansion, tmpl);
2947 smac->in_progress = false;
2948 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002949 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002950 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002951
2952 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002953 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002954 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002955 smac->expand = smacro_expand_default;
2956 if (tmpl) {
2957 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002958 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002959 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002960 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002961 if (tmpl->expand)
2962 smac->expand = tmpl->expand;
2963 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002964 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002965 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2966 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002967 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002968 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002969
2970fail:
2971 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002972 if (tmpl)
2973 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002974 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002975}
2976
2977/*
2978 * Undefine an smacro
2979 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002980static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002981{
2982 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002983 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002984 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002985
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002986 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002987 smtbl = ctx ? &ctx->localmac : &smacros;
2988 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002989
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002990 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002991 /*
2992 * We now have a macro name... go hunt for it.
2993 */
2994 sp = smhead;
2995 while ((s = *sp) != NULL) {
2996 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002997 if (s->alias && !undefalias) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002998 if (!do_aliases)
2999 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003000 if (s->in_progress) {
3001 nasm_nonfatal("macro alias loop");
3002 } else {
3003 s->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003004 undef_smacro(tok_text(s->expansion), false);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003005 s->in_progress = false;
3006 }
3007 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003008 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003009 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3010 ctx, s);
3011 *sp = s->next;
3012 free_smacro(s);
3013 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003014 } else {
3015 sp = &s->next;
3016 }
3017 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003018 }
3019}
3020
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003021/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003022 * Parse a mmacro specification.
3023 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003024static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003025{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003026 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003027 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003028 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003029 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003030 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003031 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003032 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003033
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003034#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003035 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003036#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003037 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003038 def->plus = false;
3039 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003040 def->nparam_min = 0;
3041 def->nparam_max = 0;
3042
H. Peter Anvina26433d2008-07-16 14:40:01 -07003043 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003044 tline = skip_white(tline);
3045 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003046 nasm_nonfatal("`%s' expects a parameter count", directive);
3047 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003048 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003049 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003050 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003051 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003052 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003053 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003054 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003055 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003056 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003057 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003058 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003059 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003060 }
3061 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003062 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003063 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003064 tline = tline->next;
3065 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003066 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003067 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003068 tline->next->len == 7 &&
3069 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003070 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003071 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003072 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003073
H. Peter Anvina26433d2008-07-16 14:40:01 -07003074 /*
3075 * Handle default parameters.
3076 */
3077 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003078 def->dlist = tline->next;
3079 tline->next = NULL;
3080 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003081 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003082 def->dlist = NULL;
3083 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003084 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003085 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003086
H. Peter Anvin89cee572009-07-15 09:16:54 -04003087 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003088 !def->plus) {
3089 /*
3090 *!macro-defaults [on] macros with more default than optional parameters
3091 *! warns when a macro has more default parameters than optional parameters.
3092 *! See \k{mlmacdef} for why might want to disable this warning.
3093 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003094 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003095 "too many default macro parameters in macro `%s'", def->name);
3096 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003097
H. Peter Anvina26433d2008-07-16 14:40:01 -07003098 return true;
3099}
3100
3101
3102/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003103 * Decode a size directive
3104 */
3105static int parse_size(const char *str) {
3106 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003107 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003108 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003109 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003110 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003111}
3112
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003113/*
3114 * Process a preprocessor %pragma directive. Currently there are none.
3115 * Gets passed the token list starting with the "preproc" token from
3116 * "%pragma preproc".
3117 */
3118static void do_pragma_preproc(Token *tline)
3119{
3120 /* Skip to the real stuff */
3121 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003122 tline = skip_white(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003123 if (!tline)
3124 return;
3125
3126 (void)tline; /* Nothing else to do at present */
3127}
3128
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003129static bool is_macro_id(const Token *t)
3130{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003131 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003132}
3133
H. Peter Anvin8571f062019-09-23 16:40:03 -07003134static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003135{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003136 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003137 Token *t = *tp;
3138
3139 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003140 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003141 t = expand_id(t);
3142
3143 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003144 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003145 return NULL;
3146 }
3147
H. Peter Anvin8571f062019-09-23 16:40:03 -07003148 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003149 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003150 *tp = t;
3151 return id;
3152}
3153
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003154/* Parse a %use package name and find the package. Set *err on syntax error. */
3155static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003156get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003157{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003158 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003159
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003160 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003161 t = expand_smacro(t);
3162
H. Peter Anvin8571f062019-09-23 16:40:03 -07003163 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003164
H. Peter Anvin8571f062019-09-23 16:40:03 -07003165 if (!t) {
3166 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3167 return NULL;
3168 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3169 nasm_nonfatal("`%s' expects a package name, got `%s'",
3170 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003171 return NULL;
3172 }
3173
H. Peter Anvin8571f062019-09-23 16:40:03 -07003174 *name = id = unquote_token(t);
3175
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003176 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003177 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003178 if (t)
3179 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3180
3181 return nasm_find_use_package(id);
3182}
3183
Ed Beroset3ab3f412002-06-11 03:31:49 +00003184/**
3185 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003186 * Find out if a line contains a preprocessor directive, and deal
3187 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003188 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003189 * If a directive _is_ found, it is the responsibility of this routine
3190 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003191 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003192 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003193 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003194 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003195 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003196 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003197static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003198{
H. Peter Anvin4169a472007-09-12 01:29:43 +00003199 enum preproc_token i;
3200 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003201 bool err;
3202 int nparam;
3203 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003204 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003205 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003206 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003207 const char *p;
3208 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003209 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003210 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003211 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003212 Include *inc;
3213 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003214 Cond *cond;
3215 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003216 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003217 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003218 struct tokenval tokval;
3219 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003220 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003221 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003222 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003223 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003224
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003225 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003226 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003227
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003228 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003229 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3230 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003231
H. Peter Anvin8571f062019-09-23 16:40:03 -07003232 dname = tok_text(tline);
3233 if (dname[1] == '%')
3234 return NO_DIRECTIVE_FOUND;
3235
3236 i = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003237
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003238 casesense = true;
3239 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
3240 casesense = false;
3241 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003242 }
3243
3244 /*
3245 * If we're in a non-emitting branch of a condition construct,
3246 * or walking to the end of an already terminated %rep block,
3247 * we should ignore all directives except for condition
3248 * directives.
3249 */
3250 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003251 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
3252 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003253 return NO_DIRECTIVE_FOUND;
3254 }
3255
3256 /*
3257 * If we're defining a macro or reading a %rep block, we should
3258 * ignore all directives except for %macro/%imacro (which nest),
3259 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
3260 * If we're in a %rep block, another %rep nests, so should be let through.
3261 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003262 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003263 i != PP_ENDMACRO && i != PP_ENDM &&
3264 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
3265 return NO_DIRECTIVE_FOUND;
3266 }
3267
3268 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003269 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003270 nested_mac_count++;
3271 return NO_DIRECTIVE_FOUND;
3272 } else if (nested_mac_count > 0) {
3273 if (i == PP_ENDMACRO) {
3274 nested_mac_count--;
3275 return NO_DIRECTIVE_FOUND;
3276 }
3277 }
3278 if (!defining->name) {
3279 if (i == PP_REP) {
3280 nested_rep_count++;
3281 return NO_DIRECTIVE_FOUND;
3282 } else if (nested_rep_count > 0) {
3283 if (i == PP_ENDREP) {
3284 nested_rep_count--;
3285 return NO_DIRECTIVE_FOUND;
3286 }
3287 }
3288 }
3289 }
3290
H. Peter Anvin4169a472007-09-12 01:29:43 +00003291 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003292 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003293 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003294 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003295
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003296 case PP_PRAGMA:
3297 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003298 * %pragma namespace options...
3299 *
3300 * The namespace "preproc" is reserved for the preprocessor;
3301 * all other namespaces generate a [pragma] assembly directive.
3302 *
3303 * Invalid %pragmas are ignored and may have different
3304 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003305 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003306 t = tline;
3307 tline = tline->next;
3308 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003309 tline = zap_white(expand_smacro(tline));
3310 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003311 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003312 /* Preprocessor pragma */
3313 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003314 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003315 } else {
3316 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003317
3318 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003319 for (t = tline; t->next; t = t->next)
3320 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003321 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003322
3323 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003324 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003325 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003326 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003327 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003328 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003329 }
3330 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003331 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003332
H. Peter Anvine2c80182005-01-15 22:15:51 +00003333 case PP_STACKSIZE:
3334 /* Directive to tell NASM what the default stack size is. The
3335 * default is for a 16-bit stack, and this can be overriden with
3336 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003337 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003338 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003339 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003340 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003342 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 /* All subsequent ARG directives are for a 32-bit stack */
3344 StackSize = 4;
3345 StackPointer = "ebp";
3346 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003347 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003348 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003349 /* All subsequent ARG directives are for a 64-bit stack */
3350 StackSize = 8;
3351 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003352 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003353 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003354 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003355 /* All subsequent ARG directives are for a 16-bit stack,
3356 * far function call.
3357 */
3358 StackSize = 2;
3359 StackPointer = "bp";
3360 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003361 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003362 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003363 /* All subsequent ARG directives are for a 16-bit stack,
3364 * far function call. We don't support near functions.
3365 */
3366 StackSize = 2;
3367 StackPointer = "bp";
3368 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003369 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003370 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003371 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003372 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003373 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003374
H. Peter Anvine2c80182005-01-15 22:15:51 +00003375 case PP_ARG:
3376 /* TASM like ARG directive to define arguments to functions, in
3377 * the following form:
3378 *
3379 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3380 */
3381 offset = ArgOffset;
3382 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003383 const char *arg;
3384 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003385 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003386
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003388 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003390 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003391 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003393 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003394
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 /* Find the argument size type */
3396 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003397 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003398 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003399 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 }
3401 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003402 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003403 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003404 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003406
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003408 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003410 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003411 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003412 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003413 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003414 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 }
3416 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003417
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003418 /* Round up to even stack slots */
3419 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003420
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 /* Now define the macro for the argument */
3422 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3423 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003424 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003425 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003426
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003428 tline = skip_white(tline->next);
3429 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003431 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003432
H. Peter Anvine2c80182005-01-15 22:15:51 +00003433 case PP_LOCAL:
3434 /* TASM like LOCAL directive to define local variables for a
3435 * function, in the following form:
3436 *
3437 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3438 *
3439 * The '= LocalSize' at the end is ignored by NASM, but is
3440 * required by TASM to define the local parameter size (and used
3441 * by the TASM macro package).
3442 */
3443 offset = LocalOffset;
3444 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003445 const char *local;
3446 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003448
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003450 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003452 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003453 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003455 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003456
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 /* Find the argument size type */
3458 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003459 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003460 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003461 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 }
3463 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003464 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003465 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003466 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003468
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003470 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003472 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003473 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003474 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003475 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003476 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 }
3478 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003479
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003480 /* Round up to even stack slots */
3481 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003482
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003483 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003484
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003485 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3487 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003488 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003489
H. Peter Anvine2c80182005-01-15 22:15:51 +00003490 /* Now define the assign to setup the enter_c macro correctly */
3491 snprintf(directive, sizeof(directive),
3492 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003493 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003496 tline = skip_white(tline->next);
3497 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003498 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003499 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 case PP_CLEAR:
3502 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003503 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003504 free_macros();
3505 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003506 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003507
H. Peter Anvin418ca702008-05-30 10:42:30 -07003508 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003509 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003510 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003511 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003512 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003513 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003514 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003515 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003516 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003517 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003518
3519 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003520 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003521
3522 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003523 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003524 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003525
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003526 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003528 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003529 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003531 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003532 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003533 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003534 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003536 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003537 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003538 (pp_mode == PP_DEPS)
3539 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003540 if (!inc->fp) {
3541 /* -MG given but file not found */
3542 nasm_free(inc);
3543 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003544 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003545 inc->lineno = src_set_linnum(0);
3546 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003547 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003548 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003549 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003551 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003552
H. Peter Anvind2456592008-06-19 15:04:18 -07003553 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003554 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003555 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003556 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003557
H. Peter Anvin8571f062019-09-23 16:40:03 -07003558 pkg = get_use_pkg(tline->next, dname, &name);
3559 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003560 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003561 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003562 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003563 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003564 /*
3565 * Not already included, go ahead and include it.
3566 * Treat it as an include file for the purpose of
3567 * producing a listing.
3568 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003569 use_loaded[pkg->index] = true;
3570 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003571 nasm_new(inc);
3572 inc->next = istk;
3573 inc->fname = src_set_fname(NULL);
3574 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003575 inc->nolist = !list_option('b') || istk->nolist;
3576 istk = inc;
3577 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003578 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003579 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003580 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003583 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003585 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003587 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003588 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003589 nasm_nonfatal("`%s' expects a context identifier",
3590 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003591 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003592 }
3593 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003594 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003595 pp_directives[i]);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003596 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003597 } else {
3598 p = NULL; /* Anonymous */
3599 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003600
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003601 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003602 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003603 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003605 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003606 ctx->number = unique++;
3607 cstk = ctx;
3608 } else {
3609 /* %pop or %repl */
3610 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003611 nasm_nonfatal("`%s': context stack is empty",
3612 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003613 } else if (i == PP_POP) {
3614 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003615 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003616 "expected %s",
3617 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003618 else
3619 ctx_pop();
3620 } else {
3621 /* i == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003622 nasm_free((char *)cstk->name);
3623 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003624 p = NULL;
3625 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003626 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003627 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003628 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003629 severity = ERR_FATAL;
3630 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003632 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003633 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003634 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003635 /*!
3636 *!user [on] %warning directives
3637 *! controls output of \c{%warning} directives (see \k{pperror}).
3638 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003639 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003640 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003641
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003642issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003643 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003644 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 tline->next = expand_smacro(tline->next);
3646 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003647 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003648 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003649 t = skip_white(t);
3650 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003651 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003652 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003653 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003654 } else {
3655 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003656 q = detoken(tline, false);
3657 nasm_error(severity, "%s", q);
3658 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003659 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003660 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003661 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003662
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003663 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003664 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003665 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003666 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003667 j = if_condition(tline->next, i);
3668 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003669 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003670 cond = nasm_malloc(sizeof(Cond));
3671 cond->next = istk->conds;
3672 cond->state = j;
3673 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003674 if(istk->mstk.mstk)
3675 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003676 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003677
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003678 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003679 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003680 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003681 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003682 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003683 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003684 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003685
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003686 case COND_DONE:
3687 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003688 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003689
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003690 case COND_ELSE_TRUE:
3691 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003692 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003693 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003694 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003695 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003696
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 case COND_IF_FALSE:
3698 /*
3699 * IMPORTANT: In the case of %if, we will already have
3700 * called expand_mmac_params(); however, if we're
3701 * processing an %elif we must have been in a
3702 * non-emitting mode, which would have inhibited
3703 * the normal invocation of expand_mmac_params().
3704 * Therefore, we have to do it explicitly here.
3705 */
3706 j = if_condition(expand_mmac_params(tline->next), i);
3707 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003708 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003709 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003710 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003711 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003712
H. Peter Anvine2c80182005-01-15 22:15:51 +00003713 case PP_ELSE:
3714 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003715 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003716 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003717 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003718 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003719 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003720 case COND_IF_TRUE:
3721 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003722 istk->conds->state = COND_ELSE_FALSE;
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_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003726 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003727
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003728 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003729 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003730 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003731
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003732 case COND_ELSE_TRUE:
3733 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003734 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003735 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003736 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003737 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003738 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003739 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003740
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 case PP_ENDIF:
3742 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003743 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003744 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003745 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003746 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003747 cond = istk->conds;
3748 istk->conds = cond->next;
3749 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003750 if(istk->mstk.mstk)
3751 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003752 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003753
H. Peter Anvin8b262472019-02-26 14:00:54 -08003754 case PP_RMACRO:
3755 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003756 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003757 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003758 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003759 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003760 if (i == PP_RMACRO)
3761 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003762 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003763 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003764 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003765 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003766
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003767 src_get(&defining->xline, &defining->fname);
3768
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003769 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3770 while (mmac) {
3771 if (!strcmp(mmac->name, defining->name) &&
3772 (mmac->nparam_min <= defining->nparam_max
3773 || defining->plus)
3774 && (defining->nparam_min <= mmac->nparam_max
3775 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003776 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003777 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003778 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003779 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003780 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003781 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003782 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003783
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 case PP_ENDM:
3785 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003786 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003787 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003788 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003789 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003790 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3791 defining->next = *mmhead;
3792 *mmhead = defining;
3793 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003794 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003795
H. Peter Anvin89cee572009-07-15 09:16:54 -04003796 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003797 /*
3798 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003799 * macro-end marker for a macro with a name. Then we
3800 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003801 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003802 list_for_each(l, istk->expansion)
3803 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003804 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003805
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003806 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003807 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003808 * Remove all conditional entries relative to this
3809 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003810 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003811 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3812 cond = istk->conds;
3813 istk->conds = cond->next;
3814 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003815 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003816 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003817 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003818 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003819 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003820 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003821
H. Peter Anvina26433d2008-07-16 14:40:01 -07003822 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003823 casesense = false;
3824 /* fall through */
3825 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003826 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003827 MMacro **mmac_p;
3828 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003829
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003830 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003831 spec.casesense = casesense;
3832 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003833 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003834 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003835 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3836 while (mmac_p && *mmac_p) {
3837 mmac = *mmac_p;
3838 if (mmac->casesense == spec.casesense &&
3839 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3840 mmac->nparam_min == spec.nparam_min &&
3841 mmac->nparam_max == spec.nparam_max &&
3842 mmac->plus == spec.plus) {
3843 *mmac_p = mmac->next;
3844 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003846 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003847 }
3848 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003849 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003850 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003851 }
3852
H. Peter Anvine2c80182005-01-15 22:15:51 +00003853 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003854 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003855 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003856 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003857 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003858 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 return DIRECTIVE_FOUND;
3860 }
3861 t = expand_smacro(tline->next);
3862 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003863 pps.tptr = tline = t;
3864 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003865 tokval.t_type = TOKEN_INVALID;
3866 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003867 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 free_tlist(tline);
3869 if (!evalresult)
3870 return DIRECTIVE_FOUND;
3871 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003872 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003873 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003874 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003875 return DIRECTIVE_FOUND;
3876 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003877 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003878 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003879 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003880 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003881 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003882 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003883 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003884
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003885 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003886 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003887 rotate += mmac->nparam;
3888
3889 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003890 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003891 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003892
H. Peter Anvine2c80182005-01-15 22:15:51 +00003893 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003894 {
3895 MMacro *tmp_defining;
3896
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003897 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003898 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003899 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
3900 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003901 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003902 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003903 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003904
H. Peter Anvine2c80182005-01-15 22:15:51 +00003905 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003906 pps.tptr = expand_smacro(tline);
3907 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003908 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003909 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003910 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003911 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003912 if (!evalresult)
3913 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003914 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003915 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003916 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003917 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003918 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003920 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003921 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003922 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3923 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003924 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003925 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003926 /*!
3927 *!negative-rep [on] regative %rep count
3928 *! warns about negative counts given to the \c{%rep}
3929 *! preprocessor directive.
3930 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003931 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003932 "negative `%%rep' count: %"PRId64, count);
3933 count = 0;
3934 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003935 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003936 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003938 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003939 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003940 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003941 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003942 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003943 defining->nolist = nolist;
3944 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003945 defining->mstk = istk->mstk;
3946 defining->dstk.mstk = tmp_defining;
3947 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003948 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003949 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003950 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003951
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003953 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003954 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003955 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003957
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 /*
3959 * Now we have a "macro" defined - although it has no name
3960 * and we won't be entering it in the hash tables - we must
3961 * push a macro-end marker for it on to istk->expansion.
3962 * After that, it will take care of propagating itself (a
3963 * macro-end marker line for a macro which is really a %rep
3964 * block will cause the macro to be re-expanded, complete
3965 * with another macro-end marker to ensure the process
3966 * continues) until the whole expansion is forcibly removed
3967 * from istk->expansion by a %exitrep.
3968 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003969 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003970 l->next = istk->expansion;
3971 l->finishes = defining;
3972 l->first = NULL;
3973 istk->expansion = l;
3974
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003975 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003976
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003977 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003978 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003979 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003980
H. Peter Anvine2c80182005-01-15 22:15:51 +00003981 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003982 /*
3983 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003984 * macro-end marker for a macro with no name. Then we set
3985 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003986 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003987 list_for_each(l, istk->expansion)
3988 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003989 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003990
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003991 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07003992 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003993 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003994 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003995 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003996
H. Peter Anvin8b262472019-02-26 14:00:54 -08003997 case PP_DEFINE:
3998 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003999 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004000 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004001 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004002 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004003
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004004 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004005 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004006
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004007 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004008 lastp = &tline->next;
4009 nparam = parse_smacro_template(&lastp, &tmpl);
4010 tline = *lastp;
4011 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004012
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07004013 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004014 macro_start = tline;
4015 if (!is_macro_id(macro_start)) {
4016 nasm_nonfatal("`%s' expects a macro identifier to alias",
4017 dname);
4018 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004019 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004020 tt = macro_start->next;
4021 macro_start->next = NULL;
4022 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004023 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004024 if (tline && tline->type) {
4025 nasm_warn(WARN_OTHER,
4026 "trailing garbage after aliasing identifier ignored");
4027 }
4028 free_tlist(tt);
4029 tmpl.alias = true;
4030 } else {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004031 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004032 macro_start = NULL;
4033 t = tline;
4034 while (t) {
4035 if (t->type == TOK_ID) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004036 const char *ttext = tok_text(t);
4037 size_t tlen = t->len;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004038 for (i = 0; i < nparam; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004039 if (tmpl.params[i].name.len == t->len &&
4040 !memcmp(ttext, tok_text(&tmpl.params[i].name), tlen)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004041 t->type = tok_smac_param(i);
4042 break;
4043 }
4044 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004045 }
4046 tt = t->next;
4047 t->next = macro_start;
4048 macro_start = t;
4049 t = tt;
4050 }
H. Peter Anvin (Intel)e86fa7f2019-10-16 14:51:16 -07004051
4052 /*
4053 * Expand the macro definition now for %xdefine and %ixdefine.
4054 * This is done AFTER marking of parameter tokens: this is the
4055 * only way in which %xdefine(xxx) yyyy zzzz differs from
4056 * %define(xxx) yyyy %[zzzz]
4057 */
4058 if (i == PP_XDEFINE) {
4059 tline = reverse_tokens(tline);
4060 tline = expand_smacro(tline);
4061 tline = reverse_tokens(tline);
4062 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004063 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004064
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 /*
4066 * Good. We now have a macro name, a parameter count, and a
4067 * token list (in reverse order) for an expansion. We ought
4068 * to be OK just to create an SMacro, store it, and let
4069 * free_tlist have the rest of the line (which we have
4070 * carefully re-terminated after chopping off the expansion
4071 * from the end).
4072 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004073 define_smacro(mname, casesense, macro_start, &tmpl);
4074 break;
4075 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004076
H. Peter Anvine2c80182005-01-15 22:15:51 +00004077 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004078 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004079 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004080 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004081 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004082 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004083
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004084 undef_smacro(mname, i == PP_UNDEFALIAS);
4085 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004086
H. Peter Anvin8b262472019-02-26 14:00:54 -08004087 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004088 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004089 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004090
H. Peter Anvin9e200162008-06-04 17:23:14 -07004091 last = tline;
4092 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004093 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004094
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004095 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004096 q = detoken(tline, false);
4097 macro_start = make_tok_qstr(NULL, q);
4098 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004099
4100 /*
4101 * We now have a macro name, an implicit parameter count of
4102 * zero, and a string token to use as an expansion. Create
4103 * and store an SMacro.
4104 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004105 define_smacro(mname, casesense, macro_start, NULL);
4106 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004107
H. Peter Anvin8b262472019-02-26 14:00:54 -08004108 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004109 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004110 goto done;
4111
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004112 last = tline;
4113 tline = expand_smacro(tline->next);
4114 last->next = NULL;
4115
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004116 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004117 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004118 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004119 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004120 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004121 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004122 }
4123
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004124 /*
4125 * Convert the string to a token stream. Note that smacros
4126 * are stored with the token stream reversed, so we have to
4127 * reverse the output of tokenize().
4128 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004129 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004130
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004131 /*
4132 * We now have a macro name, an implicit parameter count of
4133 * zero, and a numeric token to use as an expansion. Create
4134 * and store an SMacro.
4135 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004136 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004137 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004138 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004139
H. Peter Anvin418ca702008-05-30 10:42:30 -07004140 case PP_PATHSEARCH:
4141 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004142 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004143
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004144 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004145 goto done;
4146
H. Peter Anvin418ca702008-05-30 10:42:30 -07004147 last = tline;
4148 tline = expand_smacro(tline->next);
4149 last->next = NULL;
4150
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004151 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004152 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004153 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004154 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004155 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004156 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004157 }
4158 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004159 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004160
4161 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004162
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004163 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004164 if (!found_path)
4165 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004166 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004167
4168 /*
4169 * We now have a macro name, an implicit parameter count of
4170 * zero, and a string token to use as an expansion. Create
4171 * and store an SMacro.
4172 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004173 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004174 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004175 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004176 }
4177
H. Peter Anvine2c80182005-01-15 22:15:51 +00004178 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004179 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004180 goto done;
4181
H. Peter Anvine2c80182005-01-15 22:15:51 +00004182 last = tline;
4183 tline = expand_smacro(tline->next);
4184 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004185
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004186 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004187 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004188 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004189 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 free_tlist(tline);
4191 free_tlist(origline);
4192 return DIRECTIVE_FOUND;
4193 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004194
H. Peter Anvin8571f062019-09-23 16:40:03 -07004195 unquote_token(t);
4196 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004197
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 /*
4199 * We now have a macro name, an implicit parameter count of
4200 * zero, and a numeric token to use as an expansion. Create
4201 * and store an SMacro.
4202 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004203 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004204 free_tlist(tline);
4205 free_tlist(origline);
4206 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004207
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004208 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004209 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004210 goto done;
4211
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004212 last = tline;
4213 tline = expand_smacro(tline->next);
4214 last->next = NULL;
4215
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004216 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004217 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004218 switch (t->type) {
4219 case TOK_WHITESPACE:
4220 break;
4221 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004222 unquote_token(t);
4223 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004224 break;
4225 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004226 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004227 break;
4228 /* else fall through */
4229 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004230 nasm_nonfatal("non-string passed to `%s': %s", dname,
4231 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004232 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004233 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004234 }
4235 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004236
H. Peter Anvin8571f062019-09-23 16:40:03 -07004237 q = qbuf = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004238 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004239 if (t->type == TOK_INTERNAL_STRING)
4240 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004241 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004242 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004243
4244 /*
4245 * We now have a macro name, an implicit parameter count of
4246 * zero, and a numeric token to use as an expansion. Create
4247 * and store an SMacro.
4248 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004249 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004250 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004251 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004252 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004253 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004254
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004256 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004257 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004258 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004259 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004260
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004261 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004262 goto done;
4263
H. Peter Anvine2c80182005-01-15 22:15:51 +00004264 last = tline;
4265 tline = expand_smacro(tline->next);
4266 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004267
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004268 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004269 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004270
4271 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004272
H. Peter Anvine2c80182005-01-15 22:15:51 +00004273 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004274 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004275 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004276 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004277 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004278 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004279
H. Peter Anvin8b262472019-02-26 14:00:54 -08004280 pps.tptr = t->next;
4281 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004283 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 if (!evalresult) {
4285 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004286 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004287 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004288 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004289 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004290 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004292 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004293
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004294 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004295 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004296 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004297 } else {
4298 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004299 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004300 if (!evalresult) {
4301 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004302 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004303 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004304 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004305 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004306 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004307 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004308 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004309 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004310
H. Peter Anvin8571f062019-09-23 16:40:03 -07004311 unquote_token(t);
4312 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004313
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004314 /* make start and count being in range */
4315 if (start < 0)
4316 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004317 if (count < 0)
4318 count = len + count + 1 - start;
4319 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004320 count = len - start;
4321 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004322 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004323
H. Peter Anvin8571f062019-09-23 16:40:03 -07004324 txt = (start < 0) ? "" : tok_text(t) + start;
4325 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004326 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004327
H. Peter Anvine2c80182005-01-15 22:15:51 +00004328 /*
4329 * We now have a macro name, an implicit parameter count of
4330 * zero, and a numeric token to use as an expansion. Create
4331 * and store an SMacro.
4332 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004333 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004335 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004336 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004337
H. Peter Anvin8b262472019-02-26 14:00:54 -08004338 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004339 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004340 goto done;
4341
H. Peter Anvine2c80182005-01-15 22:15:51 +00004342 last = tline;
4343 tline = expand_smacro(tline->next);
4344 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004345
H. Peter Anvin8b262472019-02-26 14:00:54 -08004346 pps.tptr = tline;
4347 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004348 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004349 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004350 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004351 if (!evalresult)
4352 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004353
H. Peter Anvine2c80182005-01-15 22:15:51 +00004354 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004355 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004356
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004358 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004359 free_tlist(origline);
4360 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004361 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004362
H. Peter Anvin8571f062019-09-23 16:40:03 -07004363 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004364
H. Peter Anvine2c80182005-01-15 22:15:51 +00004365 /*
4366 * We now have a macro name, an implicit parameter count of
4367 * zero, and a numeric token to use as an expansion. Create
4368 * and store an SMacro.
4369 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004370 define_smacro(mname, casesense, macro_start, NULL);
4371 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004372
H. Peter Anvind2354082019-08-27 16:38:48 -07004373 case PP_ALIASES:
4374 tline = tline->next;
4375 tline = expand_smacro(tline);
4376 do_aliases = pp_get_boolean_option(tline, do_aliases);
4377 break;
4378
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 case PP_LINE:
4380 /*
4381 * Syntax is `%line nnn[+mmm] [filename]'
4382 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004383 if (unlikely(pp_noline))
4384 goto done;
4385
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004387 tline = skip_white(tline);
4388 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004389 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004390 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004392 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004393 m = 1;
4394 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004395 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004396 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004397 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004398 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004399 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004400 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004401 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004402 tline = tline->next;
4403 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004404 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004405 src_set_linnum(k);
4406 istk->lineinc = m;
4407 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004408 char *fname = detoken(tline, false);
4409 src_set_fname(fname);
4410 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004411 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004412 break;
4413 }
4414
4415done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004416 free_tlist(origline);
4417 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004418}
4419
4420/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004421 * Ensure that a macro parameter contains a condition code and
4422 * nothing else. Return the condition code index if so, or -1
4423 * otherwise.
4424 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004426{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004427 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004428
H. Peter Anvin25a99342007-09-22 17:45:45 -07004429 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004430 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004431
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004432 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004433 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004435 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004436 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004437 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004439
H. Peter Anvin8571f062019-09-23 16:40:03 -07004440 return bsii(tok_text(t), (const char **)conditions,
4441 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004442}
4443
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004444static inline bool pp_concat_match(const Token *t, unsigned int mask)
4445{
4446 return t && (PP_CONCAT_MASK(t->type) & mask);
4447}
4448
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004449/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004450 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004451 * pasting, if @handle_explicit passed then explicit pasting
4452 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004453 * The @m array can contain a series of token types which are
4454 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004455 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004456static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004457 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004458{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004459 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004460 bool pasted = false;
4461 char *buf, *p;
4462 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004463
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004464 /*
4465 * The last token before pasting. We need it
4466 * to be able to connect new handled tokens.
4467 * In other words if there were a tokens stream
4468 *
4469 * A -> B -> C -> D
4470 *
4471 * and we've joined tokens B and C, the resulting
4472 * stream should be
4473 *
4474 * A -> BC -> D
4475 */
4476 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004477 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004478
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004479 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004480 prev_nonspace = NULL;
4481
4482 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004483 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004484
4485 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004486 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004487 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004488 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004489 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004490
4491 case TOK_PASTE:
4492 /* Explicit pasting */
4493 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004494 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004495
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004496 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004497 if (!prev_nonspace) {
4498 nasm_nonfatal("No lvalue found on pasting");
4499 tok = delete_Token(tok);
4500 break;
4501 }
4502
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004503 did_paste = true;
4504
4505 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004506 t = *prev_nonspace;
4507
4508 /* Delete leading whitespace */
4509 next = zap_white(t->next);
4510
4511 /* Delete the %+ token itself */
4512 nasm_assert(next == tok);
4513 next = delete_Token(next);
4514
4515 /* Delete trailing whitespace */
4516 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004517
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004518 /*
4519 * No ending token, this might happen in two
4520 * cases
4521 *
4522 * 1) There indeed no right token at all
4523 * 2) There is a bare "%define ID" statement,
4524 * and @ID does expand to whitespace.
4525 *
4526 * So technically we need to do a grammar analysis
4527 * in another stage of parsing, but for now lets don't
4528 * change the behaviour people used to. Simply allow
4529 * whitespace after paste token.
4530 */
4531 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004532 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004533 break;
4534 }
4535
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004536 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004537 p = mempcpy(p, tok_text(t), t->len);
4538 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004539 *p = '\0';
4540 delete_Token(t);
4541 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004542 nasm_free(buf);
4543
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004544 if (unlikely(!t)) {
4545 /*
4546 * No output at all? Replace with a single whitespace.
4547 * This should never happen.
4548 */
4549 t = new_White(NULL);
4550 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004551
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004552 *prev_nonspace = tok = t;
4553 while (t->next)
4554 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004555
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004556 /* Delete the second token and attach to the end of the list */
4557 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004558
4559 /* We want to restart from the head of the pasted token */
4560 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004561 break;
4562
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004563 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004564 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004565 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004566 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004567 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004568 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004569
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004570 if (i >= mnum)
4571 break;
4572
4573 len = tok->len;
4574 while (pp_concat_match(next, m[i].mask_tail)) {
4575 len += next->len;
4576 next = next->next;
4577 }
4578
4579 /* No match or no text to process */
4580 if (len == tok->len)
4581 break;
4582
4583 p = buf = nasm_malloc(len + 1);
4584 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004585 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004586 tok = delete_Token(tok);
4587 }
4588 *p = '\0';
4589 *prev_next = tok = t = tokenize(buf);
4590 nasm_free(buf);
4591
4592 /*
4593 * Connect pasted into original stream,
4594 * ie A -> new-tokens -> B
4595 */
4596 while (t->next)
4597 t = t->next;
4598 t->next = next;
4599 prev_next = prev_nonspace = &t->next;
4600 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004601 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004602 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004603
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004604 if (did_paste) {
4605 pasted = true;
4606 } else {
4607 prev_next = &tok->next;
4608 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4609 prev_nonspace = prev_next;
4610 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004611
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004612 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004613 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004614
4615 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004616}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004617
4618/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004619 * Computes the proper rotation of mmacro parameters
4620 */
4621static int mmac_rotate(const MMacro *mac, unsigned int n)
4622{
4623 if (--n < mac->nparam)
4624 n = (n + mac->rotate) % mac->nparam;
4625
4626 return n+1;
4627}
4628
4629/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004630 * expands to a list of tokens from %{x:y}
4631 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004632static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004633{
4634 Token *t = tline, **tt, *tm, *head;
4635 char *pos;
4636 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004637
H. Peter Anvin8571f062019-09-23 16:40:03 -07004638 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004639 nasm_assert(pos);
4640
4641 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004642 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004643
4644 /*
4645 * only macros params are accounted so
4646 * if someone passes %0 -- we reject such
4647 * value(s)
4648 */
4649 if (lst == 0 || fst == 0)
4650 goto err;
4651
4652 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004653 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4654 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004655 goto err;
4656
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004657 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4658 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004659
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004660 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004661 fst--, lst--;
4662
4663 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004664 * It will be at least one token. Note we
4665 * need to scan params until separator, otherwise
4666 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004667 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004668 j = (fst + mac->rotate) % mac->nparam;
4669 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004670 if (!tm)
4671 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004672 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004673 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004674 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004675 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004676 *tt = t, tt = &t->next, tm = tm->next;
4677 }
4678
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004679 if (fst < lst) {
4680 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004681 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004682 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004683 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004684 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004685 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004686 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004687 *tt = t, tt = &t->next, tm = tm->next;
4688 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004689 }
4690 } else {
4691 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004692 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004693 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004694 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004695 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004696 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004697 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004698 *tt = t, tt = &t->next, tm = tm->next;
4699 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004700 }
4701 }
4702
4703 *last = tt;
4704 return head;
4705
4706err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004707 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004708 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004709 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004710}
4711
H. Peter Anvin76690a12002-04-30 20:52:49 +00004712/*
4713 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004714 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004715 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004716 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004717static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004718{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004719 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004720 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004721 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004722
4723 tail = &thead;
4724 thead = NULL;
4725
H. Peter Anvine2c80182005-01-15 22:15:51 +00004726 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004727 bool change;
4728 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004729 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004730 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004731
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004732 tline = tline->next;
4733 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004734
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004735 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004736 case TOK_LOCAL_SYMBOL:
4737 type = TOK_ID;
4738 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4739 change = true;
4740 break;
4741 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004742 {
4743 Token *tt = NULL;
4744
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004745 change = true;
4746
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004747 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004748 nasm_nonfatal("`%s': not in a macro call", text);
4749 text = NULL;
4750 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004751 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004752
4753 if (strchr(text, ':')) {
4754 /*
4755 * seems we have a parameters range here
4756 */
4757 Token *head, **last;
4758 head = expand_mmac_params_range(mac, t, &last);
4759 if (head) {
4760 *tail = head;
4761 *last = tline;
4762 text = NULL;
4763 }
4764 break;
4765 }
4766
4767 switch (text[1]) {
4768 /*
4769 * We have to make a substitution of one of the
4770 * forms %1, %-1, %+1, %%foo, %0, %00.
4771 */
4772 case '0':
4773 if (!text[2]) {
4774 type = TOK_NUMBER;
4775 text = nasm_asprintf("%d", mac->nparam);
4776 break;
4777 }
4778 if (text[2] != '0' || text[3])
4779 goto invalid;
4780 /* a possible captured label == mac->params[0] */
4781 /* fall through */
4782 default:
4783 {
4784 unsigned long n;
4785 char *ep;
4786
4787 n = strtoul(text + 1, &ep, 10);
4788 if (unlikely(*ep))
4789 goto invalid;
4790
4791 if (n <= mac->nparam) {
4792 n = mmac_rotate(mac, n);
4793 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4794 }
4795 text = NULL;
4796 break;
4797 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004798 case '-':
4799 case '+':
4800 {
4801 int cc;
4802 unsigned long n;
4803 char *ep;
4804
4805 text = NULL;
4806
H. Peter Anvin8571f062019-09-23 16:40:03 -07004807 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004808 if (unlikely(*ep))
4809 goto invalid;
4810
4811 if (n && n < mac->nparam) {
4812 n = mmac_rotate(mac, n);
4813 tt = mac->params[n];
4814 }
4815 cc = find_cc(tt);
4816 if (cc == -1) {
4817 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004818 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004819 text = NULL;
4820 break;
4821 }
4822
4823 type = TOK_ID;
4824 if (text[1] == '-') {
4825 int ncc = inverse_ccs[cc];
4826 if (unlikely(ncc == -1)) {
4827 nasm_nonfatal("condition code `%s' is not invertible",
4828 conditions[cc]);
4829 break;
4830 }
4831 cc = ncc;
4832 }
4833 text = nasm_strdup(conditions[cc]);
4834 break;
4835 }
4836
4837 invalid:
4838 nasm_nonfatal("invalid macro parameter: `%s'", text);
4839 text = NULL;
4840 break;
4841 }
4842 break;
4843 }
4844
4845 case TOK_PREPROC_Q:
4846 if (mac) {
4847 type = TOK_ID;
4848 text = nasm_strdup(mac->iname);
4849 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004850 } else {
4851 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004852 }
4853 break;
4854
4855 case TOK_PREPROC_QQ:
4856 if (mac) {
4857 type = TOK_ID;
4858 text = nasm_strdup(mac->name);
4859 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004860 } else {
4861 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004862 }
4863 break;
4864
4865 case TOK_INDIRECT:
4866 {
4867 Token *tt;
4868
H. Peter Anvin8571f062019-09-23 16:40:03 -07004869 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004870 tt = expand_mmac_params(tt);
4871 tt = expand_smacro(tt);
4872 /* Why dup_tlist() here? We should own tt... */
4873 dup_tlist(tt, &tail);
4874 text = NULL;
4875 change = true;
4876 break;
4877 }
4878
4879 default:
4880 change = false;
4881 break;
4882 }
4883
4884 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004885 if (!text) {
4886 delete_Token(t);
4887 } else {
4888 *tail = t;
4889 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004890 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004891 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004892 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004893 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004894 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004895 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004896 tail = &t->next;
4897 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004898 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004899
H. Peter Anvineba20a72002-04-30 20:53:55 +00004900 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004901
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004902 if (changed) {
4903 const struct tokseq_match t[] = {
4904 {
4905 PP_CONCAT_MASK(TOK_ID) |
4906 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4907 PP_CONCAT_MASK(TOK_ID) |
4908 PP_CONCAT_MASK(TOK_NUMBER) |
4909 PP_CONCAT_MASK(TOK_FLOAT) |
4910 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4911 },
4912 {
4913 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4914 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4915 }
4916 };
4917 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4918 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004919
H. Peter Anvin76690a12002-04-30 20:52:49 +00004920 return thead;
4921}
4922
H. Peter Anvin322bee02019-08-10 01:38:06 -07004923static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004924
H. Peter Anvin76690a12002-04-30 20:52:49 +00004925/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004926 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004927 * a macro at all, it is simply copied to the output and the pointer
4928 * advanced. tpp should be a pointer to a pointer (usually the next
4929 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004930 * to point to the first token of the expansion, and *tpp updated to
4931 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004932 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004933 * If the expansion is empty, *tpp will be unchanged but **tpp will
4934 * be advanced past the macro call.
4935 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004936 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004937 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004938static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004939{
4940 Token **params = NULL;
4941 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004942 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004943 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004944 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004945 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004946 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004947 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004948 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004949
4950 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004951 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004952
H. Peter Anvin8571f062019-09-23 16:40:03 -07004953 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004954
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004955 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004956 smacro_deadman.levels--;
4957
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004958 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004959 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004960 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004961 smacro_deadman.triggered = true;
4962 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004963 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004964 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004965 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004966 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004967 Context *ctx = get_ctx(mname, &mname);
4968 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4969 } else {
4970 goto not_a_macro;
4971 }
4972
4973 /*
4974 * We've hit an identifier of some sort. First check whether the
4975 * identifier is a single-line macro at all, then think about
4976 * checking for parameters if necessary.
4977 */
4978 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004979 if (unlikely(m->alias && !do_aliases))
4980 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004981 if (!mstrcmp(m->name, mname, m->casesense))
4982 break;
4983 }
4984
4985 if (!m) {
4986 goto not_a_macro;
4987 }
4988
4989 /* Parse parameters, if applicable */
4990
4991 params = NULL;
4992 nparam = 0;
4993
4994 if (m->nparam == 0) {
4995 /*
4996 * Simple case: the macro is parameterless.
4997 * Nothing to parse; the expansion code will
4998 * drop the macro name token.
4999 */
5000 } else {
5001 /*
5002 * Complicated case: at least one macro with this name
5003 * exists and takes parameters. We must find the
5004 * parameters in the call, count them, find the SMacro
5005 * that corresponds to that form of the macro call, and
5006 * substitute for the parameters when we expand. What a
5007 * pain.
5008 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005009 Token *t;
5010 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005011
5012 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005013 tline = skip_white(tline);
5014 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005015 /*
5016 * This macro wasn't called with parameters: ignore
5017 * the call. (Behaviour borrowed from gnu cpp.)
5018 */
5019 goto not_a_macro;
5020 }
5021
5022 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005023 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005024 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005025 t = tline; /* tline points to leading ( */
5026
5027 while (paren) {
5028 t = t->next;
5029
5030 if (!t) {
5031 nasm_nonfatal("macro call expects terminating `)'");
5032 goto not_a_macro;
5033 }
5034
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005035 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005036 continue;
5037
H. Peter Anvin8571f062019-09-23 16:40:03 -07005038 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005039 case ',':
5040 if (!brackets)
5041 nparam++;
5042 break;
5043
5044 case '{':
5045 brackets++;
5046 break;
5047
5048 case '}':
5049 if (brackets > 0)
5050 brackets--;
5051 break;
5052
5053 case '(':
5054 if (!brackets)
5055 paren++;
5056 break;
5057
5058 case ')':
5059 if (!brackets)
5060 paren--;
5061 break;
5062
5063 default:
5064 break; /* Normal token */
5065 }
5066 }
5067
5068 /*
5069 * Look for a macro matching in both name and parameter count.
5070 * We already know any matches cannot be anywhere before the
5071 * current position of "m", so there is no reason to
5072 * backtrack.
5073 */
5074 while (1) {
5075 if (!m) {
5076 /*!
5077 *!macro-params-single [on] single-line macro calls with wrong parameter count
5078 *! warns about \i{single-line macros} being invoked
5079 *! with the wrong number of parameters.
5080 */
5081 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
5082 "single-line macro `%s' exists, "
5083 "but not taking %d parameter%s",
5084 mname, nparam, (nparam == 1) ? "" : "s");
5085 goto not_a_macro;
5086 }
5087
5088 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005089 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005090 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005091 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005092 break; /* Also good */
5093 }
5094 m = m->next;
5095 }
5096 }
5097
5098 if (m->in_progress)
5099 goto not_a_macro;
5100
5101 /* Expand the macro */
5102 m->in_progress = true;
5103
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005104 if (nparam) {
5105 /* Extract parameters */
5106 Token **phead, **pep;
5107 int white = 0;
5108 int brackets = 0;
5109 int paren;
5110 bool bracketed = false;
5111 bool bad_bracket = false;
5112 enum sparmflags flags;
5113
5114 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005115 paren = 1;
5116 nasm_newn(params, nparam);
5117 i = 0;
5118 flags = m->params[i].flags;
5119 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005120 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005121
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005122 while (paren) {
5123 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005124 char ch;
5125
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005126 tline = tline->next;
5127
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005128 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005129 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005130
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005131 ch = 0;
5132 skip = false;
5133
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005134
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005135 switch (tline->type) {
5136 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005137 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005138 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005139 break;
5140
5141 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005142 if (!(flags & SPARM_NOSTRIP)) {
5143 if (brackets || *phead)
5144 white++; /* Keep interior whitespace */
5145 skip = true;
5146 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005147 break;
5148
5149 default:
5150 break;
5151 }
5152
5153 switch (ch) {
5154 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005155 if (!brackets && !(flags & SPARM_GREEDY)) {
5156 i++;
5157 nasm_assert(i < nparam);
5158 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005159 *pep = NULL;
5160 bracketed = false;
5161 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005162 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005163 }
5164 break;
5165
5166 case '{':
5167 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005168 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5169 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005170 }
5171 brackets++;
5172 break;
5173
5174 case '}':
5175 if (brackets > 0) {
5176 if (!--brackets)
5177 skip = bracketed;
5178 }
5179 break;
5180
5181 case '(':
5182 if (!brackets)
5183 paren++;
5184 break;
5185
5186 case ')':
5187 if (!brackets) {
5188 paren--;
5189 if (!paren) {
5190 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005191 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005192 }
5193 }
5194 break;
5195
5196 default:
5197 break; /* Normal token */
5198 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005199
5200 if (!skip) {
5201 Token *t;
5202
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005203 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005204
5205 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005206 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005207 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005208 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005209 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005210 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005211 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005212 }
5213 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005214
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005215 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005216 * Possible further processing of parameters. Note that the
5217 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005218 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005219 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005220 enum sparmflags flags = m->params[i].flags;
5221
5222 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005223 /* Evaluate this parameter as a number */
5224 struct ppscan pps;
5225 struct tokenval tokval;
5226 expr *evalresult;
5227 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005228
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005229 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5230 pps.ntokens = -1;
5231 tokval.t_type = TOKEN_INVALID;
5232 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005233
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005234 free_tlist(eval_param);
5235 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005236
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005237 if (!evalresult) {
5238 /* Nothing meaningful to do */
5239 } else if (tokval.t_type) {
5240 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5241 } else if (!is_simple(evalresult)) {
5242 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5243 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005244 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005245 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005246 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005247
5248 if (flags & SPARM_STR) {
5249 /* Convert expansion to a quoted string */
5250 char *arg;
5251 Token *qs;
5252
5253 qs = expand_smacro_noreset(params[i]);
5254 arg = detoken(qs, false);
5255 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005256 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005257 nasm_free(arg);
5258 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005259 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005260 }
5261
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005262 /* Note: we own the expansion this returns. */
5263 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005264
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005265 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005266 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005267 tline = tafter;
5268
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005269 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005270 cond_comma = false;
5271
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005272 while (t) {
5273 enum pp_token_type type = t->type;
5274 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005275
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005276 switch (type) {
5277 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005278 delete_Token(t);
5279 t = dup_Token(tline, mstart);
5280 break;
5281
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005282 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005283 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005284 size_t mlen = strlen(m->name);
5285 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005286 char *p;
5287
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005288 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005289 if (t->type == TOK_LOCAL_MACRO) {
5290 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005291 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005292 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005293
H. Peter Anvin8571f062019-09-23 16:40:03 -07005294 psp = tok_text(mstart);
5295 get_ctx(psp, &pep);
5296 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005297
H. Peter Anvin8571f062019-09-23 16:40:03 -07005298 len = mlen + plen;
5299 p = nasm_malloc(len + 1);
5300 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005301 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005302 len = mlen;
5303 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005304 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005305 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005306 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005307 set_text_free(t, p, len);
5308
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005309 t->next = tline;
5310 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005311 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005312
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005313 case TOK_COND_COMMA:
5314 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005315 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005316 break;
5317
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005318 case TOK_ID:
5319 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005320 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005321 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005322 /*
5323 * Chain this into the target line *before* expanding,
5324 * that way we pick up any arguments to the new macro call,
5325 * if applicable.
5326 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005327 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005328 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005329 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005330 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005331 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005332 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005333 default:
5334 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005335 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005336 nasm_assert(!tup && param < nparam);
5337 delete_Token(t);
5338 t = NULL;
5339 tup = tnext;
5340 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005341 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005342 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005343 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005344 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005345 }
5346
5347 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005348 Token *endt = tline;
5349
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005350 tline = t;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005351 while (!cond_comma && t && t != endt)
5352 cond_comma = t->type != TOK_WHITESPACE;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005353 }
5354
5355 if (tnext) {
5356 t = tnext;
5357 } else {
5358 t = tup;
5359 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005360 }
5361 }
5362
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005363 **tpp = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005364 for (t = tline; t != tafter; t = t->next)
5365 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005366
5367 m->in_progress = false;
5368
5369 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005370 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005371 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005372
5373 /*
5374 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005375 * and then advance to the next input token. Note that this is
5376 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005377 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005378not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005379 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005380 m = NULL;
5381done:
5382 smacro_deadman.levels++;
5383 if (unlikely(params))
5384 free_tlist_array(params, nparam);
5385 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005386}
5387
5388/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005389 * Expand all single-line macro calls made in the given line.
5390 * Return the expanded version of the line. The original is deemed
5391 * to be destroyed in the process. (In reality we'll just move
5392 * Tokens from input to output a lot of the time, rather than
5393 * actually bothering to destroy and replicate.)
5394 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005395static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005396{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005397 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005398 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5399 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005400 return expand_smacro_noreset(tline);
5401}
5402
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005403static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005404{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005405 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005406 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005407
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005408 if (!org_tline)
5409 return NULL; /* Empty input */
5410
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005411 /*
5412 * Trick: we should avoid changing the start token pointer since it can
5413 * be contained in "next" field of other token. Because of this
5414 * we allocate a copy of first token and work with it; at the end of
5415 * routine we copy it back
5416 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005417 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005418
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005419 /*
5420 * Pretend that we always end up doing expansion on the first pass;
5421 * that way %+ get processed. However, if we process %+ before the
5422 * first pass, we end up with things like MACRO %+ TAIL trying to
5423 * look up the macro "MACROTAIL", which we don't want.
5424 */
5425 expanded = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005426 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005427 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005428 {
5429 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005430 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5431 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005432 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5433 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005434 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5435 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005436 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5437 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5438 }
5439 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005440 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005441
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005442 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005443 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005444
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005445 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005446 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005447
5448 /*
5449 * Now scan the entire line and look for successive TOK_IDs
5450 * that resulted after expansion (they can't be produced by
5451 * tokenize()). The successive TOK_IDs should be concatenated.
5452 * Also we look for %+ tokens and concatenate the tokens
5453 * before and after them (without white spaces in between).
5454 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005455 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5456 break; /* Done again! */
5457
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005458 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005459 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005460
H. Peter Anvin8571f062019-09-23 16:40:03 -07005461 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005462 /*
5463 * The expression expanded to empty line;
5464 * we can't return NULL because of the "trick" above.
5465 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005466 */
5467
5468 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005469 }
5470
H. Peter Anvin8571f062019-09-23 16:40:03 -07005471 steal_Token(org_tline, tline);
5472 org_tline->next = tline->next;
5473 delete_Token(tline);
5474
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005475 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005476}
5477
5478/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005479 * Similar to expand_smacro but used exclusively with macro identifiers
5480 * right before they are fetched in. The reason is that there can be
5481 * identifiers consisting of several subparts. We consider that if there
5482 * are more than one element forming the name, user wants a expansion,
5483 * otherwise it will be left as-is. Example:
5484 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005485 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005486 *
5487 * the identifier %$abc will be left as-is so that the handler for %define
5488 * will suck it and define the corresponding value. Other case:
5489 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005490 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005491 *
5492 * In this case user wants name to be expanded *before* %define starts
5493 * working, so we'll expand %$abc into something (if it has a value;
5494 * otherwise it will be left as-is) then concatenate all successive
5495 * PP_IDs into one.
5496 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005497static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005498{
5499 Token *cur, *oldnext = NULL;
5500
H. Peter Anvin734b1882002-04-30 21:01:08 +00005501 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005502 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005503
5504 cur = tline;
5505 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005506 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5507 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005508 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005509
5510 /* If identifier consists of just one token, don't expand */
5511 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005512 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005513
H. Peter Anvine2c80182005-01-15 22:15:51 +00005514 if (cur) {
5515 oldnext = cur->next; /* Detach the tail past identifier */
5516 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005517 }
5518
H. Peter Anvin734b1882002-04-30 21:01:08 +00005519 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005520
H. Peter Anvine2c80182005-01-15 22:15:51 +00005521 if (cur) {
5522 /* expand_smacro possibly changhed tline; re-scan for EOL */
5523 cur = tline;
5524 while (cur && cur->next)
5525 cur = cur->next;
5526 if (cur)
5527 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005528 }
5529
5530 return tline;
5531}
5532
5533/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005534 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005535 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005536 * to check for an initial label - that's taken care of in
5537 * expand_mmacro - but must check numbers of parameters. Guaranteed
5538 * to be called with tline->type == TOK_ID, so the putative macro
5539 * name is easy to find.
5540 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005541static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005542{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005543 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005544 Token **params;
5545 int nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005546 const char *finding = tok_text(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005547
H. Peter Anvin8571f062019-09-23 16:40:03 -07005548 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005549
5550 /*
5551 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005552 * name which isn't already excluded by macro cycle removal.
5553 * (The cycle removal test here helps optimize the case of wrapping
5554 * instructions, and is cheap to do here.)
5555 *
5556 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005557 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005558 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005559 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005560 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005561 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005562 (m->in_progress != 1 || m->max_depth > 0))
5563 break; /* Found something that needs consideration */
5564 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005565 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005566 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005567
5568 /*
5569 * OK, we have a potential macro. Count and demarcate the
5570 * parameters.
5571 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005572 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005573
5574 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005575 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005576 * structure that handles this number.
5577 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005578 while (m) {
5579 if (m->nparam_min <= nparam
5580 && (m->plus || nparam <= m->nparam_max)) {
5581 /*
5582 * This one is right. Just check if cycle removal
5583 * prohibits us using it before we actually celebrate...
5584 */
5585 if (m->in_progress > m->max_depth) {
5586 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005587 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005588 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005589 }
5590 nasm_free(params);
5591 return NULL;
5592 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005593 /*
5594 * It's right, and we can use it. Add its default
5595 * parameters to the end of our list if necessary.
5596 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005597 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005598 int newnparam = m->nparam_min + m->ndefs;
5599 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5600 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5601 (newnparam - nparam) * sizeof(*params));
5602 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005603 }
5604 /*
5605 * If we've gone over the maximum parameter count (and
5606 * we're in Plus mode), ignore parameters beyond
5607 * nparam_max.
5608 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005609 if (m->plus && nparam > m->nparam_max)
5610 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005611
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005612 /*
5613 * If nparam was adjusted above, make sure the list is still
5614 * NULL-terminated.
5615 */
5616 params[nparam+1] = NULL;
5617
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005618 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005619 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005620 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005621 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005622 }
5623 /*
5624 * This one wasn't right: look for the next one with the
5625 * same name.
5626 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005627 list_for_each(m, m->next)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005628 if (!mstrcmp(m->name, tok_text(tline), m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005629 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005630 }
5631
5632 /*
5633 * After all that, we didn't find one with the right number of
5634 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005635 *!
5636 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5637 *! warns about \i{multi-line macros} being invoked
5638 *! with the wrong number of parameters. See \k{mlmacover} for an
5639 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005640 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005641 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5642 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005643 tok_text(tline), nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005644 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005645 return NULL;
5646}
5647
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005648
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005649#if 0
5650
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005651/*
5652 * Save MMacro invocation specific fields in
5653 * preparation for a recursive macro expansion
5654 */
5655static void push_mmacro(MMacro *m)
5656{
5657 MMacroInvocation *i;
5658
5659 i = nasm_malloc(sizeof(MMacroInvocation));
5660 i->prev = m->prev;
5661 i->params = m->params;
5662 i->iline = m->iline;
5663 i->nparam = m->nparam;
5664 i->rotate = m->rotate;
5665 i->paramlen = m->paramlen;
5666 i->unique = m->unique;
5667 i->condcnt = m->condcnt;
5668 m->prev = i;
5669}
5670
5671
5672/*
5673 * Restore MMacro invocation specific fields that were
5674 * saved during a previous recursive macro expansion
5675 */
5676static void pop_mmacro(MMacro *m)
5677{
5678 MMacroInvocation *i;
5679
5680 if (m->prev) {
5681 i = m->prev;
5682 m->prev = i->prev;
5683 m->params = i->params;
5684 m->iline = i->iline;
5685 m->nparam = i->nparam;
5686 m->rotate = i->rotate;
5687 m->paramlen = i->paramlen;
5688 m->unique = i->unique;
5689 m->condcnt = i->condcnt;
5690 nasm_free(i);
5691 }
5692}
5693
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005694#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005695
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005696/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005697 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005698 */
5699static void list_mmacro_call(const MMacro *m)
5700{
5701 const char prefix[] = " ;;; [macro] ";
5702 size_t namelen, size;
5703 char *buf, *p;
5704 unsigned int i;
5705 const Token *t;
5706
5707 namelen = strlen(m->iname);
5708 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5709
5710 for (i = 1; i <= m->nparam; i++) {
5711 int j = 0;
5712 size += 3; /* Braces and space/comma */
5713 list_for_each(t, m->params[i]) {
5714 if (j++ >= m->paramlen[i])
5715 break;
5716 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5717 }
5718 }
5719
5720 buf = p = nasm_malloc(size);
5721 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5722 p = mempcpy(p, m->iname, namelen);
5723 *p++ = ' ';
5724
5725 for (i = 1; i <= m->nparam; i++) {
5726 int j = 0;
5727 *p++ = '{';
5728 list_for_each(t, m->params[i]) {
5729 if (j++ >= m->paramlen[i])
5730 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005731 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005732 }
5733 *p++ = '}';
5734 *p++ = ',';
5735 }
5736
5737 *--p = '\0'; /* Replace last delimeter with null */
5738 lfmt->line(LIST_MACRO, -1, buf);
5739 nasm_free(buf);
5740}
5741
5742/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005743 * Expand the multi-line macro call made by the given line, if
5744 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005745 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005746 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005747static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005748{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005749 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005750 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005751 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005752 Token **params, *t, *tt;
5753 MMacro *m;
5754 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005755 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005756 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005757 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005758
5759 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005760 t = skip_white(t);
5761 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005762 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005763 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005764 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005765 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005766 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07005767 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005768 Token *last;
5769 /*
5770 * We have an id which isn't a macro call. We'll assume
5771 * it might be a label; we'll also check to see if a
5772 * colon follows it. Then, if there's another id after
5773 * that lot, we'll check it again for macro-hood.
5774 */
5775 label = last = t;
5776 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005777 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005778 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005779 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005780 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005781 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005782 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005783 last = t, t = t->next;
5784 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005785 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005786 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005787 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005788 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005789 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005790 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005791
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005792 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5793 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5794 if (!mmacro_deadman.triggered) {
5795 nasm_nonfatal("interminable multiline macro recursion");
5796 mmacro_deadman.triggered = true;
5797 }
5798 return 0;
5799 }
5800
5801 mmacro_deadman.total++;
5802 mmacro_deadman.levels++;
5803
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005804 /*
5805 * Fix up the parameters: this involves stripping leading and
5806 * trailing whitespace, then stripping braces if they are
5807 * present.
5808 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005809 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005810
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005811 nasm_assert(params[nparam+1] == NULL);
5812
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005813 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005814 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005815 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005816 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005817 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005818
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005819 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005820 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005821 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005822 brace = 1;
5823 braced = true;
5824 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005825 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005826
5827 params[i] = t;
5828 for (; t; t = t->next) {
5829 if (tok_white(t)) {
5830 white++;
5831 continue;
5832 }
5833
5834 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005835 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005836 case ',':
5837 if (comma && !brace)
5838 goto endparam;
5839 break;
5840
5841 case '{':
5842 brace++;
5843 break;
5844
5845 case '}':
5846 brace--;
5847 if (braced && !brace) {
5848 paramlen[i] += white;
5849 goto endparam;
5850 }
5851 break;
5852
5853 default:
5854 break;
5855 }
5856 }
5857
5858 paramlen[i] += white + 1;
5859 white = 0;
5860 }
5861 endparam:
5862 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005863 }
5864
5865 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005866 * OK, we have a MMacro structure together with a set of
5867 * parameters. We must now go through the expansion and push
5868 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005869 * parameter tokens and macro-local tokens doesn't get done
5870 * until the single-line macro substitution process; this is
5871 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005872 * later through %rotate and give the right semantics for
5873 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005874 *
5875 * First, push an end marker on to istk->expansion, mark this
5876 * macro as in progress, and set up its invocation-specific
5877 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005878 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005879 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005880 ll->next = istk->expansion;
5881 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005882 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005883
5884 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005885 * Save the previous MMacro expansion in the case of
5886 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005887 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005888#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005889 if (m->max_depth && m->in_progress)
5890 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005891#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005892
5893 m->in_progress ++;
5894 m->params = params;
5895 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005896 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005897 m->nparam = nparam;
5898 m->rotate = 0;
5899 m->paramlen = paramlen;
5900 m->unique = unique++;
5901 m->lineno = 0;
5902 m->condcnt = 0;
5903
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005904 m->mstk = istk->mstk;
5905 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005906
5907 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005908 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005909 ll->next = istk->expansion;
5910 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005911 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005912 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005913
5914 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005915 * If we had a label, and this macro definition does not include
5916 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005917 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005918 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005919 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005920 /*
5921 * We had a label. If this macro contains an %00 parameter,
5922 * save the value as a special parameter (which is what it
5923 * is), otherwise push it as the first line of the macro
5924 * expansion.
5925 */
5926 if (m->capture_label) {
5927 params[0] = dup_Token(NULL, label);
5928 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005929 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005930 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005931 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005932 ll->finishes = NULL;
5933 ll->next = istk->expansion;
5934 istk->expansion = ll;
5935 ll->first = startline;
5936 if (!dont_prepend) {
5937 while (label->next)
5938 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005939 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005940 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005941 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005942 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005943
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005944 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005945
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005946 if (list_option('m') && !m->nolist)
5947 list_mmacro_call(m);
5948
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005949 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005950}
5951
H. Peter Anvin130736c2016-02-17 20:27:41 -08005952/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005953 * This function decides if an error message should be suppressed.
5954 * It will never be called with a severity level of ERR_FATAL or
5955 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005956 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005957static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005958{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005959 /*
5960 * If we're in a dead branch of IF or something like it, ignore the error.
5961 * However, because %else etc are evaluated in the state context
5962 * of the previous branch, errors might get lost:
5963 * %if 0 ... %else trailing garbage ... %endif
5964 * So %else etc should set the ERR_PP_PRECOND flag.
5965 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005966 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005967 ((severity & ERR_PP_PRECOND) ?
5968 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005969 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005970 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005971
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005972 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005973}
5974
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005975static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005976stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005977{
5978 (void)s;
5979 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005980 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005981
H. Peter Anvin8571f062019-09-23 16:40:03 -07005982 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08005983}
5984
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005985static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005986stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005987{
5988 (void)s;
5989 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005990 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005991
H. Peter Anvin8571f062019-09-23 16:40:03 -07005992 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08005993}
5994
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005995static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005996stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005997{
5998 (void)s;
5999 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006000 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006001
H. Peter Anvin8571f062019-09-23 16:40:03 -07006002 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006003}
6004
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006005static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006006stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006007{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006008 (void)s;
6009 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006010 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006011
6012 switch (globalbits) {
6013 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006014 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006015 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006016 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006017 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006018 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006019 default:
6020 panic();
6021 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006022}
6023
H. Peter Anvin8b262472019-02-26 14:00:54 -08006024/* Add magic standard macros */
6025struct magic_macros {
6026 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006027 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006028 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006029};
6030static const struct magic_macros magic_macros[] =
6031{
H. Peter Anvind2354082019-08-27 16:38:48 -07006032 { "__?FILE?__", 0, stdmac_file },
6033 { "__?LINE?__", 0, stdmac_line },
6034 { "__?BITS?__", 0, stdmac_bits },
6035 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006036 { NULL, 0, NULL }
6037};
6038
6039static void pp_add_magic_stdmac(void)
6040{
6041 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006042 SMacro tmpl;
6043
6044 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006045
6046 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006047 tmpl.nparam = m->nparam;
6048 tmpl.expand = m->func;
6049 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006050 }
6051}
6052
H. Peter Anvin734b1882002-04-30 21:01:08 +00006053static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006054pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006055{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006056 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006057 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006058
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006059 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006060 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006061 nested_mac_count = 0;
6062 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006063 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006064 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006065 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006066 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07006067 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07006068
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006069 if (!use_loaded)
6070 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6071 memset(use_loaded, 0, use_package_count * sizeof(bool));
6072
H. Peter Anvin6686de22019-08-10 05:33:14 -07006073 /* First set up the top level input file */
6074 nasm_new(istk);
6075 istk->fp = nasm_open_read(file, NF_TEXT);
6076 src_set(0, file);
6077 istk->lineinc = 1;
6078 if (!istk->fp)
6079 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6080
6081 strlist_add(deplist, file);
6082
6083 /*
6084 * Set up the stdmac packages as a virtual include file,
6085 * indicated by a null file pointer.
6086 */
6087 nasm_new(inc);
6088 inc->next = istk;
6089 inc->fname = src_set_fname(NULL);
6090 inc->nolist = !list_option('b');
6091 istk = inc;
6092 lfmt->uplevel(LIST_INCLUDE, 0);
6093
H. Peter Anvin8b262472019-02-26 14:00:54 -08006094 pp_add_magic_stdmac();
6095
H. Peter Anvinf7606612016-07-13 14:23:48 -07006096 if (tasm_compatible_mode)
6097 pp_add_stdmac(nasm_stdmac_tasm);
6098
6099 pp_add_stdmac(nasm_stdmac_nasm);
6100 pp_add_stdmac(nasm_stdmac_version);
6101
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006102 if (extrastdmac)
6103 pp_add_stdmac(extrastdmac);
6104
H. Peter Anvinf7606612016-07-13 14:23:48 -07006105 stdmacpos = stdmacros[0];
6106 stdmacnext = &stdmacros[1];
6107
H. Peter Anvind2456592008-06-19 15:04:18 -07006108 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006109
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006110 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006111 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006112 * other builtins, because it is special -- it varies between
6113 * passes -- but there is really no particular reason to make it
6114 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006115 *
6116 * 0 = dependencies only
6117 * 1 = preparatory passes
6118 * 2 = final pass
6119 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006120 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006121 switch (mode) {
6122 case PP_NORMAL:
6123 apass = pass_final() ? 2 : 1;
6124 break;
6125 case PP_DEPS:
6126 apass = 0;
6127 break;
6128 case PP_PREPROC:
6129 apass = 3;
6130 break;
6131 default:
6132 panic();
6133 }
6134
H. Peter Anvin8571f062019-09-23 16:40:03 -07006135 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006136}
6137
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006138static void pp_init(void)
6139{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006140}
6141
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006142/*
6143 * Get a line of tokens. If we popped the macro expansion/include stack,
6144 * we return a pointer to the dummy token tok_pop; at that point if
6145 * istk is NULL then we have reached end of input;
6146 */
6147static Token tok_pop; /* Dummy token placeholder */
6148
6149static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006150{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006151 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006152 Line *l = istk->expansion;
6153 Token *tline = NULL;
6154 Token *dtline;
6155
H. Peter Anvine2c80182005-01-15 22:15:51 +00006156 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006157 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006158 * buffer or from the input file.
6159 */
6160 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006161 while (l && l->finishes) {
6162 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006163
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006164 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006165 /*
6166 * This is a macro-end marker for a macro with no
6167 * name, which means it's not really a macro at all
6168 * but a %rep block, and the `in_progress' field is
6169 * more than 1, meaning that we still need to
6170 * repeat. (1 means the natural last repetition; 0
6171 * means termination by %exitrep.) We have
6172 * therefore expanded up to the %endrep, and must
6173 * push the whole block on to the expansion buffer
6174 * again. We don't bother to remove the macro-end
6175 * marker: we'd only have to generate another one
6176 * if we did.
6177 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006178 fm->in_progress--;
6179 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006180 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006181 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006182
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006183 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006184 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006185 tail = &ll->first;
6186
6187 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006188 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006189 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006190 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006191 }
6192 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006193 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006194 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006195 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006196 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006197 MMacro *m = istk->mstk.mstk;
6198
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006199 /*
6200 * Check whether a `%rep' was started and not ended
6201 * within this macro expansion. This can happen and
6202 * should be detected. It's a fatal error because
6203 * I'm too confused to work out how to recover
6204 * sensibly from it.
6205 */
6206 if (defining) {
6207 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006208 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006209 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006210 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006211 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006212 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006213
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006214 /*
6215 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006216 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006217 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006218 istk->mstk = m->mstk;
6219 if (m->name) {
6220 /*
6221 * This was a real macro call, not a %rep, and
6222 * therefore the parameter information needs to
6223 * be freed and the iteration count/nesting
6224 * depth adjusted.
6225 */
6226
6227 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006228 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006229 * If all mmacro processing done,
6230 * clear all counters and the deadman
6231 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006232 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006233 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006234 }
6235
Adam Majer91e72402017-07-25 10:42:01 +02006236#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006237 if (m->prev) {
6238 pop_mmacro(m);
6239 fm->in_progress --;
6240 } else
Adam Majer91e72402017-07-25 10:42:01 +02006241#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006242 {
6243 nasm_free(m->params);
6244 free_tlist(m->iline);
6245 nasm_free(m->paramlen);
6246 fm->in_progress = 0;
6247 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006248 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006249
6250 /*
6251 * FIXME It is incorrect to always free_mmacro here.
6252 * It leads to usage-after-free.
6253 *
6254 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6255 */
6256#if 0
6257 else
6258 free_mmacro(m);
6259#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006260 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006261 istk->expansion = l->next;
6262 nasm_free(l);
6263 lfmt->downlevel(LIST_MACRO);
6264 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006265 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006266
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006267 do { /* until we get a line we can use */
6268 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006269
6270 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006271 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006272 int32_t lineno;
6273
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006274 if (istk->mstk.mstk) {
6275 istk->mstk.mstk->lineno++;
6276 if (istk->mstk.mstk->fname)
6277 lineno = istk->mstk.mstk->lineno +
6278 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006279 else
6280 lineno = 0; /* Defined at init time or builtin */
6281 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006282 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006283 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006284
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006285 tline = l->first;
6286 istk->expansion = l->next;
6287 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006288
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006289 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006290 if (!istk->nolist)
6291 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006292 nasm_free(line);
6293 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006294 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006295 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006296 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006297 } else {
6298 /*
6299 * The current file has ended; work down the istk
6300 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006301 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006302 if (i->fp)
6303 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006304 if (i->conds) {
6305 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006306 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006307 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006308 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006309 if (i->next)
6310 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006311 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006312 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006313 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006314 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006315 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006316 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006317
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006318 /*
6319 * We must expand MMacro parameters and MMacro-local labels
6320 * _before_ we plunge into directive processing, to cope
6321 * with things like `%define something %1' such as STRUC
6322 * uses. Unless we're _defining_ a MMacro, in which case
6323 * those tokens should be left alone to go into the
6324 * definition; and unless we're in a non-emitting
6325 * condition, in which case we don't want to meddle with
6326 * anything.
6327 */
6328 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006329 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006330 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006331 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006332
H. Peter Anvine2c80182005-01-15 22:15:51 +00006333 /*
6334 * Check the line to see if it's a preprocessor directive.
6335 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006336 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6337 if (dtline)
6338 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006339 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006340 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006341 * We're defining a multi-line macro. We emit nothing
6342 * at all, and just
6343 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006344 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006345 MMacro *mmac = defining->dstk.mmac;
6346
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006347 Line *l = nasm_malloc(sizeof(Line));
6348 l->next = defining->expansion;
6349 l->first = tline;
6350 l->finishes = NULL;
6351 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006352
6353 /*
6354 * Remember if this mmacro expansion contains %00:
6355 * if it does, we will have to handle leading labels
6356 * specially.
6357 */
6358 if (mmac) {
6359 const Token *t;
6360 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006361 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006362 mmac->capture_label = true;
6363 }
6364 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006365 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006366 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006367 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006368 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006369 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006370 * directive so we keep our place correctly.
6371 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006372 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006373 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006374 /*
6375 * We're in a %rep block which has been terminated, so
6376 * we're walking through to the %endrep without
6377 * emitting anything. Emit nothing at all, not even a
6378 * blank line: when we emerge from the %rep block we'll
6379 * give a line-number directive so we keep our place
6380 * correctly.
6381 */
6382 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006383 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006384 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006385 if (!expand_mmacro(tline))
6386 return tline;
6387 }
6388 }
6389}
6390
6391static char *pp_getline(void)
6392{
6393 char *line = NULL;
6394 Token *tline;
6395
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006396 while (true) {
6397 tline = pp_tokline();
6398 if (tline == &tok_pop) {
6399 /*
6400 * We popped the macro/include stack. If istk is empty,
6401 * we are at end of input, otherwise just loop back.
6402 */
6403 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006404 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006405 } else {
6406 /*
6407 * De-tokenize the line and emit it.
6408 */
6409 line = detoken(tline, true);
6410 free_tlist(tline);
6411 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006412 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006413 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006414
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006415 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006416 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006417 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006418 nasm_free(buf);
6419 }
6420
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006421 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006422}
6423
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006424static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006425{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006426 if (defining) {
6427 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006428 nasm_nonfatal("end of file while still defining macro `%s'",
6429 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006430 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006431 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006432 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006433
6434 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006435 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006436 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006437
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006438 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006439 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006440 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006441 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006442 Include *i = istk;
6443 istk = istk->next;
6444 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006445 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006446 }
6447 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006448 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006449 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006450}
6451
6452static void pp_cleanup_session(void)
6453{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006454 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006455 free_llist(predef);
6456 predef = NULL;
6457 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006458 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006459}
6460
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006461static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006462{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006463 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006464}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006465
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006466static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006467{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006468 Token *inc, *space, *name;
6469 Line *l;
6470
H. Peter Anvin734b1882002-04-30 21:01:08 +00006471 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006472 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006473 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006474
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006475 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006476 l->next = predef;
6477 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006478 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006479 predef = l;
6480}
6481
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006482static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006483{
6484 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006485 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006486 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006487
6488 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006489 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006490 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006491 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006492 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006493 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006494 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006495 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006496
H. Peter Anvin8571f062019-09-23 16:40:03 -07006497 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006498 if (space->next->type != TOK_PREPROC_ID &&
6499 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006500 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006501
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006502 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006503 l->next = predef;
6504 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006505 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006506 predef = l;
6507}
6508
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006509static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006510{
6511 Token *def, *space;
6512 Line *l;
6513
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006514 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006515 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006516 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006517
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006518 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006519 l->next = predef;
6520 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006521 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006522 predef = l;
6523}
6524
H. Peter Anvin05990342018-06-11 13:32:42 -07006525/* Insert an early preprocessor command that doesn't need special handling */
6526static void pp_pre_command(const char *what, char *string)
6527{
6528 char *cmd;
6529 Token *def, *space;
6530 Line *l;
6531
6532 def = tokenize(string);
6533 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006534 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006535 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6536 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6537 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006538 }
6539
6540 l = nasm_malloc(sizeof(Line));
6541 l->next = predef;
6542 l->first = def;
6543 l->finishes = NULL;
6544 predef = l;
6545}
6546
H. Peter Anvinf7606612016-07-13 14:23:48 -07006547static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006548{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006549 macros_t **mp;
6550
6551 /* Find the end of the list and avoid duplicates */
6552 for (mp = stdmacros; *mp; mp++) {
6553 if (*mp == macros)
6554 return; /* Nothing to do */
6555 }
6556
6557 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6558
6559 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006560}
6561
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006562static void pp_extra_stdmac(macros_t *macros)
6563{
6564 extrastdmac = macros;
6565}
6566
H. Peter Anvin8571f062019-09-23 16:40:03 -07006567/* Create a numeric token */
6568static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006569{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006570 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006571 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006572 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006573}
6574
H. Peter Anvin8571f062019-09-23 16:40:03 -07006575/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006576static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006577{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006578 char *p = nasm_quote(str, &len);
6579 return new_Token_free(next, TOK_STRING, p, len);
6580}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006581static Token *make_tok_qstr(Token *next, const char *str)
6582{
6583 return make_tok_qstr_len(next, str, strlen(str));
6584}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006585
6586/* Create a single-character operator token */
6587static Token *make_tok_char(Token *next, char op)
6588{
6589 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6590 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006591 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006592}
6593
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006594static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006595{
6596 if (!m)
6597 return;
6598
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006599 /* We need to print the mstk.mmac list in reverse order */
6600 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006601
6602 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006603 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006604 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006605 }
6606}
6607
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006608static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006609{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006610 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006611
H. Peter Anvinddb29062018-12-11 00:06:29 -08006612 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6613 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006614
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006615 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006616 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006617
H. Peter Anvinddb29062018-12-11 00:06:29 -08006618 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006619}
6620
H. Peter Anvine7469712016-02-18 02:20:59 -08006621const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006622 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006623 pp_reset,
6624 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006625 pp_cleanup_pass,
6626 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006627 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006628 pp_pre_define,
6629 pp_pre_undefine,
6630 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006631 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006632 pp_include_path,
6633 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006634 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006635};