blob: 14a73885ac2304118047ced75b3a2571ee59c033 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
84typedef struct Line Line;
85typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087
88/*
H. Peter Anvin8571f062019-09-23 16:40:03 -070089 * This is the internal form which we break input lines up into.
90 * Typically stored in linked lists.
91 *
92 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
93 * not necessarily used as-is, but is also used to encode the number
94 * and expansion type of substituted parameter. So in the definition
95 *
96 * %define a(x,=y) ( (x) & ~(y) )
97 *
98 * the token representing `x' will have its type changed to
99 * tok_smac_param(0) but the one representing `y' will be
100 * tok_smac_param(1); see the accessor functions below.
101 *
102 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
103 * which doesn't need quotes around it. Used in the pre-include
104 * mechanism as an alternative to trying to find a sensible type of
105 * quote to use on the filename we were passed.
106 */
107enum pp_token_type {
108 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT,
109 TOK_CORRUPT, /* Token text modified in an unsafe manner, now bogus */
110 TOK_BLOCK, /* Storage block pointer, not a real token */
111 TOK_ID,
112 TOK_PREPROC_ID, TOK_MMACRO_PARAM, TOK_LOCAL_SYMBOL,
113 TOK_LOCAL_MACRO, TOK_ENVIRON, TOK_STRING,
114 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
115 TOK_INTERNAL_STRING,
116 TOK_PREPROC_Q, TOK_PREPROC_QQ,
117 TOK_PASTE, /* %+ */
118 TOK_COND_COMMA, /* %, */
119 TOK_INDIRECT, /* %[...] */
120 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
121 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
122};
123
124static inline enum pp_token_type tok_smac_param(int param)
125{
126 return TOK_SMAC_START_PARAMS + param;
127}
128static int smac_nparam(enum pp_token_type toktype)
129{
130 return toktype - TOK_SMAC_START_PARAMS;
131}
132static bool is_smac_param(enum pp_token_type toktype)
133{
134 return toktype >= TOK_SMAC_START_PARAMS;
135}
136
137#define PP_CONCAT_MASK(x) (1U << (x))
138
139struct tokseq_match {
140 int mask_head;
141 int mask_tail;
142};
143
144/*
145 * This is tuned so struct Token should be 64 bytes on 64-bit
146 * systems and 32 bytes on 32-bit systems. It enables them
147 * to be nicely cache aligned, and the text to still be kept
148 * inline for nearly all tokens.
149 *
150 * We prohibit tokens of length > MAX_TEXT even though
151 * length here is an unsigned int; this avoids problems
152 * if the length is passed through an interface with type "int",
153 * and is absurdly large anyway.
154 *
155 * For the text mode, in pointer mode the pointer is stored at the end
156 * of the union and the pad field is cleared. This allows short tokens
157 * to be unconditionally tested for by only looking at the first text
158 * bytes and not examining the type or len fields.
159 */
160#define INLINE_TEXT (7*sizeof(char *)-sizeof(enum pp_token_type)-sizeof(unsigned int)-1)
161#define MAX_TEXT (INT_MAX-2)
162
163struct Token {
164 Token *next;
165 enum pp_token_type type;
166 unsigned int len;
167 union {
168 char a[INLINE_TEXT+1];
169 struct {
170 char pad[INLINE_TEXT+1 - sizeof(char *)];
171 char *ptr;
172 } p;
173 } text;
174};
175
176/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700177 * Note on the storage of both SMacro and MMacros: the hash table
178 * indexes them case-insensitively, and we then have to go through a
179 * linked list of potential case aliases (and, for MMacros, parameter
180 * ranges); this is to preserve the matching semantics of the earlier
181 * code. If the number of case aliases for a specific macro is a
182 * performance issue, you may want to reconsider your coding style.
183 */
184
185/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700186 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700187 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700188typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700189
190/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000191 * Store the definition of a single-line macro.
192 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700193enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700194 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700195 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
196 SPARM_STR = 2, /* Convert to quoted string ($) */
197 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
198 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
199};
200
201struct smac_param {
H. Peter Anvin8571f062019-09-23 16:40:03 -0700202 Token name;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700203 enum sparmflags flags;
204};
205
H. Peter Anvine2c80182005-01-15 22:15:51 +0000206struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800207 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800208 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700209 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700210 ExpandSMacro expand;
211 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700212 struct smac_param *params;
213 int nparam;
214 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800215 bool casesense;
216 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700217 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000218};
219
220/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800221 * Store the definition of a multi-line macro. This is also used to
222 * store the interiors of `%rep...%endrep' blocks, which are
223 * effectively self-re-invoking multi-line macros which simply
224 * don't have a name or bother to appear in the hash tables. %rep
225 * blocks are signified by having a NULL `name' field.
226 *
227 * In a MMacro describing a `%rep' block, the `in_progress' field
228 * isn't merely boolean, but gives the number of repeats left to
229 * run.
230 *
231 * The `next' field is used for storing MMacros in hash tables; the
232 * `next_active' field is for stacking them on istk entries.
233 *
234 * When a MMacro is being expanded, `params', `iline', `nparam',
235 * `paramlen', `rotate' and `unique' are local to the invocation.
236 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700237
238/*
239 * Expansion stack. Note that .mmac can point back to the macro itself,
240 * whereas .mstk cannot.
241 */
242struct mstk {
243 MMacro *mstk; /* Any expansion, real macro or not */
244 MMacro *mmac; /* Highest level actual mmacro */
245};
246
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800247struct MMacro {
248 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700249#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800250 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700251#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800252 char *name;
253 int nparam_min, nparam_max;
254 bool casesense;
255 bool plus; /* is the last parameter greedy? */
256 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700257 bool capture_label; /* macro definition has %00; capture label */
258 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800259 int32_t max_depth; /* maximum number of recursive expansions allowed */
260 Token *dlist; /* All defaults as one list */
261 Token **defaults; /* Parameter default pointers */
262 int ndefs; /* number of default parameters */
263 Line *expansion;
264
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700265 struct mstk mstk; /* Macro expansion stack */
266 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800267 Token **params; /* actual parameters */
268 Token *iline; /* invocation line */
269 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700270 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800271 int *paramlen;
272 uint64_t unique;
273 int lineno; /* Current line number on expansion */
274 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700275
H. Peter Anvin274cda82016-05-10 02:56:29 -0700276 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700277 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800278};
279
280
281/* Store the definition of a multi-line macro, as defined in a
282 * previous recursive macro expansion.
283 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700284#if 0
285
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800286struct MMacroInvocation {
287 MMacroInvocation *prev; /* previous invocation */
288 Token **params; /* actual parameters */
289 Token *iline; /* invocation line */
290 unsigned int nparam, rotate;
291 int *paramlen;
292 uint64_t unique;
293 uint64_t condcnt;
294};
295
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700296#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800297
298/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000299 * The context stack is composed of a linked list of these.
300 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000301struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800302 Context *next;
H. Peter Anvin8571f062019-09-23 16:40:03 -0700303 const char *name;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800304 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700305 uint64_t number;
306 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000307};
308
H. Peter Anvin8571f062019-09-23 16:40:03 -0700309
310static inline const char *tok_text(const struct Token *t)
311{
312 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
313}
314
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000315/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700316 * Returns a mutable pointer to the text buffer. The text can be changed,
317 * but the length MUST NOT CHANGE, in either direction; nor is it permitted
318 * to pad with null characters to create an artificially shorter string.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000319 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700320static inline char *tok_text_buf(struct Token *t)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800321{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700322 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800323}
324
H. Peter Anvin8571f062019-09-23 16:40:03 -0700325static inline unsigned int tok_check_len(size_t len)
326{
327 if (unlikely(len > MAX_TEXT))
328 nasm_fatal("impossibly large token");
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400329
H. Peter Anvin8571f062019-09-23 16:40:03 -0700330 return len;
331}
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400332
H. Peter Anvin8571f062019-09-23 16:40:03 -0700333/* strlen() variant useful for set_text() and its variants */
334static size_t tok_strlen(const char *str)
335{
336 return strnlen(str, MAX_TEXT+1);
337}
338
339/*
340 * Set the text field to a copy of the given string; the length if
341 * not given should be obtained with tok_strlen().
342 */
343static Token *set_text(struct Token *t, const char *text, size_t len)
344{
345 char *textp;
346
347 if (t->len > INLINE_TEXT)
348 nasm_free(t->text.p.ptr);
349
350 nasm_zero(t->text.a);
351
352 t->len = tok_check_len(len);
353 textp = (len > INLINE_TEXT)
354 ? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
355 memcpy(textp, text, len+1);
356 return t;
357}
358
359/*
360 * Set the text field to the existing pre-allocated string, either
361 * taking over or freeing the allocation in the process.
362 */
363static Token *set_text_free(struct Token *t, char *text, unsigned int len)
364{
365 if (t->len > INLINE_TEXT)
366 nasm_free(t->text.p.ptr);
367
368 nasm_zero(t->text.a);
369
370 t->len = tok_check_len(len);
371 if (len > INLINE_TEXT) {
372 t->text.p.ptr = text;
373 } else {
374 memcpy(t->text.a, text, len+1);
375 nasm_free(text);
376 }
377
378 return t;
379}
380
381/*
382 * Allocate a new buffer containing a copy of the text field
383 * of the token.
384 */
385static char *dup_text(const struct Token *t)
386{
387 size_t size = t->len + 1;
388 char *p = nasm_malloc(size);
389
390 return memcpy(p, tok_text(t), size);
391}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392
393/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800394 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000395 * these, which is essentially a container to allow several linked
396 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700397 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398 * Note that in this module, linked lists are treated as stacks
399 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800400 * `expansion' field in MMacro structures, so that the linked list,
401 * if walked, would give the macro lines in reverse order; this
402 * means that we can walk the list when expanding a macro, and thus
403 * push the lines on to the `expansion' field in _istk_ in reverse
404 * order (so that when popped back off they are in the right
405 * order). It may seem cockeyed, and it relies on my design having
406 * an even number of steps in, but it works...
407 *
408 * Some of these structures, rather than being actual lines, are
409 * markers delimiting the end of the expansion of a given macro.
410 * This is for use in the cycle-tracking and %rep-handling code.
411 * Such structures have `finishes' non-NULL, and `first' NULL. All
412 * others have `finishes' NULL, but `first' may still be NULL if
413 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000415struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800416 Line *next;
417 MMacro *finishes;
418 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500419};
420
421/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000422 * To handle an arbitrary level of file inclusion, we maintain a
423 * stack (ie linked list) of these things.
424 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000425struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426 Include *next;
427 FILE *fp;
428 Cond *conds;
429 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700430 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700431 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700432 int lineno, lineinc;
433 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434};
435
436/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700437 * File real name hash, so we don't have to re-search the include
438 * path for every pass (and potentially more than that if a file
439 * is used more than once.)
440 */
441struct hash_table FileHash;
442
443/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700444 * Counters to trap on insane macro recursion or processing.
445 * Note: for smacros these count *down*, for mmacros they count *up*.
446 */
447struct deadman {
448 int64_t total; /* Total number of macros/tokens */
449 int64_t levels; /* Descent depth across all macros */
450 bool triggered; /* Already triggered, no need for error msg */
451};
452
453static struct deadman smacro_deadman, mmacro_deadman;
454
455/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000456 * Conditional assembly: we maintain a separate stack of these for
457 * each level of file inclusion. (The only reason we keep the
458 * stacks separate is to ensure that a stray `%endif' in a file
459 * included from within the true branch of a `%if' won't terminate
460 * it and cause confusion: instead, rightly, it'll cause an error.)
461 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700462enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000463 /*
464 * These states are for use just after %if or %elif: IF_TRUE
465 * means the condition has evaluated to truth so we are
466 * currently emitting, whereas IF_FALSE means we are not
467 * currently emitting but will start doing so if a %else comes
468 * up. In these states, all directives are admissible: %elif,
469 * %else and %endif. (And of course %if.)
470 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800471 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000472 /*
473 * These states come up after a %else: ELSE_TRUE means we're
474 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
475 * any %elif or %else will cause an error.
476 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800477 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000478 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200479 * These states mean that we're not emitting now, and also that
480 * nothing until %endif will be emitted at all. COND_DONE is
481 * used when we've had our moment of emission
482 * and have now started seeing %elifs. COND_NEVER is used when
483 * the condition construct in question is contained within a
484 * non-emitting branch of a larger condition construct,
485 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000486 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800487 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000488};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700489struct Cond {
490 Cond *next;
491 enum cond_state state;
492};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800493#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000494
H. Peter Anvin70653092007-10-19 14:42:29 -0700495/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000496 * These defines are used as the possible return values for do_directive
497 */
498#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300499#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000500
Keith Kanios852f1ee2009-07-12 00:19:55 -0500501/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000502 * Condition codes. Note that we use c_ prefix not C_ because C_ is
503 * used in nasm.h for the "real" condition codes. At _this_ level,
504 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
505 * ones, so we need a different enum...
506 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700507static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000508 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
509 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000510 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000511};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700512enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000513 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
514 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700515 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
516 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000517};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700518static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000519 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
520 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000521 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000522};
523
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800524/*
525 * Directive names.
526 */
527/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
528static int is_condition(enum preproc_token arg)
529{
530 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
531}
532
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000533/* For TASM compatibility we need to be able to recognise TASM compatible
534 * conditional compilation directives. Using the NASM pre-processor does
535 * not work, so we look for them specifically from the following list and
536 * then jam in the equivalent NASM directive into the input stream.
537 */
538
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000540 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
541 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
542};
543
H. Peter Anvin476d2862007-10-02 22:04:15 -0700544static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000545 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
546 "ifndef", "include", "local"
547};
548
549static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700550static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000551static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800552static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000553
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000554static Context *cstk;
555static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300556static const struct strlist *ipath_list;
H. Peter Anvind2354082019-08-27 16:38:48 -0700557static bool do_aliases;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000558
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300559static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300561static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000562
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800563static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700564static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800565static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000566
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800568 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800570static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000571
572/*
573 * The current set of single-line macros we have defined.
574 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700575static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000576
577/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800578 * The multi-line macro we are currently defining, or the %rep
579 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800581static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582
Charles Crayned4200be2008-07-12 16:42:33 -0700583static uint64_t nested_mac_count;
584static uint64_t nested_rep_count;
585
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586/*
587 * The number of macro parameters to allocate space for at a time.
588 */
589#define PARAM_DELTA 16
590
591/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700592 * The standard macro set: defined in macros.c in a set of arrays.
593 * This gives our position in any macro set, while we are processing it.
594 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700596static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700597static macros_t **stdmacnext;
598static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300599static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000600
601/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700602 * Map of which %use packages have been loaded
603 */
604static bool *use_loaded;
605
606/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000607 * Forward declarations.
608 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700609static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000610static Token *expand_mmac_params(Token * tline);
611static Token *expand_smacro(Token * tline);
612static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400613static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700614static Token *make_tok_num(Token *next, int64_t val);
615static Token *make_tok_qstr(Token *next, const char *str);
616static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700617static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700618 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700619static Token *new_Token_free(Token * next, enum pp_token_type type,
620 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700621static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700622static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700623static Token *delete_Token(Token *t);
624static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700625static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700626get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700628/* Safe test for token type, false on x == NULL */
629static inline bool tok_type(const Token *x, enum pp_token_type t)
630{
631 return x && x->type == t;
632}
633
634/* Whitespace token? */
635static inline bool tok_white(const Token *x)
636{
637 return tok_type(x, TOK_WHITESPACE);
638}
639
640/* Skip past any whitespace */
641static inline Token *skip_white(Token *x)
642{
643 while (tok_white(x))
644 x = x->next;
645
646 return x;
647}
648
649/* Delete any whitespace */
650static Token *zap_white(Token *x)
651{
652 while (tok_white(x))
653 x = delete_Token(x);
654
655 return x;
656}
657
H. Peter Anvin8571f062019-09-23 16:40:03 -0700658/*
659 * Single special character tests. The use of & rather than && is intentional; it
660 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
661 * a smart compiler should turn it into a 16-bit memory reference.
662 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700663static inline bool tok_is(const Token *x, char c)
664{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700665 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700666}
667
668/* True if any other kind of token that "c", but not NULL */
669static inline bool tok_isnt(const Token *x, char c)
670{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700671 return x && !((x->text.a[0] == c) & !x->text.a[1]);
672}
673
674/*
675 * Unquote a token if it is a string, and set its type to
676 * TOK_INTERNAL_STRING.
677 */
678static const char *unquote_token(Token *t)
679{
680 if (t->type != TOK_STRING)
681 return tok_text(t);
682
683 t->type = TOK_INTERNAL_STRING;
684
685 if (t->len > INLINE_TEXT) {
686 char *p = t->text.p.ptr;
687
688 t->len = nasm_unquote(p, NULL);
689
690 if (t->len <= INLINE_TEXT) {
691 nasm_zero(t->text.a);
692 memcpy(t->text.a, p, t->len);
693 nasm_free(p);
694 return t->text.a;
695 } else {
696 return p;
697 }
698 } else {
699 t->len = nasm_unquote(t->text.a, NULL);
700 return t->text.a;
701 }
702}
703
704static const char *unquote_token_cstr(Token *t)
705{
706 if (t->type != TOK_STRING)
707 return tok_text(t);
708
709 t->type = TOK_INTERNAL_STRING;
710
711 if (t->len > INLINE_TEXT) {
712 char *p = t->text.p.ptr;
713
714 t->len = nasm_unquote_cstr(p, NULL);
715
716 if (t->len <= INLINE_TEXT) {
717 nasm_zero(t->text.a);
718 memcpy(t->text.a, p, t->len);
719 nasm_free(p);
720 return t->text.a;
721 } else {
722 return p;
723 }
724 } else {
725 t->len = nasm_unquote_cstr(t->text.a, NULL);
726 return t->text.a;
727 }
728}
729
730static Token *quote_internal_string_token(Token *t)
731{
732 size_t len;
733 char *p;
734
735 p = nasm_quote(tok_text(t), &len);
736 t->type = TOK_STRING;
737 return set_text_free(t, p, len);
738}
739
740static inline Token *quote_token(Token *t)
741{
742 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
743 return t;
744
745 return quote_internal_string_token(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700746}
H. Peter Anvin76690a12002-04-30 20:52:49 +0000747
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400748/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700749 * In-place reverse a list of tokens.
750 */
751static Token *reverse_tokens(Token *t)
752{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800753 Token *prev = NULL;
754 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700755
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800756 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400757 next = t->next;
758 t->next = prev;
759 prev = t;
760 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800761 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700762
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800763 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700764}
765
766/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700767 * getenv() variant operating on an input token
768 */
769static const char *pp_getenv(const Token *t, bool warn)
770{
771 const char *txt = tok_text(t);
772 const char *v;
773 char *buf = NULL;
774 bool is_string = false;
775
776 if (!t)
777 return NULL;
778
779 switch (t->type) {
780 case TOK_ENVIRON:
781 txt += 2; /* Skip leading %! */
782 is_string = nasm_isquote(*txt);
783 break;
784
785 case TOK_STRING:
786 is_string = true;
787 break;
788
789 case TOK_INTERNAL_STRING:
790 case TOK_ID:
791 is_string = false;
792 break;
793
794 default:
795 return NULL;
796 }
797
798 if (is_string) {
799 buf = nasm_strdup(txt);
800 nasm_unquote_cstr(buf, NULL);
801 txt = buf;
802 }
803
804 v = getenv(txt);
805 if (warn && !v) {
806 /*!
807 *!environment [on] nonexistent environment variable
808 *! warns if a nonexistent environment variable
809 *! is accessed using the \c{%!} preprocessor
810 *! construct (see \k{getenv}.) Such environment
811 *! variables are treated as empty (with this
812 *! warning issued) starting in NASM 2.15;
813 *! earlier versions of NASM would treat this as
814 *! an error.
815 */
816 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
817 v = "";
818 }
819
820 if (buf)
821 nasm_free(buf);
822
823 return v;
824}
825
826/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300827 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000828 * front of them. We do it here because I could not find any other
829 * place to do it for the moment, and it is a hack (ideally it would
830 * be nice to be able to use the NASM pre-processor to do it).
831 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000832static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000833{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000834 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400835 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000836
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400837 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000838
839 /* Binary search for the directive name */
840 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400841 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400842 q = nasm_skip_word(p);
843 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000844 if (len) {
845 oldchar = p[len];
846 p[len] = 0;
847 while (j - i > 1) {
848 k = (j + i) / 2;
849 m = nasm_stricmp(p, tasm_directives[k]);
850 if (m == 0) {
851 /* We have found a directive, so jam a % in front of it
852 * so that NASM will then recognise it as one if it's own.
853 */
854 p[len] = oldchar;
855 len = strlen(p);
856 oldline = line;
857 line = nasm_malloc(len + 2);
858 line[0] = '%';
859 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700860 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300861 * NASM does not recognise IFDIFI, so we convert
862 * it to %if 0. This is not used in NASM
863 * compatible code, but does need to parse for the
864 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000865 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700866 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000867 } else {
868 memcpy(line + 1, p, len + 1);
869 }
870 nasm_free(oldline);
871 return line;
872 } else if (m < 0) {
873 j = k;
874 } else
875 i = k;
876 }
877 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000878 }
879 return line;
880}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000881
H. Peter Anvin76690a12002-04-30 20:52:49 +0000882/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000883 * The pre-preprocessing stage... This function translates line
884 * number indications as they emerge from GNU cpp (`# lineno "file"
885 * flags') into NASM preprocessor line number indications (`%line
886 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000887 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000888static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000889{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000890 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000891 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000892
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 if (line[0] == '#' && line[1] == ' ') {
894 oldline = line;
895 fname = oldline + 2;
896 lineno = atoi(fname);
897 fname += strspn(fname, "0123456789 ");
898 if (*fname == '"')
899 fname++;
900 fnlen = strcspn(fname, "\"");
901 line = nasm_malloc(20 + fnlen);
902 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
903 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000904 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000905 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000906 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000907 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000908}
909
910/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000911 * Free a linked list of tokens.
912 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000914{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400915 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000917}
918
919/*
920 * Free a linked list of lines.
921 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000923{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400924 Line *l, *tmp;
925 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000926 free_tlist(l->first);
927 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000928 }
929}
930
931/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700932 * Free an array of linked lists of tokens
933 */
934static void free_tlist_array(Token **array, size_t nlists)
935{
936 Token **listp = array;
937
938 while (nlists--)
939 free_tlist(*listp++);
940
941 nasm_free(array);
942}
943
944/*
945 * Duplicate a linked list of tokens.
946 */
947static Token *dup_tlist(const Token *list, Token ***tailp)
948{
949 Token *newlist = NULL;
950 Token **tailpp = &newlist;
951 const Token *t;
952
953 list_for_each(t, list) {
954 Token *nt;
955 *tailpp = nt = dup_Token(NULL, t);
956 tailpp = &nt->next;
957 }
958
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700959 if (tailp) {
960 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700961 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700962 }
963
964 return newlist;
965}
966
967/*
968 * Duplicate a linked list of tokens with a maximum count
969 */
970static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
971{
972 Token *newlist = NULL;
973 Token **tailpp = &newlist;
974 const Token *t;
975
976 list_for_each(t, list) {
977 Token *nt;
978 if (!cnt--)
979 break;
980 *tailpp = nt = dup_Token(NULL, t);
981 tailpp = &nt->next;
982 }
983
984 if (tailp) {
985 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -0700986 if (newlist)
987 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700988 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700989
990 return newlist;
991}
992
993/*
994 * Duplicate a linked list of tokens in reverse order
995 */
996static Token *dup_tlist_reverse(const Token *list, Token *tail)
997{
998 const Token *t;
999
1000 list_for_each(t, list)
1001 tail = dup_Token(tail, t);
1002
1003 return tail;
1004}
1005
1006/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001007 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001008 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001009static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001010{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001011 nasm_free(m->name);
1012 free_tlist(m->dlist);
1013 nasm_free(m->defaults);
1014 free_llist(m->expansion);
1015 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001016}
1017
1018/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001019 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001020 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001021static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001022{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001023 if (s->params) {
1024 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001025 for (i = 0; i < s->nparam; i++) {
1026 if (s->params[i].name.len > INLINE_TEXT)
1027 nasm_free(s->params[i].name.text.p.ptr);
1028 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001029 nasm_free(s->params);
1030 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001031 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001032 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001033}
1034
1035static void clear_smacro(SMacro *s)
1036{
1037 free_smacro_members(s);
1038 /* Wipe everything except the next pointer */
1039 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1040}
1041
1042/*
1043 * Free an SMacro
1044 */
1045static void free_smacro(SMacro *s)
1046{
1047 free_smacro_members(s);
1048 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001049}
1050
1051/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001052 * Free all currently defined macros, and free the hash tables
1053 */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001054static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001055{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001056 struct hash_iterator it;
1057 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001058
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001059 hash_for_each(smt, it, np) {
1060 SMacro *tmp;
1061 SMacro *s = np->data;
1062 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001063 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001064 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001065 }
H. Peter Anvin072771e2008-05-22 13:17:51 -07001066 hash_free(smt);
1067}
1068
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001069static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001070{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001071 struct hash_iterator it;
1072 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001073
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001074 hash_for_each(mmt, it, np) {
1075 MMacro *tmp;
1076 MMacro *m = np->data;
1077 nasm_free((void *)np->key);
1078 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001079 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001080 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001081 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001082}
1083
1084static void free_macros(void)
1085{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001086 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001087 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001088}
1089
1090/*
1091 * Initialize the hash tables
1092 */
1093static void init_macros(void)
1094{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001095}
1096
1097/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001098 * Pop the context stack.
1099 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001101{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001102 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001103
1104 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001105 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001106 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001107 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001108}
1109
H. Peter Anvin072771e2008-05-22 13:17:51 -07001110/*
1111 * Search for a key in the hash index; adding it if necessary
1112 * (in which case we initialize the data pointer to NULL.)
1113 */
1114static void **
1115hash_findi_add(struct hash_table *hash, const char *str)
1116{
1117 struct hash_insert hi;
1118 void **r;
1119 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001120 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001121
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001122 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001123 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001124 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001125
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001126 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1127 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001128 return hash_add(&hi, strx, NULL);
1129}
1130
1131/*
1132 * Like hash_findi, but returns the data element rather than a pointer
1133 * to it. Used only when not adding a new element, hence no third
1134 * argument.
1135 */
1136static void *
1137hash_findix(struct hash_table *hash, const char *str)
1138{
1139 void **p;
1140
1141 p = hash_findi(hash, str, NULL);
1142 return p ? *p : NULL;
1143}
1144
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001145/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001146 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001147 * if there no more left -- return NULL
1148 */
1149static char *line_from_stdmac(void)
1150{
1151 unsigned char c;
1152 const unsigned char *p = stdmacpos;
1153 char *line, *q;
1154 size_t len = 0;
1155
1156 if (!stdmacpos)
1157 return NULL;
1158
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001159 /*
1160 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1161 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1162 */
1163 while ((c = *p++) != 127) {
1164 uint8_t ndir = c - 128;
1165 if (ndir < 256-96)
1166 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001167 else
1168 len++;
1169 }
1170
1171 line = nasm_malloc(len + 1);
1172 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001173
1174 while ((c = *stdmacpos++) != 127) {
1175 uint8_t ndir = c - 128;
1176 if (ndir < 256-96) {
1177 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1178 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001179 *q++ = ' ';
1180 } else {
1181 *q++ = c;
1182 }
1183 }
1184 stdmacpos = p;
1185 *q = '\0';
1186
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001187 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001188 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001189 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001190 if (*stdmacnext) {
1191 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001192 } else if (do_predef) {
1193 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001194
1195 /*
1196 * Nasty hack: here we push the contents of
1197 * `predef' on to the top-level expansion stack,
1198 * since this is the most convenient way to
1199 * implement the pre-include and pre-define
1200 * features.
1201 */
1202 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001203 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001204 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001205 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001206 l->finishes = NULL;
1207
1208 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001209 }
1210 do_predef = false;
1211 }
1212 }
1213
1214 return line;
1215}
1216
H. Peter Anvin6686de22019-08-10 05:33:14 -07001217/*
1218 * Read a line from a file. Return NULL on end of file.
1219 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001220static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001221{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001222 int c;
1223 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001224 const unsigned int delta = 512;
1225 const unsigned int pad = 8;
1226 unsigned int nr_cont = 0;
1227 bool cont = false;
1228 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001229 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001230
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001231 size = delta;
1232 p = buffer = nasm_malloc(size);
1233
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001234 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001235 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001236
1237 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001238 case EOF:
1239 if (p == buffer) {
1240 nasm_free(buffer);
1241 return NULL;
1242 }
1243 c = 0;
1244 break;
1245
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001246 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001247 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001248 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001249 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001250 if (cont) {
1251 cont = false;
1252 continue;
1253 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001254 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001255 break;
1256
1257 case '\n':
1258 if (cont) {
1259 cont = false;
1260 continue;
1261 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001262 c = 0;
1263 break;
1264
1265 case 032: /* ^Z = legacy MS-DOS end of file mark */
1266 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001267 break;
1268
1269 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001270 next = fgetc(f);
1271 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001272 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001273 cont = true;
1274 nr_cont++;
1275 continue;
1276 }
1277 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001279
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001280 if (p >= (buffer + size - pad)) {
1281 buffer = nasm_realloc(buffer, size + delta);
1282 p = buffer + size - pad;
1283 size += delta;
1284 }
1285
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001286 *p++ = c;
1287 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001288
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001289 lineno = src_get_linnum() + istk->lineinc +
1290 (nr_cont * istk->lineinc);
1291 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001292
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001293 return buffer;
1294}
1295
1296/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001297 * Common read routine regardless of source
1298 */
1299static char *read_line(void)
1300{
1301 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001302 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001303
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001304 if (f)
1305 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001306 else
1307 line = line_from_stdmac();
1308
1309 if (!line)
1310 return NULL;
1311
1312 if (!istk->nolist)
1313 lfmt->line(LIST_READ, src_get_linnum(), line);
1314
1315 return line;
1316}
1317
1318/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001319 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001320 * don't need to parse the value out of e.g. numeric tokens: we
1321 * simply split one string into many.
1322 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001323static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001324{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001325 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001326 Token *list = NULL;
1327 Token *t, **tail = &list;
1328
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001330 const char *p = line;
1331 const char *ep = NULL; /* End of token, for trimming the end */
1332 size_t toklen;
1333 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001334
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001336 /*
1337 * Preprocessor construct; find the end of the token.
1338 * Classification is handled later, because %{...} can be
1339 * used to create any preprocessor token.
1340 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001342 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001343 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001344 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001345 } else if (nasm_isdigit(*p) ||
1346 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 do {
1348 p++;
1349 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001350 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001351 } else if (*p == '{' || *p == '[') {
1352 /* %{...} or %[...] */
1353 char firstchar = *p;
1354 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001356 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1357 while (lvl) {
1358 if (*p == firstchar) {
1359 lvl++;
1360 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001361 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001362 } else if (nasm_isquote(*p)) {
1363 p = nasm_skip_string(p);
1364 }
1365
1366 /*
1367 * *p can have been advanced to a null character by
1368 * nasm_skip_string()
1369 */
1370 if (!*p) {
1371 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1372 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 break;
1374 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001375 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001376 }
1377 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001378 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001379 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001381 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001382 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001383 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001384 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001385 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001386 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001387 do {
1388 p++;
1389 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001390 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001391 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001392 p = nasm_skip_string(p);
1393 if (*p)
1394 p++;
1395 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001396 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001397 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001398 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001399 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001400 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001401 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001402 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001403 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001404 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1405 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001406 do {
1407 p++;
1408 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001409 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001410 } else if (*p == '%') {
1411 /* %% operator */
1412 p++;
1413 }
1414
1415 if (!ep)
1416 ep = p;
1417 toklen = ep - line;
1418
1419 /* Classify here, to handle %{...} correctly */
1420 if (toklen < 2) {
1421 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001423 char c0 = line[1];
1424
1425 switch (c0) {
1426 case '+':
1427 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1428 break;
1429
1430 case '-':
1431 type = TOK_MMACRO_PARAM;
1432 break;
1433
1434 case '?':
1435 if (toklen == 2)
1436 type = TOK_PREPROC_Q;
1437 else if (toklen == 3 && line[2] == '?')
1438 type = TOK_PREPROC_QQ;
1439 else
1440 type = TOK_PREPROC_ID;
1441 break;
1442
1443 case '!':
1444 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1445 break;
1446
1447 case '%':
1448 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1449 break;
1450
1451 case '$':
1452 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1453 break;
1454
1455 case '[':
1456 line += 2; /* Skip %[ */
1457 firstchar = *line; /* Don't clobber */
1458 toklen -= 2;
1459 type = TOK_INDIRECT;
1460 break;
1461
1462 case ',':
1463 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1464 break;
1465
1466 case '\'':
1467 case '\"':
1468 case '`':
1469 /* %{'string'} */
1470 type = TOK_PREPROC_ID;
1471 break;
1472
1473 case ':':
1474 type = TOK_MMACRO_PARAM; /* %{:..} */
1475 break;
1476
1477 default:
1478 if (nasm_isdigit(c0))
1479 type = TOK_MMACRO_PARAM;
1480 else if (nasm_isidchar(c0) || toklen > 2)
1481 type = TOK_PREPROC_ID;
1482 else
1483 type = TOK_OTHER;
1484 break;
1485 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001487 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001488 /*
1489 * An identifier. This includes the ? operator, which is
1490 * treated as a keyword, not as a special character
1491 * operator
1492 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001493 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001494 while (nasm_isidchar(*++p))
1495 ;
1496 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497 /*
1498 * A string token.
1499 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001501 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001502
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 if (*p) {
1504 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001505 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001506 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 /* Handling unterminated strings by UNV */
1508 /* type = -1; */
1509 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001510 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001511 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001512 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001513 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001514 bool is_hex = false;
1515 bool is_float = false;
1516 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001517 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001518
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001520 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001522
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001523 if (*p == '$') {
1524 p++;
1525 is_hex = true;
1526 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001527
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 for (;;) {
1529 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001530
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001531 if (!is_hex && (c == 'e' || c == 'E')) {
1532 has_e = true;
1533 if (*p == '+' || *p == '-') {
1534 /*
1535 * e can only be followed by +/- if it is either a
1536 * prefixed hex number or a floating-point number
1537 */
1538 p++;
1539 is_float = true;
1540 }
1541 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1542 is_hex = true;
1543 } else if (c == 'P' || c == 'p') {
1544 is_float = true;
1545 if (*p == '+' || *p == '-')
1546 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001547 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 ; /* just advance */
1549 else if (c == '.') {
1550 /*
1551 * we need to deal with consequences of the legacy
1552 * parser, like "1.nolist" being two tokens
1553 * (TOK_NUMBER, TOK_ID) here; at least give it
1554 * a shot for now. In the future, we probably need
1555 * a flex-based scanner with proper pattern matching
1556 * to do it as well as it can be done. Nothing in
1557 * the world is going to help the person who wants
1558 * 0x123.p16 interpreted as two tokens, though.
1559 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001560 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001561 while (*r == '_')
1562 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001563
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001564 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1565 (!is_hex && (*r == 'e' || *r == 'E')) ||
1566 (*r == 'p' || *r == 'P')) {
1567 p = r;
1568 is_float = true;
1569 } else
1570 break; /* Terminate the token */
1571 } else
1572 break;
1573 }
1574 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001575
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001576 if (p == line+1 && *line == '$') {
1577 type = TOK_OTHER; /* TOKEN_HERE */
1578 } else {
1579 if (has_e && !is_hex) {
1580 /* 1e13 is floating-point, but 1e13h is not */
1581 is_float = true;
1582 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001583
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001584 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1585 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001586 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001588 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 /*
1590 * Whitespace just before end-of-line is discarded by
1591 * pretending it's a comment; whitespace just before a
1592 * comment gets lumped into the comment.
1593 */
1594 if (!*p || *p == ';') {
1595 type = TOK_COMMENT;
1596 while (*p)
1597 p++;
1598 }
1599 } else if (*p == ';') {
1600 type = TOK_COMMENT;
1601 while (*p)
1602 p++;
1603 } else {
1604 /*
1605 * Anything else is an operator of some kind. We check
1606 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001607 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1608 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001609 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 */
1611 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001612 switch (*p++) {
1613 case '>':
1614 if (*p == '>') {
1615 p++;
1616 if (*p == '>')
1617 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001618 } else if (*p == '=') {
1619 p++;
1620 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001621 break;
1622
1623 case '<':
1624 if (*p == '<') {
1625 p++;
1626 if (*p == '<')
1627 p++;
1628 } else if (*p == '=') {
1629 p++;
1630 if (*p == '>')
1631 p++;
1632 } else if (*p == '>') {
1633 p++;
1634 }
1635 break;
1636
1637 case '!':
1638 if (*p == '=')
1639 p++;
1640 break;
1641
1642 case '/':
1643 case '=':
1644 case '&':
1645 case '|':
1646 case '^':
1647 /* These operators can be doubled but nothing else */
1648 if (*p == p[-1])
1649 p++;
1650 break;
1651
1652 default:
1653 break;
1654 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001656
H. Peter Anvin8571f062019-09-23 16:40:03 -07001657 if (type == TOK_WHITESPACE) {
1658 *tail = t = new_White(NULL);
1659 tail = &t->next;
1660 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001661 if (!ep)
1662 ep = p;
1663 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001664 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 tail = &t->next;
1666 }
1667 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001668 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001669 return list;
1670}
1671
H. Peter Anvin8571f062019-09-23 16:40:03 -07001672/*
1673 * Tokens are allocated in blocks to improve speed. Set the blocksize
1674 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1675 *
1676 * alloc_Token() returns a zero-initialized token structure.
1677 */
1678#define TOKEN_BLOCKSIZE 4096
1679
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001680#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001681
1682static Token *freeTokens = NULL;
1683static Token *tokenblocks = NULL;
1684
1685static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001686{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001687 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688
H. Peter Anvin8571f062019-09-23 16:40:03 -07001689 if (unlikely(!t)) {
1690 Token *block;
1691 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692
H. Peter Anvin8571f062019-09-23 16:40:03 -07001693 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001694
H. Peter Anvin8571f062019-09-23 16:40:03 -07001695 /*
1696 * The first entry in each array are a linked list of
1697 * block allocations and is not used for data.
1698 */
1699 block[0].next = tokenblocks;
1700 block[0].type = TOK_BLOCK;
1701 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001702
H. Peter Anvin8571f062019-09-23 16:40:03 -07001703 /*
1704 * Add the rest to the free list
1705 */
1706 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1707 block[i].next = &block[i+1];
1708
1709 freeTokens = &block[2];
1710
1711 /*
1712 * Return the topmost usable token
1713 */
1714 return &block[1];
1715 }
1716
1717 freeTokens = t->next;
1718 t->next = NULL;
1719 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001720}
1721
H. Peter Anvin8571f062019-09-23 16:40:03 -07001722static Token *delete_Token(Token *t)
1723{
1724 Token *next = t->next;
1725
1726 nasm_zero(*t);
1727 t->next = freeTokens;
1728 freeTokens = t;
1729
1730 return next;
1731}
1732
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001734{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001735 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001736
H. Peter Anvin8571f062019-09-23 16:40:03 -07001737 list_for_each_safe(block, blocktmp, tokenblocks)
1738 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001739
H. Peter Anvin8571f062019-09-23 16:40:03 -07001740 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001742
H. Peter Anvin8571f062019-09-23 16:40:03 -07001743#else
1744
1745static inline Token *alloc_Token(void)
1746{
1747 Token *t;
1748 nasm_new(*t);
1749 return t;
1750}
1751
1752static Token *delete_Token(Token *t)
1753{
1754 Token *next = t->next;
1755 nasm_free(t);
1756 return next;
1757}
1758
1759static inline void delete_Blocks(void)
1760{
1761 /* Nothing to do */
1762}
1763
1764#endif
1765
H. Peter Anvin734b1882002-04-30 21:01:08 +00001766/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001767 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001768 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001769 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001770static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001771 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001772{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001773 Token *t = alloc_Token();
1774 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001775
H. Peter Anvin734b1882002-04-30 21:01:08 +00001776 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001777 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001778 if (type == TOK_WHITESPACE) {
1779 t->len = 1;
1780 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001782 if (text && text[0] && !txtlen)
1783 txtlen = tok_strlen(text);
1784
1785 t->len = tok_check_len(txtlen);
1786
1787 if (text) {
1788 textp = (txtlen > INLINE_TEXT)
1789 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1790 memcpy(textp, text, txtlen);
1791 textp[txtlen] = '\0'; /* In case we needed malloc() */
1792 } else {
1793 /*
1794 * Allocate a buffer but do not fill it. The caller
1795 * can fill in text, but must not change the length.
1796 * The filled in text must be exactly txtlen once
1797 * the buffer is filled and before the token is added
1798 * to any line lists.
1799 */
1800 if (txtlen > INLINE_TEXT)
1801 t->text.p.ptr = nasm_zalloc(txtlen+1);
1802 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001803 }
1804 return t;
1805}
1806
H. Peter Anvin8571f062019-09-23 16:40:03 -07001807/*
1808 * Same as new_Token(), but text belongs to the new token and is
1809 * either taken over or freed. This function MUST be called
1810 * with valid txt and txtlen, unlike new_Token().
1811 */
1812static Token *new_Token_free(Token * next, enum pp_token_type type,
1813 char *text, size_t txtlen)
1814{
1815 Token *t = alloc_Token();
1816
1817 t->next = next;
1818 t->type = type;
1819 t->len = tok_check_len(txtlen);
1820
1821 if (txtlen <= INLINE_TEXT) {
1822 memcpy(t->text.a, text, txtlen);
1823 free(text);
1824 } else {
1825 t->text.p.ptr = text;
1826 }
1827
1828 return t;
1829}
1830
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001831static Token *dup_Token(Token *next, const Token *src)
1832{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001833 Token *t = alloc_Token();
1834
1835 memcpy(t, src, sizeof *src);
1836 t->next = next;
1837
1838 if (t->len > INLINE_TEXT) {
1839 t->text.p.ptr = nasm_malloc(t->len + 1);
1840 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1841 }
1842
1843 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001844}
1845
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001846static Token *new_White(Token *next)
1847{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001848 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001849
H. Peter Anvin8571f062019-09-23 16:40:03 -07001850 t->next = next;
1851 t->type = TOK_WHITESPACE;
1852 t->len = 1;
1853 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001854
H. Peter Anvin8571f062019-09-23 16:40:03 -07001855 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001856}
1857
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001858/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001859 * This *transfers* the content from one token to another, leaving the
1860 * next pointer of the latter intact. Unlike dup_Token(), the old
1861 * token is destroyed, except for its next pointer, and the text
1862 * pointer allocation, if any, is simply transferred.
1863 */
1864static Token *steal_Token(Token *dst, Token *src)
1865{
1866 /* Overwrite everything except the next pointers */
1867 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1868 sizeof(Token) - sizeof(Token *));
1869
1870 /* Clear the donor token */
1871 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1872
1873 return dst;
1874}
1875
1876/*
1877 * Convert a line of tokens back into text. This modifies the list
1878 * by expanding environment variables.
1879 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001880 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001881 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001882 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001883static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001884{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001885 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001886 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001887 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001888
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001889 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001890 switch (t->type) {
1891 case TOK_ENVIRON:
1892 {
1893 const char *v = pp_getenv(t, true);
1894 set_text(t, v, tok_strlen(v));
1895 t->type = TOK_INTERNAL_STRING;
1896 break;
1897 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001898
H. Peter Anvin8571f062019-09-23 16:40:03 -07001899 case TOK_LOCAL_MACRO:
1900 case TOK_LOCAL_SYMBOL:
1901 if (expand_locals) {
1902 const char *q;
1903 char *p;
1904 Context *ctx = get_ctx(tok_text(t), &q);
1905 if (ctx) {
1906 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1907 set_text_free(t, p, nasm_last_string_len());
1908 t->type = TOK_ID;
1909 }
1910 }
1911 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001912
H. Peter Anvin8571f062019-09-23 16:40:03 -07001913 default:
1914 break; /* No modifications */
1915 }
1916
1917 if (debug_level(2)) {
1918 unsigned int t_len = t->len;
1919 unsigned int s_len = tok_strlen(tok_text(t));
1920 if (t_len != s_len) {
1921 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
1922 tok_text(t), t->type, s_len, t_len);
1923 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001924 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001926
H. Peter Anvin8571f062019-09-23 16:40:03 -07001927 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001928 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001929
H. Peter Anvin734b1882002-04-30 21:01:08 +00001930 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001931
H. Peter Anvin8571f062019-09-23 16:40:03 -07001932 list_for_each(t, tlist)
1933 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001934 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001935
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001936 return line;
1937}
1938
1939/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001940 * A scanner, suitable for use by the expression evaluator, which
1941 * operates on a line of Tokens. Expects a pointer to a pointer to
1942 * the first token in the line to be passed in as its private_data
1943 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001944 *
1945 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001946 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001947struct ppscan {
1948 Token *tptr;
1949 int ntokens;
1950};
1951
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001953{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001954 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001955 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001956 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001957
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001959 if (pps->ntokens && (tline = pps->tptr)) {
1960 pps->ntokens--;
1961 pps->tptr = tline->next;
1962 } else {
1963 pps->tptr = NULL;
1964 pps->ntokens = 0;
1965 return tokval->t_type = TOKEN_EOS;
1966 }
1967 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001968
H. Peter Anvin8571f062019-09-23 16:40:03 -07001969 txt = tok_text(tline);
1970 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001971
H. Peter Anvin8571f062019-09-23 16:40:03 -07001972 if (txt[0] == '$') {
1973 if (!txt[1]) {
1974 return tokval->t_type = TOKEN_HERE;
1975 } else if (txt[1] == '$' && !txt[2]) {
1976 return tokval->t_type = TOKEN_BASE;
1977 } else if (tline->type == TOK_ID) {
1978 tokval->t_charptr++;
1979 return tokval->t_type = TOKEN_ID;
1980 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001981 }
1982
H. Peter Anvin8571f062019-09-23 16:40:03 -07001983 switch (tline->type) {
1984 default:
1985 if (tline->len == 1)
1986 return tokval->t_type = txt[0];
1987 /* fall through */
1988 case TOK_ID:
1989 return nasm_token_hash(txt, tokval);
1990
1991 case TOK_NUMBER:
1992 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001994 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 if (rn_error)
1996 return tokval->t_type = TOKEN_ERRNUM;
1997 else
1998 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001999 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002000
H. Peter Anvin8571f062019-09-23 16:40:03 -07002001 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002002 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002003
2004 case TOK_STRING:
2005 tokval->t_charptr = (char *)unquote_token(tline);
2006 tokval->t_inttwo = tline->len;
2007 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002008 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002009}
2010
2011/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002012 * 1. An expression (true if nonzero 0)
2013 * 2. The keywords true, on, yes for true
2014 * 3. The keywords false, off, no for false
2015 * 4. An empty line, for true
2016 *
2017 * On error, return defval (usually the previous value)
2018 */
2019static bool pp_get_boolean_option(Token *tline, bool defval)
2020{
2021 static const char * const noyes[] = {
2022 "no", "yes",
2023 "false", "true",
2024 "off", "on"
2025 };
2026 struct ppscan pps;
2027 struct tokenval tokval;
2028 expr *evalresult;
2029
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002030 tline = skip_white(tline);
2031 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002032 return true;
2033
2034 if (tline->type == TOK_ID) {
2035 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002036 const char *txt = tok_text(tline);
2037
H. Peter Anvind2354082019-08-27 16:38:48 -07002038 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002039 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002040 return i & 1;
2041 }
2042
2043 pps.tptr = NULL;
2044 pps.tptr = tline;
2045 pps.ntokens = -1;
2046 tokval.t_type = TOKEN_INVALID;
2047 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2048
2049 if (!evalresult)
2050 return true;
2051
2052 if (tokval.t_type)
2053 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2054 if (!is_really_simple(evalresult)) {
2055 nasm_nonfatal("boolean flag expression must be a constant");
2056 return defval;
2057 }
2058
2059 return reloc_value(evalresult) != 0;
2060}
2061
2062/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002063 * Compare a string to the name of an existing macro; this is a
2064 * simple wrapper which calls either strcmp or nasm_stricmp
2065 * depending on the value of the `casesense' parameter.
2066 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002067static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002068{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002069 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002070}
2071
2072/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002073 * Compare a string to the name of an existing macro; this is a
2074 * simple wrapper which calls either strcmp or nasm_stricmp
2075 * depending on the value of the `casesense' parameter.
2076 */
2077static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2078{
2079 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2080}
2081
2082/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002083 * Return the Context structure associated with a %$ token. Return
2084 * NULL, having _already_ reported an error condition, if the
2085 * context stack isn't deep enough for the supplied number of $
2086 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002087 *
2088 * If "namep" is non-NULL, set it to the pointer to the macro name
2089 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002090 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002091static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002092{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002093 Context *ctx;
2094 int i;
2095
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002096 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002097 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002098
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002099 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002100 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002101
H. Peter Anvine2c80182005-01-15 22:15:51 +00002102 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002103 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002105 }
2106
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002107 name += 2;
2108 ctx = cstk;
2109 i = 0;
2110 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002111 name++;
2112 i++;
2113 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002114 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002115 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002116 nasm_nonfatal("`%s': context stack is only"
2117 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002118 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002119 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002120
2121 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002123
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002124 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002125}
2126
2127/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002128 * Open an include file. This routine must always return a valid
2129 * file pointer if it returns - it's responsible for throwing an
2130 * ERR_FATAL and bombing out completely if not. It should also try
2131 * the include path one by one until it finds the file or reaches
2132 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002133 *
2134 * Note: for INC_PROBE the function returns NULL at all times;
2135 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002136 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002137enum incopen_mode {
2138 INC_NEEDED, /* File must exist */
2139 INC_OPTIONAL, /* Missing is OK */
2140 INC_PROBE /* Only an existence probe */
2141};
2142
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002143/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002144static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002145 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002146{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002147 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002148 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002149 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002150 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002151 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002152
H. Peter Anvine2c80182005-01-15 22:15:51 +00002153 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002154 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002155 if (omode == INC_PROBE) {
2156 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002157 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002158 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002159 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002160 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002162 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002163 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002164 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002165 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002166
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002167 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002168
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002169 if (!ip) {
2170 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002171 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002172 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002173
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002174 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002175 ip = ip->next;
2176 }
2177}
2178
2179/*
2180 * Open a file, or test for the presence of one (depending on omode),
2181 * considering the include path.
2182 */
2183static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002184 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002185 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002186 enum incopen_mode omode,
2187 enum file_flags fmode)
2188{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002189 struct hash_insert hi;
2190 void **hp;
2191 char *path;
2192 FILE *fp = NULL;
2193
2194 hp = hash_find(&FileHash, file, &hi);
2195 if (hp) {
2196 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002197 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002198 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002199 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002200 } else {
2201 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002202 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002203
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002204 /* Positive or negative result */
2205 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002206
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002207 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002208 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002209 */
2210 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002211 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002212 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002213
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002214 if (!path) {
2215 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002216 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002217 } else {
2218 if (!fp && omode != INC_PROBE)
2219 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002220 }
2221
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002222 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002223 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002224
2225 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002226}
2227
2228/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002229 * Opens an include or input file. Public version, for use by modules
2230 * that get a file:lineno pair and need to look at the file again
2231 * (e.g. the CodeView debug backend). Returns NULL on failure.
2232 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002233FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002234{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002235 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002236}
2237
2238/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002239 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002240 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002241 * return true if _any_ single-line macro of that name is defined.
2242 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002243 * `nparam' or no parameters is defined.
2244 *
2245 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002246 * defined, or nparam is -1, the address of the definition structure
2247 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002248 * is NULL, no action will be taken regarding its contents, and no
2249 * error will occur.
2250 *
2251 * Note that this is also called with nparam zero to resolve
2252 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002253 *
2254 * If you already know which context macro belongs to, you can pass
2255 * the context pointer as first parameter; if you won't but name begins
2256 * with %$ the context will be automatically computed. If all_contexts
2257 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002258 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002259static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002260smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002261 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002262{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002263 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002264 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002265
H. Peter Anvin97a23472007-09-16 17:57:25 -07002266 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002267 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002268 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002269 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002270 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002272 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002273 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002274 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002275 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002276 }
H. Peter Anvind2354082019-08-27 16:38:48 -07002277
2278restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002279 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002280
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 while (m) {
2282 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002283 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002284 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002285 if (m->alias && !find_alias) {
2286 if (do_aliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002287 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002288 goto restart;
2289 } else {
2290 continue;
2291 }
2292 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002293 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002294 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002295 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002296 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 }
2298 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002299 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002300
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002301 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002302}
2303
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002304/* param should be a natural number [0; INT_MAX] */
2305static int read_param_count(const char *str)
2306{
2307 int result;
2308 bool err;
2309
2310 result = readnum(str, &err);
2311 if (result < 0 || result > INT_MAX) {
2312 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002313 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2314 str, 0, INT_MAX);
2315 } else if (err)
2316 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002317 return result;
2318}
2319
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002320/*
2321 * Count and mark off the parameters in a multi-line macro call.
2322 * This is called both from within the multi-line macro expansion
2323 * code, and also to mark off the default parameters when provided
2324 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002325 *
2326 * Note that we need space in the params array for parameter 0 being
2327 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002328 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002329static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002330{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002331 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002332 int nparam = 0;
2333 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002334
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002335 paramsize = PARAM_DELTA;
2336 params = nasm_malloc(paramsize * sizeof(*params));
2337 params[0] = NULL;
2338
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002339 while ((t = skip_white(t))) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002340 /* 2 slots for captured label and NULL */
2341 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002342 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002343 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002345 params[++nparam] = t;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002346 if (tok_is(t, '{')) {
2347 int brace = 1;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002348 while (brace && (t = t->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002349 brace += tok_is(t, '{');
2350 brace -= tok_is(t, '}');
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002351 }
2352
2353 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 /*
2355 * Now we've found the closing brace, look further
2356 * for the comma.
2357 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002358 t = skip_white(t->next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002359 if (tok_isnt(t, ','))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002360 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002361 } else {
2362 nasm_nonfatal("expecting closing brace in macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002364 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002365
2366 while (tok_isnt(t, ','))
2367 t = t->next;
2368
2369 if (t) /* got a comma */
2370 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002371 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002372
2373 params[nparam+1] = NULL;
2374 *paramsp = params;
2375 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002376}
2377
2378/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002379 * Determine whether one of the various `if' conditions is true or
2380 * not.
2381 *
2382 * We must free the tline we get passed.
2383 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002384static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002385{
H. Peter Anvin70055962007-10-11 00:05:31 -07002386 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002387 Token *t, *tt, *origline;
2388 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002389 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002390 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002391 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002392 const char *dname = pp_directives[ct];
2393 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002394 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002395
2396 origline = tline;
2397
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002398 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002399 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002400 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002401 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002402 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002403 if (!tline)
2404 break;
2405 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002406 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002407 dname);
2408 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002409 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002410 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002411 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 tline = tline->next;
2413 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002414 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002415
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002416 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002417 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002418 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002419 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002421 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002422 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002423 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002424 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002425 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002426 if (smacro_defined(NULL, tok_text(tline), 0, NULL, true, false))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002427 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002429 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002430 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002431
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002432 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002433 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002434 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002435 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002436 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002437 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002438 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002439 tline->type != TOK_INTERNAL_STRING &&
2440 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002441 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002442 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002443 goto fail;
2444 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002445
2446 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002447 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002448 }
2449 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002450
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002451 case PP_IFIDNI:
2452 casesense = false;
2453 /* fall through */
2454 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 tline = expand_smacro(tline);
2456 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002457 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 tt = tt->next;
2459 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002460 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002461 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002462 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 }
2464 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002465 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002466 while (tok_isnt(t, ',') && tt) {
2467 unsigned int l1, l2;
2468 const char *t1, *t2;
2469
2470 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002471 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002472 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002473 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 }
2475 if (t->type == TOK_WHITESPACE) {
2476 t = t->next;
2477 continue;
2478 }
2479 if (tt->type == TOK_WHITESPACE) {
2480 tt = tt->next;
2481 continue;
2482 }
2483 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002484 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 break;
2486 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002487
H. Peter Anvin8571f062019-09-23 16:40:03 -07002488 t1 = unquote_token(t);
2489 t2 = unquote_token(tt);
2490 l1 = t->len;
2491 l2 = tt->len;
2492
2493 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2494 j = false;
2495 break;
2496 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002497
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 t = t->next;
2499 tt = tt->next;
2500 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002501 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002502 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002503 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002504
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002505 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002506 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002507 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002508 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002509
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002510 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002511 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002512 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002513 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002514 goto fail;
2515 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002516 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002517 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002518 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002519 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002520 searching.nparam_max = INT_MAX;
2521 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002522 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002523 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002524 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002525 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002526 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002527 } else {
2528 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002529 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002530 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002531 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002533 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002535 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002536 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002537 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002538 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002539 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002540 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002541 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002542 searching.nparam_max = searching.nparam_min;
2543 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002544 }
2545 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002546 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002547 tline = tline->next;
2548 searching.plus = true;
2549 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002550 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2551 while (mmac) {
2552 if (!strcmp(mmac->name, searching.name) &&
2553 (mmac->nparam_min <= searching.nparam_max
2554 || searching.plus)
2555 && (searching.nparam_min <= mmac->nparam_max
2556 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 found = true;
2558 break;
2559 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002560 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 }
2562 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002563 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002564 nasm_free(searching.name);
2565 j = found;
2566 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002567 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002568
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002569 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 needtype = TOK_ID;
2571 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002572 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002573 needtype = TOK_NUMBER;
2574 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002575 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 needtype = TOK_STRING;
2577 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002578
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579iftype:
2580 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002581
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002582 while (tok_white(t) ||
2583 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002584 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002585
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002586 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002587 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002588
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002589 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002590 tline = expand_smacro(tline);
2591 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002592
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 j = false;
2594 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002595 t = skip_white(t->next); /* Skip the actual token + whitespace */
2596 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002597 }
2598 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002599
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002600 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002601 tline = expand_smacro(tline);
2602 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 j = !t; /* Should be empty */
2604 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002605
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002606 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002607 pps.tptr = tline = expand_smacro(tline);
2608 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002610 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002611 if (!evalresult)
2612 return -1;
2613 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002614 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002616 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002617 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002618 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002620 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002622
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002623 case PP_IFUSING:
2624 case PP_IFUSABLE:
2625 {
2626 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002627 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002628
H. Peter Anvin8571f062019-09-23 16:40:03 -07002629 pkg = get_use_pkg(tline, dname, &name);
2630 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002631 goto fail;
2632
2633 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2634 break;
2635 }
2636
H. Peter Anvine2c80182005-01-15 22:15:51 +00002637 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002638 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002640 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002641
2642 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002643 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002644
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002645fail:
2646 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002647 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002648}
2649
2650/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002651 * Default smacro expansion routine: just returns a copy of the
2652 * expansion list.
2653 */
2654static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002655smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002656{
2657 (void)params;
2658 (void)nparams;
2659
2660 return dup_tlist(s->expansion, NULL);
2661}
2662
2663/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002664 * Emit a macro defintion or undef to the listing file, if
2665 * desired. This is similar to detoken(), but it handles the reverse
2666 * expansion list, does not expand %! or local variable tokens, and
2667 * does some special handling for macro parameters.
2668 */
2669static void
2670list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2671{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002672 Token *t;
2673 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002674 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002675 char *context_prefix = NULL;
2676 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002677
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002678 namelen = strlen(m->name);
2679 size = namelen + 2; /* Include room for space after name + NUL */
2680
2681 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002682 int context_depth = cstk->depth - ctx->depth + 1;
2683 context_prefix =
2684 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2685 ctx->name ? ctx->name : "",
2686 ctx->number, context_depth, "");
2687
2688 context_len = nasm_last_string_len();
2689 memset(context_prefix + context_len - context_depth,
2690 '$', context_depth);
2691 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002692 }
2693
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002694 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002695 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002696
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002697 if (m->nparam) {
2698 /*
2699 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002700 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002701 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002702 int i;
2703
2704 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002705 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002706 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002707 }
2708
2709 def = nasm_malloc(size);
2710 p = def+size;
2711 *--p = '\0';
2712
2713 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002714 p -= t->len;
2715 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002716 }
2717
2718 *--p = ' ';
2719
2720 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002721 int i;
2722
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002723 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002724 for (i = m->nparam-1; i >= 0; i--) {
2725 enum sparmflags flags = m->params[i].flags;
2726 if (flags & SPARM_GREEDY)
2727 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002728 p -= m->params[i].name.len;
2729 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2730
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002731 if (flags & SPARM_NOSTRIP)
2732 *--p = '!';
2733 if (flags & SPARM_STR)
2734 *--p = '&';
2735 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002736 *--p = '=';
2737 *--p = ',';
2738 }
2739 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002740 }
2741
2742 p -= namelen;
2743 memcpy(p, m->name, namelen);
2744
H. Peter Anvin6686de22019-08-10 05:33:14 -07002745 if (context_prefix) {
2746 p -= context_len;
2747 memcpy(p, context_prefix, context_len);
2748 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002749 }
2750
2751 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002752 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002753}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002754
2755/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002756 * Parse smacro arguments, return argument count. If the tmpl argument
2757 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002758 * *tpp is updated to point to the pointer to the first token after the
2759 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002760 *
2761 * The text values from any argument tokens are "stolen" and the
2762 * corresponding text fields set to NULL.
2763 */
2764static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2765{
2766 int nparam = 0;
2767 enum sparmflags flags;
2768 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002769 bool err, done;
2770 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002771 Token **tn = *tpp;
2772 Token *t = *tn;
2773 Token *name;
2774
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002775 /*
2776 * DO NOT skip whitespace here, or we won't be able to distinguish:
2777 *
2778 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2779 * %define bar(a,b) ; two arguments, empty expansion
2780 *
2781 * This ambiguity was inherited from C.
2782 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002783
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002784 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002785 goto finish;
2786
2787 if (tmpl) {
2788 Token *tx = t;
2789 Token **txpp = &tx;
2790 int sparam;
2791
2792 /* Count parameters first */
2793 sparam = parse_smacro_template(&txpp, NULL);
2794 if (!sparam)
2795 goto finish; /* No parameters, we're done */
2796 nasm_newn(params, sparam);
2797 }
2798
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002799 /* Skip leading paren */
2800 tn = &t->next;
2801 t = *tn;
2802
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002803 name = NULL;
2804 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002805 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002806
2807 while (!done) {
2808 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002809 if (name || flags)
2810 nasm_nonfatal("`)' expected to terminate macro template");
2811 else
2812 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002813 break;
2814 }
2815
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002816 switch (t->type) {
2817 case TOK_ID:
2818 if (name)
2819 goto bad;
2820 name = t;
2821 break;
2822
2823 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002824 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002825 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002826 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002827 case '=':
2828 flags |= SPARM_EVAL;
2829 break;
2830 case '&':
2831 flags |= SPARM_STR;
2832 break;
2833 case '!':
2834 flags |= SPARM_NOSTRIP;
2835 break;
2836 case '+':
2837 flags |= SPARM_GREEDY;
2838 greedy = true;
2839 break;
2840 case ',':
2841 if (greedy)
2842 nasm_nonfatal("greedy parameter must be last");
2843 /* fall through */
2844 case ')':
2845 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002846 if (name)
2847 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002848 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002849 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002850 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002851 name = NULL;
2852 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002853 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002854 break;
2855 default:
2856 goto bad;
2857 }
2858 break;
2859
2860 case TOK_WHITESPACE:
2861 break;
2862
2863 default:
2864 bad:
2865 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002866 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002867 err = true;
2868 }
2869 break;
2870 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002871
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002872 tn = &t->next;
2873 t = *tn;
2874 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002875
2876finish:
2877 while (t && t->type == TOK_WHITESPACE) {
2878 tn = &t->next;
2879 t = t->next;
2880 }
2881 *tpp = tn;
2882 if (tmpl) {
2883 tmpl->nparam = nparam;
2884 tmpl->greedy = greedy;
2885 tmpl->params = params;
2886 }
2887 return nparam;
2888}
2889
2890/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002891 * Common code for defining an smacro. The tmpl argument, if not NULL,
2892 * contains any macro parameters that aren't explicit arguments;
2893 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002894 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002895static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002896 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002897{
2898 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002899 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002900 Context *ctx;
2901 bool defining_alias = false;
2902 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002903
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002904 if (tmpl) {
2905 defining_alias = tmpl->alias;
2906 nparam = tmpl->nparam;
2907 }
2908
2909 while (1) {
2910 ctx = get_ctx(mname, &mname);
2911
H. Peter Anvind2354082019-08-27 16:38:48 -07002912 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002913 /* Create a new macro */
2914 smtbl = ctx ? &ctx->localmac : &smacros;
2915 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2916 nasm_new(smac);
2917 smac->next = *smhead;
2918 *smhead = smac;
2919 break;
2920 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002921 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002922 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002923 /*
2924 * Some instances of the old code considered this a failure,
2925 * some others didn't. What is the right thing to do here?
2926 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002927 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002928 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002929 /*
2930 * We're redefining, so we have to take over an
2931 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002932 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002933 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002934 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002935 break;
2936 } else if (smac->in_progress) {
2937 nasm_nonfatal("macro alias loop");
2938 goto fail;
2939 } else {
2940 /* It is an alias macro; follow the alias link */
2941 SMacro *s;
2942
2943 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002944 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002945 expansion, tmpl);
2946 smac->in_progress = false;
2947 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002948 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002949 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002950
2951 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002952 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002953 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002954 smac->expand = smacro_expand_default;
2955 if (tmpl) {
2956 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002957 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002958 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002959 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002960 if (tmpl->expand)
2961 smac->expand = tmpl->expand;
2962 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002963 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002964 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2965 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002966 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002967 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002968
2969fail:
2970 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002971 if (tmpl)
2972 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002973 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002974}
2975
2976/*
2977 * Undefine an smacro
2978 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002979static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002980{
2981 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002982 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002983 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002984
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002985 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002986 smtbl = ctx ? &ctx->localmac : &smacros;
2987 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002988
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002989 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002990 /*
2991 * We now have a macro name... go hunt for it.
2992 */
2993 sp = smhead;
2994 while ((s = *sp) != NULL) {
2995 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002996 if (s->alias && !undefalias) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002997 if (!do_aliases)
2998 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002999 if (s->in_progress) {
3000 nasm_nonfatal("macro alias loop");
3001 } else {
3002 s->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003003 undef_smacro(tok_text(s->expansion), false);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003004 s->in_progress = false;
3005 }
3006 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003007 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003008 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3009 ctx, s);
3010 *sp = s->next;
3011 free_smacro(s);
3012 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003013 } else {
3014 sp = &s->next;
3015 }
3016 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003017 }
3018}
3019
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003020/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003021 * Parse a mmacro specification.
3022 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003023static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003024{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003025 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003026 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003027 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003028 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003029 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003030 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003031 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003032
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003033#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003034 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003035#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003036 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003037 def->plus = false;
3038 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003039 def->nparam_min = 0;
3040 def->nparam_max = 0;
3041
H. Peter Anvina26433d2008-07-16 14:40:01 -07003042 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003043 tline = skip_white(tline);
3044 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003045 nasm_nonfatal("`%s' expects a parameter count", directive);
3046 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003047 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003048 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003049 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003050 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003051 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003052 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003053 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003054 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003055 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003056 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003057 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003058 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003059 }
3060 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003061 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003062 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003063 tline = tline->next;
3064 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003065 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003066 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003067 tline->next->len == 7 &&
3068 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003069 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003070 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003071 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003072
H. Peter Anvina26433d2008-07-16 14:40:01 -07003073 /*
3074 * Handle default parameters.
3075 */
3076 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003077 def->dlist = tline->next;
3078 tline->next = NULL;
3079 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003080 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003081 def->dlist = NULL;
3082 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003083 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003084 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003085
H. Peter Anvin89cee572009-07-15 09:16:54 -04003086 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003087 !def->plus) {
3088 /*
3089 *!macro-defaults [on] macros with more default than optional parameters
3090 *! warns when a macro has more default parameters than optional parameters.
3091 *! See \k{mlmacdef} for why might want to disable this warning.
3092 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003093 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003094 "too many default macro parameters in macro `%s'", def->name);
3095 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003096
H. Peter Anvina26433d2008-07-16 14:40:01 -07003097 return true;
3098}
3099
3100
3101/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003102 * Decode a size directive
3103 */
3104static int parse_size(const char *str) {
3105 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003106 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003107 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003108 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003109 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003110}
3111
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003112/*
3113 * Process a preprocessor %pragma directive. Currently there are none.
3114 * Gets passed the token list starting with the "preproc" token from
3115 * "%pragma preproc".
3116 */
3117static void do_pragma_preproc(Token *tline)
3118{
3119 /* Skip to the real stuff */
3120 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003121 tline = skip_white(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003122 if (!tline)
3123 return;
3124
3125 (void)tline; /* Nothing else to do at present */
3126}
3127
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003128static bool is_macro_id(const Token *t)
3129{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003130 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003131}
3132
H. Peter Anvin8571f062019-09-23 16:40:03 -07003133static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003134{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003135 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003136 Token *t = *tp;
3137
3138 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003139 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003140 t = expand_id(t);
3141
3142 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003143 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003144 return NULL;
3145 }
3146
H. Peter Anvin8571f062019-09-23 16:40:03 -07003147 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003148 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003149 *tp = t;
3150 return id;
3151}
3152
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003153/* Parse a %use package name and find the package. Set *err on syntax error. */
3154static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003155get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003156{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003157 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003158
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003159 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003160 t = expand_smacro(t);
3161
H. Peter Anvin8571f062019-09-23 16:40:03 -07003162 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003163
H. Peter Anvin8571f062019-09-23 16:40:03 -07003164 if (!t) {
3165 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3166 return NULL;
3167 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3168 nasm_nonfatal("`%s' expects a package name, got `%s'",
3169 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003170 return NULL;
3171 }
3172
H. Peter Anvin8571f062019-09-23 16:40:03 -07003173 *name = id = unquote_token(t);
3174
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003175 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003176 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003177 if (t)
3178 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3179
3180 return nasm_find_use_package(id);
3181}
3182
Ed Beroset3ab3f412002-06-11 03:31:49 +00003183/**
3184 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003185 * Find out if a line contains a preprocessor directive, and deal
3186 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003187 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003188 * If a directive _is_ found, it is the responsibility of this routine
3189 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003190 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003191 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003192 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003193 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003194 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003195 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003196static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003197{
H. Peter Anvin4169a472007-09-12 01:29:43 +00003198 enum preproc_token i;
3199 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003200 bool err;
3201 int nparam;
3202 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003203 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003204 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003205 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003206 const char *p;
3207 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003208 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003209 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003210 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003211 Include *inc;
3212 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003213 Cond *cond;
3214 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003215 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003216 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003217 struct tokenval tokval;
3218 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003219 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003220 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003221 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003222 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003223
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003224 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003225 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003226
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003227 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003228 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3229 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003230
H. Peter Anvin8571f062019-09-23 16:40:03 -07003231 dname = tok_text(tline);
3232 if (dname[1] == '%')
3233 return NO_DIRECTIVE_FOUND;
3234
3235 i = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003236
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003237 casesense = true;
3238 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
3239 casesense = false;
3240 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003241 }
3242
3243 /*
3244 * If we're in a non-emitting branch of a condition construct,
3245 * or walking to the end of an already terminated %rep block,
3246 * we should ignore all directives except for condition
3247 * directives.
3248 */
3249 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003250 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
3251 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003252 return NO_DIRECTIVE_FOUND;
3253 }
3254
3255 /*
3256 * If we're defining a macro or reading a %rep block, we should
3257 * ignore all directives except for %macro/%imacro (which nest),
3258 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
3259 * If we're in a %rep block, another %rep nests, so should be let through.
3260 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003261 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003262 i != PP_ENDMACRO && i != PP_ENDM &&
3263 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
3264 return NO_DIRECTIVE_FOUND;
3265 }
3266
3267 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003268 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003269 nested_mac_count++;
3270 return NO_DIRECTIVE_FOUND;
3271 } else if (nested_mac_count > 0) {
3272 if (i == PP_ENDMACRO) {
3273 nested_mac_count--;
3274 return NO_DIRECTIVE_FOUND;
3275 }
3276 }
3277 if (!defining->name) {
3278 if (i == PP_REP) {
3279 nested_rep_count++;
3280 return NO_DIRECTIVE_FOUND;
3281 } else if (nested_rep_count > 0) {
3282 if (i == PP_ENDREP) {
3283 nested_rep_count--;
3284 return NO_DIRECTIVE_FOUND;
3285 }
3286 }
3287 }
3288 }
3289
H. Peter Anvin4169a472007-09-12 01:29:43 +00003290 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003291 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003292 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003293 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003294
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003295 case PP_PRAGMA:
3296 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003297 * %pragma namespace options...
3298 *
3299 * The namespace "preproc" is reserved for the preprocessor;
3300 * all other namespaces generate a [pragma] assembly directive.
3301 *
3302 * Invalid %pragmas are ignored and may have different
3303 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003304 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003305 t = tline;
3306 tline = tline->next;
3307 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003308 tline = zap_white(expand_smacro(tline));
3309 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003310 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003311 /* Preprocessor pragma */
3312 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003313 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003314 } else {
3315 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003316
3317 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003318 for (t = tline; t->next; t = t->next)
3319 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003320 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003321
3322 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003323 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003324 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003325 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003326 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003327 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003328 }
3329 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003330 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003331
H. Peter Anvine2c80182005-01-15 22:15:51 +00003332 case PP_STACKSIZE:
3333 /* Directive to tell NASM what the default stack size is. The
3334 * default is for a 16-bit stack, and this can be overriden with
3335 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003337 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003339 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003340 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003341 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003342 /* All subsequent ARG directives are for a 32-bit stack */
3343 StackSize = 4;
3344 StackPointer = "ebp";
3345 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003346 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003347 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003348 /* All subsequent ARG directives are for a 64-bit stack */
3349 StackSize = 8;
3350 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003351 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003352 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003353 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 /* All subsequent ARG directives are for a 16-bit stack,
3355 * far function call.
3356 */
3357 StackSize = 2;
3358 StackPointer = "bp";
3359 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003360 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003361 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003362 /* All subsequent ARG directives are for a 16-bit stack,
3363 * far function call. We don't support near functions.
3364 */
3365 StackSize = 2;
3366 StackPointer = "bp";
3367 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003368 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003370 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003371 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003372 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003373
H. Peter Anvine2c80182005-01-15 22:15:51 +00003374 case PP_ARG:
3375 /* TASM like ARG directive to define arguments to functions, in
3376 * the following form:
3377 *
3378 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3379 */
3380 offset = ArgOffset;
3381 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003382 const char *arg;
3383 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003384 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003385
H. Peter Anvine2c80182005-01-15 22:15:51 +00003386 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003387 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003388 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003389 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003390 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003392 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003393
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 /* Find the argument size type */
3395 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003396 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003397 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003398 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 }
3400 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003401 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003402 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003403 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003405
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003407 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003409 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003410 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003411 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003412 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003413 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 }
3415 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003416
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003417 /* Round up to even stack slots */
3418 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003419
H. Peter Anvine2c80182005-01-15 22:15:51 +00003420 /* Now define the macro for the argument */
3421 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3422 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003423 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003425
H. Peter Anvine2c80182005-01-15 22:15:51 +00003426 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003427 tline = skip_white(tline->next);
3428 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003429 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003430 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003431
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 case PP_LOCAL:
3433 /* TASM like LOCAL directive to define local variables for a
3434 * function, in the following form:
3435 *
3436 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3437 *
3438 * The '= LocalSize' at the end is ignored by NASM, but is
3439 * required by TASM to define the local parameter size (and used
3440 * by the TASM macro package).
3441 */
3442 offset = LocalOffset;
3443 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003444 const char *local;
3445 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003447
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003449 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003450 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003451 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003452 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003454 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003455
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 /* Find the argument size type */
3457 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003458 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003459 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003460 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 }
3462 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003463 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003464 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003465 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003467
H. Peter Anvine2c80182005-01-15 22:15:51 +00003468 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003469 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003471 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003473 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003474 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003475 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003476 }
3477 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003478
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 /* Round up to even stack slots */
3480 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003481
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003483
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003485 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3486 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003487 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003488
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 /* Now define the assign to setup the enter_c macro correctly */
3490 snprintf(directive, sizeof(directive),
3491 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003492 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003493
H. Peter Anvine2c80182005-01-15 22:15:51 +00003494 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003495 tline = skip_white(tline->next);
3496 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003497 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003498 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003499
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 case PP_CLEAR:
3501 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003502 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003503 free_macros();
3504 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003505 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003506
H. Peter Anvin418ca702008-05-30 10:42:30 -07003507 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003508 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003509 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003510 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003511 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003512 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003513 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003514 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003515 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003516 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003517
3518 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003519 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003520
3521 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003522 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003523 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003524
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003525 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003526 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003527 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003528 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003530 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003531 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003532 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003533 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003535 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003536 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003537 (pp_mode == PP_DEPS)
3538 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003539 if (!inc->fp) {
3540 /* -MG given but file not found */
3541 nasm_free(inc);
3542 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003543 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003544 inc->lineno = src_set_linnum(0);
3545 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003546 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003547 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003548 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003550 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003551
H. Peter Anvind2456592008-06-19 15:04:18 -07003552 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003553 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003554 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003555 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003556
H. Peter Anvin8571f062019-09-23 16:40:03 -07003557 pkg = get_use_pkg(tline->next, dname, &name);
3558 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003559 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003560 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003561 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003562 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003563 /*
3564 * Not already included, go ahead and include it.
3565 * Treat it as an include file for the purpose of
3566 * producing a listing.
3567 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003568 use_loaded[pkg->index] = true;
3569 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003570 nasm_new(inc);
3571 inc->next = istk;
3572 inc->fname = src_set_fname(NULL);
3573 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003574 inc->nolist = !list_option('b') || istk->nolist;
3575 istk = inc;
3576 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003577 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003578 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003579 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003582 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003584 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003586 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003587 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003588 nasm_nonfatal("`%s' expects a context identifier",
3589 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003590 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003591 }
3592 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003593 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003594 pp_directives[i]);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003595 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003596 } else {
3597 p = NULL; /* Anonymous */
3598 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003599
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003600 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003601 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003602 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003603 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003604 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003605 ctx->number = unique++;
3606 cstk = ctx;
3607 } else {
3608 /* %pop or %repl */
3609 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003610 nasm_nonfatal("`%s': context stack is empty",
3611 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003612 } else if (i == PP_POP) {
3613 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003614 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003615 "expected %s",
3616 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003617 else
3618 ctx_pop();
3619 } else {
3620 /* i == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003621 nasm_free((char *)cstk->name);
3622 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003623 p = NULL;
3624 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003625 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003626 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003627 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003628 severity = ERR_FATAL;
3629 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003631 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003632 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003633 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003634 /*!
3635 *!user [on] %warning directives
3636 *! controls output of \c{%warning} directives (see \k{pperror}).
3637 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003638 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003639 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003640
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003641issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003642 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003644 tline->next = expand_smacro(tline->next);
3645 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003646 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003647 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003648 t = skip_white(t);
3649 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003650 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003651 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003652 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003653 } else {
3654 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003655 q = detoken(tline, false);
3656 nasm_error(severity, "%s", q);
3657 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003658 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003659 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003660 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003661
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003662 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003663 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003664 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003665 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003666 j = if_condition(tline->next, i);
3667 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003668 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003669 cond = nasm_malloc(sizeof(Cond));
3670 cond->next = istk->conds;
3671 cond->state = j;
3672 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003673 if(istk->mstk.mstk)
3674 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003675 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003676
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003677 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003678 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003679 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003680 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003681 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003682 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003683 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003684
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003685 case COND_DONE:
3686 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003687 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003688
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003689 case COND_ELSE_TRUE:
3690 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003691 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003692 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003693 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003694 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003695
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003696 case COND_IF_FALSE:
3697 /*
3698 * IMPORTANT: In the case of %if, we will already have
3699 * called expand_mmac_params(); however, if we're
3700 * processing an %elif we must have been in a
3701 * non-emitting mode, which would have inhibited
3702 * the normal invocation of expand_mmac_params().
3703 * Therefore, we have to do it explicitly here.
3704 */
3705 j = if_condition(expand_mmac_params(tline->next), i);
3706 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003707 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003708 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003710 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003711
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 case PP_ELSE:
3713 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003714 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003715 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003716 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003717 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003718 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003719 case COND_IF_TRUE:
3720 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003721 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003722 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003723
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003724 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003725 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003726
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003727 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003728 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003729 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003730
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003731 case COND_ELSE_TRUE:
3732 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003733 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003734 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003735 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003736 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003737 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003738 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003739
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740 case PP_ENDIF:
3741 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003742 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003743 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003744 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003745 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003746 cond = istk->conds;
3747 istk->conds = cond->next;
3748 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003749 if(istk->mstk.mstk)
3750 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003751 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003752
H. Peter Anvin8b262472019-02-26 14:00:54 -08003753 case PP_RMACRO:
3754 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003755 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003756 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003757 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003758 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003759 if (i == PP_RMACRO)
3760 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003761 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003762 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003763 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003764 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003765
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003766 src_get(&defining->xline, &defining->fname);
3767
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003768 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3769 while (mmac) {
3770 if (!strcmp(mmac->name, defining->name) &&
3771 (mmac->nparam_min <= defining->nparam_max
3772 || defining->plus)
3773 && (defining->nparam_min <= mmac->nparam_max
3774 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003775 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003776 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003777 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003778 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003779 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003780 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003781 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003782
H. Peter Anvine2c80182005-01-15 22:15:51 +00003783 case PP_ENDM:
3784 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003785 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003786 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003787 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003788 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003789 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3790 defining->next = *mmhead;
3791 *mmhead = defining;
3792 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003793 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003794
H. Peter Anvin89cee572009-07-15 09:16:54 -04003795 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003796 /*
3797 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003798 * macro-end marker for a macro with a name. Then we
3799 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003800 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003801 list_for_each(l, istk->expansion)
3802 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003803 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003804
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003805 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003806 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003807 * Remove all conditional entries relative to this
3808 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003809 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003810 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3811 cond = istk->conds;
3812 istk->conds = cond->next;
3813 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003814 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003815 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003816 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003817 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003818 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003819 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003820
H. Peter Anvina26433d2008-07-16 14:40:01 -07003821 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003822 casesense = false;
3823 /* fall through */
3824 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003825 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003826 MMacro **mmac_p;
3827 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003828
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003829 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003830 spec.casesense = casesense;
3831 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003832 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003833 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003834 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3835 while (mmac_p && *mmac_p) {
3836 mmac = *mmac_p;
3837 if (mmac->casesense == spec.casesense &&
3838 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3839 mmac->nparam_min == spec.nparam_min &&
3840 mmac->nparam_max == spec.nparam_max &&
3841 mmac->plus == spec.plus) {
3842 *mmac_p = mmac->next;
3843 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003844 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003845 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003846 }
3847 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003848 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003849 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003850 }
3851
H. Peter Anvine2c80182005-01-15 22:15:51 +00003852 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003853 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003855 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003856 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003857 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003858 return DIRECTIVE_FOUND;
3859 }
3860 t = expand_smacro(tline->next);
3861 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003862 pps.tptr = tline = t;
3863 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003864 tokval.t_type = TOKEN_INVALID;
3865 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003866 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003867 free_tlist(tline);
3868 if (!evalresult)
3869 return DIRECTIVE_FOUND;
3870 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003871 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003872 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003873 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003874 return DIRECTIVE_FOUND;
3875 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003876 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003877 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003878 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003879 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003880 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003881 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003882 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003883
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003884 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003885 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003886 rotate += mmac->nparam;
3887
3888 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003889 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003890 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003891
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003893 {
3894 MMacro *tmp_defining;
3895
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003896 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003897 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003898 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
3899 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003900 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003901 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003903
H. Peter Anvine2c80182005-01-15 22:15:51 +00003904 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003905 pps.tptr = expand_smacro(tline);
3906 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003908 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003910 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003911 if (!evalresult)
3912 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003913 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003914 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003916 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003917 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003918 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003919 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003920 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003921 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3922 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003923 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003924 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003925 /*!
3926 *!negative-rep [on] regative %rep count
3927 *! warns about negative counts given to the \c{%rep}
3928 *! preprocessor directive.
3929 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003930 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003931 "negative `%%rep' count: %"PRId64, count);
3932 count = 0;
3933 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003934 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003935 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003937 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003938 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003940 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003941 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003942 defining->nolist = nolist;
3943 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003944 defining->mstk = istk->mstk;
3945 defining->dstk.mstk = tmp_defining;
3946 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003947 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003948 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003949 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003950
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003952 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003953 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003954 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003956
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 /*
3958 * Now we have a "macro" defined - although it has no name
3959 * and we won't be entering it in the hash tables - we must
3960 * push a macro-end marker for it on to istk->expansion.
3961 * After that, it will take care of propagating itself (a
3962 * macro-end marker line for a macro which is really a %rep
3963 * block will cause the macro to be re-expanded, complete
3964 * with another macro-end marker to ensure the process
3965 * continues) until the whole expansion is forcibly removed
3966 * from istk->expansion by a %exitrep.
3967 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003968 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003969 l->next = istk->expansion;
3970 l->finishes = defining;
3971 l->first = NULL;
3972 istk->expansion = l;
3973
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003974 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003975
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003976 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003977 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003978 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003979
H. Peter Anvine2c80182005-01-15 22:15:51 +00003980 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003981 /*
3982 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003983 * macro-end marker for a macro with no name. Then we set
3984 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003985 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003986 list_for_each(l, istk->expansion)
3987 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003988 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003989
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003990 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07003991 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003992 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003993 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003994 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003995
H. Peter Anvin8b262472019-02-26 14:00:54 -08003996 case PP_DEFINE:
3997 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003998 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003999 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004000 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004001 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004002
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004003 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004004 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004005
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004006 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004007 lastp = &tline->next;
4008 nparam = parse_smacro_template(&lastp, &tmpl);
4009 tline = *lastp;
4010 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004011
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07004012 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004013 macro_start = tline;
4014 if (!is_macro_id(macro_start)) {
4015 nasm_nonfatal("`%s' expects a macro identifier to alias",
4016 dname);
4017 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004019 tt = macro_start->next;
4020 macro_start->next = NULL;
4021 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004022 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004023 if (tline && tline->type) {
4024 nasm_warn(WARN_OTHER,
4025 "trailing garbage after aliasing identifier ignored");
4026 }
4027 free_tlist(tt);
4028 tmpl.alias = true;
4029 } else {
4030 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07004031 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004032 tline = expand_smacro(tline);
4033
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004034 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004035 macro_start = NULL;
4036 t = tline;
4037 while (t) {
4038 if (t->type == TOK_ID) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004039 const char *ttext = tok_text(t);
4040 size_t tlen = t->len;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004041 for (i = 0; i < nparam; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004042 if (tmpl.params[i].name.len == t->len &&
4043 !memcmp(ttext, tok_text(&tmpl.params[i].name), tlen)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004044 t->type = tok_smac_param(i);
4045 break;
4046 }
4047 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004048 }
4049 tt = t->next;
4050 t->next = macro_start;
4051 macro_start = t;
4052 t = tt;
4053 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004054 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004055
H. Peter Anvine2c80182005-01-15 22:15:51 +00004056 /*
4057 * Good. We now have a macro name, a parameter count, and a
4058 * token list (in reverse order) for an expansion. We ought
4059 * to be OK just to create an SMacro, store it, and let
4060 * free_tlist have the rest of the line (which we have
4061 * carefully re-terminated after chopping off the expansion
4062 * from the end).
4063 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004064 define_smacro(mname, casesense, macro_start, &tmpl);
4065 break;
4066 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004067
H. Peter Anvine2c80182005-01-15 22:15:51 +00004068 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004069 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004070 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004071 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004072 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004073 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004074
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004075 undef_smacro(mname, i == PP_UNDEFALIAS);
4076 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004077
H. Peter Anvin8b262472019-02-26 14:00:54 -08004078 case PP_DEFSTR:
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;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004081
H. Peter Anvin9e200162008-06-04 17:23:14 -07004082 last = tline;
4083 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004084 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004085
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004086 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004087 q = detoken(tline, false);
4088 macro_start = make_tok_qstr(NULL, q);
4089 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004090
4091 /*
4092 * We now have a macro name, an implicit parameter count of
4093 * zero, and a string token to use as an expansion. Create
4094 * and store an SMacro.
4095 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004096 define_smacro(mname, casesense, macro_start, NULL);
4097 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004098
H. Peter Anvin8b262472019-02-26 14:00:54 -08004099 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004100 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004101 goto done;
4102
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004103 last = tline;
4104 tline = expand_smacro(tline->next);
4105 last->next = NULL;
4106
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004107 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004108 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004109 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004110 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004111 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004112 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004113 }
4114
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004115 /*
4116 * Convert the string to a token stream. Note that smacros
4117 * are stored with the token stream reversed, so we have to
4118 * reverse the output of tokenize().
4119 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004120 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004121
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004122 /*
4123 * We now have a macro name, an implicit parameter count of
4124 * zero, and a numeric token to use as an expansion. Create
4125 * and store an SMacro.
4126 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004127 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004128 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004129 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004130
H. Peter Anvin418ca702008-05-30 10:42:30 -07004131 case PP_PATHSEARCH:
4132 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004133 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004134
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004135 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004136 goto done;
4137
H. Peter Anvin418ca702008-05-30 10:42:30 -07004138 last = tline;
4139 tline = expand_smacro(tline->next);
4140 last->next = NULL;
4141
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004142 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004143 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004144 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004145 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004146 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004147 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004148 }
4149 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004150 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004151
4152 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004153
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004154 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004155 if (!found_path)
4156 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004157 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004158
4159 /*
4160 * We now have a macro name, an implicit parameter count of
4161 * zero, and a string token to use as an expansion. Create
4162 * and store an SMacro.
4163 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004164 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004165 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004166 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004167 }
4168
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004170 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004171 goto done;
4172
H. Peter Anvine2c80182005-01-15 22:15:51 +00004173 last = tline;
4174 tline = expand_smacro(tline->next);
4175 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004176
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004177 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004178 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004179 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004180 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004181 free_tlist(tline);
4182 free_tlist(origline);
4183 return DIRECTIVE_FOUND;
4184 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004185
H. Peter Anvin8571f062019-09-23 16:40:03 -07004186 unquote_token(t);
4187 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004188
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 /*
4190 * We now have a macro name, an implicit parameter count of
4191 * zero, and a numeric token to use as an expansion. Create
4192 * and store an SMacro.
4193 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004194 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004195 free_tlist(tline);
4196 free_tlist(origline);
4197 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004198
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004199 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004200 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004201 goto done;
4202
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004203 last = tline;
4204 tline = expand_smacro(tline->next);
4205 last->next = NULL;
4206
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004207 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004208 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004209 switch (t->type) {
4210 case TOK_WHITESPACE:
4211 break;
4212 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004213 unquote_token(t);
4214 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004215 break;
4216 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004217 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004218 break;
4219 /* else fall through */
4220 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004221 nasm_nonfatal("non-string passed to `%s': %s", dname,
4222 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004223 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004224 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004225 }
4226 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004227
H. Peter Anvin8571f062019-09-23 16:40:03 -07004228 q = qbuf = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004229 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004230 if (t->type == TOK_INTERNAL_STRING)
4231 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004232 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004233
4234 /*
4235 * We now have a macro name, an implicit parameter count of
4236 * zero, and a numeric token to use as an expansion. Create
4237 * and store an SMacro.
4238 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004239 macro_start = make_tok_qstr(NULL, qbuf);
4240 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004241 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004242 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004243 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004244
H. Peter Anvine2c80182005-01-15 22:15:51 +00004245 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004246 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004247 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004248 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004249 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004250
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004251 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004252 goto done;
4253
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254 last = tline;
4255 tline = expand_smacro(tline->next);
4256 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004257
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004258 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004259 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004260
4261 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004262
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004264 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004265 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004266 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004267 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004268 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004269
H. Peter Anvin8b262472019-02-26 14:00:54 -08004270 pps.tptr = t->next;
4271 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004273 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004274 if (!evalresult) {
4275 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004276 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004277 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004278 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004279 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004280 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004281 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004282 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004283
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004284 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004285 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004286 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004287 } else {
4288 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004289 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004290 if (!evalresult) {
4291 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004292 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004293 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004294 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004295 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004296 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004297 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004298 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004299 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004300
H. Peter Anvin8571f062019-09-23 16:40:03 -07004301 unquote_token(t);
4302 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004303
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004304 /* make start and count being in range */
4305 if (start < 0)
4306 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004307 if (count < 0)
4308 count = len + count + 1 - start;
4309 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004310 count = len - start;
4311 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004312 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004313
H. Peter Anvin8571f062019-09-23 16:40:03 -07004314 txt = (start < 0) ? "" : tok_text(t) + start;
4315 len = count;
4316 macro_start = make_tok_qstr(NULL, txt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004317
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 /*
4319 * We now have a macro name, an implicit parameter count of
4320 * zero, and a numeric token to use as an expansion. Create
4321 * and store an SMacro.
4322 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004323 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004325 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004326 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004327
H. Peter Anvin8b262472019-02-26 14:00:54 -08004328 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004329 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004330 goto done;
4331
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 last = tline;
4333 tline = expand_smacro(tline->next);
4334 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004335
H. Peter Anvin8b262472019-02-26 14:00:54 -08004336 pps.tptr = tline;
4337 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004338 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004339 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004340 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004341 if (!evalresult)
4342 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004343
H. Peter Anvine2c80182005-01-15 22:15:51 +00004344 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004345 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004346
H. Peter Anvine2c80182005-01-15 22:15:51 +00004347 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004348 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004349 free_tlist(origline);
4350 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004351 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004352
H. Peter Anvin8571f062019-09-23 16:40:03 -07004353 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004354
H. Peter Anvine2c80182005-01-15 22:15:51 +00004355 /*
4356 * We now have a macro name, an implicit parameter count of
4357 * zero, and a numeric token to use as an expansion. Create
4358 * and store an SMacro.
4359 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004360 define_smacro(mname, casesense, macro_start, NULL);
4361 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004362
H. Peter Anvind2354082019-08-27 16:38:48 -07004363 case PP_ALIASES:
4364 tline = tline->next;
4365 tline = expand_smacro(tline);
4366 do_aliases = pp_get_boolean_option(tline, do_aliases);
4367 break;
4368
H. Peter Anvine2c80182005-01-15 22:15:51 +00004369 case PP_LINE:
4370 /*
4371 * Syntax is `%line nnn[+mmm] [filename]'
4372 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004373 if (unlikely(pp_noline))
4374 goto done;
4375
H. Peter Anvine2c80182005-01-15 22:15:51 +00004376 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004377 tline = skip_white(tline);
4378 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004379 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004380 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004381 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004382 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004383 m = 1;
4384 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004385 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004387 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004388 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004389 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004390 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004391 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392 tline = tline->next;
4393 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004394 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004395 src_set_linnum(k);
4396 istk->lineinc = m;
4397 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004398 char *fname = detoken(tline, false);
4399 src_set_fname(fname);
4400 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004401 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004402 break;
4403 }
4404
4405done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004406 free_tlist(origline);
4407 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004408}
4409
4410/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004411 * Ensure that a macro parameter contains a condition code and
4412 * nothing else. Return the condition code index if so, or -1
4413 * otherwise.
4414 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004415static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004416{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004417 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004418
H. Peter Anvin25a99342007-09-22 17:45:45 -07004419 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004420 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004421
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004422 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004423 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004424 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004425 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004426 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004427 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004428 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004429
H. Peter Anvin8571f062019-09-23 16:40:03 -07004430 return bsii(tok_text(t), (const char **)conditions,
4431 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004432}
4433
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004434static inline bool pp_concat_match(const Token *t, unsigned int mask)
4435{
4436 return t && (PP_CONCAT_MASK(t->type) & mask);
4437}
4438
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004439/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004440 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004441 * pasting, if @handle_explicit passed then explicit pasting
4442 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004443 * The @m array can contain a series of token types which are
4444 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004445 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004446static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004447 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004448{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004449 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004450 bool pasted = false;
4451 char *buf, *p;
4452 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004453
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004454 /*
4455 * The last token before pasting. We need it
4456 * to be able to connect new handled tokens.
4457 * In other words if there were a tokens stream
4458 *
4459 * A -> B -> C -> D
4460 *
4461 * and we've joined tokens B and C, the resulting
4462 * stream should be
4463 *
4464 * A -> BC -> D
4465 */
4466 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004467 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004468
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004469 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004470 prev_nonspace = NULL;
4471
4472 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004473 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004474
4475 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004476 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004477 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004478 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004479 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004480
4481 case TOK_PASTE:
4482 /* Explicit pasting */
4483 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004484 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004485
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004486 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004487 if (!prev_nonspace) {
4488 nasm_nonfatal("No lvalue found on pasting");
4489 tok = delete_Token(tok);
4490 break;
4491 }
4492
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004493 did_paste = true;
4494
4495 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004496 t = *prev_nonspace;
4497
4498 /* Delete leading whitespace */
4499 next = zap_white(t->next);
4500
4501 /* Delete the %+ token itself */
4502 nasm_assert(next == tok);
4503 next = delete_Token(next);
4504
4505 /* Delete trailing whitespace */
4506 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004507
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004508 /*
4509 * No ending token, this might happen in two
4510 * cases
4511 *
4512 * 1) There indeed no right token at all
4513 * 2) There is a bare "%define ID" statement,
4514 * and @ID does expand to whitespace.
4515 *
4516 * So technically we need to do a grammar analysis
4517 * in another stage of parsing, but for now lets don't
4518 * change the behaviour people used to. Simply allow
4519 * whitespace after paste token.
4520 */
4521 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004522 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004523 break;
4524 }
4525
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004526 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004527 p = mempcpy(p, tok_text(t), t->len);
4528 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004529 *p = '\0';
4530 delete_Token(t);
4531 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004532 nasm_free(buf);
4533
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004534 if (unlikely(!t)) {
4535 /*
4536 * No output at all? Replace with a single whitespace.
4537 * This should never happen.
4538 */
4539 t = new_White(NULL);
4540 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004541
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004542 *prev_nonspace = tok = t;
4543 while (t->next)
4544 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004545
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004546 /* Delete the second token and attach to the end of the list */
4547 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004548
4549 /* We want to restart from the head of the pasted token */
4550 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004551 break;
4552
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004553 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004554 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004555 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004556 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004557 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004558 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004559
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004560 if (i >= mnum)
4561 break;
4562
4563 len = tok->len;
4564 while (pp_concat_match(next, m[i].mask_tail)) {
4565 len += next->len;
4566 next = next->next;
4567 }
4568
4569 /* No match or no text to process */
4570 if (len == tok->len)
4571 break;
4572
4573 p = buf = nasm_malloc(len + 1);
4574 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004575 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004576 tok = delete_Token(tok);
4577 }
4578 *p = '\0';
4579 *prev_next = tok = t = tokenize(buf);
4580 nasm_free(buf);
4581
4582 /*
4583 * Connect pasted into original stream,
4584 * ie A -> new-tokens -> B
4585 */
4586 while (t->next)
4587 t = t->next;
4588 t->next = next;
4589 prev_next = prev_nonspace = &t->next;
4590 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004591 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004592 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004593
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004594 if (did_paste) {
4595 pasted = true;
4596 } else {
4597 prev_next = &tok->next;
4598 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4599 prev_nonspace = prev_next;
4600 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004601
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004602 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004603 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004604
4605 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004606}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004607
4608/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004609 * Computes the proper rotation of mmacro parameters
4610 */
4611static int mmac_rotate(const MMacro *mac, unsigned int n)
4612{
4613 if (--n < mac->nparam)
4614 n = (n + mac->rotate) % mac->nparam;
4615
4616 return n+1;
4617}
4618
4619/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004620 * expands to a list of tokens from %{x:y}
4621 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004622static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004623{
4624 Token *t = tline, **tt, *tm, *head;
4625 char *pos;
4626 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004627
H. Peter Anvin8571f062019-09-23 16:40:03 -07004628 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004629 nasm_assert(pos);
4630
4631 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004632 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004633
4634 /*
4635 * only macros params are accounted so
4636 * if someone passes %0 -- we reject such
4637 * value(s)
4638 */
4639 if (lst == 0 || fst == 0)
4640 goto err;
4641
4642 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004643 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4644 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004645 goto err;
4646
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004647 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4648 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004649
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004650 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004651 fst--, lst--;
4652
4653 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004654 * It will be at least one token. Note we
4655 * need to scan params until separator, otherwise
4656 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004657 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004658 j = (fst + mac->rotate) % mac->nparam;
4659 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004660 if (!tm)
4661 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004662 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004663 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004664 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004665 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004666 *tt = t, tt = &t->next, tm = tm->next;
4667 }
4668
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004669 if (fst < lst) {
4670 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004671 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004672 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004673 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004674 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004675 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004676 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004677 *tt = t, tt = &t->next, tm = tm->next;
4678 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004679 }
4680 } else {
4681 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004682 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004683 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004684 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004685 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004686 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004687 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004688 *tt = t, tt = &t->next, tm = tm->next;
4689 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004690 }
4691 }
4692
4693 *last = tt;
4694 return head;
4695
4696err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004697 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004698 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004699 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004700}
4701
H. Peter Anvin76690a12002-04-30 20:52:49 +00004702/*
4703 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004704 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004705 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004706 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004707static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004708{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004709 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004710 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004711 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004712
4713 tail = &thead;
4714 thead = NULL;
4715
H. Peter Anvine2c80182005-01-15 22:15:51 +00004716 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004717 bool change;
4718 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004719 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004720 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004721
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004722 tline = tline->next;
4723 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004724
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004725 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004726 case TOK_LOCAL_SYMBOL:
4727 type = TOK_ID;
4728 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4729 change = true;
4730 break;
4731 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004732 {
4733 Token *tt = NULL;
4734
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004735 change = true;
4736
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004737 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004738 nasm_nonfatal("`%s': not in a macro call", text);
4739 text = NULL;
4740 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004741 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004742
4743 if (strchr(text, ':')) {
4744 /*
4745 * seems we have a parameters range here
4746 */
4747 Token *head, **last;
4748 head = expand_mmac_params_range(mac, t, &last);
4749 if (head) {
4750 *tail = head;
4751 *last = tline;
4752 text = NULL;
4753 }
4754 break;
4755 }
4756
4757 switch (text[1]) {
4758 /*
4759 * We have to make a substitution of one of the
4760 * forms %1, %-1, %+1, %%foo, %0, %00.
4761 */
4762 case '0':
4763 if (!text[2]) {
4764 type = TOK_NUMBER;
4765 text = nasm_asprintf("%d", mac->nparam);
4766 break;
4767 }
4768 if (text[2] != '0' || text[3])
4769 goto invalid;
4770 /* a possible captured label == mac->params[0] */
4771 /* fall through */
4772 default:
4773 {
4774 unsigned long n;
4775 char *ep;
4776
4777 n = strtoul(text + 1, &ep, 10);
4778 if (unlikely(*ep))
4779 goto invalid;
4780
4781 if (n <= mac->nparam) {
4782 n = mmac_rotate(mac, n);
4783 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4784 }
4785 text = NULL;
4786 break;
4787 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004788 case '-':
4789 case '+':
4790 {
4791 int cc;
4792 unsigned long n;
4793 char *ep;
4794
4795 text = NULL;
4796
H. Peter Anvin8571f062019-09-23 16:40:03 -07004797 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004798 if (unlikely(*ep))
4799 goto invalid;
4800
4801 if (n && n < mac->nparam) {
4802 n = mmac_rotate(mac, n);
4803 tt = mac->params[n];
4804 }
4805 cc = find_cc(tt);
4806 if (cc == -1) {
4807 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004808 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004809 text = NULL;
4810 break;
4811 }
4812
4813 type = TOK_ID;
4814 if (text[1] == '-') {
4815 int ncc = inverse_ccs[cc];
4816 if (unlikely(ncc == -1)) {
4817 nasm_nonfatal("condition code `%s' is not invertible",
4818 conditions[cc]);
4819 break;
4820 }
4821 cc = ncc;
4822 }
4823 text = nasm_strdup(conditions[cc]);
4824 break;
4825 }
4826
4827 invalid:
4828 nasm_nonfatal("invalid macro parameter: `%s'", text);
4829 text = NULL;
4830 break;
4831 }
4832 break;
4833 }
4834
4835 case TOK_PREPROC_Q:
4836 if (mac) {
4837 type = TOK_ID;
4838 text = nasm_strdup(mac->iname);
4839 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004840 } else {
4841 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004842 }
4843 break;
4844
4845 case TOK_PREPROC_QQ:
4846 if (mac) {
4847 type = TOK_ID;
4848 text = nasm_strdup(mac->name);
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_INDIRECT:
4856 {
4857 Token *tt;
4858
H. Peter Anvin8571f062019-09-23 16:40:03 -07004859 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004860 tt = expand_mmac_params(tt);
4861 tt = expand_smacro(tt);
4862 /* Why dup_tlist() here? We should own tt... */
4863 dup_tlist(tt, &tail);
4864 text = NULL;
4865 change = true;
4866 break;
4867 }
4868
4869 default:
4870 change = false;
4871 break;
4872 }
4873
4874 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004875 if (!text) {
4876 delete_Token(t);
4877 } else {
4878 *tail = t;
4879 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004880 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004881 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004882 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004883 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004884 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004885 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004886 tail = &t->next;
4887 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004888 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004889
H. Peter Anvineba20a72002-04-30 20:53:55 +00004890 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004891
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004892 if (changed) {
4893 const struct tokseq_match t[] = {
4894 {
4895 PP_CONCAT_MASK(TOK_ID) |
4896 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4897 PP_CONCAT_MASK(TOK_ID) |
4898 PP_CONCAT_MASK(TOK_NUMBER) |
4899 PP_CONCAT_MASK(TOK_FLOAT) |
4900 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4901 },
4902 {
4903 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4904 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4905 }
4906 };
4907 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4908 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004909
H. Peter Anvin76690a12002-04-30 20:52:49 +00004910 return thead;
4911}
4912
H. Peter Anvin322bee02019-08-10 01:38:06 -07004913static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004914
H. Peter Anvin76690a12002-04-30 20:52:49 +00004915/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004916 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004917 * a macro at all, it is simply copied to the output and the pointer
4918 * advanced. tpp should be a pointer to a pointer (usually the next
4919 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004920 * to point to the first token of the expansion, and *tpp updated to
4921 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004922 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004923 * If the expansion is empty, *tpp will be unchanged but **tpp will
4924 * be advanced past the macro call.
4925 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004926 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004927 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004928static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004929{
4930 Token **params = NULL;
4931 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004932 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004933 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004934 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004935 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004936 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004937 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004938 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004939
4940 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004941 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004942
H. Peter Anvin8571f062019-09-23 16:40:03 -07004943 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004944
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004945 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004946 smacro_deadman.levels--;
4947
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004948 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004949 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004950 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004951 smacro_deadman.triggered = true;
4952 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004953 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004954 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004955 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004956 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004957 Context *ctx = get_ctx(mname, &mname);
4958 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4959 } else {
4960 goto not_a_macro;
4961 }
4962
4963 /*
4964 * We've hit an identifier of some sort. First check whether the
4965 * identifier is a single-line macro at all, then think about
4966 * checking for parameters if necessary.
4967 */
4968 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004969 if (unlikely(m->alias && !do_aliases))
4970 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004971 if (!mstrcmp(m->name, mname, m->casesense))
4972 break;
4973 }
4974
4975 if (!m) {
4976 goto not_a_macro;
4977 }
4978
4979 /* Parse parameters, if applicable */
4980
4981 params = NULL;
4982 nparam = 0;
4983
4984 if (m->nparam == 0) {
4985 /*
4986 * Simple case: the macro is parameterless.
4987 * Nothing to parse; the expansion code will
4988 * drop the macro name token.
4989 */
4990 } else {
4991 /*
4992 * Complicated case: at least one macro with this name
4993 * exists and takes parameters. We must find the
4994 * parameters in the call, count them, find the SMacro
4995 * that corresponds to that form of the macro call, and
4996 * substitute for the parameters when we expand. What a
4997 * pain.
4998 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004999 Token *t;
5000 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005001
5002 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005003 tline = skip_white(tline);
5004 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005005 /*
5006 * This macro wasn't called with parameters: ignore
5007 * the call. (Behaviour borrowed from gnu cpp.)
5008 */
5009 goto not_a_macro;
5010 }
5011
5012 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005013 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005014 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005015 t = tline; /* tline points to leading ( */
5016
5017 while (paren) {
5018 t = t->next;
5019
5020 if (!t) {
5021 nasm_nonfatal("macro call expects terminating `)'");
5022 goto not_a_macro;
5023 }
5024
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005025 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005026 continue;
5027
H. Peter Anvin8571f062019-09-23 16:40:03 -07005028 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005029 case ',':
5030 if (!brackets)
5031 nparam++;
5032 break;
5033
5034 case '{':
5035 brackets++;
5036 break;
5037
5038 case '}':
5039 if (brackets > 0)
5040 brackets--;
5041 break;
5042
5043 case '(':
5044 if (!brackets)
5045 paren++;
5046 break;
5047
5048 case ')':
5049 if (!brackets)
5050 paren--;
5051 break;
5052
5053 default:
5054 break; /* Normal token */
5055 }
5056 }
5057
5058 /*
5059 * Look for a macro matching in both name and parameter count.
5060 * We already know any matches cannot be anywhere before the
5061 * current position of "m", so there is no reason to
5062 * backtrack.
5063 */
5064 while (1) {
5065 if (!m) {
5066 /*!
5067 *!macro-params-single [on] single-line macro calls with wrong parameter count
5068 *! warns about \i{single-line macros} being invoked
5069 *! with the wrong number of parameters.
5070 */
5071 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
5072 "single-line macro `%s' exists, "
5073 "but not taking %d parameter%s",
5074 mname, nparam, (nparam == 1) ? "" : "s");
5075 goto not_a_macro;
5076 }
5077
5078 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005079 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005080 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005081 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005082 break; /* Also good */
5083 }
5084 m = m->next;
5085 }
5086 }
5087
5088 if (m->in_progress)
5089 goto not_a_macro;
5090
5091 /* Expand the macro */
5092 m->in_progress = true;
5093
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005094 if (nparam) {
5095 /* Extract parameters */
5096 Token **phead, **pep;
5097 int white = 0;
5098 int brackets = 0;
5099 int paren;
5100 bool bracketed = false;
5101 bool bad_bracket = false;
5102 enum sparmflags flags;
5103
5104 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005105 paren = 1;
5106 nasm_newn(params, nparam);
5107 i = 0;
5108 flags = m->params[i].flags;
5109 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005110 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005111
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005112 while (paren) {
5113 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005114 char ch;
5115
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005116 tline = tline->next;
5117
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005118 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005119 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005120
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005121 ch = 0;
5122 skip = false;
5123
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005124
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005125 switch (tline->type) {
5126 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005127 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005128 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005129 break;
5130
5131 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005132 if (!(flags & SPARM_NOSTRIP)) {
5133 if (brackets || *phead)
5134 white++; /* Keep interior whitespace */
5135 skip = true;
5136 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005137 break;
5138
5139 default:
5140 break;
5141 }
5142
5143 switch (ch) {
5144 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005145 if (!brackets && !(flags & SPARM_GREEDY)) {
5146 i++;
5147 nasm_assert(i < nparam);
5148 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005149 *pep = NULL;
5150 bracketed = false;
5151 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005152 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005153 }
5154 break;
5155
5156 case '{':
5157 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005158 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5159 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005160 }
5161 brackets++;
5162 break;
5163
5164 case '}':
5165 if (brackets > 0) {
5166 if (!--brackets)
5167 skip = bracketed;
5168 }
5169 break;
5170
5171 case '(':
5172 if (!brackets)
5173 paren++;
5174 break;
5175
5176 case ')':
5177 if (!brackets) {
5178 paren--;
5179 if (!paren) {
5180 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005181 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005182 }
5183 }
5184 break;
5185
5186 default:
5187 break; /* Normal token */
5188 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005189
5190 if (!skip) {
5191 Token *t;
5192
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005193 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005194
5195 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005196 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005197 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005198 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005199 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005200 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005201 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005202 }
5203 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005204
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005205 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005206 * Possible further processing of parameters. Note that the
5207 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005208 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005209 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005210 enum sparmflags flags = m->params[i].flags;
5211
5212 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005213 /* Evaluate this parameter as a number */
5214 struct ppscan pps;
5215 struct tokenval tokval;
5216 expr *evalresult;
5217 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005218
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005219 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5220 pps.ntokens = -1;
5221 tokval.t_type = TOKEN_INVALID;
5222 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005223
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005224 free_tlist(eval_param);
5225 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005226
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005227 if (!evalresult) {
5228 /* Nothing meaningful to do */
5229 } else if (tokval.t_type) {
5230 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5231 } else if (!is_simple(evalresult)) {
5232 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5233 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005234 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005235 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005236 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005237
5238 if (flags & SPARM_STR) {
5239 /* Convert expansion to a quoted string */
5240 char *arg;
5241 Token *qs;
5242
5243 qs = expand_smacro_noreset(params[i]);
5244 arg = detoken(qs, false);
5245 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005246 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005247 nasm_free(arg);
5248 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005249 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005250 }
5251
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005252 /* Note: we own the expansion this returns. */
5253 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005254
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005255 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005256 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005257 tline = tafter;
5258
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005259 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005260 cond_comma = false;
5261
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005262 while (t) {
5263 enum pp_token_type type = t->type;
5264 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005265
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005266 switch (type) {
5267 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005268 delete_Token(t);
5269 t = dup_Token(tline, mstart);
5270 break;
5271
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005272 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005273 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005274 size_t mlen = strlen(m->name);
5275 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005276 char *p;
5277
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005278 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005279 if (t->type == TOK_LOCAL_MACRO) {
5280 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005281 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005282 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005283
H. Peter Anvin8571f062019-09-23 16:40:03 -07005284 psp = tok_text(mstart);
5285 get_ctx(psp, &pep);
5286 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005287
H. Peter Anvin8571f062019-09-23 16:40:03 -07005288 len = mlen + plen;
5289 p = nasm_malloc(len + 1);
5290 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005291 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005292 len = mlen;
5293 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005294 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005295 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005296 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005297 set_text_free(t, p, len);
5298
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005299 t->next = tline;
5300 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005301 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005302
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005303 case TOK_COND_COMMA:
5304 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005305 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005306 break;
5307
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005308 case TOK_ID:
5309 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005310 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005311 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005312 /*
5313 * Chain this into the target line *before* expanding,
5314 * that way we pick up any arguments to the new macro call,
5315 * if applicable.
5316 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005317 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005318 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005319 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005320 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005321 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005322 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005323 default:
5324 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005325 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005326 nasm_assert(!tup && param < nparam);
5327 delete_Token(t);
5328 t = NULL;
5329 tup = tnext;
5330 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005331 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005332 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005333 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005334 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005335 }
5336
5337 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005338 Token *endt = tline;
5339
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005340 tline = t;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005341 while (!cond_comma && t && t != endt)
5342 cond_comma = t->type != TOK_WHITESPACE;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005343 }
5344
5345 if (tnext) {
5346 t = tnext;
5347 } else {
5348 t = tup;
5349 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005350 }
5351 }
5352
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005353 **tpp = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005354 for (t = tline; t != tafter; t = t->next)
5355 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005356
5357 m->in_progress = false;
5358
5359 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005360 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005361 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005362
5363 /*
5364 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005365 * and then advance to the next input token. Note that this is
5366 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005367 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005368not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005369 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005370 m = NULL;
5371done:
5372 smacro_deadman.levels++;
5373 if (unlikely(params))
5374 free_tlist_array(params, nparam);
5375 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005376}
5377
5378/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005379 * Expand all single-line macro calls made in the given line.
5380 * Return the expanded version of the line. The original is deemed
5381 * to be destroyed in the process. (In reality we'll just move
5382 * Tokens from input to output a lot of the time, rather than
5383 * actually bothering to destroy and replicate.)
5384 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005385static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005386{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005387 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005388 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5389 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005390 return expand_smacro_noreset(tline);
5391}
5392
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005393static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005394{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005395 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005396 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005397
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005398 if (!org_tline)
5399 return NULL; /* Empty input */
5400
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005401 /*
5402 * Trick: we should avoid changing the start token pointer since it can
5403 * be contained in "next" field of other token. Because of this
5404 * we allocate a copy of first token and work with it; at the end of
5405 * routine we copy it back
5406 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005407 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005408
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005409 /*
5410 * Pretend that we always end up doing expansion on the first pass;
5411 * that way %+ get processed. However, if we process %+ before the
5412 * first pass, we end up with things like MACRO %+ TAIL trying to
5413 * look up the macro "MACROTAIL", which we don't want.
5414 */
5415 expanded = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005416 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005417 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005418 {
5419 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005420 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5421 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005422 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5423 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005424 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5425 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005426 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5427 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5428 }
5429 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005430 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005431
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005432 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005433 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005434
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005435 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005436 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005437
5438 /*
5439 * Now scan the entire line and look for successive TOK_IDs
5440 * that resulted after expansion (they can't be produced by
5441 * tokenize()). The successive TOK_IDs should be concatenated.
5442 * Also we look for %+ tokens and concatenate the tokens
5443 * before and after them (without white spaces in between).
5444 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005445 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5446 break; /* Done again! */
5447
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005448 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005449 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005450
H. Peter Anvin8571f062019-09-23 16:40:03 -07005451 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005452 /*
5453 * The expression expanded to empty line;
5454 * we can't return NULL because of the "trick" above.
5455 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005456 */
5457
5458 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005459 }
5460
H. Peter Anvin8571f062019-09-23 16:40:03 -07005461 steal_Token(org_tline, tline);
5462 org_tline->next = tline->next;
5463 delete_Token(tline);
5464
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005465 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005466}
5467
5468/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005469 * Similar to expand_smacro but used exclusively with macro identifiers
5470 * right before they are fetched in. The reason is that there can be
5471 * identifiers consisting of several subparts. We consider that if there
5472 * are more than one element forming the name, user wants a expansion,
5473 * otherwise it will be left as-is. Example:
5474 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005475 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005476 *
5477 * the identifier %$abc will be left as-is so that the handler for %define
5478 * will suck it and define the corresponding value. Other case:
5479 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005480 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005481 *
5482 * In this case user wants name to be expanded *before* %define starts
5483 * working, so we'll expand %$abc into something (if it has a value;
5484 * otherwise it will be left as-is) then concatenate all successive
5485 * PP_IDs into one.
5486 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005487static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005488{
5489 Token *cur, *oldnext = NULL;
5490
H. Peter Anvin734b1882002-04-30 21:01:08 +00005491 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005492 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005493
5494 cur = tline;
5495 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005496 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5497 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005498 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005499
5500 /* If identifier consists of just one token, don't expand */
5501 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005502 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005503
H. Peter Anvine2c80182005-01-15 22:15:51 +00005504 if (cur) {
5505 oldnext = cur->next; /* Detach the tail past identifier */
5506 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005507 }
5508
H. Peter Anvin734b1882002-04-30 21:01:08 +00005509 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005510
H. Peter Anvine2c80182005-01-15 22:15:51 +00005511 if (cur) {
5512 /* expand_smacro possibly changhed tline; re-scan for EOL */
5513 cur = tline;
5514 while (cur && cur->next)
5515 cur = cur->next;
5516 if (cur)
5517 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005518 }
5519
5520 return tline;
5521}
5522
5523/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005524 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005525 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005526 * to check for an initial label - that's taken care of in
5527 * expand_mmacro - but must check numbers of parameters. Guaranteed
5528 * to be called with tline->type == TOK_ID, so the putative macro
5529 * name is easy to find.
5530 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005531static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005532{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005533 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005534 Token **params;
5535 int nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005536 const char *finding = tok_text(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005537
H. Peter Anvin8571f062019-09-23 16:40:03 -07005538 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005539
5540 /*
5541 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005542 * name which isn't already excluded by macro cycle removal.
5543 * (The cycle removal test here helps optimize the case of wrapping
5544 * instructions, and is cheap to do here.)
5545 *
5546 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005547 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005548 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005549 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005550 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005551 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005552 (m->in_progress != 1 || m->max_depth > 0))
5553 break; /* Found something that needs consideration */
5554 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005555 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005556 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005557
5558 /*
5559 * OK, we have a potential macro. Count and demarcate the
5560 * parameters.
5561 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005562 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005563
5564 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005565 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005566 * structure that handles this number.
5567 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005568 while (m) {
5569 if (m->nparam_min <= nparam
5570 && (m->plus || nparam <= m->nparam_max)) {
5571 /*
5572 * This one is right. Just check if cycle removal
5573 * prohibits us using it before we actually celebrate...
5574 */
5575 if (m->in_progress > m->max_depth) {
5576 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005577 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005578 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005579 }
5580 nasm_free(params);
5581 return NULL;
5582 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005583 /*
5584 * It's right, and we can use it. Add its default
5585 * parameters to the end of our list if necessary.
5586 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005587 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005588 int newnparam = m->nparam_min + m->ndefs;
5589 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5590 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5591 (newnparam - nparam) * sizeof(*params));
5592 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005593 }
5594 /*
5595 * If we've gone over the maximum parameter count (and
5596 * we're in Plus mode), ignore parameters beyond
5597 * nparam_max.
5598 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005599 if (m->plus && nparam > m->nparam_max)
5600 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005601
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005602 /*
5603 * If nparam was adjusted above, make sure the list is still
5604 * NULL-terminated.
5605 */
5606 params[nparam+1] = NULL;
5607
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005608 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005609 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005610 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005611 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005612 }
5613 /*
5614 * This one wasn't right: look for the next one with the
5615 * same name.
5616 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005617 list_for_each(m, m->next)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005618 if (!mstrcmp(m->name, tok_text(tline), m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005619 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005620 }
5621
5622 /*
5623 * After all that, we didn't find one with the right number of
5624 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005625 *!
5626 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5627 *! warns about \i{multi-line macros} being invoked
5628 *! with the wrong number of parameters. See \k{mlmacover} for an
5629 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005630 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005631 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5632 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005633 tok_text(tline), nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005634 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005635 return NULL;
5636}
5637
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005638
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005639#if 0
5640
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005641/*
5642 * Save MMacro invocation specific fields in
5643 * preparation for a recursive macro expansion
5644 */
5645static void push_mmacro(MMacro *m)
5646{
5647 MMacroInvocation *i;
5648
5649 i = nasm_malloc(sizeof(MMacroInvocation));
5650 i->prev = m->prev;
5651 i->params = m->params;
5652 i->iline = m->iline;
5653 i->nparam = m->nparam;
5654 i->rotate = m->rotate;
5655 i->paramlen = m->paramlen;
5656 i->unique = m->unique;
5657 i->condcnt = m->condcnt;
5658 m->prev = i;
5659}
5660
5661
5662/*
5663 * Restore MMacro invocation specific fields that were
5664 * saved during a previous recursive macro expansion
5665 */
5666static void pop_mmacro(MMacro *m)
5667{
5668 MMacroInvocation *i;
5669
5670 if (m->prev) {
5671 i = m->prev;
5672 m->prev = i->prev;
5673 m->params = i->params;
5674 m->iline = i->iline;
5675 m->nparam = i->nparam;
5676 m->rotate = i->rotate;
5677 m->paramlen = i->paramlen;
5678 m->unique = i->unique;
5679 m->condcnt = i->condcnt;
5680 nasm_free(i);
5681 }
5682}
5683
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005684#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005685
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005686/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005687 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005688 */
5689static void list_mmacro_call(const MMacro *m)
5690{
5691 const char prefix[] = " ;;; [macro] ";
5692 size_t namelen, size;
5693 char *buf, *p;
5694 unsigned int i;
5695 const Token *t;
5696
5697 namelen = strlen(m->iname);
5698 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5699
5700 for (i = 1; i <= m->nparam; i++) {
5701 int j = 0;
5702 size += 3; /* Braces and space/comma */
5703 list_for_each(t, m->params[i]) {
5704 if (j++ >= m->paramlen[i])
5705 break;
5706 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5707 }
5708 }
5709
5710 buf = p = nasm_malloc(size);
5711 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5712 p = mempcpy(p, m->iname, namelen);
5713 *p++ = ' ';
5714
5715 for (i = 1; i <= m->nparam; i++) {
5716 int j = 0;
5717 *p++ = '{';
5718 list_for_each(t, m->params[i]) {
5719 if (j++ >= m->paramlen[i])
5720 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005721 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005722 }
5723 *p++ = '}';
5724 *p++ = ',';
5725 }
5726
5727 *--p = '\0'; /* Replace last delimeter with null */
5728 lfmt->line(LIST_MACRO, -1, buf);
5729 nasm_free(buf);
5730}
5731
5732/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005733 * Expand the multi-line macro call made by the given line, if
5734 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005735 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005736 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005737static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005738{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005739 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005740 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005741 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005742 Token **params, *t, *tt;
5743 MMacro *m;
5744 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005745 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005746 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005747 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005748
5749 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005750 t = skip_white(t);
5751 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005752 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005753 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005754 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005755 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005756 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07005757 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005758 Token *last;
5759 /*
5760 * We have an id which isn't a macro call. We'll assume
5761 * it might be a label; we'll also check to see if a
5762 * colon follows it. Then, if there's another id after
5763 * that lot, we'll check it again for macro-hood.
5764 */
5765 label = last = t;
5766 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005767 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005768 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005769 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005770 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005771 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005772 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005773 last = t, t = t->next;
5774 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005775 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005776 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005777 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005778 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005779 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005780 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005781
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005782 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5783 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5784 if (!mmacro_deadman.triggered) {
5785 nasm_nonfatal("interminable multiline macro recursion");
5786 mmacro_deadman.triggered = true;
5787 }
5788 return 0;
5789 }
5790
5791 mmacro_deadman.total++;
5792 mmacro_deadman.levels++;
5793
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005794 /*
5795 * Fix up the parameters: this involves stripping leading and
5796 * trailing whitespace, then stripping braces if they are
5797 * present.
5798 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005799 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005800
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005801 nasm_assert(params[nparam+1] == NULL);
5802
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005803 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005804 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005805 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005806 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005807 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005808
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005809 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005810 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005811 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005812 brace = 1;
5813 braced = true;
5814 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005815 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005816
5817 params[i] = t;
5818 for (; t; t = t->next) {
5819 if (tok_white(t)) {
5820 white++;
5821 continue;
5822 }
5823
5824 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005825 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005826 case ',':
5827 if (comma && !brace)
5828 goto endparam;
5829 break;
5830
5831 case '{':
5832 brace++;
5833 break;
5834
5835 case '}':
5836 brace--;
5837 if (braced && !brace) {
5838 paramlen[i] += white;
5839 goto endparam;
5840 }
5841 break;
5842
5843 default:
5844 break;
5845 }
5846 }
5847
5848 paramlen[i] += white + 1;
5849 white = 0;
5850 }
5851 endparam:
5852 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005853 }
5854
5855 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005856 * OK, we have a MMacro structure together with a set of
5857 * parameters. We must now go through the expansion and push
5858 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005859 * parameter tokens and macro-local tokens doesn't get done
5860 * until the single-line macro substitution process; this is
5861 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005862 * later through %rotate and give the right semantics for
5863 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005864 *
5865 * First, push an end marker on to istk->expansion, mark this
5866 * macro as in progress, and set up its invocation-specific
5867 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005868 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005869 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005870 ll->next = istk->expansion;
5871 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005872 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005873
5874 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005875 * Save the previous MMacro expansion in the case of
5876 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005877 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005878#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005879 if (m->max_depth && m->in_progress)
5880 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005881#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005882
5883 m->in_progress ++;
5884 m->params = params;
5885 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005886 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005887 m->nparam = nparam;
5888 m->rotate = 0;
5889 m->paramlen = paramlen;
5890 m->unique = unique++;
5891 m->lineno = 0;
5892 m->condcnt = 0;
5893
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005894 m->mstk = istk->mstk;
5895 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005896
5897 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005898 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005899 ll->next = istk->expansion;
5900 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005901 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005902 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005903
5904 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005905 * If we had a label, and this macro definition does not include
5906 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005907 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005908 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005909 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005910 /*
5911 * We had a label. If this macro contains an %00 parameter,
5912 * save the value as a special parameter (which is what it
5913 * is), otherwise push it as the first line of the macro
5914 * expansion.
5915 */
5916 if (m->capture_label) {
5917 params[0] = dup_Token(NULL, label);
5918 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005919 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005920 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005921 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005922 ll->finishes = NULL;
5923 ll->next = istk->expansion;
5924 istk->expansion = ll;
5925 ll->first = startline;
5926 if (!dont_prepend) {
5927 while (label->next)
5928 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005929 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005930 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005931 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005932 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005933
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005934 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005935
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005936 if (list_option('m') && !m->nolist)
5937 list_mmacro_call(m);
5938
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005939 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005940}
5941
H. Peter Anvin130736c2016-02-17 20:27:41 -08005942/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005943 * This function decides if an error message should be suppressed.
5944 * It will never be called with a severity level of ERR_FATAL or
5945 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005946 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005947static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005948{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005949 /*
5950 * If we're in a dead branch of IF or something like it, ignore the error.
5951 * However, because %else etc are evaluated in the state context
5952 * of the previous branch, errors might get lost:
5953 * %if 0 ... %else trailing garbage ... %endif
5954 * So %else etc should set the ERR_PP_PRECOND flag.
5955 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005956 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005957 ((severity & ERR_PP_PRECOND) ?
5958 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005959 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005960 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005961
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005962 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005963}
5964
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005965static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005966stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005967{
5968 (void)s;
5969 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005970 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005971
H. Peter Anvin8571f062019-09-23 16:40:03 -07005972 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08005973}
5974
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005975static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005976stdmac_line(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_num(NULL, src_get_linnum());
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_bits(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, globalbits);
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_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005997{
H. Peter Anvin8b262472019-02-26 14:00:54 -08005998 (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
6002 switch (globalbits) {
6003 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006004 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006005 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006006 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006007 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006008 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006009 default:
6010 panic();
6011 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006012}
6013
H. Peter Anvin8b262472019-02-26 14:00:54 -08006014/* Add magic standard macros */
6015struct magic_macros {
6016 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006017 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006018 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006019};
6020static const struct magic_macros magic_macros[] =
6021{
H. Peter Anvind2354082019-08-27 16:38:48 -07006022 { "__?FILE?__", 0, stdmac_file },
6023 { "__?LINE?__", 0, stdmac_line },
6024 { "__?BITS?__", 0, stdmac_bits },
6025 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006026 { NULL, 0, NULL }
6027};
6028
6029static void pp_add_magic_stdmac(void)
6030{
6031 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006032 SMacro tmpl;
6033
6034 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006035
6036 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006037 tmpl.nparam = m->nparam;
6038 tmpl.expand = m->func;
6039 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006040 }
6041}
6042
H. Peter Anvin734b1882002-04-30 21:01:08 +00006043static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006044pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006045{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006046 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006047 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006048
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006049 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006050 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006051 nested_mac_count = 0;
6052 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006053 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006054 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006055 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006056 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07006057 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07006058
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006059 if (!use_loaded)
6060 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6061 memset(use_loaded, 0, use_package_count * sizeof(bool));
6062
H. Peter Anvin6686de22019-08-10 05:33:14 -07006063 /* First set up the top level input file */
6064 nasm_new(istk);
6065 istk->fp = nasm_open_read(file, NF_TEXT);
6066 src_set(0, file);
6067 istk->lineinc = 1;
6068 if (!istk->fp)
6069 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6070
6071 strlist_add(deplist, file);
6072
6073 /*
6074 * Set up the stdmac packages as a virtual include file,
6075 * indicated by a null file pointer.
6076 */
6077 nasm_new(inc);
6078 inc->next = istk;
6079 inc->fname = src_set_fname(NULL);
6080 inc->nolist = !list_option('b');
6081 istk = inc;
6082 lfmt->uplevel(LIST_INCLUDE, 0);
6083
H. Peter Anvin8b262472019-02-26 14:00:54 -08006084 pp_add_magic_stdmac();
6085
H. Peter Anvinf7606612016-07-13 14:23:48 -07006086 if (tasm_compatible_mode)
6087 pp_add_stdmac(nasm_stdmac_tasm);
6088
6089 pp_add_stdmac(nasm_stdmac_nasm);
6090 pp_add_stdmac(nasm_stdmac_version);
6091
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006092 if (extrastdmac)
6093 pp_add_stdmac(extrastdmac);
6094
H. Peter Anvinf7606612016-07-13 14:23:48 -07006095 stdmacpos = stdmacros[0];
6096 stdmacnext = &stdmacros[1];
6097
H. Peter Anvind2456592008-06-19 15:04:18 -07006098 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006099
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006100 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006101 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006102 * other builtins, because it is special -- it varies between
6103 * passes -- but there is really no particular reason to make it
6104 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006105 *
6106 * 0 = dependencies only
6107 * 1 = preparatory passes
6108 * 2 = final pass
6109 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006110 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006111 switch (mode) {
6112 case PP_NORMAL:
6113 apass = pass_final() ? 2 : 1;
6114 break;
6115 case PP_DEPS:
6116 apass = 0;
6117 break;
6118 case PP_PREPROC:
6119 apass = 3;
6120 break;
6121 default:
6122 panic();
6123 }
6124
H. Peter Anvin8571f062019-09-23 16:40:03 -07006125 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006126}
6127
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006128static void pp_init(void)
6129{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006130}
6131
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006132/*
6133 * Get a line of tokens. If we popped the macro expansion/include stack,
6134 * we return a pointer to the dummy token tok_pop; at that point if
6135 * istk is NULL then we have reached end of input;
6136 */
6137static Token tok_pop; /* Dummy token placeholder */
6138
6139static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006140{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006141 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006142 Line *l = istk->expansion;
6143 Token *tline = NULL;
6144 Token *dtline;
6145
H. Peter Anvine2c80182005-01-15 22:15:51 +00006146 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006147 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006148 * buffer or from the input file.
6149 */
6150 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006151 while (l && l->finishes) {
6152 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006153
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006154 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006155 /*
6156 * This is a macro-end marker for a macro with no
6157 * name, which means it's not really a macro at all
6158 * but a %rep block, and the `in_progress' field is
6159 * more than 1, meaning that we still need to
6160 * repeat. (1 means the natural last repetition; 0
6161 * means termination by %exitrep.) We have
6162 * therefore expanded up to the %endrep, and must
6163 * push the whole block on to the expansion buffer
6164 * again. We don't bother to remove the macro-end
6165 * marker: we'd only have to generate another one
6166 * if we did.
6167 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006168 fm->in_progress--;
6169 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006170 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006171 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006172
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006173 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006174 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006175 tail = &ll->first;
6176
6177 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006178 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006179 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006180 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006181 }
6182 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006183 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006184 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006185 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006186 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006187 MMacro *m = istk->mstk.mstk;
6188
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006189 /*
6190 * Check whether a `%rep' was started and not ended
6191 * within this macro expansion. This can happen and
6192 * should be detected. It's a fatal error because
6193 * I'm too confused to work out how to recover
6194 * sensibly from it.
6195 */
6196 if (defining) {
6197 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006198 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006199 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006200 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006201 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006202 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006203
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006204 /*
6205 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006206 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006207 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006208 istk->mstk = m->mstk;
6209 if (m->name) {
6210 /*
6211 * This was a real macro call, not a %rep, and
6212 * therefore the parameter information needs to
6213 * be freed and the iteration count/nesting
6214 * depth adjusted.
6215 */
6216
6217 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006218 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006219 * If all mmacro processing done,
6220 * clear all counters and the deadman
6221 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006222 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006223 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006224 }
6225
Adam Majer91e72402017-07-25 10:42:01 +02006226#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006227 if (m->prev) {
6228 pop_mmacro(m);
6229 fm->in_progress --;
6230 } else
Adam Majer91e72402017-07-25 10:42:01 +02006231#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006232 {
6233 nasm_free(m->params);
6234 free_tlist(m->iline);
6235 nasm_free(m->paramlen);
6236 fm->in_progress = 0;
6237 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006238 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006239
6240 /*
6241 * FIXME It is incorrect to always free_mmacro here.
6242 * It leads to usage-after-free.
6243 *
6244 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6245 */
6246#if 0
6247 else
6248 free_mmacro(m);
6249#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006250 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006251 istk->expansion = l->next;
6252 nasm_free(l);
6253 lfmt->downlevel(LIST_MACRO);
6254 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006255 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006256
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006257 do { /* until we get a line we can use */
6258 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006259
6260 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006261 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006262 int32_t lineno;
6263
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006264 if (istk->mstk.mstk) {
6265 istk->mstk.mstk->lineno++;
6266 if (istk->mstk.mstk->fname)
6267 lineno = istk->mstk.mstk->lineno +
6268 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006269 else
6270 lineno = 0; /* Defined at init time or builtin */
6271 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006272 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006273 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006274
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006275 tline = l->first;
6276 istk->expansion = l->next;
6277 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006278
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006279 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006280 if (!istk->nolist)
6281 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006282 nasm_free(line);
6283 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006284 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006285 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006286 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006287 } else {
6288 /*
6289 * The current file has ended; work down the istk
6290 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006291 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006292 if (i->fp)
6293 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006294 if (i->conds) {
6295 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006296 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006297 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006298 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006299 if (i->next)
6300 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006301 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006302 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006303 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006304 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006305 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006306 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006307
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006308 /*
6309 * We must expand MMacro parameters and MMacro-local labels
6310 * _before_ we plunge into directive processing, to cope
6311 * with things like `%define something %1' such as STRUC
6312 * uses. Unless we're _defining_ a MMacro, in which case
6313 * those tokens should be left alone to go into the
6314 * definition; and unless we're in a non-emitting
6315 * condition, in which case we don't want to meddle with
6316 * anything.
6317 */
6318 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006319 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006320 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006321 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006322
H. Peter Anvine2c80182005-01-15 22:15:51 +00006323 /*
6324 * Check the line to see if it's a preprocessor directive.
6325 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006326 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6327 if (dtline)
6328 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006329 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006330 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006331 * We're defining a multi-line macro. We emit nothing
6332 * at all, and just
6333 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006334 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006335 MMacro *mmac = defining->dstk.mmac;
6336
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006337 Line *l = nasm_malloc(sizeof(Line));
6338 l->next = defining->expansion;
6339 l->first = tline;
6340 l->finishes = NULL;
6341 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006342
6343 /*
6344 * Remember if this mmacro expansion contains %00:
6345 * if it does, we will have to handle leading labels
6346 * specially.
6347 */
6348 if (mmac) {
6349 const Token *t;
6350 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006351 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006352 mmac->capture_label = true;
6353 }
6354 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006355 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006356 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006357 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006358 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006359 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006360 * directive so we keep our place correctly.
6361 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006362 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006363 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006364 /*
6365 * We're in a %rep block which has been terminated, so
6366 * we're walking through to the %endrep without
6367 * emitting anything. Emit nothing at all, not even a
6368 * blank line: when we emerge from the %rep block we'll
6369 * give a line-number directive so we keep our place
6370 * correctly.
6371 */
6372 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006373 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006374 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006375 if (!expand_mmacro(tline))
6376 return tline;
6377 }
6378 }
6379}
6380
6381static char *pp_getline(void)
6382{
6383 char *line = NULL;
6384 Token *tline;
6385
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006386 while (true) {
6387 tline = pp_tokline();
6388 if (tline == &tok_pop) {
6389 /*
6390 * We popped the macro/include stack. If istk is empty,
6391 * we are at end of input, otherwise just loop back.
6392 */
6393 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006394 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006395 } else {
6396 /*
6397 * De-tokenize the line and emit it.
6398 */
6399 line = detoken(tline, true);
6400 free_tlist(tline);
6401 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006402 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006403 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006404
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006405 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006406 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006407 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006408 nasm_free(buf);
6409 }
6410
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006411 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006412}
6413
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006414static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006415{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006416 if (defining) {
6417 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006418 nasm_nonfatal("end of file while still defining macro `%s'",
6419 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006420 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006421 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006422 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006423
6424 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006425 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006426 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006427
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006428 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006429 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006430 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006431 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006432 Include *i = istk;
6433 istk = istk->next;
6434 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006435 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006436 }
6437 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006438 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006439 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006440}
6441
6442static void pp_cleanup_session(void)
6443{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006444 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006445 free_llist(predef);
6446 predef = NULL;
6447 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006448 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006449}
6450
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006451static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006452{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006453 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006454}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006455
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006456static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006457{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006458 Token *inc, *space, *name;
6459 Line *l;
6460
H. Peter Anvin734b1882002-04-30 21:01:08 +00006461 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006462 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006463 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006464
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006465 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006466 l->next = predef;
6467 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006468 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006469 predef = l;
6470}
6471
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006472static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006473{
6474 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006475 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006476 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006477
6478 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006479 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006480 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006481 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006482 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006483 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006484 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006485 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006486
H. Peter Anvin8571f062019-09-23 16:40:03 -07006487 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006488 if (space->next->type != TOK_PREPROC_ID &&
6489 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006490 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006491
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006492 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006493 l->next = predef;
6494 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006495 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006496 predef = l;
6497}
6498
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006499static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006500{
6501 Token *def, *space;
6502 Line *l;
6503
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006504 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006505 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006506 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006507
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006508 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006509 l->next = predef;
6510 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006511 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006512 predef = l;
6513}
6514
H. Peter Anvin05990342018-06-11 13:32:42 -07006515/* Insert an early preprocessor command that doesn't need special handling */
6516static void pp_pre_command(const char *what, char *string)
6517{
6518 char *cmd;
6519 Token *def, *space;
6520 Line *l;
6521
6522 def = tokenize(string);
6523 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006524 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006525 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6526 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6527 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006528 }
6529
6530 l = nasm_malloc(sizeof(Line));
6531 l->next = predef;
6532 l->first = def;
6533 l->finishes = NULL;
6534 predef = l;
6535}
6536
H. Peter Anvinf7606612016-07-13 14:23:48 -07006537static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006538{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006539 macros_t **mp;
6540
6541 /* Find the end of the list and avoid duplicates */
6542 for (mp = stdmacros; *mp; mp++) {
6543 if (*mp == macros)
6544 return; /* Nothing to do */
6545 }
6546
6547 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6548
6549 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006550}
6551
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006552static void pp_extra_stdmac(macros_t *macros)
6553{
6554 extrastdmac = macros;
6555}
6556
H. Peter Anvin8571f062019-09-23 16:40:03 -07006557/* Create a numeric token */
6558static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006559{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006560 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006561 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006562 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006563}
6564
H. Peter Anvin8571f062019-09-23 16:40:03 -07006565/* Create a quoted string token */
6566static Token *make_tok_qstr(Token *next, const char *str)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006567{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006568 size_t len = strlen(str);
6569 char *p = nasm_quote(str, &len);
6570 return new_Token_free(next, TOK_STRING, p, len);
6571}
6572
6573/* Create a single-character operator token */
6574static Token *make_tok_char(Token *next, char op)
6575{
6576 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6577 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006578 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006579}
6580
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006581static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006582{
6583 if (!m)
6584 return;
6585
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006586 /* We need to print the mstk.mmac list in reverse order */
6587 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006588
6589 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006590 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006591 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006592 }
6593}
6594
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006595static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006596{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006597 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006598
H. Peter Anvinddb29062018-12-11 00:06:29 -08006599 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6600 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006601
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006602 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006603 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006604
H. Peter Anvinddb29062018-12-11 00:06:29 -08006605 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006606}
6607
H. Peter Anvine7469712016-02-18 02:20:59 -08006608const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006609 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006610 pp_reset,
6611 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006612 pp_cleanup_pass,
6613 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006614 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006615 pp_pre_define,
6616 pp_pre_undefine,
6617 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006618 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006619 pp_include_path,
6620 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006621 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006622};