blob: 785d6e306c7480d47d2ed0932d16220e23450fb1 [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 {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004030 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004031 macro_start = NULL;
4032 t = tline;
4033 while (t) {
4034 if (t->type == TOK_ID) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004035 const char *ttext = tok_text(t);
4036 size_t tlen = t->len;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004037 for (i = 0; i < nparam; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004038 if (tmpl.params[i].name.len == t->len &&
4039 !memcmp(ttext, tok_text(&tmpl.params[i].name), tlen)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004040 t->type = tok_smac_param(i);
4041 break;
4042 }
4043 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004044 }
4045 tt = t->next;
4046 t->next = macro_start;
4047 macro_start = t;
4048 t = tt;
4049 }
H. Peter Anvin (Intel)e86fa7f2019-10-16 14:51:16 -07004050
4051 /*
4052 * Expand the macro definition now for %xdefine and %ixdefine.
4053 * This is done AFTER marking of parameter tokens: this is the
4054 * only way in which %xdefine(xxx) yyyy zzzz differs from
4055 * %define(xxx) yyyy %[zzzz]
4056 */
4057 if (i == PP_XDEFINE) {
4058 tline = reverse_tokens(tline);
4059 tline = expand_smacro(tline);
4060 tline = reverse_tokens(tline);
4061 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004062 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004063
H. Peter Anvine2c80182005-01-15 22:15:51 +00004064 /*
4065 * Good. We now have a macro name, a parameter count, and a
4066 * token list (in reverse order) for an expansion. We ought
4067 * to be OK just to create an SMacro, store it, and let
4068 * free_tlist have the rest of the line (which we have
4069 * carefully re-terminated after chopping off the expansion
4070 * from the end).
4071 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004072 define_smacro(mname, casesense, macro_start, &tmpl);
4073 break;
4074 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004075
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004077 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004078 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004079 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004080 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004081 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004082
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004083 undef_smacro(mname, i == PP_UNDEFALIAS);
4084 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004085
H. Peter Anvin8b262472019-02-26 14:00:54 -08004086 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004087 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004088 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004089
H. Peter Anvin9e200162008-06-04 17:23:14 -07004090 last = tline;
4091 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004092 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004093
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004094 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004095 q = detoken(tline, false);
4096 macro_start = make_tok_qstr(NULL, q);
4097 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004098
4099 /*
4100 * We now have a macro name, an implicit parameter count of
4101 * zero, and a string token to use as an expansion. Create
4102 * and store an SMacro.
4103 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004104 define_smacro(mname, casesense, macro_start, NULL);
4105 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004106
H. Peter Anvin8b262472019-02-26 14:00:54 -08004107 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004108 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004109 goto done;
4110
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004111 last = tline;
4112 tline = expand_smacro(tline->next);
4113 last->next = NULL;
4114
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004115 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004116 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004117 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004118 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004119 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004120 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004121 }
4122
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004123 /*
4124 * Convert the string to a token stream. Note that smacros
4125 * are stored with the token stream reversed, so we have to
4126 * reverse the output of tokenize().
4127 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004128 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004129
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004130 /*
4131 * We now have a macro name, an implicit parameter count of
4132 * zero, and a numeric token to use as an expansion. Create
4133 * and store an SMacro.
4134 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004135 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004136 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004137 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004138
H. Peter Anvin418ca702008-05-30 10:42:30 -07004139 case PP_PATHSEARCH:
4140 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004141 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004142
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004143 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004144 goto done;
4145
H. Peter Anvin418ca702008-05-30 10:42:30 -07004146 last = tline;
4147 tline = expand_smacro(tline->next);
4148 last->next = NULL;
4149
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004150 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004151 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004152 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004153 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004154 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004155 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004156 }
4157 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004158 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004159
4160 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004161
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004162 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004163 if (!found_path)
4164 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004165 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004166
4167 /*
4168 * We now have a macro name, an implicit parameter count of
4169 * zero, and a string token to use as an expansion. Create
4170 * and store an SMacro.
4171 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004172 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004173 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004174 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004175 }
4176
H. Peter Anvine2c80182005-01-15 22:15:51 +00004177 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004178 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004179 goto done;
4180
H. Peter Anvine2c80182005-01-15 22:15:51 +00004181 last = tline;
4182 tline = expand_smacro(tline->next);
4183 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004184
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004185 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004186 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004187 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004188 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 free_tlist(tline);
4190 free_tlist(origline);
4191 return DIRECTIVE_FOUND;
4192 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004193
H. Peter Anvin8571f062019-09-23 16:40:03 -07004194 unquote_token(t);
4195 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004196
H. Peter Anvine2c80182005-01-15 22:15:51 +00004197 /*
4198 * We now have a macro name, an implicit parameter count of
4199 * zero, and a numeric token to use as an expansion. Create
4200 * and store an SMacro.
4201 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004202 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004203 free_tlist(tline);
4204 free_tlist(origline);
4205 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004206
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004207 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004208 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004209 goto done;
4210
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004211 last = tline;
4212 tline = expand_smacro(tline->next);
4213 last->next = NULL;
4214
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004215 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004216 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004217 switch (t->type) {
4218 case TOK_WHITESPACE:
4219 break;
4220 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004221 unquote_token(t);
4222 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004223 break;
4224 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004225 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004226 break;
4227 /* else fall through */
4228 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004229 nasm_nonfatal("non-string passed to `%s': %s", dname,
4230 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004231 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004232 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004233 }
4234 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004235
H. Peter Anvin8571f062019-09-23 16:40:03 -07004236 q = qbuf = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004237 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004238 if (t->type == TOK_INTERNAL_STRING)
4239 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004240 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004241
4242 /*
4243 * We now have a macro name, an implicit parameter count of
4244 * zero, and a numeric token to use as an expansion. Create
4245 * and store an SMacro.
4246 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004247 macro_start = make_tok_qstr(NULL, qbuf);
4248 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004249 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004250 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004251 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004252
H. Peter Anvine2c80182005-01-15 22:15:51 +00004253 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004254 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004255 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004256 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004257 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004258
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004259 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004260 goto done;
4261
H. Peter Anvine2c80182005-01-15 22:15:51 +00004262 last = tline;
4263 tline = expand_smacro(tline->next);
4264 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004265
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004266 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004267 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004268
4269 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004270
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004272 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004273 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004274 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004275 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004276 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004277
H. Peter Anvin8b262472019-02-26 14:00:54 -08004278 pps.tptr = t->next;
4279 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004281 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282 if (!evalresult) {
4283 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004284 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004285 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004286 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004287 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004288 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004289 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004290 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004291
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004292 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004293 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004294 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004295 } else {
4296 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004297 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004298 if (!evalresult) {
4299 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004300 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004301 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004302 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004303 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004304 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004305 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004306 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004307 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004308
H. Peter Anvin8571f062019-09-23 16:40:03 -07004309 unquote_token(t);
4310 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004311
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004312 /* make start and count being in range */
4313 if (start < 0)
4314 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004315 if (count < 0)
4316 count = len + count + 1 - start;
4317 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004318 count = len - start;
4319 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004320 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004321
H. Peter Anvin8571f062019-09-23 16:40:03 -07004322 txt = (start < 0) ? "" : tok_text(t) + start;
4323 len = count;
4324 macro_start = make_tok_qstr(NULL, txt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004325
H. Peter Anvine2c80182005-01-15 22:15:51 +00004326 /*
4327 * We now have a macro name, an implicit parameter count of
4328 * zero, and a numeric token to use as an expansion. Create
4329 * and store an SMacro.
4330 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004331 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004333 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004334 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004335
H. Peter Anvin8b262472019-02-26 14:00:54 -08004336 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004337 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004338 goto done;
4339
H. Peter Anvine2c80182005-01-15 22:15:51 +00004340 last = tline;
4341 tline = expand_smacro(tline->next);
4342 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004343
H. Peter Anvin8b262472019-02-26 14:00:54 -08004344 pps.tptr = tline;
4345 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004346 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004347 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004348 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004349 if (!evalresult)
4350 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004351
H. Peter Anvine2c80182005-01-15 22:15:51 +00004352 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004353 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004354
H. Peter Anvine2c80182005-01-15 22:15:51 +00004355 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004356 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 free_tlist(origline);
4358 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004359 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004360
H. Peter Anvin8571f062019-09-23 16:40:03 -07004361 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004362
H. Peter Anvine2c80182005-01-15 22:15:51 +00004363 /*
4364 * We now have a macro name, an implicit parameter count of
4365 * zero, and a numeric token to use as an expansion. Create
4366 * and store an SMacro.
4367 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004368 define_smacro(mname, casesense, macro_start, NULL);
4369 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004370
H. Peter Anvind2354082019-08-27 16:38:48 -07004371 case PP_ALIASES:
4372 tline = tline->next;
4373 tline = expand_smacro(tline);
4374 do_aliases = pp_get_boolean_option(tline, do_aliases);
4375 break;
4376
H. Peter Anvine2c80182005-01-15 22:15:51 +00004377 case PP_LINE:
4378 /*
4379 * Syntax is `%line nnn[+mmm] [filename]'
4380 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004381 if (unlikely(pp_noline))
4382 goto done;
4383
H. Peter Anvine2c80182005-01-15 22:15:51 +00004384 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004385 tline = skip_white(tline);
4386 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004387 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004388 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004389 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004390 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 m = 1;
4392 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004393 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004394 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004395 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004396 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004397 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004398 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004399 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004400 tline = tline->next;
4401 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004402 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004403 src_set_linnum(k);
4404 istk->lineinc = m;
4405 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004406 char *fname = detoken(tline, false);
4407 src_set_fname(fname);
4408 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004409 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004410 break;
4411 }
4412
4413done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004414 free_tlist(origline);
4415 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004416}
4417
4418/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004419 * Ensure that a macro parameter contains a condition code and
4420 * nothing else. Return the condition code index if so, or -1
4421 * otherwise.
4422 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004423static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004424{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004425 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004426
H. Peter Anvin25a99342007-09-22 17:45:45 -07004427 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004428 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004429
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004430 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004431 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004432 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004433 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004434 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004435 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004437
H. Peter Anvin8571f062019-09-23 16:40:03 -07004438 return bsii(tok_text(t), (const char **)conditions,
4439 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004440}
4441
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004442static inline bool pp_concat_match(const Token *t, unsigned int mask)
4443{
4444 return t && (PP_CONCAT_MASK(t->type) & mask);
4445}
4446
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004447/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004448 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004449 * pasting, if @handle_explicit passed then explicit pasting
4450 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004451 * The @m array can contain a series of token types which are
4452 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004453 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004454static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004455 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004456{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004457 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004458 bool pasted = false;
4459 char *buf, *p;
4460 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004461
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004462 /*
4463 * The last token before pasting. We need it
4464 * to be able to connect new handled tokens.
4465 * In other words if there were a tokens stream
4466 *
4467 * A -> B -> C -> D
4468 *
4469 * and we've joined tokens B and C, the resulting
4470 * stream should be
4471 *
4472 * A -> BC -> D
4473 */
4474 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004475 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004476
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004477 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004478 prev_nonspace = NULL;
4479
4480 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004481 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004482
4483 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004484 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004485 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004486 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004487 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004488
4489 case TOK_PASTE:
4490 /* Explicit pasting */
4491 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004492 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004493
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004494 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004495 if (!prev_nonspace) {
4496 nasm_nonfatal("No lvalue found on pasting");
4497 tok = delete_Token(tok);
4498 break;
4499 }
4500
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004501 did_paste = true;
4502
4503 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004504 t = *prev_nonspace;
4505
4506 /* Delete leading whitespace */
4507 next = zap_white(t->next);
4508
4509 /* Delete the %+ token itself */
4510 nasm_assert(next == tok);
4511 next = delete_Token(next);
4512
4513 /* Delete trailing whitespace */
4514 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004515
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004516 /*
4517 * No ending token, this might happen in two
4518 * cases
4519 *
4520 * 1) There indeed no right token at all
4521 * 2) There is a bare "%define ID" statement,
4522 * and @ID does expand to whitespace.
4523 *
4524 * So technically we need to do a grammar analysis
4525 * in another stage of parsing, but for now lets don't
4526 * change the behaviour people used to. Simply allow
4527 * whitespace after paste token.
4528 */
4529 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004530 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004531 break;
4532 }
4533
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004534 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004535 p = mempcpy(p, tok_text(t), t->len);
4536 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004537 *p = '\0';
4538 delete_Token(t);
4539 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004540 nasm_free(buf);
4541
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004542 if (unlikely(!t)) {
4543 /*
4544 * No output at all? Replace with a single whitespace.
4545 * This should never happen.
4546 */
4547 t = new_White(NULL);
4548 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004549
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004550 *prev_nonspace = tok = t;
4551 while (t->next)
4552 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004553
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004554 /* Delete the second token and attach to the end of the list */
4555 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004556
4557 /* We want to restart from the head of the pasted token */
4558 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004559 break;
4560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004561 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004562 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004563 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004564 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004565 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004566 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004567
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004568 if (i >= mnum)
4569 break;
4570
4571 len = tok->len;
4572 while (pp_concat_match(next, m[i].mask_tail)) {
4573 len += next->len;
4574 next = next->next;
4575 }
4576
4577 /* No match or no text to process */
4578 if (len == tok->len)
4579 break;
4580
4581 p = buf = nasm_malloc(len + 1);
4582 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004583 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004584 tok = delete_Token(tok);
4585 }
4586 *p = '\0';
4587 *prev_next = tok = t = tokenize(buf);
4588 nasm_free(buf);
4589
4590 /*
4591 * Connect pasted into original stream,
4592 * ie A -> new-tokens -> B
4593 */
4594 while (t->next)
4595 t = t->next;
4596 t->next = next;
4597 prev_next = prev_nonspace = &t->next;
4598 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004599 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004600 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004601
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004602 if (did_paste) {
4603 pasted = true;
4604 } else {
4605 prev_next = &tok->next;
4606 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4607 prev_nonspace = prev_next;
4608 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004609
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004610 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004611 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004612
4613 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004614}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004615
4616/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004617 * Computes the proper rotation of mmacro parameters
4618 */
4619static int mmac_rotate(const MMacro *mac, unsigned int n)
4620{
4621 if (--n < mac->nparam)
4622 n = (n + mac->rotate) % mac->nparam;
4623
4624 return n+1;
4625}
4626
4627/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004628 * expands to a list of tokens from %{x:y}
4629 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004630static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004631{
4632 Token *t = tline, **tt, *tm, *head;
4633 char *pos;
4634 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004635
H. Peter Anvin8571f062019-09-23 16:40:03 -07004636 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004637 nasm_assert(pos);
4638
4639 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004640 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004641
4642 /*
4643 * only macros params are accounted so
4644 * if someone passes %0 -- we reject such
4645 * value(s)
4646 */
4647 if (lst == 0 || fst == 0)
4648 goto err;
4649
4650 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004651 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4652 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004653 goto err;
4654
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004655 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4656 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004657
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004658 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004659 fst--, lst--;
4660
4661 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004662 * It will be at least one token. Note we
4663 * need to scan params until separator, otherwise
4664 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004665 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004666 j = (fst + mac->rotate) % mac->nparam;
4667 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004668 if (!tm)
4669 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004670 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004671 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004672 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004673 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004674 *tt = t, tt = &t->next, tm = tm->next;
4675 }
4676
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004677 if (fst < lst) {
4678 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004679 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004680 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004681 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004682 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004683 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004684 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004685 *tt = t, tt = &t->next, tm = tm->next;
4686 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004687 }
4688 } else {
4689 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004690 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004691 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004692 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004693 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004694 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004695 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004696 *tt = t, tt = &t->next, tm = tm->next;
4697 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004698 }
4699 }
4700
4701 *last = tt;
4702 return head;
4703
4704err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004705 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004706 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004707 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004708}
4709
H. Peter Anvin76690a12002-04-30 20:52:49 +00004710/*
4711 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004712 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004713 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004714 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004715static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004716{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004717 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004718 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004719 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004720
4721 tail = &thead;
4722 thead = NULL;
4723
H. Peter Anvine2c80182005-01-15 22:15:51 +00004724 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004725 bool change;
4726 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004727 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004728 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004729
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004730 tline = tline->next;
4731 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004732
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004733 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004734 case TOK_LOCAL_SYMBOL:
4735 type = TOK_ID;
4736 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4737 change = true;
4738 break;
4739 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004740 {
4741 Token *tt = NULL;
4742
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004743 change = true;
4744
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004745 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004746 nasm_nonfatal("`%s': not in a macro call", text);
4747 text = NULL;
4748 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004749 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004750
4751 if (strchr(text, ':')) {
4752 /*
4753 * seems we have a parameters range here
4754 */
4755 Token *head, **last;
4756 head = expand_mmac_params_range(mac, t, &last);
4757 if (head) {
4758 *tail = head;
4759 *last = tline;
4760 text = NULL;
4761 }
4762 break;
4763 }
4764
4765 switch (text[1]) {
4766 /*
4767 * We have to make a substitution of one of the
4768 * forms %1, %-1, %+1, %%foo, %0, %00.
4769 */
4770 case '0':
4771 if (!text[2]) {
4772 type = TOK_NUMBER;
4773 text = nasm_asprintf("%d", mac->nparam);
4774 break;
4775 }
4776 if (text[2] != '0' || text[3])
4777 goto invalid;
4778 /* a possible captured label == mac->params[0] */
4779 /* fall through */
4780 default:
4781 {
4782 unsigned long n;
4783 char *ep;
4784
4785 n = strtoul(text + 1, &ep, 10);
4786 if (unlikely(*ep))
4787 goto invalid;
4788
4789 if (n <= mac->nparam) {
4790 n = mmac_rotate(mac, n);
4791 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4792 }
4793 text = NULL;
4794 break;
4795 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004796 case '-':
4797 case '+':
4798 {
4799 int cc;
4800 unsigned long n;
4801 char *ep;
4802
4803 text = NULL;
4804
H. Peter Anvin8571f062019-09-23 16:40:03 -07004805 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004806 if (unlikely(*ep))
4807 goto invalid;
4808
4809 if (n && n < mac->nparam) {
4810 n = mmac_rotate(mac, n);
4811 tt = mac->params[n];
4812 }
4813 cc = find_cc(tt);
4814 if (cc == -1) {
4815 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004816 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004817 text = NULL;
4818 break;
4819 }
4820
4821 type = TOK_ID;
4822 if (text[1] == '-') {
4823 int ncc = inverse_ccs[cc];
4824 if (unlikely(ncc == -1)) {
4825 nasm_nonfatal("condition code `%s' is not invertible",
4826 conditions[cc]);
4827 break;
4828 }
4829 cc = ncc;
4830 }
4831 text = nasm_strdup(conditions[cc]);
4832 break;
4833 }
4834
4835 invalid:
4836 nasm_nonfatal("invalid macro parameter: `%s'", text);
4837 text = NULL;
4838 break;
4839 }
4840 break;
4841 }
4842
4843 case TOK_PREPROC_Q:
4844 if (mac) {
4845 type = TOK_ID;
4846 text = nasm_strdup(mac->iname);
4847 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004848 } else {
4849 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004850 }
4851 break;
4852
4853 case TOK_PREPROC_QQ:
4854 if (mac) {
4855 type = TOK_ID;
4856 text = nasm_strdup(mac->name);
4857 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004858 } else {
4859 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004860 }
4861 break;
4862
4863 case TOK_INDIRECT:
4864 {
4865 Token *tt;
4866
H. Peter Anvin8571f062019-09-23 16:40:03 -07004867 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004868 tt = expand_mmac_params(tt);
4869 tt = expand_smacro(tt);
4870 /* Why dup_tlist() here? We should own tt... */
4871 dup_tlist(tt, &tail);
4872 text = NULL;
4873 change = true;
4874 break;
4875 }
4876
4877 default:
4878 change = false;
4879 break;
4880 }
4881
4882 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004883 if (!text) {
4884 delete_Token(t);
4885 } else {
4886 *tail = t;
4887 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004888 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004889 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004890 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004891 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004892 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004893 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004894 tail = &t->next;
4895 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004896 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004897
H. Peter Anvineba20a72002-04-30 20:53:55 +00004898 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004899
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004900 if (changed) {
4901 const struct tokseq_match t[] = {
4902 {
4903 PP_CONCAT_MASK(TOK_ID) |
4904 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4905 PP_CONCAT_MASK(TOK_ID) |
4906 PP_CONCAT_MASK(TOK_NUMBER) |
4907 PP_CONCAT_MASK(TOK_FLOAT) |
4908 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4909 },
4910 {
4911 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4912 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4913 }
4914 };
4915 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4916 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004917
H. Peter Anvin76690a12002-04-30 20:52:49 +00004918 return thead;
4919}
4920
H. Peter Anvin322bee02019-08-10 01:38:06 -07004921static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004922
H. Peter Anvin76690a12002-04-30 20:52:49 +00004923/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004924 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004925 * a macro at all, it is simply copied to the output and the pointer
4926 * advanced. tpp should be a pointer to a pointer (usually the next
4927 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004928 * to point to the first token of the expansion, and *tpp updated to
4929 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004930 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004931 * If the expansion is empty, *tpp will be unchanged but **tpp will
4932 * be advanced past the macro call.
4933 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004934 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004935 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004936static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004937{
4938 Token **params = NULL;
4939 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004940 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004941 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004942 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004943 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004944 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004945 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004946 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004947
4948 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004949 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004950
H. Peter Anvin8571f062019-09-23 16:40:03 -07004951 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004952
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004953 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004954 smacro_deadman.levels--;
4955
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004956 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004957 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004958 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004959 smacro_deadman.triggered = true;
4960 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004961 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004962 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004963 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004964 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004965 Context *ctx = get_ctx(mname, &mname);
4966 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4967 } else {
4968 goto not_a_macro;
4969 }
4970
4971 /*
4972 * We've hit an identifier of some sort. First check whether the
4973 * identifier is a single-line macro at all, then think about
4974 * checking for parameters if necessary.
4975 */
4976 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004977 if (unlikely(m->alias && !do_aliases))
4978 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004979 if (!mstrcmp(m->name, mname, m->casesense))
4980 break;
4981 }
4982
4983 if (!m) {
4984 goto not_a_macro;
4985 }
4986
4987 /* Parse parameters, if applicable */
4988
4989 params = NULL;
4990 nparam = 0;
4991
4992 if (m->nparam == 0) {
4993 /*
4994 * Simple case: the macro is parameterless.
4995 * Nothing to parse; the expansion code will
4996 * drop the macro name token.
4997 */
4998 } else {
4999 /*
5000 * Complicated case: at least one macro with this name
5001 * exists and takes parameters. We must find the
5002 * parameters in the call, count them, find the SMacro
5003 * that corresponds to that form of the macro call, and
5004 * substitute for the parameters when we expand. What a
5005 * pain.
5006 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005007 Token *t;
5008 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005009
5010 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005011 tline = skip_white(tline);
5012 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005013 /*
5014 * This macro wasn't called with parameters: ignore
5015 * the call. (Behaviour borrowed from gnu cpp.)
5016 */
5017 goto not_a_macro;
5018 }
5019
5020 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005021 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005022 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005023 t = tline; /* tline points to leading ( */
5024
5025 while (paren) {
5026 t = t->next;
5027
5028 if (!t) {
5029 nasm_nonfatal("macro call expects terminating `)'");
5030 goto not_a_macro;
5031 }
5032
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005033 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005034 continue;
5035
H. Peter Anvin8571f062019-09-23 16:40:03 -07005036 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005037 case ',':
5038 if (!brackets)
5039 nparam++;
5040 break;
5041
5042 case '{':
5043 brackets++;
5044 break;
5045
5046 case '}':
5047 if (brackets > 0)
5048 brackets--;
5049 break;
5050
5051 case '(':
5052 if (!brackets)
5053 paren++;
5054 break;
5055
5056 case ')':
5057 if (!brackets)
5058 paren--;
5059 break;
5060
5061 default:
5062 break; /* Normal token */
5063 }
5064 }
5065
5066 /*
5067 * Look for a macro matching in both name and parameter count.
5068 * We already know any matches cannot be anywhere before the
5069 * current position of "m", so there is no reason to
5070 * backtrack.
5071 */
5072 while (1) {
5073 if (!m) {
5074 /*!
5075 *!macro-params-single [on] single-line macro calls with wrong parameter count
5076 *! warns about \i{single-line macros} being invoked
5077 *! with the wrong number of parameters.
5078 */
5079 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
5080 "single-line macro `%s' exists, "
5081 "but not taking %d parameter%s",
5082 mname, nparam, (nparam == 1) ? "" : "s");
5083 goto not_a_macro;
5084 }
5085
5086 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005087 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005088 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005089 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005090 break; /* Also good */
5091 }
5092 m = m->next;
5093 }
5094 }
5095
5096 if (m->in_progress)
5097 goto not_a_macro;
5098
5099 /* Expand the macro */
5100 m->in_progress = true;
5101
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005102 if (nparam) {
5103 /* Extract parameters */
5104 Token **phead, **pep;
5105 int white = 0;
5106 int brackets = 0;
5107 int paren;
5108 bool bracketed = false;
5109 bool bad_bracket = false;
5110 enum sparmflags flags;
5111
5112 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005113 paren = 1;
5114 nasm_newn(params, nparam);
5115 i = 0;
5116 flags = m->params[i].flags;
5117 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005118 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005119
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005120 while (paren) {
5121 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005122 char ch;
5123
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005124 tline = tline->next;
5125
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005126 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005127 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005128
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005129 ch = 0;
5130 skip = false;
5131
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005132
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005133 switch (tline->type) {
5134 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005135 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005136 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005137 break;
5138
5139 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005140 if (!(flags & SPARM_NOSTRIP)) {
5141 if (brackets || *phead)
5142 white++; /* Keep interior whitespace */
5143 skip = true;
5144 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005145 break;
5146
5147 default:
5148 break;
5149 }
5150
5151 switch (ch) {
5152 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005153 if (!brackets && !(flags & SPARM_GREEDY)) {
5154 i++;
5155 nasm_assert(i < nparam);
5156 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005157 *pep = NULL;
5158 bracketed = false;
5159 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005160 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005161 }
5162 break;
5163
5164 case '{':
5165 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005166 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5167 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005168 }
5169 brackets++;
5170 break;
5171
5172 case '}':
5173 if (brackets > 0) {
5174 if (!--brackets)
5175 skip = bracketed;
5176 }
5177 break;
5178
5179 case '(':
5180 if (!brackets)
5181 paren++;
5182 break;
5183
5184 case ')':
5185 if (!brackets) {
5186 paren--;
5187 if (!paren) {
5188 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005189 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005190 }
5191 }
5192 break;
5193
5194 default:
5195 break; /* Normal token */
5196 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005197
5198 if (!skip) {
5199 Token *t;
5200
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005201 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005202
5203 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005204 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005205 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005206 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005207 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005208 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005209 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005210 }
5211 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005212
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005213 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005214 * Possible further processing of parameters. Note that the
5215 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005216 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005217 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005218 enum sparmflags flags = m->params[i].flags;
5219
5220 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005221 /* Evaluate this parameter as a number */
5222 struct ppscan pps;
5223 struct tokenval tokval;
5224 expr *evalresult;
5225 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005226
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005227 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5228 pps.ntokens = -1;
5229 tokval.t_type = TOKEN_INVALID;
5230 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005231
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005232 free_tlist(eval_param);
5233 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005234
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005235 if (!evalresult) {
5236 /* Nothing meaningful to do */
5237 } else if (tokval.t_type) {
5238 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5239 } else if (!is_simple(evalresult)) {
5240 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5241 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005242 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005243 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005244 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005245
5246 if (flags & SPARM_STR) {
5247 /* Convert expansion to a quoted string */
5248 char *arg;
5249 Token *qs;
5250
5251 qs = expand_smacro_noreset(params[i]);
5252 arg = detoken(qs, false);
5253 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005254 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005255 nasm_free(arg);
5256 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005257 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005258 }
5259
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005260 /* Note: we own the expansion this returns. */
5261 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005262
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005263 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005264 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005265 tline = tafter;
5266
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005267 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005268 cond_comma = false;
5269
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005270 while (t) {
5271 enum pp_token_type type = t->type;
5272 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005273
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005274 switch (type) {
5275 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005276 delete_Token(t);
5277 t = dup_Token(tline, mstart);
5278 break;
5279
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005280 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005281 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005282 size_t mlen = strlen(m->name);
5283 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005284 char *p;
5285
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005286 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005287 if (t->type == TOK_LOCAL_MACRO) {
5288 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005289 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005290 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005291
H. Peter Anvin8571f062019-09-23 16:40:03 -07005292 psp = tok_text(mstart);
5293 get_ctx(psp, &pep);
5294 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005295
H. Peter Anvin8571f062019-09-23 16:40:03 -07005296 len = mlen + plen;
5297 p = nasm_malloc(len + 1);
5298 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005299 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005300 len = mlen;
5301 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005302 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005303 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005304 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005305 set_text_free(t, p, len);
5306
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005307 t->next = tline;
5308 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005309 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005310
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005311 case TOK_COND_COMMA:
5312 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005313 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005314 break;
5315
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005316 case TOK_ID:
5317 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005318 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005319 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005320 /*
5321 * Chain this into the target line *before* expanding,
5322 * that way we pick up any arguments to the new macro call,
5323 * if applicable.
5324 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005325 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005326 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005327 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005328 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005329 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005330 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005331 default:
5332 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005333 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005334 nasm_assert(!tup && param < nparam);
5335 delete_Token(t);
5336 t = NULL;
5337 tup = tnext;
5338 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005339 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005340 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005341 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005342 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005343 }
5344
5345 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005346 Token *endt = tline;
5347
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005348 tline = t;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005349 while (!cond_comma && t && t != endt)
5350 cond_comma = t->type != TOK_WHITESPACE;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005351 }
5352
5353 if (tnext) {
5354 t = tnext;
5355 } else {
5356 t = tup;
5357 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005358 }
5359 }
5360
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005361 **tpp = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005362 for (t = tline; t != tafter; t = t->next)
5363 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005364
5365 m->in_progress = false;
5366
5367 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005368 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005369 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005370
5371 /*
5372 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005373 * and then advance to the next input token. Note that this is
5374 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005375 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005376not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005377 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005378 m = NULL;
5379done:
5380 smacro_deadman.levels++;
5381 if (unlikely(params))
5382 free_tlist_array(params, nparam);
5383 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005384}
5385
5386/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005387 * Expand all single-line macro calls made in the given line.
5388 * Return the expanded version of the line. The original is deemed
5389 * to be destroyed in the process. (In reality we'll just move
5390 * Tokens from input to output a lot of the time, rather than
5391 * actually bothering to destroy and replicate.)
5392 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005393static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005394{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005395 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005396 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5397 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005398 return expand_smacro_noreset(tline);
5399}
5400
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005401static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005402{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005403 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005404 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005405
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005406 if (!org_tline)
5407 return NULL; /* Empty input */
5408
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005409 /*
5410 * Trick: we should avoid changing the start token pointer since it can
5411 * be contained in "next" field of other token. Because of this
5412 * we allocate a copy of first token and work with it; at the end of
5413 * routine we copy it back
5414 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005415 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005416
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005417 /*
5418 * Pretend that we always end up doing expansion on the first pass;
5419 * that way %+ get processed. However, if we process %+ before the
5420 * first pass, we end up with things like MACRO %+ TAIL trying to
5421 * look up the macro "MACROTAIL", which we don't want.
5422 */
5423 expanded = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005424 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005425 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005426 {
5427 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005428 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5429 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005430 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5431 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005432 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5433 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005434 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5435 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5436 }
5437 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005438 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005439
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005440 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005441 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005442
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005443 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005444 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005445
5446 /*
5447 * Now scan the entire line and look for successive TOK_IDs
5448 * that resulted after expansion (they can't be produced by
5449 * tokenize()). The successive TOK_IDs should be concatenated.
5450 * Also we look for %+ tokens and concatenate the tokens
5451 * before and after them (without white spaces in between).
5452 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005453 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5454 break; /* Done again! */
5455
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005456 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005457 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005458
H. Peter Anvin8571f062019-09-23 16:40:03 -07005459 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005460 /*
5461 * The expression expanded to empty line;
5462 * we can't return NULL because of the "trick" above.
5463 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005464 */
5465
5466 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005467 }
5468
H. Peter Anvin8571f062019-09-23 16:40:03 -07005469 steal_Token(org_tline, tline);
5470 org_tline->next = tline->next;
5471 delete_Token(tline);
5472
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005473 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005474}
5475
5476/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005477 * Similar to expand_smacro but used exclusively with macro identifiers
5478 * right before they are fetched in. The reason is that there can be
5479 * identifiers consisting of several subparts. We consider that if there
5480 * are more than one element forming the name, user wants a expansion,
5481 * otherwise it will be left as-is. Example:
5482 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005483 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005484 *
5485 * the identifier %$abc will be left as-is so that the handler for %define
5486 * will suck it and define the corresponding value. Other case:
5487 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005488 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005489 *
5490 * In this case user wants name to be expanded *before* %define starts
5491 * working, so we'll expand %$abc into something (if it has a value;
5492 * otherwise it will be left as-is) then concatenate all successive
5493 * PP_IDs into one.
5494 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005495static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005496{
5497 Token *cur, *oldnext = NULL;
5498
H. Peter Anvin734b1882002-04-30 21:01:08 +00005499 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005500 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005501
5502 cur = tline;
5503 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005504 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5505 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005506 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005507
5508 /* If identifier consists of just one token, don't expand */
5509 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005510 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005511
H. Peter Anvine2c80182005-01-15 22:15:51 +00005512 if (cur) {
5513 oldnext = cur->next; /* Detach the tail past identifier */
5514 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005515 }
5516
H. Peter Anvin734b1882002-04-30 21:01:08 +00005517 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005518
H. Peter Anvine2c80182005-01-15 22:15:51 +00005519 if (cur) {
5520 /* expand_smacro possibly changhed tline; re-scan for EOL */
5521 cur = tline;
5522 while (cur && cur->next)
5523 cur = cur->next;
5524 if (cur)
5525 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005526 }
5527
5528 return tline;
5529}
5530
5531/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005532 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005533 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005534 * to check for an initial label - that's taken care of in
5535 * expand_mmacro - but must check numbers of parameters. Guaranteed
5536 * to be called with tline->type == TOK_ID, so the putative macro
5537 * name is easy to find.
5538 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005539static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005540{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005541 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005542 Token **params;
5543 int nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005544 const char *finding = tok_text(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005545
H. Peter Anvin8571f062019-09-23 16:40:03 -07005546 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005547
5548 /*
5549 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005550 * name which isn't already excluded by macro cycle removal.
5551 * (The cycle removal test here helps optimize the case of wrapping
5552 * instructions, and is cheap to do here.)
5553 *
5554 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005555 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005556 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005557 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005558 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005559 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005560 (m->in_progress != 1 || m->max_depth > 0))
5561 break; /* Found something that needs consideration */
5562 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005563 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005564 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005565
5566 /*
5567 * OK, we have a potential macro. Count and demarcate the
5568 * parameters.
5569 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005570 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005571
5572 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005573 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005574 * structure that handles this number.
5575 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005576 while (m) {
5577 if (m->nparam_min <= nparam
5578 && (m->plus || nparam <= m->nparam_max)) {
5579 /*
5580 * This one is right. Just check if cycle removal
5581 * prohibits us using it before we actually celebrate...
5582 */
5583 if (m->in_progress > m->max_depth) {
5584 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005585 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005586 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005587 }
5588 nasm_free(params);
5589 return NULL;
5590 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005591 /*
5592 * It's right, and we can use it. Add its default
5593 * parameters to the end of our list if necessary.
5594 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005595 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005596 int newnparam = m->nparam_min + m->ndefs;
5597 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5598 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5599 (newnparam - nparam) * sizeof(*params));
5600 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005601 }
5602 /*
5603 * If we've gone over the maximum parameter count (and
5604 * we're in Plus mode), ignore parameters beyond
5605 * nparam_max.
5606 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005607 if (m->plus && nparam > m->nparam_max)
5608 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005609
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005610 /*
5611 * If nparam was adjusted above, make sure the list is still
5612 * NULL-terminated.
5613 */
5614 params[nparam+1] = NULL;
5615
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005616 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005617 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005618 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005619 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005620 }
5621 /*
5622 * This one wasn't right: look for the next one with the
5623 * same name.
5624 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005625 list_for_each(m, m->next)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005626 if (!mstrcmp(m->name, tok_text(tline), m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005627 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005628 }
5629
5630 /*
5631 * After all that, we didn't find one with the right number of
5632 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005633 *!
5634 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5635 *! warns about \i{multi-line macros} being invoked
5636 *! with the wrong number of parameters. See \k{mlmacover} for an
5637 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005638 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005639 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5640 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005641 tok_text(tline), nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005642 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005643 return NULL;
5644}
5645
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005646
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005647#if 0
5648
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005649/*
5650 * Save MMacro invocation specific fields in
5651 * preparation for a recursive macro expansion
5652 */
5653static void push_mmacro(MMacro *m)
5654{
5655 MMacroInvocation *i;
5656
5657 i = nasm_malloc(sizeof(MMacroInvocation));
5658 i->prev = m->prev;
5659 i->params = m->params;
5660 i->iline = m->iline;
5661 i->nparam = m->nparam;
5662 i->rotate = m->rotate;
5663 i->paramlen = m->paramlen;
5664 i->unique = m->unique;
5665 i->condcnt = m->condcnt;
5666 m->prev = i;
5667}
5668
5669
5670/*
5671 * Restore MMacro invocation specific fields that were
5672 * saved during a previous recursive macro expansion
5673 */
5674static void pop_mmacro(MMacro *m)
5675{
5676 MMacroInvocation *i;
5677
5678 if (m->prev) {
5679 i = m->prev;
5680 m->prev = i->prev;
5681 m->params = i->params;
5682 m->iline = i->iline;
5683 m->nparam = i->nparam;
5684 m->rotate = i->rotate;
5685 m->paramlen = i->paramlen;
5686 m->unique = i->unique;
5687 m->condcnt = i->condcnt;
5688 nasm_free(i);
5689 }
5690}
5691
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005692#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005693
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005694/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005695 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005696 */
5697static void list_mmacro_call(const MMacro *m)
5698{
5699 const char prefix[] = " ;;; [macro] ";
5700 size_t namelen, size;
5701 char *buf, *p;
5702 unsigned int i;
5703 const Token *t;
5704
5705 namelen = strlen(m->iname);
5706 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5707
5708 for (i = 1; i <= m->nparam; i++) {
5709 int j = 0;
5710 size += 3; /* Braces and space/comma */
5711 list_for_each(t, m->params[i]) {
5712 if (j++ >= m->paramlen[i])
5713 break;
5714 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5715 }
5716 }
5717
5718 buf = p = nasm_malloc(size);
5719 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5720 p = mempcpy(p, m->iname, namelen);
5721 *p++ = ' ';
5722
5723 for (i = 1; i <= m->nparam; i++) {
5724 int j = 0;
5725 *p++ = '{';
5726 list_for_each(t, m->params[i]) {
5727 if (j++ >= m->paramlen[i])
5728 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005729 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005730 }
5731 *p++ = '}';
5732 *p++ = ',';
5733 }
5734
5735 *--p = '\0'; /* Replace last delimeter with null */
5736 lfmt->line(LIST_MACRO, -1, buf);
5737 nasm_free(buf);
5738}
5739
5740/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005741 * Expand the multi-line macro call made by the given line, if
5742 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005743 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005744 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005745static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005746{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005747 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005748 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005749 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005750 Token **params, *t, *tt;
5751 MMacro *m;
5752 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005753 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005754 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005755 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005756
5757 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005758 t = skip_white(t);
5759 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005760 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005761 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005762 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005763 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005764 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07005765 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005766 Token *last;
5767 /*
5768 * We have an id which isn't a macro call. We'll assume
5769 * it might be a label; we'll also check to see if a
5770 * colon follows it. Then, if there's another id after
5771 * that lot, we'll check it again for macro-hood.
5772 */
5773 label = last = t;
5774 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005775 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005776 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005777 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005778 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005779 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005780 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005781 last = t, t = t->next;
5782 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005783 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005784 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005785 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005786 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005787 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005788 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005789
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005790 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5791 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5792 if (!mmacro_deadman.triggered) {
5793 nasm_nonfatal("interminable multiline macro recursion");
5794 mmacro_deadman.triggered = true;
5795 }
5796 return 0;
5797 }
5798
5799 mmacro_deadman.total++;
5800 mmacro_deadman.levels++;
5801
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005802 /*
5803 * Fix up the parameters: this involves stripping leading and
5804 * trailing whitespace, then stripping braces if they are
5805 * present.
5806 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005807 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005808
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005809 nasm_assert(params[nparam+1] == NULL);
5810
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005811 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005812 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005813 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005814 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005815 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005816
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005817 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005818 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005819 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005820 brace = 1;
5821 braced = true;
5822 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005823 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005824
5825 params[i] = t;
5826 for (; t; t = t->next) {
5827 if (tok_white(t)) {
5828 white++;
5829 continue;
5830 }
5831
5832 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005833 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005834 case ',':
5835 if (comma && !brace)
5836 goto endparam;
5837 break;
5838
5839 case '{':
5840 brace++;
5841 break;
5842
5843 case '}':
5844 brace--;
5845 if (braced && !brace) {
5846 paramlen[i] += white;
5847 goto endparam;
5848 }
5849 break;
5850
5851 default:
5852 break;
5853 }
5854 }
5855
5856 paramlen[i] += white + 1;
5857 white = 0;
5858 }
5859 endparam:
5860 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005861 }
5862
5863 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005864 * OK, we have a MMacro structure together with a set of
5865 * parameters. We must now go through the expansion and push
5866 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005867 * parameter tokens and macro-local tokens doesn't get done
5868 * until the single-line macro substitution process; this is
5869 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005870 * later through %rotate and give the right semantics for
5871 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005872 *
5873 * First, push an end marker on to istk->expansion, mark this
5874 * macro as in progress, and set up its invocation-specific
5875 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005876 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005877 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005878 ll->next = istk->expansion;
5879 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005880 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005881
5882 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005883 * Save the previous MMacro expansion in the case of
5884 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005885 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005886#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005887 if (m->max_depth && m->in_progress)
5888 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005889#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005890
5891 m->in_progress ++;
5892 m->params = params;
5893 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005894 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005895 m->nparam = nparam;
5896 m->rotate = 0;
5897 m->paramlen = paramlen;
5898 m->unique = unique++;
5899 m->lineno = 0;
5900 m->condcnt = 0;
5901
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005902 m->mstk = istk->mstk;
5903 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005904
5905 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005906 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005907 ll->next = istk->expansion;
5908 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005909 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005910 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005911
5912 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005913 * If we had a label, and this macro definition does not include
5914 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005915 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005916 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005917 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005918 /*
5919 * We had a label. If this macro contains an %00 parameter,
5920 * save the value as a special parameter (which is what it
5921 * is), otherwise push it as the first line of the macro
5922 * expansion.
5923 */
5924 if (m->capture_label) {
5925 params[0] = dup_Token(NULL, label);
5926 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005927 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005928 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005929 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005930 ll->finishes = NULL;
5931 ll->next = istk->expansion;
5932 istk->expansion = ll;
5933 ll->first = startline;
5934 if (!dont_prepend) {
5935 while (label->next)
5936 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005937 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005938 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005939 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005940 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005941
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005942 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005943
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005944 if (list_option('m') && !m->nolist)
5945 list_mmacro_call(m);
5946
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005947 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005948}
5949
H. Peter Anvin130736c2016-02-17 20:27:41 -08005950/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005951 * This function decides if an error message should be suppressed.
5952 * It will never be called with a severity level of ERR_FATAL or
5953 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005954 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005955static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005956{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005957 /*
5958 * If we're in a dead branch of IF or something like it, ignore the error.
5959 * However, because %else etc are evaluated in the state context
5960 * of the previous branch, errors might get lost:
5961 * %if 0 ... %else trailing garbage ... %endif
5962 * So %else etc should set the ERR_PP_PRECOND flag.
5963 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005964 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005965 ((severity & ERR_PP_PRECOND) ?
5966 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005967 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005968 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005969
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005970 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005971}
5972
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005973static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005974stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005975{
5976 (void)s;
5977 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005978 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005979
H. Peter Anvin8571f062019-09-23 16:40:03 -07005980 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08005981}
5982
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005983static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005984stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005985{
5986 (void)s;
5987 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005988 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005989
H. Peter Anvin8571f062019-09-23 16:40:03 -07005990 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08005991}
5992
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005993static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005994stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005995{
5996 (void)s;
5997 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005998 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005999
H. Peter Anvin8571f062019-09-23 16:40:03 -07006000 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006001}
6002
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006003static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006004stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006005{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006006 (void)s;
6007 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006008 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006009
6010 switch (globalbits) {
6011 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006012 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006013 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006014 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006015 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006016 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006017 default:
6018 panic();
6019 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006020}
6021
H. Peter Anvin8b262472019-02-26 14:00:54 -08006022/* Add magic standard macros */
6023struct magic_macros {
6024 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006025 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006026 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006027};
6028static const struct magic_macros magic_macros[] =
6029{
H. Peter Anvind2354082019-08-27 16:38:48 -07006030 { "__?FILE?__", 0, stdmac_file },
6031 { "__?LINE?__", 0, stdmac_line },
6032 { "__?BITS?__", 0, stdmac_bits },
6033 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006034 { NULL, 0, NULL }
6035};
6036
6037static void pp_add_magic_stdmac(void)
6038{
6039 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006040 SMacro tmpl;
6041
6042 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006043
6044 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006045 tmpl.nparam = m->nparam;
6046 tmpl.expand = m->func;
6047 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006048 }
6049}
6050
H. Peter Anvin734b1882002-04-30 21:01:08 +00006051static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006052pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006053{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006054 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006055 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006056
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006057 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006058 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006059 nested_mac_count = 0;
6060 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006061 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006062 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006063 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006064 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07006065 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07006066
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006067 if (!use_loaded)
6068 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6069 memset(use_loaded, 0, use_package_count * sizeof(bool));
6070
H. Peter Anvin6686de22019-08-10 05:33:14 -07006071 /* First set up the top level input file */
6072 nasm_new(istk);
6073 istk->fp = nasm_open_read(file, NF_TEXT);
6074 src_set(0, file);
6075 istk->lineinc = 1;
6076 if (!istk->fp)
6077 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6078
6079 strlist_add(deplist, file);
6080
6081 /*
6082 * Set up the stdmac packages as a virtual include file,
6083 * indicated by a null file pointer.
6084 */
6085 nasm_new(inc);
6086 inc->next = istk;
6087 inc->fname = src_set_fname(NULL);
6088 inc->nolist = !list_option('b');
6089 istk = inc;
6090 lfmt->uplevel(LIST_INCLUDE, 0);
6091
H. Peter Anvin8b262472019-02-26 14:00:54 -08006092 pp_add_magic_stdmac();
6093
H. Peter Anvinf7606612016-07-13 14:23:48 -07006094 if (tasm_compatible_mode)
6095 pp_add_stdmac(nasm_stdmac_tasm);
6096
6097 pp_add_stdmac(nasm_stdmac_nasm);
6098 pp_add_stdmac(nasm_stdmac_version);
6099
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006100 if (extrastdmac)
6101 pp_add_stdmac(extrastdmac);
6102
H. Peter Anvinf7606612016-07-13 14:23:48 -07006103 stdmacpos = stdmacros[0];
6104 stdmacnext = &stdmacros[1];
6105
H. Peter Anvind2456592008-06-19 15:04:18 -07006106 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006107
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006108 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006109 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006110 * other builtins, because it is special -- it varies between
6111 * passes -- but there is really no particular reason to make it
6112 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006113 *
6114 * 0 = dependencies only
6115 * 1 = preparatory passes
6116 * 2 = final pass
6117 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006118 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006119 switch (mode) {
6120 case PP_NORMAL:
6121 apass = pass_final() ? 2 : 1;
6122 break;
6123 case PP_DEPS:
6124 apass = 0;
6125 break;
6126 case PP_PREPROC:
6127 apass = 3;
6128 break;
6129 default:
6130 panic();
6131 }
6132
H. Peter Anvin8571f062019-09-23 16:40:03 -07006133 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006134}
6135
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006136static void pp_init(void)
6137{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006138}
6139
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006140/*
6141 * Get a line of tokens. If we popped the macro expansion/include stack,
6142 * we return a pointer to the dummy token tok_pop; at that point if
6143 * istk is NULL then we have reached end of input;
6144 */
6145static Token tok_pop; /* Dummy token placeholder */
6146
6147static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006148{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006149 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006150 Line *l = istk->expansion;
6151 Token *tline = NULL;
6152 Token *dtline;
6153
H. Peter Anvine2c80182005-01-15 22:15:51 +00006154 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006155 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006156 * buffer or from the input file.
6157 */
6158 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006159 while (l && l->finishes) {
6160 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006161
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006162 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006163 /*
6164 * This is a macro-end marker for a macro with no
6165 * name, which means it's not really a macro at all
6166 * but a %rep block, and the `in_progress' field is
6167 * more than 1, meaning that we still need to
6168 * repeat. (1 means the natural last repetition; 0
6169 * means termination by %exitrep.) We have
6170 * therefore expanded up to the %endrep, and must
6171 * push the whole block on to the expansion buffer
6172 * again. We don't bother to remove the macro-end
6173 * marker: we'd only have to generate another one
6174 * if we did.
6175 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006176 fm->in_progress--;
6177 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006178 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006179 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006180
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006181 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006182 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006183 tail = &ll->first;
6184
6185 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006186 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006187 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006188 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006189 }
6190 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006191 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006192 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006193 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006194 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006195 MMacro *m = istk->mstk.mstk;
6196
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006197 /*
6198 * Check whether a `%rep' was started and not ended
6199 * within this macro expansion. This can happen and
6200 * should be detected. It's a fatal error because
6201 * I'm too confused to work out how to recover
6202 * sensibly from it.
6203 */
6204 if (defining) {
6205 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006206 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006207 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006208 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006209 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006210 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006211
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006212 /*
6213 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006214 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006215 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006216 istk->mstk = m->mstk;
6217 if (m->name) {
6218 /*
6219 * This was a real macro call, not a %rep, and
6220 * therefore the parameter information needs to
6221 * be freed and the iteration count/nesting
6222 * depth adjusted.
6223 */
6224
6225 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006226 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006227 * If all mmacro processing done,
6228 * clear all counters and the deadman
6229 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006230 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006231 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006232 }
6233
Adam Majer91e72402017-07-25 10:42:01 +02006234#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006235 if (m->prev) {
6236 pop_mmacro(m);
6237 fm->in_progress --;
6238 } else
Adam Majer91e72402017-07-25 10:42:01 +02006239#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006240 {
6241 nasm_free(m->params);
6242 free_tlist(m->iline);
6243 nasm_free(m->paramlen);
6244 fm->in_progress = 0;
6245 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006246 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006247
6248 /*
6249 * FIXME It is incorrect to always free_mmacro here.
6250 * It leads to usage-after-free.
6251 *
6252 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6253 */
6254#if 0
6255 else
6256 free_mmacro(m);
6257#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006258 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006259 istk->expansion = l->next;
6260 nasm_free(l);
6261 lfmt->downlevel(LIST_MACRO);
6262 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006263 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006264
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006265 do { /* until we get a line we can use */
6266 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006267
6268 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006269 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006270 int32_t lineno;
6271
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006272 if (istk->mstk.mstk) {
6273 istk->mstk.mstk->lineno++;
6274 if (istk->mstk.mstk->fname)
6275 lineno = istk->mstk.mstk->lineno +
6276 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006277 else
6278 lineno = 0; /* Defined at init time or builtin */
6279 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006280 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006281 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006282
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006283 tline = l->first;
6284 istk->expansion = l->next;
6285 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006286
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006287 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006288 if (!istk->nolist)
6289 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006290 nasm_free(line);
6291 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006292 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006293 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006294 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006295 } else {
6296 /*
6297 * The current file has ended; work down the istk
6298 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006299 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006300 if (i->fp)
6301 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006302 if (i->conds) {
6303 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006304 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006305 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006306 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006307 if (i->next)
6308 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006309 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006310 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006311 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006312 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006313 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006314 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006315
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006316 /*
6317 * We must expand MMacro parameters and MMacro-local labels
6318 * _before_ we plunge into directive processing, to cope
6319 * with things like `%define something %1' such as STRUC
6320 * uses. Unless we're _defining_ a MMacro, in which case
6321 * those tokens should be left alone to go into the
6322 * definition; and unless we're in a non-emitting
6323 * condition, in which case we don't want to meddle with
6324 * anything.
6325 */
6326 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006327 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006328 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006329 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006330
H. Peter Anvine2c80182005-01-15 22:15:51 +00006331 /*
6332 * Check the line to see if it's a preprocessor directive.
6333 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006334 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6335 if (dtline)
6336 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006337 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006338 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006339 * We're defining a multi-line macro. We emit nothing
6340 * at all, and just
6341 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006342 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006343 MMacro *mmac = defining->dstk.mmac;
6344
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006345 Line *l = nasm_malloc(sizeof(Line));
6346 l->next = defining->expansion;
6347 l->first = tline;
6348 l->finishes = NULL;
6349 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006350
6351 /*
6352 * Remember if this mmacro expansion contains %00:
6353 * if it does, we will have to handle leading labels
6354 * specially.
6355 */
6356 if (mmac) {
6357 const Token *t;
6358 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006359 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006360 mmac->capture_label = true;
6361 }
6362 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006363 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006364 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006365 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006366 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006367 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006368 * directive so we keep our place correctly.
6369 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006370 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006371 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006372 /*
6373 * We're in a %rep block which has been terminated, so
6374 * we're walking through to the %endrep without
6375 * emitting anything. Emit nothing at all, not even a
6376 * blank line: when we emerge from the %rep block we'll
6377 * give a line-number directive so we keep our place
6378 * correctly.
6379 */
6380 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006381 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006382 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006383 if (!expand_mmacro(tline))
6384 return tline;
6385 }
6386 }
6387}
6388
6389static char *pp_getline(void)
6390{
6391 char *line = NULL;
6392 Token *tline;
6393
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006394 while (true) {
6395 tline = pp_tokline();
6396 if (tline == &tok_pop) {
6397 /*
6398 * We popped the macro/include stack. If istk is empty,
6399 * we are at end of input, otherwise just loop back.
6400 */
6401 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006402 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006403 } else {
6404 /*
6405 * De-tokenize the line and emit it.
6406 */
6407 line = detoken(tline, true);
6408 free_tlist(tline);
6409 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006410 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006411 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006412
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006413 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006414 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006415 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006416 nasm_free(buf);
6417 }
6418
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006419 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006420}
6421
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006422static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006423{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006424 if (defining) {
6425 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006426 nasm_nonfatal("end of file while still defining macro `%s'",
6427 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006428 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006429 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006430 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006431
6432 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006433 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006434 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006435
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006436 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006437 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006438 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006439 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006440 Include *i = istk;
6441 istk = istk->next;
6442 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006443 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006444 }
6445 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006446 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006447 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006448}
6449
6450static void pp_cleanup_session(void)
6451{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006452 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006453 free_llist(predef);
6454 predef = NULL;
6455 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006456 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006457}
6458
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006459static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006460{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006461 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006462}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006463
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006464static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006465{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006466 Token *inc, *space, *name;
6467 Line *l;
6468
H. Peter Anvin734b1882002-04-30 21:01:08 +00006469 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006470 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006471 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006472
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006473 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006474 l->next = predef;
6475 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006476 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006477 predef = l;
6478}
6479
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006480static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006481{
6482 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006483 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006484 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006485
6486 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006487 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006488 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006489 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006490 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006491 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006492 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006493 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006494
H. Peter Anvin8571f062019-09-23 16:40:03 -07006495 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006496 if (space->next->type != TOK_PREPROC_ID &&
6497 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006498 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006499
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006500 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006501 l->next = predef;
6502 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006503 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006504 predef = l;
6505}
6506
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006507static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006508{
6509 Token *def, *space;
6510 Line *l;
6511
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006512 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006513 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006514 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006515
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006516 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006517 l->next = predef;
6518 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006519 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006520 predef = l;
6521}
6522
H. Peter Anvin05990342018-06-11 13:32:42 -07006523/* Insert an early preprocessor command that doesn't need special handling */
6524static void pp_pre_command(const char *what, char *string)
6525{
6526 char *cmd;
6527 Token *def, *space;
6528 Line *l;
6529
6530 def = tokenize(string);
6531 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006532 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006533 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6534 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6535 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006536 }
6537
6538 l = nasm_malloc(sizeof(Line));
6539 l->next = predef;
6540 l->first = def;
6541 l->finishes = NULL;
6542 predef = l;
6543}
6544
H. Peter Anvinf7606612016-07-13 14:23:48 -07006545static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006546{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006547 macros_t **mp;
6548
6549 /* Find the end of the list and avoid duplicates */
6550 for (mp = stdmacros; *mp; mp++) {
6551 if (*mp == macros)
6552 return; /* Nothing to do */
6553 }
6554
6555 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6556
6557 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006558}
6559
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006560static void pp_extra_stdmac(macros_t *macros)
6561{
6562 extrastdmac = macros;
6563}
6564
H. Peter Anvin8571f062019-09-23 16:40:03 -07006565/* Create a numeric token */
6566static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006567{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006568 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006569 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006570 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006571}
6572
H. Peter Anvin8571f062019-09-23 16:40:03 -07006573/* Create a quoted string token */
6574static Token *make_tok_qstr(Token *next, const char *str)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006575{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006576 size_t len = strlen(str);
6577 char *p = nasm_quote(str, &len);
6578 return new_Token_free(next, TOK_STRING, p, len);
6579}
6580
6581/* Create a single-character operator token */
6582static Token *make_tok_char(Token *next, char op)
6583{
6584 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6585 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006586 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006587}
6588
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006589static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006590{
6591 if (!m)
6592 return;
6593
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006594 /* We need to print the mstk.mmac list in reverse order */
6595 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006596
6597 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006598 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006599 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006600 }
6601}
6602
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006603static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006604{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006605 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006606
H. Peter Anvinddb29062018-12-11 00:06:29 -08006607 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6608 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006609
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006610 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006611 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006612
H. Peter Anvinddb29062018-12-11 00:06:29 -08006613 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006614}
6615
H. Peter Anvine7469712016-02-18 02:20:59 -08006616const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006617 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006618 pp_reset,
6619 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006620 pp_cleanup_pass,
6621 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006622 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006623 pp_pre_define,
6624 pp_pre_undefine,
6625 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006626 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006627 pp_include_path,
6628 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006629 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006630};