blob: c023426006f5aeecc2dcac5a292d7b99d9b36a80 [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 *
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700102 * TOK_INTERNAL_STRING is a string which has been unquoted, but should
103 * be treated as if it was a quoted string. The code is free to change
104 * one into the other at will. TOK_NAKED_STRING is a text token which
105 * should be treated as a string, but which MUST NOT be turned into a
106 * quoted string. TOK_INTERNAL_STRINGs can contain any character,
107 * including NUL, but TOK_NAKED_STRING must be a valid C string.
H. Peter Anvin8571f062019-09-23 16:40:03 -0700108 */
109enum pp_token_type {
110 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT,
111 TOK_CORRUPT, /* Token text modified in an unsafe manner, now bogus */
112 TOK_BLOCK, /* Storage block pointer, not a real token */
113 TOK_ID,
114 TOK_PREPROC_ID, TOK_MMACRO_PARAM, TOK_LOCAL_SYMBOL,
115 TOK_LOCAL_MACRO, TOK_ENVIRON, TOK_STRING,
116 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700117 TOK_INTERNAL_STRING, TOK_NAKED_STRING,
H. Peter Anvin8571f062019-09-23 16:40:03 -0700118 TOK_PREPROC_Q, TOK_PREPROC_QQ,
119 TOK_PASTE, /* %+ */
120 TOK_COND_COMMA, /* %, */
121 TOK_INDIRECT, /* %[...] */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700122 TOK_XDEF_PARAM, /* Used during %xdefine processing */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700123 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
124 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
125};
126
127static inline enum pp_token_type tok_smac_param(int param)
128{
129 return TOK_SMAC_START_PARAMS + param;
130}
131static int smac_nparam(enum pp_token_type toktype)
132{
133 return toktype - TOK_SMAC_START_PARAMS;
134}
135static bool is_smac_param(enum pp_token_type toktype)
136{
137 return toktype >= TOK_SMAC_START_PARAMS;
138}
139
140#define PP_CONCAT_MASK(x) (1U << (x))
141
142struct tokseq_match {
143 int mask_head;
144 int mask_tail;
145};
146
147/*
148 * This is tuned so struct Token should be 64 bytes on 64-bit
149 * systems and 32 bytes on 32-bit systems. It enables them
150 * to be nicely cache aligned, and the text to still be kept
151 * inline for nearly all tokens.
152 *
153 * We prohibit tokens of length > MAX_TEXT even though
154 * length here is an unsigned int; this avoids problems
155 * if the length is passed through an interface with type "int",
156 * and is absurdly large anyway.
157 *
158 * For the text mode, in pointer mode the pointer is stored at the end
159 * of the union and the pad field is cleared. This allows short tokens
160 * to be unconditionally tested for by only looking at the first text
161 * bytes and not examining the type or len fields.
162 */
163#define INLINE_TEXT (7*sizeof(char *)-sizeof(enum pp_token_type)-sizeof(unsigned int)-1)
164#define MAX_TEXT (INT_MAX-2)
165
166struct Token {
167 Token *next;
168 enum pp_token_type type;
169 unsigned int len;
170 union {
171 char a[INLINE_TEXT+1];
172 struct {
173 char pad[INLINE_TEXT+1 - sizeof(char *)];
174 char *ptr;
175 } p;
176 } text;
177};
178
179/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700180 * Note on the storage of both SMacro and MMacros: the hash table
181 * indexes them case-insensitively, and we then have to go through a
182 * linked list of potential case aliases (and, for MMacros, parameter
183 * ranges); this is to preserve the matching semantics of the earlier
184 * code. If the number of case aliases for a specific macro is a
185 * performance issue, you may want to reconsider your coding style.
186 */
187
188/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700189 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700190 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700191typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700192
193/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000194 * Store the definition of a single-line macro.
195 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700196enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700197 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700198 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
199 SPARM_STR = 2, /* Convert to quoted string ($) */
200 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
201 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
202};
203
204struct smac_param {
H. Peter Anvin8571f062019-09-23 16:40:03 -0700205 Token name;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700206 enum sparmflags flags;
207};
208
H. Peter Anvine2c80182005-01-15 22:15:51 +0000209struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800210 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700212 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700213 ExpandSMacro expand;
214 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700215 struct smac_param *params;
216 int nparam;
217 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800218 bool casesense;
219 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700220 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000221};
222
223/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800224 * Store the definition of a multi-line macro. This is also used to
225 * store the interiors of `%rep...%endrep' blocks, which are
226 * effectively self-re-invoking multi-line macros which simply
227 * don't have a name or bother to appear in the hash tables. %rep
228 * blocks are signified by having a NULL `name' field.
229 *
230 * In a MMacro describing a `%rep' block, the `in_progress' field
231 * isn't merely boolean, but gives the number of repeats left to
232 * run.
233 *
234 * The `next' field is used for storing MMacros in hash tables; the
235 * `next_active' field is for stacking them on istk entries.
236 *
237 * When a MMacro is being expanded, `params', `iline', `nparam',
238 * `paramlen', `rotate' and `unique' are local to the invocation.
239 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700240
241/*
242 * Expansion stack. Note that .mmac can point back to the macro itself,
243 * whereas .mstk cannot.
244 */
245struct mstk {
246 MMacro *mstk; /* Any expansion, real macro or not */
247 MMacro *mmac; /* Highest level actual mmacro */
248};
249
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800250struct MMacro {
251 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700252#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800253 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700254#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800255 char *name;
256 int nparam_min, nparam_max;
257 bool casesense;
258 bool plus; /* is the last parameter greedy? */
259 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700260 bool capture_label; /* macro definition has %00; capture label */
261 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800262 int32_t max_depth; /* maximum number of recursive expansions allowed */
263 Token *dlist; /* All defaults as one list */
264 Token **defaults; /* Parameter default pointers */
265 int ndefs; /* number of default parameters */
266 Line *expansion;
267
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700268 struct mstk mstk; /* Macro expansion stack */
269 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800270 Token **params; /* actual parameters */
271 Token *iline; /* invocation line */
272 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700273 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 int *paramlen;
275 uint64_t unique;
276 int lineno; /* Current line number on expansion */
277 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700278
H. Peter Anvin274cda82016-05-10 02:56:29 -0700279 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700280 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800281};
282
283
284/* Store the definition of a multi-line macro, as defined in a
285 * previous recursive macro expansion.
286 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700287#if 0
288
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800289struct MMacroInvocation {
290 MMacroInvocation *prev; /* previous invocation */
291 Token **params; /* actual parameters */
292 Token *iline; /* invocation line */
293 unsigned int nparam, rotate;
294 int *paramlen;
295 uint64_t unique;
296 uint64_t condcnt;
297};
298
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700299#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800300
301/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000302 * The context stack is composed of a linked list of these.
303 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000304struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305 Context *next;
H. Peter Anvin8571f062019-09-23 16:40:03 -0700306 const char *name;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800307 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700308 uint64_t number;
309 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310};
311
H. Peter Anvin8571f062019-09-23 16:40:03 -0700312
313static inline const char *tok_text(const struct Token *t)
314{
315 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
316}
317
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000318/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700319 * Returns a mutable pointer to the text buffer. The text can be changed,
320 * but the length MUST NOT CHANGE, in either direction; nor is it permitted
321 * to pad with null characters to create an artificially shorter string.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700323static inline char *tok_text_buf(struct Token *t)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800324{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700325 return (t->len <= INLINE_TEXT) ? t->text.a : t->text.p.ptr;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800326}
327
H. Peter Anvin8571f062019-09-23 16:40:03 -0700328static inline unsigned int tok_check_len(size_t len)
329{
330 if (unlikely(len > MAX_TEXT))
331 nasm_fatal("impossibly large token");
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400332
H. Peter Anvin8571f062019-09-23 16:40:03 -0700333 return len;
334}
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400335
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700336static inline bool tok_text_match(const struct Token *a, const struct Token *b)
337{
338 return a->len == b->len && !memcmp(tok_text(a), tok_text(b), a->len);
339}
340
341static inline bool tok_match(const struct Token *a, const struct Token *b)
342{
343 return a->type == b->type && tok_text_match(a, b);
344}
345
H. Peter Anvin8571f062019-09-23 16:40:03 -0700346/* strlen() variant useful for set_text() and its variants */
347static size_t tok_strlen(const char *str)
348{
349 return strnlen(str, MAX_TEXT+1);
350}
351
352/*
353 * Set the text field to a copy of the given string; the length if
354 * not given should be obtained with tok_strlen().
355 */
356static Token *set_text(struct Token *t, const char *text, size_t len)
357{
358 char *textp;
359
360 if (t->len > INLINE_TEXT)
361 nasm_free(t->text.p.ptr);
362
363 nasm_zero(t->text.a);
364
365 t->len = tok_check_len(len);
366 textp = (len > INLINE_TEXT)
367 ? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
368 memcpy(textp, text, len+1);
369 return t;
370}
371
372/*
373 * Set the text field to the existing pre-allocated string, either
374 * taking over or freeing the allocation in the process.
375 */
376static Token *set_text_free(struct Token *t, char *text, unsigned int len)
377{
378 if (t->len > INLINE_TEXT)
379 nasm_free(t->text.p.ptr);
380
381 nasm_zero(t->text.a);
382
383 t->len = tok_check_len(len);
384 if (len > INLINE_TEXT) {
385 t->text.p.ptr = text;
386 } else {
387 memcpy(t->text.a, text, len+1);
388 nasm_free(text);
389 }
390
391 return t;
392}
393
394/*
395 * Allocate a new buffer containing a copy of the text field
396 * of the token.
397 */
398static char *dup_text(const struct Token *t)
399{
400 size_t size = t->len + 1;
401 char *p = nasm_malloc(size);
402
403 return memcpy(p, tok_text(t), size);
404}
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
406/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800407 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408 * these, which is essentially a container to allow several linked
409 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700410 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411 * Note that in this module, linked lists are treated as stacks
412 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413 * `expansion' field in MMacro structures, so that the linked list,
414 * if walked, would give the macro lines in reverse order; this
415 * means that we can walk the list when expanding a macro, and thus
416 * push the lines on to the `expansion' field in _istk_ in reverse
417 * order (so that when popped back off they are in the right
418 * order). It may seem cockeyed, and it relies on my design having
419 * an even number of steps in, but it works...
420 *
421 * Some of these structures, rather than being actual lines, are
422 * markers delimiting the end of the expansion of a given macro.
423 * This is for use in the cycle-tracking and %rep-handling code.
424 * Such structures have `finishes' non-NULL, and `first' NULL. All
425 * others have `finishes' NULL, but `first' may still be NULL if
426 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800429 Line *next;
430 MMacro *finishes;
431 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500432};
433
434/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000435 * To handle an arbitrary level of file inclusion, we maintain a
436 * stack (ie linked list) of these things.
437 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000438struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800439 Include *next;
440 FILE *fp;
441 Cond *conds;
442 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700443 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700444 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700445 int lineno, lineinc;
446 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000447};
448
449/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700450 * File real name hash, so we don't have to re-search the include
451 * path for every pass (and potentially more than that if a file
452 * is used more than once.)
453 */
454struct hash_table FileHash;
455
456/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700457 * Counters to trap on insane macro recursion or processing.
458 * Note: for smacros these count *down*, for mmacros they count *up*.
459 */
460struct deadman {
461 int64_t total; /* Total number of macros/tokens */
462 int64_t levels; /* Descent depth across all macros */
463 bool triggered; /* Already triggered, no need for error msg */
464};
465
466static struct deadman smacro_deadman, mmacro_deadman;
467
468/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469 * Conditional assembly: we maintain a separate stack of these for
470 * each level of file inclusion. (The only reason we keep the
471 * stacks separate is to ensure that a stray `%endif' in a file
472 * included from within the true branch of a `%if' won't terminate
473 * it and cause confusion: instead, rightly, it'll cause an error.)
474 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700475enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000476 /*
477 * These states are for use just after %if or %elif: IF_TRUE
478 * means the condition has evaluated to truth so we are
479 * currently emitting, whereas IF_FALSE means we are not
480 * currently emitting but will start doing so if a %else comes
481 * up. In these states, all directives are admissible: %elif,
482 * %else and %endif. (And of course %if.)
483 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800484 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000485 /*
486 * These states come up after a %else: ELSE_TRUE means we're
487 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
488 * any %elif or %else will cause an error.
489 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800490 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000491 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200492 * These states mean that we're not emitting now, and also that
493 * nothing until %endif will be emitted at all. COND_DONE is
494 * used when we've had our moment of emission
495 * and have now started seeing %elifs. COND_NEVER is used when
496 * the condition construct in question is contained within a
497 * non-emitting branch of a larger condition construct,
498 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000499 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000501};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700502struct Cond {
503 Cond *next;
504 enum cond_state state;
505};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800506#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000507
H. Peter Anvin70653092007-10-19 14:42:29 -0700508/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000509 * These defines are used as the possible return values for do_directive
510 */
511#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300512#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000513
Keith Kanios852f1ee2009-07-12 00:19:55 -0500514/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000515 * Condition codes. Note that we use c_ prefix not C_ because C_ is
516 * used in nasm.h for the "real" condition codes. At _this_ level,
517 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
518 * ones, so we need a different enum...
519 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700520static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000521 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
522 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000523 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000524};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700525enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000526 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
527 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 -0700528 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
529 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000530};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700531static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000532 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
533 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 +0000534 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000535};
536
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800537/*
538 * Directive names.
539 */
540/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
541static int is_condition(enum preproc_token arg)
542{
543 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
544}
545
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000546/* For TASM compatibility we need to be able to recognise TASM compatible
547 * conditional compilation directives. Using the NASM pre-processor does
548 * not work, so we look for them specifically from the following list and
549 * then jam in the equivalent NASM directive into the input stream.
550 */
551
H. Peter Anvine2c80182005-01-15 22:15:51 +0000552enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000553 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
554 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
555};
556
H. Peter Anvin476d2862007-10-02 22:04:15 -0700557static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000558 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
559 "ifndef", "include", "local"
560};
561
562static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700563static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000564static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800565static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000566
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567static Context *cstk;
568static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300569static const struct strlist *ipath_list;
H. Peter Anvind2354082019-08-27 16:38:48 -0700570static bool do_aliases;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000571
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300572static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000573
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300574static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000575
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800576static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700577static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800578static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000579
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000580/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800581 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800583static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584
585/*
586 * The current set of single-line macros we have defined.
587 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700588static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589
590/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800591 * The multi-line macro we are currently defining, or the %rep
592 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000593 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800594static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595
Charles Crayned4200be2008-07-12 16:42:33 -0700596static uint64_t nested_mac_count;
597static uint64_t nested_rep_count;
598
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000599/*
600 * The number of macro parameters to allocate space for at a time.
601 */
602#define PARAM_DELTA 16
603
604/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700605 * The standard macro set: defined in macros.c in a set of arrays.
606 * This gives our position in any macro set, while we are processing it.
607 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000608 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700609static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700610static macros_t **stdmacnext;
611static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300612static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000613
614/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700615 * Map of which %use packages have been loaded
616 */
617static bool *use_loaded;
618
619/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000620 * Forward declarations.
621 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700622static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000623static Token *expand_mmac_params(Token * tline);
624static Token *expand_smacro(Token * tline);
625static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400626static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700627static Token *make_tok_num(Token *next, int64_t val);
628static Token *make_tok_qstr(Token *next, const char *str);
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -0700629static Token *make_tok_qstr_len(Token *next, const char *str, size_t len);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700630static Token *make_tok_char(Token *next, char op);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700631static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700632 const char *text, size_t txtlen);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700633static Token *new_Token_free(Token * next, enum pp_token_type type,
634 char *text, size_t txtlen);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700635static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700636static Token *new_White(Token *next);
H. Peter Anvin8571f062019-09-23 16:40:03 -0700637static Token *delete_Token(Token *t);
638static Token *steal_Token(Token *dst, Token *src);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700639static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -0700640get_use_pkg(Token *t, const char *dname, const char **name);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000641
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700642/* Safe test for token type, false on x == NULL */
643static inline bool tok_type(const Token *x, enum pp_token_type t)
644{
645 return x && x->type == t;
646}
647
648/* Whitespace token? */
649static inline bool tok_white(const Token *x)
650{
651 return tok_type(x, TOK_WHITESPACE);
652}
653
654/* Skip past any whitespace */
655static inline Token *skip_white(Token *x)
656{
657 while (tok_white(x))
658 x = x->next;
659
660 return x;
661}
662
663/* Delete any whitespace */
664static Token *zap_white(Token *x)
665{
666 while (tok_white(x))
667 x = delete_Token(x);
668
669 return x;
670}
671
H. Peter Anvin8571f062019-09-23 16:40:03 -0700672/*
673 * Single special character tests. The use of & rather than && is intentional; it
674 * tells the compiler that it is safe to access text.a[1] unconditionally; hopefully
675 * a smart compiler should turn it into a 16-bit memory reference.
676 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700677static inline bool tok_is(const Token *x, char c)
678{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700679 return x && ((x->text.a[0] == c) & !x->text.a[1]);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700680}
681
682/* True if any other kind of token that "c", but not NULL */
683static inline bool tok_isnt(const Token *x, char c)
684{
H. Peter Anvin8571f062019-09-23 16:40:03 -0700685 return x && !((x->text.a[0] == c) & !x->text.a[1]);
686}
687
688/*
689 * Unquote a token if it is a string, and set its type to
690 * TOK_INTERNAL_STRING.
691 */
692static const char *unquote_token(Token *t)
693{
694 if (t->type != TOK_STRING)
695 return tok_text(t);
696
697 t->type = TOK_INTERNAL_STRING;
698
699 if (t->len > INLINE_TEXT) {
700 char *p = t->text.p.ptr;
701
702 t->len = nasm_unquote(p, NULL);
703
704 if (t->len <= INLINE_TEXT) {
705 nasm_zero(t->text.a);
706 memcpy(t->text.a, p, t->len);
707 nasm_free(p);
708 return t->text.a;
709 } else {
710 return p;
711 }
712 } else {
713 t->len = nasm_unquote(t->text.a, NULL);
714 return t->text.a;
715 }
716}
717
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700718/*
719 * Same as unquote_token(), but error out if the resulting string
720 * contains unacceptable control characters.
721 */
H. Peter Anvin8571f062019-09-23 16:40:03 -0700722static const char *unquote_token_cstr(Token *t)
723{
724 if (t->type != TOK_STRING)
725 return tok_text(t);
726
727 t->type = TOK_INTERNAL_STRING;
728
729 if (t->len > INLINE_TEXT) {
730 char *p = t->text.p.ptr;
731
732 t->len = nasm_unquote_cstr(p, NULL);
733
734 if (t->len <= INLINE_TEXT) {
735 nasm_zero(t->text.a);
736 memcpy(t->text.a, p, t->len);
737 nasm_free(p);
738 return t->text.a;
739 } else {
740 return p;
741 }
742 } else {
743 t->len = nasm_unquote_cstr(t->text.a, NULL);
744 return t->text.a;
745 }
746}
747
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700748/*
749 * Convert a TOK_INTERNAL_STRING token to a quoted
750 * TOK_STRING tokens.
751 */
752static Token *quote_any_token(Token *t);
753static inline Token *quote_token(Token *t)
754{
755 if (likely(!tok_is(t, TOK_INTERNAL_STRING)))
756 return t;
757
758 return quote_any_token(t);
759}
760
761/*
762 * Convert *any* kind of token to a quoted
763 * TOK_STRING token.
764 */
765static Token *quote_any_token(Token *t)
H. Peter Anvin8571f062019-09-23 16:40:03 -0700766{
767 size_t len;
768 char *p;
769
770 p = nasm_quote(tok_text(t), &len);
771 t->type = TOK_STRING;
772 return set_text_free(t, p, len);
773}
774
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400775/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700776 * In-place reverse a list of tokens.
777 */
778static Token *reverse_tokens(Token *t)
779{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800780 Token *prev = NULL;
781 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700782
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800783 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400784 next = t->next;
785 t->next = prev;
786 prev = t;
787 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800788 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700789
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800790 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700791}
792
793/*
H. Peter Anvin8571f062019-09-23 16:40:03 -0700794 * getenv() variant operating on an input token
795 */
796static const char *pp_getenv(const Token *t, bool warn)
797{
798 const char *txt = tok_text(t);
799 const char *v;
800 char *buf = NULL;
801 bool is_string = false;
802
803 if (!t)
804 return NULL;
805
806 switch (t->type) {
807 case TOK_ENVIRON:
808 txt += 2; /* Skip leading %! */
809 is_string = nasm_isquote(*txt);
810 break;
811
812 case TOK_STRING:
813 is_string = true;
814 break;
815
816 case TOK_INTERNAL_STRING:
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -0700817 case TOK_NAKED_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -0700818 case TOK_ID:
819 is_string = false;
820 break;
821
822 default:
823 return NULL;
824 }
825
826 if (is_string) {
827 buf = nasm_strdup(txt);
828 nasm_unquote_cstr(buf, NULL);
829 txt = buf;
830 }
831
832 v = getenv(txt);
833 if (warn && !v) {
834 /*!
835 *!environment [on] nonexistent environment variable
836 *! warns if a nonexistent environment variable
837 *! is accessed using the \c{%!} preprocessor
838 *! construct (see \k{getenv}.) Such environment
839 *! variables are treated as empty (with this
840 *! warning issued) starting in NASM 2.15;
841 *! earlier versions of NASM would treat this as
842 *! an error.
843 */
844 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", txt);
845 v = "";
846 }
847
848 if (buf)
849 nasm_free(buf);
850
851 return v;
852}
853
854/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300855 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000856 * front of them. We do it here because I could not find any other
857 * place to do it for the moment, and it is a hack (ideally it would
858 * be nice to be able to use the NASM pre-processor to do it).
859 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000860static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000861{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000862 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400863 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000864
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400865 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000866
867 /* Binary search for the directive name */
868 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400869 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400870 q = nasm_skip_word(p);
871 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000872 if (len) {
873 oldchar = p[len];
874 p[len] = 0;
875 while (j - i > 1) {
876 k = (j + i) / 2;
877 m = nasm_stricmp(p, tasm_directives[k]);
878 if (m == 0) {
879 /* We have found a directive, so jam a % in front of it
880 * so that NASM will then recognise it as one if it's own.
881 */
882 p[len] = oldchar;
883 len = strlen(p);
884 oldline = line;
885 line = nasm_malloc(len + 2);
886 line[0] = '%';
887 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700888 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300889 * NASM does not recognise IFDIFI, so we convert
890 * it to %if 0. This is not used in NASM
891 * compatible code, but does need to parse for the
892 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000893 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700894 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000895 } else {
896 memcpy(line + 1, p, len + 1);
897 }
898 nasm_free(oldline);
899 return line;
900 } else if (m < 0) {
901 j = k;
902 } else
903 i = k;
904 }
905 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000906 }
907 return line;
908}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000909
H. Peter Anvin76690a12002-04-30 20:52:49 +0000910/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000911 * The pre-preprocessing stage... This function translates line
912 * number indications as they emerge from GNU cpp (`# lineno "file"
913 * flags') into NASM preprocessor line number indications (`%line
914 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000915 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000916static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000917{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000918 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000919 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000920
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 if (line[0] == '#' && line[1] == ' ') {
922 oldline = line;
923 fname = oldline + 2;
924 lineno = atoi(fname);
925 fname += strspn(fname, "0123456789 ");
926 if (*fname == '"')
927 fname++;
928 fnlen = strcspn(fname, "\"");
929 line = nasm_malloc(20 + fnlen);
930 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
931 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000932 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000933 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000935 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000936}
937
938/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000939 * Free a linked list of tokens.
940 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000942{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400943 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000945}
946
947/*
948 * Free a linked list of lines.
949 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000951{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400952 Line *l, *tmp;
953 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 free_tlist(l->first);
955 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000956 }
957}
958
959/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700960 * Free an array of linked lists of tokens
961 */
962static void free_tlist_array(Token **array, size_t nlists)
963{
964 Token **listp = array;
965
966 while (nlists--)
967 free_tlist(*listp++);
968
969 nasm_free(array);
970}
971
972/*
973 * Duplicate a linked list of tokens.
974 */
975static Token *dup_tlist(const Token *list, Token ***tailp)
976{
977 Token *newlist = NULL;
978 Token **tailpp = &newlist;
979 const Token *t;
980
981 list_for_each(t, list) {
982 Token *nt;
983 *tailpp = nt = dup_Token(NULL, t);
984 tailpp = &nt->next;
985 }
986
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700987 if (tailp) {
988 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700989 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700990 }
991
992 return newlist;
993}
994
995/*
996 * Duplicate a linked list of tokens with a maximum count
997 */
998static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
999{
1000 Token *newlist = NULL;
1001 Token **tailpp = &newlist;
1002 const Token *t;
1003
1004 list_for_each(t, list) {
1005 Token *nt;
1006 if (!cnt--)
1007 break;
1008 *tailpp = nt = dup_Token(NULL, t);
1009 tailpp = &nt->next;
1010 }
1011
1012 if (tailp) {
1013 **tailp = newlist;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07001014 if (newlist)
1015 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001016 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001017
1018 return newlist;
1019}
1020
1021/*
1022 * Duplicate a linked list of tokens in reverse order
1023 */
1024static Token *dup_tlist_reverse(const Token *list, Token *tail)
1025{
1026 const Token *t;
1027
1028 list_for_each(t, list)
1029 tail = dup_Token(tail, t);
1030
1031 return tail;
1032}
1033
1034/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001035 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +00001036 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001037static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001038{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001039 nasm_free(m->name);
1040 free_tlist(m->dlist);
1041 nasm_free(m->defaults);
1042 free_llist(m->expansion);
1043 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001044}
1045
1046/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001047 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -08001048 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001049static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -08001050{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001051 if (s->params) {
1052 int i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001053 for (i = 0; i < s->nparam; i++) {
1054 if (s->params[i].name.len > INLINE_TEXT)
1055 nasm_free(s->params[i].name.text.p.ptr);
1056 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001057 nasm_free(s->params);
1058 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08001059 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001060 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001061}
1062
1063static void clear_smacro(SMacro *s)
1064{
1065 free_smacro_members(s);
1066 /* Wipe everything except the next pointer */
1067 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
1068}
1069
1070/*
1071 * Free an SMacro
1072 */
1073static void free_smacro(SMacro *s)
1074{
1075 free_smacro_members(s);
1076 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001077}
1078
1079/*
H. Peter Anvin97a23472007-09-16 17:57:25 -07001080 * Free all currently defined macros, and free the hash tables
1081 */
H. Peter Anvin072771e2008-05-22 13:17:51 -07001082static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -07001083{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001084 struct hash_iterator it;
1085 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001086
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001087 hash_for_each(smt, it, np) {
1088 SMacro *tmp;
1089 SMacro *s = np->data;
1090 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -08001091 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001092 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001093 }
H. Peter Anvin072771e2008-05-22 13:17:51 -07001094 hash_free(smt);
1095}
1096
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001097static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -07001098{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001099 struct hash_iterator it;
1100 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001101
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001102 hash_for_each(mmt, it, np) {
1103 MMacro *tmp;
1104 MMacro *m = np->data;
1105 nasm_free((void *)np->key);
1106 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001107 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001108 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001109 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001110}
1111
1112static void free_macros(void)
1113{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001114 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001115 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -07001116}
1117
1118/*
1119 * Initialize the hash tables
1120 */
1121static void init_macros(void)
1122{
H. Peter Anvin97a23472007-09-16 17:57:25 -07001123}
1124
1125/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001126 * Pop the context stack.
1127 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001129{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001130 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001131
1132 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -07001133 free_smacro_table(&c->localmac);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001134 nasm_free((char *)c->name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00001135 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001136}
1137
H. Peter Anvin072771e2008-05-22 13:17:51 -07001138/*
1139 * Search for a key in the hash index; adding it if necessary
1140 * (in which case we initialize the data pointer to NULL.)
1141 */
1142static void **
1143hash_findi_add(struct hash_table *hash, const char *str)
1144{
1145 struct hash_insert hi;
1146 void **r;
1147 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001148 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001149
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001150 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001151 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001152 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -07001153
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08001154 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
1155 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -07001156 return hash_add(&hi, strx, NULL);
1157}
1158
1159/*
1160 * Like hash_findi, but returns the data element rather than a pointer
1161 * to it. Used only when not adding a new element, hence no third
1162 * argument.
1163 */
1164static void *
1165hash_findix(struct hash_table *hash, const char *str)
1166{
1167 void **p;
1168
1169 p = hash_findi(hash, str, NULL);
1170 return p ? *p : NULL;
1171}
1172
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001173/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001174 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001175 * if there no more left -- return NULL
1176 */
1177static char *line_from_stdmac(void)
1178{
1179 unsigned char c;
1180 const unsigned char *p = stdmacpos;
1181 char *line, *q;
1182 size_t len = 0;
1183
1184 if (!stdmacpos)
1185 return NULL;
1186
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001187 /*
1188 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
1189 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
1190 */
1191 while ((c = *p++) != 127) {
1192 uint8_t ndir = c - 128;
1193 if (ndir < 256-96)
1194 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001195 else
1196 len++;
1197 }
1198
1199 line = nasm_malloc(len + 1);
1200 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001201
1202 while ((c = *stdmacpos++) != 127) {
1203 uint8_t ndir = c - 128;
1204 if (ndir < 256-96) {
1205 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
1206 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001207 *q++ = ' ';
1208 } else {
1209 *q++ = c;
1210 }
1211 }
1212 stdmacpos = p;
1213 *q = '\0';
1214
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -07001215 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -07001216 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001217 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -07001218 if (*stdmacnext) {
1219 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001220 } else if (do_predef) {
1221 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001222
1223 /*
1224 * Nasty hack: here we push the contents of
1225 * `predef' on to the top-level expansion stack,
1226 * since this is the most convenient way to
1227 * implement the pre-include and pre-define
1228 * features.
1229 */
1230 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07001231 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001232 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07001233 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001234 l->finishes = NULL;
1235
1236 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +04001237 }
1238 do_predef = false;
1239 }
1240 }
1241
1242 return line;
1243}
1244
H. Peter Anvin6686de22019-08-10 05:33:14 -07001245/*
1246 * Read a line from a file. Return NULL on end of file.
1247 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001248static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001249{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001250 int c;
1251 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001252 const unsigned int delta = 512;
1253 const unsigned int pad = 8;
1254 unsigned int nr_cont = 0;
1255 bool cont = false;
1256 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001257 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001258
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001259 size = delta;
1260 p = buffer = nasm_malloc(size);
1261
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001262 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001263 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001264
1265 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001266 case EOF:
1267 if (p == buffer) {
1268 nasm_free(buffer);
1269 return NULL;
1270 }
1271 c = 0;
1272 break;
1273
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001274 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001275 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001276 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001277 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001278 if (cont) {
1279 cont = false;
1280 continue;
1281 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001282 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001283 break;
1284
1285 case '\n':
1286 if (cont) {
1287 cont = false;
1288 continue;
1289 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001290 c = 0;
1291 break;
1292
1293 case 032: /* ^Z = legacy MS-DOS end of file mark */
1294 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001295 break;
1296
1297 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001298 next = fgetc(f);
1299 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001300 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001301 cont = true;
1302 nr_cont++;
1303 continue;
1304 }
1305 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001307
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001308 if (p >= (buffer + size - pad)) {
1309 buffer = nasm_realloc(buffer, size + delta);
1310 p = buffer + size - pad;
1311 size += delta;
1312 }
1313
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001314 *p++ = c;
1315 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001317 lineno = src_get_linnum() + istk->lineinc +
1318 (nr_cont * istk->lineinc);
1319 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001320
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001321 return buffer;
1322}
1323
1324/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001325 * Common read routine regardless of source
1326 */
1327static char *read_line(void)
1328{
1329 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001330 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001331
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001332 if (f)
1333 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001334 else
1335 line = line_from_stdmac();
1336
1337 if (!line)
1338 return NULL;
1339
1340 if (!istk->nolist)
1341 lfmt->line(LIST_READ, src_get_linnum(), line);
1342
1343 return line;
1344}
1345
1346/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001347 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001348 * don't need to parse the value out of e.g. numeric tokens: we
1349 * simply split one string into many.
1350 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001351static Token *tokenize(const char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001352{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001353 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001354 Token *list = NULL;
1355 Token *t, **tail = &list;
1356
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 while (*line) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001358 const char *p = line;
1359 const char *ep = NULL; /* End of token, for trimming the end */
1360 size_t toklen;
1361 char firstchar = *p; /* Can be used to override the first char */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001362
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 if (*p == '%') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001364 /*
1365 * Preprocessor construct; find the end of the token.
1366 * Classification is handled later, because %{...} can be
1367 * used to create any preprocessor token.
1368 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001370 if (*p == '+' && !nasm_isdigit(p[1])) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001371 /* Paste token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001372 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001373 } else if (nasm_isdigit(*p) ||
1374 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 do {
1376 p++;
1377 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001378 while (nasm_isdigit(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001379 } else if (*p == '{' || *p == '[') {
1380 /* %{...} or %[...] */
1381 char firstchar = *p;
1382 char endchar = *p + 2; /* } or ] */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001383 int lvl = 1;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001384 line += (*p++ == '{'); /* Skip { but not [ (yet) */
1385 while (lvl) {
1386 if (*p == firstchar) {
1387 lvl++;
1388 } else if (*p == endchar) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001389 lvl--;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001390 } else if (nasm_isquote(*p)) {
1391 p = nasm_skip_string(p);
1392 }
1393
1394 /*
1395 * *p can have been advanced to a null character by
1396 * nasm_skip_string()
1397 */
1398 if (!*p) {
1399 nasm_warn(WARN_OTHER, "unterminated %%%c construct",
1400 firstchar);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001401 break;
1402 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001403 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001404 }
1405 ep = lvl ? p : p-1; /* Terminal character not part of token */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001406 } else if (*p == '?') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001407 /* %? or %?? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001408 p++;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001409 if (*p == '?')
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001410 p++;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001411 } else if (*p == '!') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001412 /* Environment variable reference */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001413 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001414 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001415 do {
1416 p++;
1417 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001418 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001419 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001420 p = nasm_skip_string(p);
1421 if (*p)
1422 p++;
1423 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001424 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001425 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001426 /* %! without anything else... */
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001427 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001428 } else if (*p == ',') {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001429 /* Conditional comma */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001430 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001431 } else if (nasm_isidchar(*p) ||
H. Peter Anvin8571f062019-09-23 16:40:03 -07001432 ((*p == '%' || *p == '$') && nasm_isidchar(p[1]))) {
1433 /* Identifier or some sort */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 do {
1435 p++;
1436 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001437 while (nasm_isidchar(*p));
H. Peter Anvin8571f062019-09-23 16:40:03 -07001438 } else if (*p == '%') {
1439 /* %% operator */
1440 p++;
1441 }
1442
1443 if (!ep)
1444 ep = p;
1445 toklen = ep - line;
1446
1447 /* Classify here, to handle %{...} correctly */
1448 if (toklen < 2) {
1449 type = TOK_OTHER; /* % operator */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001451 char c0 = line[1];
1452
1453 switch (c0) {
1454 case '+':
1455 type = (toklen == 2) ? TOK_PASTE : TOK_MMACRO_PARAM;
1456 break;
1457
1458 case '-':
1459 type = TOK_MMACRO_PARAM;
1460 break;
1461
1462 case '?':
1463 if (toklen == 2)
1464 type = TOK_PREPROC_Q;
1465 else if (toklen == 3 && line[2] == '?')
1466 type = TOK_PREPROC_QQ;
1467 else
1468 type = TOK_PREPROC_ID;
1469 break;
1470
1471 case '!':
1472 type = (toklen == 2) ? TOK_OTHER : TOK_ENVIRON;
1473 break;
1474
1475 case '%':
1476 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_SYMBOL;
1477 break;
1478
1479 case '$':
1480 type = (toklen == 2) ? TOK_OTHER : TOK_LOCAL_MACRO;
1481 break;
1482
1483 case '[':
1484 line += 2; /* Skip %[ */
1485 firstchar = *line; /* Don't clobber */
1486 toklen -= 2;
1487 type = TOK_INDIRECT;
1488 break;
1489
1490 case ',':
1491 type = (toklen == 2) ? TOK_COND_COMMA : TOK_PREPROC_ID;
1492 break;
1493
1494 case '\'':
1495 case '\"':
1496 case '`':
1497 /* %{'string'} */
1498 type = TOK_PREPROC_ID;
1499 break;
1500
1501 case ':':
1502 type = TOK_MMACRO_PARAM; /* %{:..} */
1503 break;
1504
1505 default:
1506 if (nasm_isdigit(c0))
1507 type = TOK_MMACRO_PARAM;
1508 else if (nasm_isidchar(c0) || toklen > 2)
1509 type = TOK_PREPROC_ID;
1510 else
1511 type = TOK_OTHER;
1512 break;
1513 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001514 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001515 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001516 /*
1517 * An identifier. This includes the ? operator, which is
1518 * treated as a keyword, not as a special character
1519 * operator
1520 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001522 while (nasm_isidchar(*++p))
1523 ;
1524 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 /*
1526 * A string token.
1527 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001530
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 if (*p) {
1532 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001533 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001534 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 /* Handling unterminated strings by UNV */
1536 /* type = -1; */
1537 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001538 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001539 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001540 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001541 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001542 bool is_hex = false;
1543 bool is_float = false;
1544 bool has_e = false;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001545 char c;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001546
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001548 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001550
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001551 if (*p == '$') {
1552 p++;
1553 is_hex = true;
1554 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001555
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001556 for (;;) {
1557 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001558
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001559 if (!is_hex && (c == 'e' || c == 'E')) {
1560 has_e = true;
1561 if (*p == '+' || *p == '-') {
1562 /*
1563 * e can only be followed by +/- if it is either a
1564 * prefixed hex number or a floating-point number
1565 */
1566 p++;
1567 is_float = true;
1568 }
1569 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1570 is_hex = true;
1571 } else if (c == 'P' || c == 'p') {
1572 is_float = true;
1573 if (*p == '+' || *p == '-')
1574 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001575 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001576 ; /* just advance */
1577 else if (c == '.') {
1578 /*
1579 * we need to deal with consequences of the legacy
1580 * parser, like "1.nolist" being two tokens
1581 * (TOK_NUMBER, TOK_ID) here; at least give it
1582 * a shot for now. In the future, we probably need
1583 * a flex-based scanner with proper pattern matching
1584 * to do it as well as it can be done. Nothing in
1585 * the world is going to help the person who wants
1586 * 0x123.p16 interpreted as two tokens, though.
1587 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07001588 const char *r = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001589 while (*r == '_')
1590 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001591
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001592 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1593 (!is_hex && (*r == 'e' || *r == 'E')) ||
1594 (*r == 'p' || *r == 'P')) {
1595 p = r;
1596 is_float = true;
1597 } else
1598 break; /* Terminate the token */
1599 } else
1600 break;
1601 }
1602 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001603
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001604 if (p == line+1 && *line == '$') {
1605 type = TOK_OTHER; /* TOKEN_HERE */
1606 } else {
1607 if (has_e && !is_hex) {
1608 /* 1e13 is floating-point, but 1e13h is not */
1609 is_float = true;
1610 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001611
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001612 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1613 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001614 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001616 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 /*
1618 * Whitespace just before end-of-line is discarded by
1619 * pretending it's a comment; whitespace just before a
1620 * comment gets lumped into the comment.
1621 */
1622 if (!*p || *p == ';') {
1623 type = TOK_COMMENT;
1624 while (*p)
1625 p++;
1626 }
1627 } else if (*p == ';') {
1628 type = TOK_COMMENT;
1629 while (*p)
1630 p++;
1631 } else {
1632 /*
1633 * Anything else is an operator of some kind. We check
1634 * for all the double-character operators (>>, <<, //,
H. Peter Anvin8571f062019-09-23 16:40:03 -07001635 * %%, <=, >=, ==, !=, <>, &&, ||, ^^) and the triple-
1636 * character operators (<<<, >>>, <=>) but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001637 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 */
1639 type = TOK_OTHER;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001640 switch (*p++) {
1641 case '>':
1642 if (*p == '>') {
1643 p++;
1644 if (*p == '>')
1645 p++;
H. Peter Anvind03a6c82019-10-07 21:29:05 -07001646 } else if (*p == '=') {
1647 p++;
1648 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07001649 break;
1650
1651 case '<':
1652 if (*p == '<') {
1653 p++;
1654 if (*p == '<')
1655 p++;
1656 } else if (*p == '=') {
1657 p++;
1658 if (*p == '>')
1659 p++;
1660 } else if (*p == '>') {
1661 p++;
1662 }
1663 break;
1664
1665 case '!':
1666 if (*p == '=')
1667 p++;
1668 break;
1669
1670 case '/':
1671 case '=':
1672 case '&':
1673 case '|':
1674 case '^':
1675 /* These operators can be doubled but nothing else */
1676 if (*p == p[-1])
1677 p++;
1678 break;
1679
1680 default:
1681 break;
1682 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001683 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001684
H. Peter Anvin8571f062019-09-23 16:40:03 -07001685 if (type == TOK_WHITESPACE) {
1686 *tail = t = new_White(NULL);
1687 tail = &t->next;
1688 } else if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001689 if (!ep)
1690 ep = p;
1691 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvin8571f062019-09-23 16:40:03 -07001692 *tok_text_buf(t) = firstchar; /* E.g. %{foo} -> {foo -> %foo */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 tail = &t->next;
1694 }
1695 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001697 return list;
1698}
1699
H. Peter Anvin8571f062019-09-23 16:40:03 -07001700/*
1701 * Tokens are allocated in blocks to improve speed. Set the blocksize
1702 * to 0 to use regular nasm_malloc(); this is useful for debugging.
1703 *
1704 * alloc_Token() returns a zero-initialized token structure.
1705 */
1706#define TOKEN_BLOCKSIZE 4096
1707
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001708#if TOKEN_BLOCKSIZE
H. Peter Anvin8571f062019-09-23 16:40:03 -07001709
1710static Token *freeTokens = NULL;
1711static Token *tokenblocks = NULL;
1712
1713static Token *alloc_Token(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001714{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001715 Token *t = freeTokens;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716
H. Peter Anvin8571f062019-09-23 16:40:03 -07001717 if (unlikely(!t)) {
1718 Token *block;
1719 size_t i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720
H. Peter Anvin8571f062019-09-23 16:40:03 -07001721 nasm_newn(block, TOKEN_BLOCKSIZE);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001722
H. Peter Anvin8571f062019-09-23 16:40:03 -07001723 /*
1724 * The first entry in each array are a linked list of
1725 * block allocations and is not used for data.
1726 */
1727 block[0].next = tokenblocks;
1728 block[0].type = TOK_BLOCK;
1729 tokenblocks = block;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001730
H. Peter Anvin8571f062019-09-23 16:40:03 -07001731 /*
1732 * Add the rest to the free list
1733 */
1734 for (i = 2; i < TOKEN_BLOCKSIZE - 1; i++)
1735 block[i].next = &block[i+1];
1736
1737 freeTokens = &block[2];
1738
1739 /*
1740 * Return the topmost usable token
1741 */
1742 return &block[1];
1743 }
1744
1745 freeTokens = t->next;
1746 t->next = NULL;
1747 return t;
H. Peter Anvince616072002-04-30 21:02:23 +00001748}
1749
H. Peter Anvin8571f062019-09-23 16:40:03 -07001750static Token *delete_Token(Token *t)
1751{
1752 Token *next = t->next;
1753
1754 nasm_zero(*t);
1755 t->next = freeTokens;
1756 freeTokens = t;
1757
1758 return next;
1759}
1760
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001762{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001763 Token *block, *blocktmp;
H. Peter Anvince616072002-04-30 21:02:23 +00001764
H. Peter Anvin8571f062019-09-23 16:40:03 -07001765 list_for_each_safe(block, blocktmp, tokenblocks)
1766 nasm_free(block);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001767
H. Peter Anvin8571f062019-09-23 16:40:03 -07001768 freeTokens = tokenblocks = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001770
H. Peter Anvin8571f062019-09-23 16:40:03 -07001771#else
1772
1773static inline Token *alloc_Token(void)
1774{
1775 Token *t;
1776 nasm_new(*t);
1777 return t;
1778}
1779
1780static Token *delete_Token(Token *t)
1781{
1782 Token *next = t->next;
1783 nasm_free(t);
1784 return next;
1785}
1786
1787static inline void delete_Blocks(void)
1788{
1789 /* Nothing to do */
1790}
1791
1792#endif
1793
H. Peter Anvin734b1882002-04-30 21:01:08 +00001794/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001795 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001796 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001797 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001798static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001799 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001800{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001801 Token *t = alloc_Token();
1802 char *textp;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001803
H. Peter Anvin734b1882002-04-30 21:01:08 +00001804 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001805 t->type = type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001806 if (type == TOK_WHITESPACE) {
1807 t->len = 1;
1808 t->text.a[0] = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001810 if (text && text[0] && !txtlen)
1811 txtlen = tok_strlen(text);
1812
1813 t->len = tok_check_len(txtlen);
1814
1815 if (text) {
1816 textp = (txtlen > INLINE_TEXT)
1817 ? (t->text.p.ptr = nasm_malloc(txtlen+1)) : t->text.a;
1818 memcpy(textp, text, txtlen);
1819 textp[txtlen] = '\0'; /* In case we needed malloc() */
1820 } else {
1821 /*
1822 * Allocate a buffer but do not fill it. The caller
1823 * can fill in text, but must not change the length.
1824 * The filled in text must be exactly txtlen once
1825 * the buffer is filled and before the token is added
1826 * to any line lists.
1827 */
1828 if (txtlen > INLINE_TEXT)
1829 t->text.p.ptr = nasm_zalloc(txtlen+1);
1830 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00001831 }
1832 return t;
1833}
1834
H. Peter Anvin8571f062019-09-23 16:40:03 -07001835/*
1836 * Same as new_Token(), but text belongs to the new token and is
1837 * either taken over or freed. This function MUST be called
1838 * with valid txt and txtlen, unlike new_Token().
1839 */
1840static Token *new_Token_free(Token * next, enum pp_token_type type,
1841 char *text, size_t txtlen)
1842{
1843 Token *t = alloc_Token();
1844
1845 t->next = next;
1846 t->type = type;
1847 t->len = tok_check_len(txtlen);
1848
1849 if (txtlen <= INLINE_TEXT) {
1850 memcpy(t->text.a, text, txtlen);
1851 free(text);
1852 } else {
1853 t->text.p.ptr = text;
1854 }
1855
1856 return t;
1857}
1858
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001859static Token *dup_Token(Token *next, const Token *src)
1860{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001861 Token *t = alloc_Token();
1862
1863 memcpy(t, src, sizeof *src);
1864 t->next = next;
1865
1866 if (t->len > INLINE_TEXT) {
1867 t->text.p.ptr = nasm_malloc(t->len + 1);
1868 memcpy(t->text.p.ptr, src->text.p.ptr, t->len+1);
1869 }
1870
1871 return t;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001872}
1873
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001874static Token *new_White(Token *next)
1875{
H. Peter Anvin8571f062019-09-23 16:40:03 -07001876 Token *t = alloc_Token();
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001877
H. Peter Anvin8571f062019-09-23 16:40:03 -07001878 t->next = next;
1879 t->type = TOK_WHITESPACE;
1880 t->len = 1;
1881 t->text.a[0] = ' ';
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001882
H. Peter Anvin8571f062019-09-23 16:40:03 -07001883 return t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001884}
1885
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001886/*
H. Peter Anvin8571f062019-09-23 16:40:03 -07001887 * This *transfers* the content from one token to another, leaving the
1888 * next pointer of the latter intact. Unlike dup_Token(), the old
1889 * token is destroyed, except for its next pointer, and the text
1890 * pointer allocation, if any, is simply transferred.
1891 */
1892static Token *steal_Token(Token *dst, Token *src)
1893{
1894 /* Overwrite everything except the next pointers */
1895 memcpy((char *)dst + sizeof(Token *), (char *)src + sizeof(Token *),
1896 sizeof(Token) - sizeof(Token *));
1897
1898 /* Clear the donor token */
1899 memset((char *)src + sizeof(Token *), 0, sizeof(Token) - sizeof(Token *));
1900
1901 return dst;
1902}
1903
1904/*
1905 * Convert a line of tokens back into text. This modifies the list
1906 * by expanding environment variables.
1907 *
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001908 * If expand_locals is not zero, identifiers of the form "%$*xxx"
H. Peter Anvin8571f062019-09-23 16:40:03 -07001909 * are also transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001910 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001911static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001912{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001913 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001914 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001915 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001916
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001917 list_for_each(t, tlist) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07001918 switch (t->type) {
1919 case TOK_ENVIRON:
1920 {
1921 const char *v = pp_getenv(t, true);
1922 set_text(t, v, tok_strlen(v));
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07001923 t->type = TOK_NAKED_STRING;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001924 break;
1925 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001926
H. Peter Anvin8571f062019-09-23 16:40:03 -07001927 case TOK_LOCAL_MACRO:
1928 case TOK_LOCAL_SYMBOL:
1929 if (expand_locals) {
1930 const char *q;
1931 char *p;
1932 Context *ctx = get_ctx(tok_text(t), &q);
1933 if (ctx) {
1934 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1935 set_text_free(t, p, nasm_last_string_len());
1936 t->type = TOK_ID;
1937 }
1938 }
1939 break;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001940
H. Peter Anvin8571f062019-09-23 16:40:03 -07001941 default:
1942 break; /* No modifications */
1943 }
1944
1945 if (debug_level(2)) {
1946 unsigned int t_len = t->len;
1947 unsigned int s_len = tok_strlen(tok_text(t));
1948 if (t_len != s_len) {
1949 nasm_panic("assertion failed: token \"%s\" type %u len %u has t->len %u\n",
1950 tok_text(t), t->type, s_len, t_len);
1951 t->len = s_len;
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001952 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001953 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001954
H. Peter Anvin8571f062019-09-23 16:40:03 -07001955 len += t->len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001956 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001957
H. Peter Anvin734b1882002-04-30 21:01:08 +00001958 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001959
H. Peter Anvin8571f062019-09-23 16:40:03 -07001960 list_for_each(t, tlist)
1961 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001962 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001963
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001964 return line;
1965}
1966
1967/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001968 * A scanner, suitable for use by the expression evaluator, which
1969 * operates on a line of Tokens. Expects a pointer to a pointer to
1970 * the first token in the line to be passed in as its private_data
1971 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001972 *
1973 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001974 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001975struct ppscan {
1976 Token *tptr;
1977 int ntokens;
1978};
1979
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001981{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001982 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001983 Token *tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07001984 const char *txt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001985
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001987 if (pps->ntokens && (tline = pps->tptr)) {
1988 pps->ntokens--;
1989 pps->tptr = tline->next;
1990 } else {
1991 pps->tptr = NULL;
1992 pps->ntokens = 0;
1993 return tokval->t_type = TOKEN_EOS;
1994 }
1995 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001996
H. Peter Anvin8571f062019-09-23 16:40:03 -07001997 txt = tok_text(tline);
1998 tokval->t_charptr = (char *)txt; /* Fix this */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001999
H. Peter Anvin8571f062019-09-23 16:40:03 -07002000 if (txt[0] == '$') {
2001 if (!txt[1]) {
2002 return tokval->t_type = TOKEN_HERE;
2003 } else if (txt[1] == '$' && !txt[2]) {
2004 return tokval->t_type = TOKEN_BASE;
2005 } else if (tline->type == TOK_ID) {
2006 tokval->t_charptr++;
2007 return tokval->t_type = TOKEN_ID;
2008 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002009 }
2010
H. Peter Anvin8571f062019-09-23 16:40:03 -07002011 switch (tline->type) {
2012 default:
2013 if (tline->len == 1)
2014 return tokval->t_type = txt[0];
2015 /* fall through */
2016 case TOK_ID:
2017 return nasm_token_hash(txt, tokval);
2018
2019 case TOK_NUMBER:
2020 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 bool rn_error;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002022 tokval->t_integer = readnum(txt, &rn_error);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 if (rn_error)
2024 return tokval->t_type = TOKEN_ERRNUM;
2025 else
2026 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07002027 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002028
H. Peter Anvin8571f062019-09-23 16:40:03 -07002029 case TOK_FLOAT:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002031
2032 case TOK_STRING:
2033 tokval->t_charptr = (char *)unquote_token(tline);
2034 tokval->t_inttwo = tline->len;
2035 return tokval->t_type = TOKEN_STR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002036 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00002037}
2038
2039/*
H. Peter Anvind2354082019-08-27 16:38:48 -07002040 * 1. An expression (true if nonzero 0)
2041 * 2. The keywords true, on, yes for true
2042 * 3. The keywords false, off, no for false
2043 * 4. An empty line, for true
2044 *
2045 * On error, return defval (usually the previous value)
2046 */
2047static bool pp_get_boolean_option(Token *tline, bool defval)
2048{
2049 static const char * const noyes[] = {
2050 "no", "yes",
2051 "false", "true",
2052 "off", "on"
2053 };
2054 struct ppscan pps;
2055 struct tokenval tokval;
2056 expr *evalresult;
2057
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002058 tline = skip_white(tline);
2059 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07002060 return true;
2061
2062 if (tline->type == TOK_ID) {
2063 size_t i;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002064 const char *txt = tok_text(tline);
2065
H. Peter Anvind2354082019-08-27 16:38:48 -07002066 for (i = 0; i < ARRAY_SIZE(noyes); i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002067 if (!nasm_stricmp(txt, noyes[i]))
H. Peter Anvind2354082019-08-27 16:38:48 -07002068 return i & 1;
2069 }
2070
2071 pps.tptr = NULL;
2072 pps.tptr = tline;
2073 pps.ntokens = -1;
2074 tokval.t_type = TOKEN_INVALID;
2075 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
2076
2077 if (!evalresult)
2078 return true;
2079
2080 if (tokval.t_type)
2081 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
2082 if (!is_really_simple(evalresult)) {
2083 nasm_nonfatal("boolean flag expression must be a constant");
2084 return defval;
2085 }
2086
2087 return reloc_value(evalresult) != 0;
2088}
2089
2090/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002091 * Compare a string to the name of an existing macro; this is a
2092 * simple wrapper which calls either strcmp or nasm_stricmp
2093 * depending on the value of the `casesense' parameter.
2094 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002095static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002096{
H. Peter Anvin734b1882002-04-30 21:01:08 +00002097 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002098}
2099
2100/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002101 * Compare a string to the name of an existing macro; this is a
2102 * simple wrapper which calls either strcmp or nasm_stricmp
2103 * depending on the value of the `casesense' parameter.
2104 */
2105static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
2106{
2107 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
2108}
2109
2110/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002111 * Return the Context structure associated with a %$ token. Return
2112 * NULL, having _already_ reported an error condition, if the
2113 * context stack isn't deep enough for the supplied number of $
2114 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002115 *
2116 * If "namep" is non-NULL, set it to the pointer to the macro name
2117 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002118 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002119static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002120{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002121 Context *ctx;
2122 int i;
2123
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002124 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002126
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002127 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00002128 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002129
H. Peter Anvine2c80182005-01-15 22:15:51 +00002130 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002131 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002133 }
2134
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002135 name += 2;
2136 ctx = cstk;
2137 i = 0;
2138 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 name++;
2140 i++;
2141 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002142 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002144 nasm_nonfatal("`%s': context stack is only"
2145 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002146 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002147 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002148
2149 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002150 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002151
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002152 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002153}
2154
2155/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002156 * Open an include file. This routine must always return a valid
2157 * file pointer if it returns - it's responsible for throwing an
2158 * ERR_FATAL and bombing out completely if not. It should also try
2159 * the include path one by one until it finds the file or reaches
2160 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07002161 *
2162 * Note: for INC_PROBE the function returns NULL at all times;
2163 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002164 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07002165enum incopen_mode {
2166 INC_NEEDED, /* File must exist */
2167 INC_OPTIONAL, /* Missing is OK */
2168 INC_PROBE /* Only an existence probe */
2169};
2170
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002171/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002172static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002173 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002174{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08002175 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002176 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002177 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02002178 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07002179 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002180
H. Peter Anvine2c80182005-01-15 22:15:51 +00002181 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02002182 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002183 if (omode == INC_PROBE) {
2184 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002185 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07002186 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002187 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07002188 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002189 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07002190 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002191 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002193 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002194
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002195 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002196
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002197 if (!ip) {
2198 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002199 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002200 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002201
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002202 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002203 ip = ip->next;
2204 }
2205}
2206
2207/*
2208 * Open a file, or test for the presence of one (depending on omode),
2209 * considering the include path.
2210 */
2211static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002212 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002213 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002214 enum incopen_mode omode,
2215 enum file_flags fmode)
2216{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002217 struct hash_insert hi;
2218 void **hp;
2219 char *path;
2220 FILE *fp = NULL;
2221
2222 hp = hash_find(&FileHash, file, &hi);
2223 if (hp) {
2224 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002225 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002226 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03002227 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002228 } else {
2229 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002230 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002231
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002232 /* Positive or negative result */
2233 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002234
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002235 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002236 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002237 */
2238 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002239 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00002240 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002241
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002242 if (!path) {
2243 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002244 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002245 } else {
2246 if (!fp && omode != INC_PROBE)
2247 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002248 }
2249
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002250 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002251 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07002252
2253 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00002254}
2255
2256/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002257 * Opens an include or input file. Public version, for use by modules
2258 * that get a file:lineno pair and need to look at the file again
2259 * (e.g. the CodeView debug backend). Returns NULL on failure.
2260 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07002261FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002262{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07002263 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07002264}
2265
2266/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002267 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002268 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002269 * return true if _any_ single-line macro of that name is defined.
2270 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002271 * `nparam' or no parameters is defined.
2272 *
2273 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002274 * defined, or nparam is -1, the address of the definition structure
2275 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002276 * is NULL, no action will be taken regarding its contents, and no
2277 * error will occur.
2278 *
2279 * Note that this is also called with nparam zero to resolve
2280 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002281 *
2282 * If you already know which context macro belongs to, you can pass
2283 * the context pointer as first parameter; if you won't but name begins
2284 * with %$ the context will be automatically computed. If all_contexts
2285 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002286 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002287static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07002288smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07002289 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002290{
H. Peter Anvin166c2472008-05-28 12:28:58 -07002291 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002292 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002293
H. Peter Anvin97a23472007-09-16 17:57:25 -07002294 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002295 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002296 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002297 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04002298 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002300 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002301 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002302 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002303 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07002304 }
H. Peter Anvind2354082019-08-27 16:38:48 -07002305
2306restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07002307 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002308
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309 while (m) {
2310 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002311 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002312 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002313 if (m->alias && !find_alias) {
2314 if (do_aliases) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002315 name = tok_text(m->expansion);
H. Peter Anvind2354082019-08-27 16:38:48 -07002316 goto restart;
2317 } else {
2318 continue;
2319 }
2320 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002321 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002322 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002323 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002324 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002325 }
2326 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002327 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002328
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002329 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002330}
2331
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002332/* param should be a natural number [0; INT_MAX] */
2333static int read_param_count(const char *str)
2334{
2335 int result;
2336 bool err;
2337
2338 result = readnum(str, &err);
2339 if (result < 0 || result > INT_MAX) {
2340 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002341 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
2342 str, 0, INT_MAX);
2343 } else if (err)
2344 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002345 return result;
2346}
2347
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002348/*
2349 * Count and mark off the parameters in a multi-line macro call.
2350 * This is called both from within the multi-line macro expansion
2351 * code, and also to mark off the default parameters when provided
2352 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002353 *
2354 * Note that we need space in the params array for parameter 0 being
2355 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002356 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002357static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002358{
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002359 int paramsize;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002360 int nparam = 0;
2361 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002362
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002363 paramsize = PARAM_DELTA;
2364 params = nasm_malloc(paramsize * sizeof(*params));
2365 params[0] = NULL;
2366
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002367 while ((t = skip_white(t))) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002368 /* 2 slots for captured label and NULL */
2369 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002371 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002373 params[++nparam] = t;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002374 if (tok_is(t, '{')) {
2375 int brace = 1;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002376 while (brace && (t = t->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002377 brace += tok_is(t, '{');
2378 brace -= tok_is(t, '}');
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002379 }
2380
2381 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 /*
2383 * Now we've found the closing brace, look further
2384 * for the comma.
2385 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002386 t = skip_white(t->next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002387 if (tok_isnt(t, ','))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002388 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07002389 } else {
2390 nasm_nonfatal("expecting closing brace in macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002392 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002393
2394 while (tok_isnt(t, ','))
2395 t = t->next;
2396
2397 if (t) /* got a comma */
2398 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002399 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002400
2401 params[nparam+1] = NULL;
2402 *paramsp = params;
2403 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002404}
2405
2406/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002407 * Determine whether one of the various `if' conditions is true or
2408 * not.
2409 *
2410 * We must free the tline we get passed.
2411 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002412static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002413{
H. Peter Anvin70055962007-10-11 00:05:31 -07002414 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002415 Token *t, *tt, *origline;
2416 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002417 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002418 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002419 enum pp_token_type needtype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002420 const char *dname = pp_directives[ct];
2421 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002422 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002423
2424 origline = tline;
2425
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002426 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002427 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002428 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002429 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002430 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002431 if (!tline)
2432 break;
2433 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002434 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002435 dname);
2436 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002438 if (cstk && cstk->name && !nasm_stricmp(tok_text(tline), cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002439 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002440 tline = tline->next;
2441 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002442 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002443
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002444 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002445 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002446 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002447 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 if (!tline || (tline->type != TOK_ID &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002449 tline->type != TOK_LOCAL_MACRO)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002450 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002451 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002452 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002454 if (smacro_defined(NULL, tok_text(tline), 0, NULL, true, false))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002455 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002457 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002458 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002459
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002460 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002461 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002462 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002463 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002464 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002465 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002466 tline->type != TOK_STRING &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07002467 tline->type != TOK_INTERNAL_STRING &&
2468 tline->type != TOK_ENVIRON)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002469 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002470 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002471 goto fail;
2472 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002473
2474 j |= !!pp_getenv(tline, false);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002475 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002476 }
2477 break;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002478
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002479 case PP_IFIDNI:
2480 casesense = false;
2481 /* fall through */
2482 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 tline = expand_smacro(tline);
2484 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002485 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 tt = tt->next;
2487 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002488 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002489 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002490 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 }
2492 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002493 j = true; /* assume equality unless proved not */
H. Peter Anvin8571f062019-09-23 16:40:03 -07002494 while (tok_isnt(t, ',') && tt) {
2495 unsigned int l1, l2;
2496 const char *t1, *t2;
2497
2498 if (tok_is(tt, ',')) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002499 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002500 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002502 }
2503 if (t->type == TOK_WHITESPACE) {
2504 t = t->next;
2505 continue;
2506 }
2507 if (tt->type == TOK_WHITESPACE) {
2508 tt = tt->next;
2509 continue;
2510 }
2511 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002512 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 break;
2514 }
H. Peter Anvind2456592008-06-19 15:04:18 -07002515
H. Peter Anvin8571f062019-09-23 16:40:03 -07002516 t1 = unquote_token(t);
2517 t2 = unquote_token(tt);
2518 l1 = t->len;
2519 l2 = tt->len;
2520
2521 if (l1 != l2 || mmemcmp(t1, t2, l1, casesense)) {
2522 j = false;
2523 break;
2524 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002525
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 t = t->next;
2527 tt = tt->next;
2528 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07002529 if (!tok_is(t, ',') || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002530 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002531 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002532
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002533 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002534 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002535 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002536 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002537
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002538 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002539 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002540 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002541 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 goto fail;
2543 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002544 nasm_zero(searching);
H. Peter Anvin8571f062019-09-23 16:40:03 -07002545 searching.name = dup_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002547 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 searching.nparam_max = INT_MAX;
2549 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002550 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002552 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002553 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002554 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 } else {
2556 searching.nparam_min = searching.nparam_max =
H. Peter Anvin8571f062019-09-23 16:40:03 -07002557 read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002559 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002560 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002561 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002563 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002564 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002565 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002567 searching.nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002568 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002569 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002570 searching.nparam_max = searching.nparam_min;
2571 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002572 }
2573 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002574 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 tline = tline->next;
2576 searching.plus = true;
2577 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002578 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2579 while (mmac) {
2580 if (!strcmp(mmac->name, searching.name) &&
2581 (mmac->nparam_min <= searching.nparam_max
2582 || searching.plus)
2583 && (searching.nparam_min <= mmac->nparam_max
2584 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002585 found = true;
2586 break;
2587 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002588 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002589 }
2590 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002591 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 nasm_free(searching.name);
2593 j = found;
2594 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002595 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002596
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002597 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 needtype = TOK_ID;
2599 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002600 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 needtype = TOK_NUMBER;
2602 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002603 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 needtype = TOK_STRING;
2605 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002606
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607iftype:
2608 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002609
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002610 while (tok_white(t) ||
2611 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002613
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002614 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002616
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002617 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002618 tline = expand_smacro(tline);
2619 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002620
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 j = false;
2622 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002623 t = skip_white(t->next); /* Skip the actual token + whitespace */
2624 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 }
2626 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002627
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002628 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002629 tline = expand_smacro(tline);
2630 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 j = !t; /* Should be empty */
2632 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002633
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002634 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002635 pps.tptr = tline = expand_smacro(tline);
2636 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002637 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002638 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 if (!evalresult)
2640 return -1;
2641 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002642 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002643 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002644 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002645 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002648 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002649 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002650
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002651 case PP_IFUSING:
2652 case PP_IFUSABLE:
2653 {
2654 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002655 const char *name;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002656
H. Peter Anvin8571f062019-09-23 16:40:03 -07002657 pkg = get_use_pkg(tline, dname, &name);
2658 if (!name)
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002659 goto fail;
2660
2661 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2662 break;
2663 }
2664
H. Peter Anvine2c80182005-01-15 22:15:51 +00002665 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002666 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002668 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002669
2670 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002671 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002672
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002673fail:
2674 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002675 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002676}
2677
2678/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002679 * Default smacro expansion routine: just returns a copy of the
2680 * expansion list.
2681 */
2682static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002683smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002684{
2685 (void)params;
2686 (void)nparams;
2687
2688 return dup_tlist(s->expansion, NULL);
2689}
2690
2691/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002692 * Emit a macro defintion or undef to the listing file, if
2693 * desired. This is similar to detoken(), but it handles the reverse
2694 * expansion list, does not expand %! or local variable tokens, and
2695 * does some special handling for macro parameters.
2696 */
2697static void
2698list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2699{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002700 Token *t;
2701 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002702 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002703 char *context_prefix = NULL;
2704 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002705
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002706 namelen = strlen(m->name);
2707 size = namelen + 2; /* Include room for space after name + NUL */
2708
2709 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002710 int context_depth = cstk->depth - ctx->depth + 1;
2711 context_prefix =
2712 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2713 ctx->name ? ctx->name : "",
2714 ctx->number, context_depth, "");
2715
2716 context_len = nasm_last_string_len();
2717 memset(context_prefix + context_len - context_depth,
2718 '$', context_depth);
2719 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002720 }
2721
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002722 list_for_each(t, m->expansion)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002723 size += t->len;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002724
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002725 if (m->nparam) {
2726 /*
2727 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002728 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002729 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002730 int i;
2731
2732 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002733 for (i = 0; i < m->nparam; i++)
H. Peter Anvin8571f062019-09-23 16:40:03 -07002734 size += m->params[i].name.len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002735 }
2736
2737 def = nasm_malloc(size);
2738 p = def+size;
2739 *--p = '\0';
2740
2741 list_for_each(t, m->expansion) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002742 p -= t->len;
2743 memcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002744 }
2745
2746 *--p = ' ';
2747
2748 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002749 int i;
2750
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002751 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002752 for (i = m->nparam-1; i >= 0; i--) {
2753 enum sparmflags flags = m->params[i].flags;
2754 if (flags & SPARM_GREEDY)
2755 *--p = '+';
H. Peter Anvin8571f062019-09-23 16:40:03 -07002756 p -= m->params[i].name.len;
2757 memcpy(p, tok_text(&m->params[i].name), m->params[i].name.len);
2758
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002759 if (flags & SPARM_NOSTRIP)
2760 *--p = '!';
2761 if (flags & SPARM_STR)
2762 *--p = '&';
2763 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002764 *--p = '=';
2765 *--p = ',';
2766 }
2767 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002768 }
2769
2770 p -= namelen;
2771 memcpy(p, m->name, namelen);
2772
H. Peter Anvin6686de22019-08-10 05:33:14 -07002773 if (context_prefix) {
2774 p -= context_len;
2775 memcpy(p, context_prefix, context_len);
2776 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002777 }
2778
2779 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002780 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002781}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002782
2783/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002784 * Parse smacro arguments, return argument count. If the tmpl argument
2785 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002786 * *tpp is updated to point to the pointer to the first token after the
2787 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002788 *
2789 * The text values from any argument tokens are "stolen" and the
2790 * corresponding text fields set to NULL.
2791 */
2792static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2793{
2794 int nparam = 0;
2795 enum sparmflags flags;
2796 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002797 bool err, done;
2798 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002799 Token **tn = *tpp;
2800 Token *t = *tn;
2801 Token *name;
2802
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002803 /*
2804 * DO NOT skip whitespace here, or we won't be able to distinguish:
2805 *
2806 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2807 * %define bar(a,b) ; two arguments, empty expansion
2808 *
2809 * This ambiguity was inherited from C.
2810 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002811
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002812 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002813 goto finish;
2814
2815 if (tmpl) {
2816 Token *tx = t;
2817 Token **txpp = &tx;
2818 int sparam;
2819
2820 /* Count parameters first */
2821 sparam = parse_smacro_template(&txpp, NULL);
2822 if (!sparam)
2823 goto finish; /* No parameters, we're done */
2824 nasm_newn(params, sparam);
2825 }
2826
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002827 /* Skip leading paren */
2828 tn = &t->next;
2829 t = *tn;
2830
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002831 name = NULL;
2832 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002833 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002834
2835 while (!done) {
2836 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002837 if (name || flags)
2838 nasm_nonfatal("`)' expected to terminate macro template");
2839 else
2840 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002841 break;
2842 }
2843
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002844 switch (t->type) {
2845 case TOK_ID:
2846 if (name)
2847 goto bad;
2848 name = t;
2849 break;
2850
2851 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07002852 if (t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002853 goto bad;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002854 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002855 case '=':
2856 flags |= SPARM_EVAL;
2857 break;
2858 case '&':
2859 flags |= SPARM_STR;
2860 break;
2861 case '!':
2862 flags |= SPARM_NOSTRIP;
2863 break;
2864 case '+':
2865 flags |= SPARM_GREEDY;
2866 greedy = true;
2867 break;
2868 case ',':
2869 if (greedy)
2870 nasm_nonfatal("greedy parameter must be last");
2871 /* fall through */
2872 case ')':
2873 if (params) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002874 if (name)
2875 steal_Token(&params[nparam].name, name);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002876 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002877 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002878 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002879 name = NULL;
2880 flags = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002881 done = t->text.a[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002882 break;
2883 default:
2884 goto bad;
2885 }
2886 break;
2887
2888 case TOK_WHITESPACE:
2889 break;
2890
2891 default:
2892 bad:
2893 if (!err) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07002894 nasm_nonfatal("garbage `%s' in macro parameter list", tok_text(t));
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002895 err = true;
2896 }
2897 break;
2898 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002899
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002900 tn = &t->next;
2901 t = *tn;
2902 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002903
2904finish:
2905 while (t && t->type == TOK_WHITESPACE) {
2906 tn = &t->next;
2907 t = t->next;
2908 }
2909 *tpp = tn;
2910 if (tmpl) {
2911 tmpl->nparam = nparam;
2912 tmpl->greedy = greedy;
2913 tmpl->params = params;
2914 }
2915 return nparam;
2916}
2917
2918/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002919 * Common code for defining an smacro. The tmpl argument, if not NULL,
2920 * contains any macro parameters that aren't explicit arguments;
2921 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002922 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002923static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002924 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002925{
2926 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002927 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002928 Context *ctx;
2929 bool defining_alias = false;
2930 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002931
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002932 if (tmpl) {
2933 defining_alias = tmpl->alias;
2934 nparam = tmpl->nparam;
2935 }
2936
2937 while (1) {
2938 ctx = get_ctx(mname, &mname);
2939
H. Peter Anvind2354082019-08-27 16:38:48 -07002940 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002941 /* Create a new macro */
2942 smtbl = ctx ? &ctx->localmac : &smacros;
2943 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2944 nasm_new(smac);
2945 smac->next = *smhead;
2946 *smhead = smac;
2947 break;
2948 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002949 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002950 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002951 /*
2952 * Some instances of the old code considered this a failure,
2953 * some others didn't. What is the right thing to do here?
2954 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002955 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002956 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002957 /*
2958 * We're redefining, so we have to take over an
2959 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002960 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002961 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002962 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002963 break;
2964 } else if (smac->in_progress) {
2965 nasm_nonfatal("macro alias loop");
2966 goto fail;
2967 } else {
2968 /* It is an alias macro; follow the alias link */
2969 SMacro *s;
2970
2971 smac->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07002972 s = define_smacro(tok_text(smac->expansion), casesense,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002973 expansion, tmpl);
2974 smac->in_progress = false;
2975 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002976 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002977 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002978
2979 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002980 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002981 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002982 smac->expand = smacro_expand_default;
2983 if (tmpl) {
2984 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002985 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002986 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002987 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002988 if (tmpl->expand)
2989 smac->expand = tmpl->expand;
2990 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002991 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002992 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2993 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002994 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002995 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002996
2997fail:
2998 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002999 if (tmpl)
3000 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003001 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003002}
3003
3004/*
3005 * Undefine an smacro
3006 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003007static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003008{
3009 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07003010 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003011 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003012
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003013 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07003014 smtbl = ctx ? &ctx->localmac : &smacros;
3015 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07003016
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003017 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003018 /*
3019 * We now have a macro name... go hunt for it.
3020 */
3021 sp = smhead;
3022 while ((s = *sp) != NULL) {
3023 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003024 if (s->alias && !undefalias) {
H. Peter Anvind2354082019-08-27 16:38:48 -07003025 if (!do_aliases)
3026 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003027 if (s->in_progress) {
3028 nasm_nonfatal("macro alias loop");
3029 } else {
3030 s->in_progress = true;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003031 undef_smacro(tok_text(s->expansion), false);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003032 s->in_progress = false;
3033 }
3034 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07003035 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003036 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
3037 ctx, s);
3038 *sp = s->next;
3039 free_smacro(s);
3040 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003041 } else {
3042 sp = &s->next;
3043 }
3044 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07003045 }
3046}
3047
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003048/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07003049 * Parse a mmacro specification.
3050 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003051static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07003052{
H. Peter Anvina26433d2008-07-16 14:40:01 -07003053 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003054 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003055 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003056 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003057 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003058 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003059 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003060
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003061#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003062 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003063#endif
H. Peter Anvin8571f062019-09-23 16:40:03 -07003064 def->name = dup_text(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003065 def->plus = false;
3066 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003067 def->nparam_min = 0;
3068 def->nparam_max = 0;
3069
H. Peter Anvina26433d2008-07-16 14:40:01 -07003070 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003071 tline = skip_white(tline);
3072 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003073 nasm_nonfatal("`%s' expects a parameter count", directive);
3074 else
H. Peter Anvin8571f062019-09-23 16:40:03 -07003075 def->nparam_min = def->nparam_max = read_param_count(tok_text(tline));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003076 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003077 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003078 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003079 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003080 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003081 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003082 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003083 def->nparam_max = read_param_count(tok_text(tline));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003084 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003085 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03003086 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003087 }
3088 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003089 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003090 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003091 tline = tline->next;
3092 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003093 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003094 if (tline && tok_type(tline->next, TOK_ID) &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07003095 tline->next->len == 7 &&
3096 !nasm_stricmp(tline->next->text.a, ".nolist")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003097 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003098 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003099 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003100
H. Peter Anvina26433d2008-07-16 14:40:01 -07003101 /*
3102 * Handle default parameters.
3103 */
3104 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003105 def->dlist = tline->next;
3106 tline->next = NULL;
3107 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07003108 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003109 def->dlist = NULL;
3110 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003111 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003112 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003113
H. Peter Anvin89cee572009-07-15 09:16:54 -04003114 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003115 !def->plus) {
3116 /*
3117 *!macro-defaults [on] macros with more default than optional parameters
3118 *! warns when a macro has more default parameters than optional parameters.
3119 *! See \k{mlmacdef} for why might want to disable this warning.
3120 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003121 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003122 "too many default macro parameters in macro `%s'", def->name);
3123 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02003124
H. Peter Anvina26433d2008-07-16 14:40:01 -07003125 return true;
3126}
3127
3128
3129/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003130 * Decode a size directive
3131 */
3132static int parse_size(const char *str) {
3133 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003134 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003135 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003136 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03003137 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003138}
3139
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003140/*
3141 * Process a preprocessor %pragma directive. Currently there are none.
3142 * Gets passed the token list starting with the "preproc" token from
3143 * "%pragma preproc".
3144 */
3145static void do_pragma_preproc(Token *tline)
3146{
3147 /* Skip to the real stuff */
3148 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003149 tline = skip_white(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003150 if (!tline)
3151 return;
3152
3153 (void)tline; /* Nothing else to do at present */
3154}
3155
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003156static bool is_macro_id(const Token *t)
3157{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003158 return tok_type(t, TOK_ID) || tok_type(t, TOK_LOCAL_MACRO);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003159}
3160
H. Peter Anvin8571f062019-09-23 16:40:03 -07003161static const char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003162{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003163 const char *id;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003164 Token *t = *tp;
3165
3166 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003167 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003168 t = expand_id(t);
3169
3170 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003171 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003172 return NULL;
3173 }
3174
H. Peter Anvin8571f062019-09-23 16:40:03 -07003175 id = tok_text(t);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003176 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003177 *tp = t;
3178 return id;
3179}
3180
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003181/* Parse a %use package name and find the package. Set *err on syntax error. */
3182static const struct use_package *
H. Peter Anvin8571f062019-09-23 16:40:03 -07003183get_use_pkg(Token *t, const char *dname, const char **name)
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003184{
H. Peter Anvin8571f062019-09-23 16:40:03 -07003185 const char *id;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003186
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003187 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003188 t = expand_smacro(t);
3189
H. Peter Anvin8571f062019-09-23 16:40:03 -07003190 *name = NULL;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003191
H. Peter Anvin8571f062019-09-23 16:40:03 -07003192 if (!t) {
3193 nasm_nonfatal("`%s' expects a package name, got end of line", dname);
3194 return NULL;
3195 } else if (t->type != TOK_ID && t->type != TOK_STRING) {
3196 nasm_nonfatal("`%s' expects a package name, got `%s'",
3197 dname, tok_text(t));
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003198 return NULL;
3199 }
3200
H. Peter Anvin8571f062019-09-23 16:40:03 -07003201 *name = id = unquote_token(t);
3202
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003203 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003204 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003205 if (t)
3206 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
3207
3208 return nasm_find_use_package(id);
3209}
3210
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003211/*
3212 * Mark parameter tokens in an smacro definition. If the type argument
3213 * is 0, create smac param tokens, otherwise use the type specified;
3214 * normally this is used for TOK_XDEF_PARAM, which is used to protect
3215 * parameter tokens during expansion during %xdefine.
3216 */
3217static void mark_smac_params(Token *tline, const SMacro *tmpl,
3218 enum pp_token_type type)
3219{
3220 const struct smac_param *params = tmpl->params;
3221 int nparam = tmpl->nparam;
3222 Token *t;
3223 int i;
3224
3225 if (!nparam)
3226 return;
3227
3228 list_for_each(t, tline) {
3229 if (t->type != TOK_ID && t->type != TOK_XDEF_PARAM)
3230 continue;
3231
3232 for (i = 0; i < nparam; i++) {
3233 if (tok_text_match(t, &params[i].name))
3234 t->type = type ? type : tok_smac_param(i);
3235 }
3236 }
3237}
3238
Ed Beroset3ab3f412002-06-11 03:31:49 +00003239/**
3240 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003241 * Find out if a line contains a preprocessor directive, and deal
3242 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07003243 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003244 * If a directive _is_ found, it is the responsibility of this routine
3245 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003246 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00003247 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003248 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00003249 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07003250 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003251 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003252static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003253{
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003254 enum preproc_token op;
H. Peter Anvin4169a472007-09-12 01:29:43 +00003255 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07003256 bool err;
H. Peter Anvin70055962007-10-11 00:05:31 -07003257 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003258 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07003259 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003260 int offset;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003261 const char *p;
3262 char *q, *qbuf;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003263 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08003264 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003265 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003266 Include *inc;
3267 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003268 Cond *cond;
3269 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003270 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003271 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003272 struct tokenval tokval;
3273 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003274 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003275 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08003276 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003277 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003278
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003279 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00003280 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003281
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003282 tline = skip_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003283 if (!tline || !tok_type(tline, TOK_PREPROC_ID))
3284 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003285
H. Peter Anvin8571f062019-09-23 16:40:03 -07003286 dname = tok_text(tline);
3287 if (dname[1] == '%')
3288 return NO_DIRECTIVE_FOUND;
3289
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003290 op = pp_token_hash(dname);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003291
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003292 casesense = true;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003293 if (PP_HAS_CASE(op) & PP_INSENSITIVE(op)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003294 casesense = false;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003295 op--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003296 }
3297
3298 /*
3299 * If we're in a non-emitting branch of a condition construct,
3300 * or walking to the end of an already terminated %rep block,
3301 * we should ignore all directives except for condition
3302 * directives.
3303 */
3304 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003305 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003306 !is_condition(op)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003307 return NO_DIRECTIVE_FOUND;
3308 }
3309
3310 /*
3311 * If we're defining a macro or reading a %rep block, we should
3312 * ignore all directives except for %macro/%imacro (which nest),
3313 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
3314 * If we're in a %rep block, another %rep nests, so should be let through.
3315 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003316 if (defining && op != PP_MACRO && op != PP_RMACRO &&
3317 op != PP_ENDMACRO && op != PP_ENDM &&
3318 (defining->name || (op != PP_ENDREP && op != PP_REP))) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003319 return NO_DIRECTIVE_FOUND;
3320 }
3321
3322 if (defining) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003323 if (op == PP_MACRO || op == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003324 nested_mac_count++;
3325 return NO_DIRECTIVE_FOUND;
3326 } else if (nested_mac_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003327 if (op == PP_ENDMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003328 nested_mac_count--;
3329 return NO_DIRECTIVE_FOUND;
3330 }
3331 }
3332 if (!defining->name) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003333 if (op == PP_REP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003334 nested_rep_count++;
3335 return NO_DIRECTIVE_FOUND;
3336 } else if (nested_rep_count > 0) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003337 if (op == PP_ENDREP) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003338 nested_rep_count--;
3339 return NO_DIRECTIVE_FOUND;
3340 }
3341 }
3342 }
3343 }
3344
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003345 switch (op) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003346 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003347 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003349
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003350 case PP_PRAGMA:
3351 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003352 * %pragma namespace options...
3353 *
3354 * The namespace "preproc" is reserved for the preprocessor;
3355 * all other namespaces generate a [pragma] assembly directive.
3356 *
3357 * Invalid %pragmas are ignored and may have different
3358 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003359 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07003360 t = tline;
3361 tline = tline->next;
3362 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003363 tline = zap_white(expand_smacro(tline));
3364 if (tok_type(tline, TOK_ID)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003365 if (!nasm_stricmp(tok_text(tline), "preproc")) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003366 /* Preprocessor pragma */
3367 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003368 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003369 } else {
3370 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07003371
3372 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003373 for (t = tline; t->next; t = t->next)
3374 ;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003375 t->next = make_tok_char(NULL, ']');
H. Peter Anvin06335872019-08-10 06:42:55 -07003376
3377 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003378 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07003379 t = new_Token(t, TOK_ID, "pragma", 6);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003380 t = make_tok_char(t, '[');
H. Peter Anvin06335872019-08-10 06:42:55 -07003381 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003382 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003383 }
3384 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003385 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003386
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 case PP_STACKSIZE:
3388 /* Directive to tell NASM what the default stack size is. The
3389 * default is for a 16-bit stack, and this can be overriden with
3390 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003392 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003394 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003396 if (nasm_stricmp(tok_text(tline), "flat") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 /* All subsequent ARG directives are for a 32-bit stack */
3398 StackSize = 4;
3399 StackPointer = "ebp";
3400 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003401 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003402 } else if (nasm_stricmp(tok_text(tline), "flat64") == 0) {
Charles Crayne7eaf9192007-11-08 22:11:14 -08003403 /* All subsequent ARG directives are for a 64-bit stack */
3404 StackSize = 8;
3405 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003406 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003407 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003408 } else if (nasm_stricmp(tok_text(tline), "large") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 /* All subsequent ARG directives are for a 16-bit stack,
3410 * far function call.
3411 */
3412 StackSize = 2;
3413 StackPointer = "bp";
3414 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003415 LocalOffset = 0;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003416 } else if (nasm_stricmp(tok_text(tline), "small") == 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 /* All subsequent ARG directives are for a 16-bit stack,
3418 * far function call. We don't support near functions.
3419 */
3420 StackSize = 2;
3421 StackPointer = "bp";
3422 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003423 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003425 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003426 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003427 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003428
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 case PP_ARG:
3430 /* TASM like ARG directive to define arguments to functions, in
3431 * the following form:
3432 *
3433 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3434 */
3435 offset = ArgOffset;
3436 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003437 const char *arg;
3438 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003440
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003442 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003443 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003444 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003445 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003446 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003447 arg = tok_text(tline);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003448
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 /* Find the argument size type */
3450 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003451 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003452 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003453 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 }
3455 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003456 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003457 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003458 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003460
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003462 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003464 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003466 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003468 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 }
3470 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003471
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 /* Round up to even stack slots */
3473 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003474
H. Peter Anvine2c80182005-01-15 22:15:51 +00003475 /* Now define the macro for the argument */
3476 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3477 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003478 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003479 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003480
H. Peter Anvine2c80182005-01-15 22:15:51 +00003481 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003482 tline = skip_white(tline->next);
3483 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003485 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003486
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 case PP_LOCAL:
3488 /* TASM like LOCAL directive to define local variables for a
3489 * function, in the following form:
3490 *
3491 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3492 *
3493 * The '= LocalSize' at the end is ignored by NASM, but is
3494 * required by TASM to define the local parameter size (and used
3495 * by the TASM macro package).
3496 */
3497 offset = LocalOffset;
3498 do {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003499 const char *local;
3500 char directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003502
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003504 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003506 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003507 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07003509 local = tok_text(tline);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003510
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 /* Find the argument size type */
3512 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003513 if (!tok_is(tline, ':')) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003514 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003515 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 }
3517 tline = tline->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003518 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003519 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003520 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003522
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 /* Allow macro expansion of type parameter */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003524 tt = tokenize(tok_text(tline));
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 tt = expand_smacro(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003526 size = parse_size(tok_text(tt));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003528 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003530 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003531 }
3532 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003533
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003534 /* Round up to even stack slots */
3535 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003536
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003537 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003538
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003539 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3541 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003542 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003543
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 /* Now define the assign to setup the enter_c macro correctly */
3545 snprintf(directive, sizeof(directive),
3546 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003547 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003548
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003550 tline = skip_white(tline->next);
3551 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003552 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003553 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003554
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 case PP_CLEAR:
3556 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003557 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003558 free_macros();
3559 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003560 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003561
H. Peter Anvin418ca702008-05-30 10:42:30 -07003562 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003564 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003565 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003566 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003567 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003568 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003569 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003570 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003571 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003572
3573 strlist_add(deplist, unquote_token_cstr(t));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003574 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003575
3576 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003577 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003578 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003579
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003580 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003581 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003582 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003583 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003585 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003586 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003587 p = unquote_token_cstr(t);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003588 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003590 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003591 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003592 (pp_mode == PP_DEPS)
3593 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003594 if (!inc->fp) {
3595 /* -MG given but file not found */
3596 nasm_free(inc);
3597 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003598 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003599 inc->lineno = src_set_linnum(0);
3600 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003601 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003602 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003603 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003604 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003605 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003606
H. Peter Anvind2456592008-06-19 15:04:18 -07003607 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003608 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003609 const struct use_package *pkg;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003610 const char *name;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003611
H. Peter Anvin8571f062019-09-23 16:40:03 -07003612 pkg = get_use_pkg(tline->next, dname, &name);
3613 if (!name)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003614 goto done;
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003615 if (!pkg) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003616 nasm_nonfatal("unknown `%s' package: `%s'", dname, name);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003617 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003618 /*
3619 * Not already included, go ahead and include it.
3620 * Treat it as an include file for the purpose of
3621 * producing a listing.
3622 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003623 use_loaded[pkg->index] = true;
3624 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003625 nasm_new(inc);
3626 inc->next = istk;
3627 inc->fname = src_set_fname(NULL);
3628 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003629 inc->nolist = !list_option('b') || istk->nolist;
3630 istk = inc;
3631 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003632 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003633 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003634 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003635 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003637 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003639 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003640 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003641 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003642 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003643 nasm_nonfatal("`%s' expects a context identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003644 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 }
3646 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003647 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003648 dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003649 p = tok_text(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003650 } else {
3651 p = NULL; /* Anonymous */
3652 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003653
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003654 if (op == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003655 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003656 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003657 ctx->next = cstk;
H. Peter Anvin8571f062019-09-23 16:40:03 -07003658 ctx->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003659 ctx->number = unique++;
3660 cstk = ctx;
3661 } else {
3662 /* %pop or %repl */
3663 if (!cstk) {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003664 nasm_nonfatal("`%s': context stack is empty", dname);
3665 } else if (op == PP_POP) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003666 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003667 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003668 "expected %s",
3669 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003670 else
3671 ctx_pop();
3672 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003673 /* op == PP_REPL */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003674 nasm_free((char *)cstk->name);
3675 cstk->name = p ? nasm_strdup(p) : NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 p = NULL;
3677 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003678 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003679 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003680 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003681 severity = ERR_FATAL;
3682 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003684 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003685 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003686 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003687 /*!
3688 *!user [on] %warning directives
3689 *! controls output of \c{%warning} directives (see \k{pperror}).
3690 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003691 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003692 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003693
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003694issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003695 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003696 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 tline->next = expand_smacro(tline->next);
3698 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003699 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003700 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003701 t = skip_white(t);
3702 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003703 /* The line contains only a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003704 p = unquote_token(tline); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003705 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003706 } else {
3707 /* Not a quoted string, or more than a quoted string */
H. Peter Anvin8571f062019-09-23 16:40:03 -07003708 q = detoken(tline, false);
3709 nasm_error(severity, "%s", q);
3710 nasm_free(q);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003711 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003712 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003713 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003714
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003715 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003716 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003717 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003718 else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003719 j = if_condition(tline->next, op);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003720 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003721 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003722 cond = nasm_malloc(sizeof(Cond));
3723 cond->next = istk->conds;
3724 cond->state = j;
3725 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003726 if(istk->mstk.mstk)
3727 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003728 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003729
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003730 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003731 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003732 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003733 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003734 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003735 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003736 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003737
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003738 case COND_DONE:
3739 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003740 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003741
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003742 case COND_ELSE_TRUE:
3743 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003744 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003745 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003746 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003747 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003748
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003749 case COND_IF_FALSE:
3750 /*
3751 * IMPORTANT: In the case of %if, we will already have
3752 * called expand_mmac_params(); however, if we're
3753 * processing an %elif we must have been in a
3754 * non-emitting mode, which would have inhibited
3755 * the normal invocation of expand_mmac_params().
3756 * Therefore, we have to do it explicitly here.
3757 */
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003758 j = if_condition(expand_mmac_params(tline->next), op);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003759 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003760 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003761 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003762 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003763 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003764
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 case PP_ELSE:
3766 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003767 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003768 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003769 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003770 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003771 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003772 case COND_IF_TRUE:
3773 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003774 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003775 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003776
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003777 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003778 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003779
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003780 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003781 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003782 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003783
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003784 case COND_ELSE_TRUE:
3785 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003786 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003787 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003788 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003789 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003790 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003791 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003792
H. Peter Anvine2c80182005-01-15 22:15:51 +00003793 case PP_ENDIF:
3794 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003795 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003796 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003797 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003798 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003799 cond = istk->conds;
3800 istk->conds = cond->next;
3801 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003802 if(istk->mstk.mstk)
3803 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003804 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003805
H. Peter Anvin8b262472019-02-26 14:00:54 -08003806 case PP_RMACRO:
3807 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003808 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003809 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003810 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003811 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07003812 if (op == PP_RMACRO)
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003813 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003814 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003815 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003816 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003817 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003818
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003819 src_get(&defining->xline, &defining->fname);
3820
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003821 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3822 while (mmac) {
3823 if (!strcmp(mmac->name, defining->name) &&
3824 (mmac->nparam_min <= defining->nparam_max
3825 || defining->plus)
3826 && (defining->nparam_min <= mmac->nparam_max
3827 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003828 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003829 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003830 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003831 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003832 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003833 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003834 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003835
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 case PP_ENDM:
3837 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003838 if (!(defining && defining->name)) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07003839 nasm_nonfatal("`%s': not defining a macro", tok_text(tline));
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003840 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003841 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003842 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3843 defining->next = *mmhead;
3844 *mmhead = defining;
3845 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003846 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003847
H. Peter Anvin89cee572009-07-15 09:16:54 -04003848 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003849 /*
3850 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003851 * macro-end marker for a macro with a name. Then we
3852 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003853 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003854 list_for_each(l, istk->expansion)
3855 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003856 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003857
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003858 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003859 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003860 * Remove all conditional entries relative to this
3861 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003862 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003863 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3864 cond = istk->conds;
3865 istk->conds = cond->next;
3866 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003867 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003868 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003869 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003870 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003871 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003872 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003873
H. Peter Anvina26433d2008-07-16 14:40:01 -07003874 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003875 casesense = false;
3876 /* fall through */
3877 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003878 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003879 MMacro **mmac_p;
3880 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003881
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003882 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003883 spec.casesense = casesense;
3884 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003885 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003886 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003887 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3888 while (mmac_p && *mmac_p) {
3889 mmac = *mmac_p;
3890 if (mmac->casesense == spec.casesense &&
3891 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3892 mmac->nparam_min == spec.nparam_min &&
3893 mmac->nparam_max == spec.nparam_max &&
3894 mmac->plus == spec.plus) {
3895 *mmac_p = mmac->next;
3896 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003897 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003898 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003899 }
3900 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003901 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003902 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003903 }
3904
H. Peter Anvine2c80182005-01-15 22:15:51 +00003905 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003906 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003908 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003910 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 return DIRECTIVE_FOUND;
3912 }
3913 t = expand_smacro(tline->next);
3914 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003915 pps.tptr = tline = t;
3916 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003917 tokval.t_type = TOKEN_INVALID;
3918 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003919 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003920 free_tlist(tline);
3921 if (!evalresult)
3922 return DIRECTIVE_FOUND;
3923 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003924 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003926 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927 return DIRECTIVE_FOUND;
3928 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003929 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003930 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003931 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003932 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003933 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003934 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003935 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003936
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003937 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003938 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003939 rotate += mmac->nparam;
3940
3941 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003942 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003943 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003944
H. Peter Anvine2c80182005-01-15 22:15:51 +00003945 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003946 {
3947 MMacro *tmp_defining;
3948
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003949 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003950 tline = skip_white(tline->next);
H. Peter Anvin8571f062019-09-23 16:40:03 -07003951 if (tok_type(tline, TOK_ID) && tline->len == 7 &&
3952 !nasm_memicmp(tline->text.a, ".nolist", 7)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003953 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003954 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003955 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003956
H. Peter Anvine2c80182005-01-15 22:15:51 +00003957 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003958 pps.tptr = expand_smacro(tline);
3959 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003961 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003963 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003964 if (!evalresult)
3965 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003966 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003967 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003968 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003969 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003970 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003971 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003972 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003973 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003974 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3975 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003976 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003977 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003978 /*!
3979 *!negative-rep [on] regative %rep count
3980 *! warns about negative counts given to the \c{%rep}
3981 *! preprocessor directive.
3982 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003983 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003984 "negative `%%rep' count: %"PRId64, count);
3985 count = 0;
3986 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003987 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003988 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003989 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003990 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003991 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003992 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003993 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003994 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003995 defining->nolist = nolist;
3996 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003997 defining->mstk = istk->mstk;
3998 defining->dstk.mstk = tmp_defining;
3999 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07004000 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004001 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004002 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004003
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004005 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004006 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004007 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004008 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004009
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 /*
4011 * Now we have a "macro" defined - although it has no name
4012 * and we won't be entering it in the hash tables - we must
4013 * push a macro-end marker for it on to istk->expansion.
4014 * After that, it will take care of propagating itself (a
4015 * macro-end marker line for a macro which is really a %rep
4016 * block will cause the macro to be re-expanded, complete
4017 * with another macro-end marker to ensure the process
4018 * continues) until the whole expansion is forcibly removed
4019 * from istk->expansion by a %exitrep.
4020 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07004021 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004022 l->next = istk->expansion;
4023 l->finishes = defining;
4024 l->first = NULL;
4025 istk->expansion = l;
4026
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004027 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004028
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07004029 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004030 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004031 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004032
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004034 /*
4035 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004036 * macro-end marker for a macro with no name. Then we set
4037 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004038 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004039 list_for_each(l, istk->expansion)
4040 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004041 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004042
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004043 if (l)
H. Peter Anvind983b622019-10-07 21:19:32 -07004044 l->finishes->in_progress = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004045 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004046 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004047 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004048
H. Peter Anvin8b262472019-02-26 14:00:54 -08004049 case PP_DEFINE:
4050 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004051 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08004052 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004053 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004054 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07004055
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004056 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004057 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004058
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004059 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004060 lastp = &tline->next;
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004061 parse_smacro_template(&lastp, &tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004062 tline = *lastp;
4063 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004064
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004065 if (unlikely(op == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004066 macro_start = tline;
4067 if (!is_macro_id(macro_start)) {
4068 nasm_nonfatal("`%s' expects a macro identifier to alias",
4069 dname);
4070 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004071 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004072 tt = macro_start->next;
4073 macro_start->next = NULL;
4074 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004075 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004076 if (tline && tline->type) {
4077 nasm_warn(WARN_OTHER,
4078 "trailing garbage after aliasing identifier ignored");
4079 }
4080 free_tlist(tt);
4081 tmpl.alias = true;
4082 } else {
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004083 if (op == PP_XDEFINE) {
4084 /* Protect macro parameter tokens */
4085 mark_smac_params(tline, &tmpl, TOK_XDEF_PARAM);
4086 tline = expand_smacro(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004087 }
H. Peter Anvin (Intel)e86fa7f2019-10-16 14:51:16 -07004088
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004089 mark_smac_params(tline, &tmpl, 0);
4090 /* NB: Does this still make sense? */
4091 macro_start = reverse_tokens(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004092 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004093
H. Peter Anvine2c80182005-01-15 22:15:51 +00004094 /*
4095 * Good. We now have a macro name, a parameter count, and a
4096 * token list (in reverse order) for an expansion. We ought
4097 * to be OK just to create an SMacro, store it, and let
4098 * free_tlist have the rest of the line (which we have
4099 * carefully re-terminated after chopping off the expansion
4100 * from the end).
4101 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004102 define_smacro(mname, casesense, macro_start, &tmpl);
4103 break;
4104 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004105
H. Peter Anvine2c80182005-01-15 22:15:51 +00004106 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004107 case PP_UNDEFALIAS:
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;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004110 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004111 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004112
H. Peter Anvin (Intel)4b58ec12019-10-23 12:00:50 -07004113 undef_smacro(mname, op == PP_UNDEFALIAS);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004114 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004115
H. Peter Anvin8b262472019-02-26 14:00:54 -08004116 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004117 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004118 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004119
H. Peter Anvin9e200162008-06-04 17:23:14 -07004120 last = tline;
4121 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004122 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004123
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004124 tline = zap_white(tline);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004125 q = detoken(tline, false);
4126 macro_start = make_tok_qstr(NULL, q);
4127 nasm_free(q);
H. Peter Anvin9e200162008-06-04 17:23:14 -07004128
4129 /*
4130 * We now have a macro name, an implicit parameter count of
4131 * zero, and a string token to use as an expansion. Create
4132 * and store an SMacro.
4133 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004134 define_smacro(mname, casesense, macro_start, NULL);
4135 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004136
H. Peter Anvin8b262472019-02-26 14:00:54 -08004137 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004138 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004139 goto done;
4140
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004141 last = tline;
4142 tline = expand_smacro(tline->next);
4143 last->next = NULL;
4144
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004145 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004146 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004147 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004148 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004149 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004150 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004151 }
4152
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04004153 /*
4154 * Convert the string to a token stream. Note that smacros
4155 * are stored with the token stream reversed, so we have to
4156 * reverse the output of tokenize().
4157 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07004158 macro_start = reverse_tokens(tokenize(unquote_token_cstr(t)));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004159
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004160 /*
4161 * We now have a macro name, an implicit parameter count of
4162 * zero, and a numeric token to use as an expansion. Create
4163 * and store an SMacro.
4164 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004165 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05004166 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004167 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07004168
H. Peter Anvin418ca702008-05-30 10:42:30 -07004169 case PP_PATHSEARCH:
4170 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004171 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004172
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004173 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004174 goto done;
4175
H. Peter Anvin418ca702008-05-30 10:42:30 -07004176 last = tline;
4177 tline = expand_smacro(tline->next);
4178 last->next = NULL;
4179
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004180 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004181 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004182 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004183 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004184 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004185 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004186 }
4187 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004188 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004189
4190 p = unquote_token_cstr(t);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004191
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07004192 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07004193 if (!found_path)
4194 found_path = p;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004195 macro_start = make_tok_qstr(NULL, found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07004196
4197 /*
4198 * We now have a macro name, an implicit parameter count of
4199 * zero, and a string 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);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004203 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004204 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07004205 }
4206
H. Peter Anvine2c80182005-01-15 22:15:51 +00004207 case PP_STRLEN:
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 Anvine2c80182005-01-15 22:15:51 +00004211 last = tline;
4212 tline = expand_smacro(tline->next);
4213 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004214
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004215 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004216 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004217 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004218 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 free_tlist(tline);
4220 free_tlist(origline);
4221 return DIRECTIVE_FOUND;
4222 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004223
H. Peter Anvin8571f062019-09-23 16:40:03 -07004224 unquote_token(t);
4225 macro_start = make_tok_num(NULL, t->len);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004226
H. Peter Anvine2c80182005-01-15 22:15:51 +00004227 /*
4228 * We now have a macro name, an implicit parameter count of
4229 * zero, and a numeric token to use as an expansion. Create
4230 * and store an SMacro.
4231 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004232 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004233 free_tlist(tline);
4234 free_tlist(origline);
4235 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004236
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004237 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004238 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004239 goto done;
4240
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004241 last = tline;
4242 tline = expand_smacro(tline->next);
4243 last->next = NULL;
4244
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004245 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004246 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004247 switch (t->type) {
4248 case TOK_WHITESPACE:
4249 break;
4250 case TOK_STRING:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004251 unquote_token(t);
4252 len += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004253 break;
4254 case TOK_OTHER:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004255 if (tok_is(t, ',')) /* permit comma separators */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004256 break;
4257 /* else fall through */
4258 default:
H. Peter Anvin8571f062019-09-23 16:40:03 -07004259 nasm_nonfatal("non-string passed to `%s': %s", dname,
4260 tok_text(t));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004261 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004262 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004263 }
4264 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004265
H. Peter Anvin (Intel)f770ce82019-10-17 18:22:43 -07004266 q = qbuf = nasm_malloc(len+1);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04004267 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004268 if (t->type == TOK_INTERNAL_STRING)
4269 q = mempcpy(q, tok_text(t), t->len);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004270 }
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004271 *q = '\0';
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004272
4273 /*
4274 * We now have a macro name, an implicit parameter count of
4275 * zero, and a numeric token to use as an expansion. Create
4276 * and store an SMacro.
4277 */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004278 macro_start = make_tok_qstr_len(NULL, qbuf, len);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004279 nasm_free(qbuf);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004280 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004281 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004282 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07004283
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004285 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004286 int64_t start, count;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004287 const char *txt;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004288 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07004289
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004290 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004291 goto done;
4292
H. Peter Anvine2c80182005-01-15 22:15:51 +00004293 last = tline;
4294 tline = expand_smacro(tline->next);
4295 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004296
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04004297 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004298 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004299
4300 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004301
H. Peter Anvine2c80182005-01-15 22:15:51 +00004302 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004303 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004304 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004305 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004306 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004307 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004308
H. Peter Anvin8b262472019-02-26 14:00:54 -08004309 pps.tptr = t->next;
4310 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004311 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004312 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004313 if (!evalresult) {
4314 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004315 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004316 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004317 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004318 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004319 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004320 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004321 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004322
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004323 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08004324 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004325 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004326 } else {
4327 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004328 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004329 if (!evalresult) {
4330 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004331 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004332 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004333 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004334 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004335 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004336 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004337 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004338 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004339
H. Peter Anvin8571f062019-09-23 16:40:03 -07004340 unquote_token(t);
4341 len = t->len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004342
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004343 /* make start and count being in range */
4344 if (start < 0)
4345 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004346 if (count < 0)
4347 count = len + count + 1 - start;
4348 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04004349 count = len - start;
4350 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04004351 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00004352
H. Peter Anvin8571f062019-09-23 16:40:03 -07004353 txt = (start < 0) ? "" : tok_text(t) + start;
4354 len = count;
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07004355 macro_start = make_tok_qstr_len(NULL, txt, len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004356
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 /*
4358 * We now have a macro name, an implicit parameter count of
4359 * zero, and a numeric token to use as an expansion. Create
4360 * and store an SMacro.
4361 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004362 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004363 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004364 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004365 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004366
H. Peter Anvin8b262472019-02-26 14:00:54 -08004367 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004368 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004369 goto done;
4370
H. Peter Anvine2c80182005-01-15 22:15:51 +00004371 last = tline;
4372 tline = expand_smacro(tline->next);
4373 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004374
H. Peter Anvin8b262472019-02-26 14:00:54 -08004375 pps.tptr = tline;
4376 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004377 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004378 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004379 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004380 if (!evalresult)
4381 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004382
H. Peter Anvine2c80182005-01-15 22:15:51 +00004383 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004384 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004385
H. Peter Anvine2c80182005-01-15 22:15:51 +00004386 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004387 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004388 free_tlist(origline);
4389 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004390 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004391
H. Peter Anvin8571f062019-09-23 16:40:03 -07004392 macro_start = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004393
H. Peter Anvine2c80182005-01-15 22:15:51 +00004394 /*
4395 * We now have a macro name, an implicit parameter count of
4396 * zero, and a numeric token to use as an expansion. Create
4397 * and store an SMacro.
4398 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004399 define_smacro(mname, casesense, macro_start, NULL);
4400 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004401
H. Peter Anvind2354082019-08-27 16:38:48 -07004402 case PP_ALIASES:
4403 tline = tline->next;
4404 tline = expand_smacro(tline);
4405 do_aliases = pp_get_boolean_option(tline, do_aliases);
4406 break;
4407
H. Peter Anvine2c80182005-01-15 22:15:51 +00004408 case PP_LINE:
4409 /*
4410 * Syntax is `%line nnn[+mmm] [filename]'
4411 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004412 if (unlikely(pp_noline))
4413 goto done;
4414
H. Peter Anvine2c80182005-01-15 22:15:51 +00004415 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004416 tline = skip_white(tline);
4417 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004418 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004419 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004420 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004421 k = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004422 m = 1;
4423 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004424 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004426 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004427 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004428 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004429 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07004430 m = readnum(tok_text(tline), &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004431 tline = tline->next;
4432 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004433 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 src_set_linnum(k);
4435 istk->lineinc = m;
4436 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004437 char *fname = detoken(tline, false);
4438 src_set_fname(fname);
4439 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004440 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004441 break;
4442 }
4443
4444done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004445 free_tlist(origline);
4446 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004447}
4448
4449/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004450 * Ensure that a macro parameter contains a condition code and
4451 * nothing else. Return the condition code index if so, or -1
4452 * otherwise.
4453 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004455{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004456 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004457
H. Peter Anvin25a99342007-09-22 17:45:45 -07004458 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004459 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004460
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004461 t = skip_white(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004462 if (!tok_type(t, TOK_ID))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004463 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004464 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004465 tt = skip_white(tt);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004466 if (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004467 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004468
H. Peter Anvin8571f062019-09-23 16:40:03 -07004469 return bsii(tok_text(t), (const char **)conditions,
4470 ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004471}
4472
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004473static inline bool pp_concat_match(const Token *t, unsigned int mask)
4474{
4475 return t && (PP_CONCAT_MASK(t->type) & mask);
4476}
4477
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004478/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004479 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004480 * pasting, if @handle_explicit passed then explicit pasting
4481 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004482 * The @m array can contain a series of token types which are
4483 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004484 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004485static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004486 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004487{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004488 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004489 bool pasted = false;
4490 char *buf, *p;
4491 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004492
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004493 /*
4494 * The last token before pasting. We need it
4495 * to be able to connect new handled tokens.
4496 * In other words if there were a tokens stream
4497 *
4498 * A -> B -> C -> D
4499 *
4500 * and we've joined tokens B and C, the resulting
4501 * stream should be
4502 *
4503 * A -> BC -> D
4504 */
4505 tok = *head;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004506 prev_next = prev_nonspace = head;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004507
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004508 if (tok_white(tok) || tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004509 prev_nonspace = NULL;
4510
4511 while (tok && (next = tok->next)) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004512 bool did_paste = false;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004513
4514 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004515 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004516 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004517 tok->next = next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004518 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004519
4520 case TOK_PASTE:
4521 /* Explicit pasting */
4522 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004523 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004524
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004525 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004526 if (!prev_nonspace) {
4527 nasm_nonfatal("No lvalue found on pasting");
4528 tok = delete_Token(tok);
4529 break;
4530 }
4531
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004532 did_paste = true;
4533
4534 prev_next = prev_nonspace;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004535 t = *prev_nonspace;
4536
4537 /* Delete leading whitespace */
4538 next = zap_white(t->next);
4539
4540 /* Delete the %+ token itself */
4541 nasm_assert(next == tok);
4542 next = delete_Token(next);
4543
4544 /* Delete trailing whitespace */
4545 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004546
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004547 /*
4548 * No ending token, this might happen in two
4549 * cases
4550 *
4551 * 1) There indeed no right token at all
4552 * 2) There is a bare "%define ID" statement,
4553 * and @ID does expand to whitespace.
4554 *
4555 * So technically we need to do a grammar analysis
4556 * in another stage of parsing, but for now lets don't
4557 * change the behaviour people used to. Simply allow
4558 * whitespace after paste token.
4559 */
4560 if (!next) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004561 *prev_nonspace = tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004562 break;
4563 }
4564
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004565 p = buf = nasm_malloc(t->len + next->len + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004566 p = mempcpy(p, tok_text(t), t->len);
4567 p = mempcpy(p, tok_text(next), next->len);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004568 *p = '\0';
4569 delete_Token(t);
4570 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004571 nasm_free(buf);
4572
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004573 if (unlikely(!t)) {
4574 /*
4575 * No output at all? Replace with a single whitespace.
4576 * This should never happen.
4577 */
4578 t = new_White(NULL);
4579 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004580
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004581 *prev_nonspace = tok = t;
4582 while (t->next)
4583 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004584
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004585 /* Delete the second token and attach to the end of the list */
4586 t->next = delete_Token(next);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004587
4588 /* We want to restart from the head of the pasted token */
4589 next = tok;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004590 break;
4591
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004592 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004593 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004594 for (i = 0; i < mnum; i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004595 if (pp_concat_match(tok, m[i].mask_head))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004596 break;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004597 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004598
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004599 if (i >= mnum)
4600 break;
4601
4602 len = tok->len;
4603 while (pp_concat_match(next, m[i].mask_tail)) {
4604 len += next->len;
4605 next = next->next;
4606 }
4607
4608 /* No match or no text to process */
4609 if (len == tok->len)
4610 break;
4611
4612 p = buf = nasm_malloc(len + 1);
4613 while (tok != next) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004614 p = mempcpy(p, tok_text(tok), tok->len);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004615 tok = delete_Token(tok);
4616 }
4617 *p = '\0';
4618 *prev_next = tok = t = tokenize(buf);
4619 nasm_free(buf);
4620
4621 /*
4622 * Connect pasted into original stream,
4623 * ie A -> new-tokens -> B
4624 */
4625 while (t->next)
4626 t = t->next;
4627 t->next = next;
4628 prev_next = prev_nonspace = &t->next;
4629 did_paste = true;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004630 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004631 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004632
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004633 if (did_paste) {
4634 pasted = true;
4635 } else {
4636 prev_next = &tok->next;
4637 if (next && next->type != TOK_WHITESPACE && next->type != TOK_PASTE)
4638 prev_nonspace = prev_next;
4639 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004640
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07004641 tok = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004642 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004643
4644 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004645}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004646
4647/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004648 * Computes the proper rotation of mmacro parameters
4649 */
4650static int mmac_rotate(const MMacro *mac, unsigned int n)
4651{
4652 if (--n < mac->nparam)
4653 n = (n + mac->rotate) % mac->nparam;
4654
4655 return n+1;
4656}
4657
4658/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004659 * expands to a list of tokens from %{x:y}
4660 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004661static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004662{
4663 Token *t = tline, **tt, *tm, *head;
4664 char *pos;
4665 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004666
H. Peter Anvin8571f062019-09-23 16:40:03 -07004667 pos = strchr(tok_text(tline), ':');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004668 nasm_assert(pos);
4669
4670 lst = atoi(pos + 1);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004671 fst = atoi(tok_text(tline) + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004672
4673 /*
4674 * only macros params are accounted so
4675 * if someone passes %0 -- we reject such
4676 * value(s)
4677 */
4678 if (lst == 0 || fst == 0)
4679 goto err;
4680
4681 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004682 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4683 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004684 goto err;
4685
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004686 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4687 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004688
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004689 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004690 fst--, lst--;
4691
4692 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004693 * It will be at least one token. Note we
4694 * need to scan params until separator, otherwise
4695 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004696 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004697 j = (fst + mac->rotate) % mac->nparam;
4698 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004699 if (!tm)
4700 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004701 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004702 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004703 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004704 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004705 *tt = t, tt = &t->next, tm = tm->next;
4706 }
4707
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004708 if (fst < lst) {
4709 for (i = fst + 1; i <= lst; i++) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004710 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004711 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004712 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004713 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004714 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004715 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004716 *tt = t, tt = &t->next, tm = tm->next;
4717 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004718 }
4719 } else {
4720 for (i = fst - 1; i >= lst; i--) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004721 t = make_tok_char(NULL, ',');
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004722 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004723 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004724 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004725 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004726 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004727 *tt = t, tt = &t->next, tm = tm->next;
4728 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004729 }
4730 }
4731
4732 *last = tt;
4733 return head;
4734
4735err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004736 nasm_nonfatal("`%%{%s}': macro parameters out of range",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004737 tok_text(tline) + 1);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004738 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004739}
4740
H. Peter Anvin76690a12002-04-30 20:52:49 +00004741/*
4742 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004743 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004744 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004745 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004746static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004747{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004748 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004749 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004750 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004751
4752 tail = &thead;
4753 thead = NULL;
4754
H. Peter Anvine2c80182005-01-15 22:15:51 +00004755 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004756 bool change;
4757 Token *t = tline;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004758 const char *text = tok_text(t);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004759 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004760
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004761 tline = tline->next;
4762 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004763
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004764 switch (type) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07004765 case TOK_LOCAL_SYMBOL:
4766 type = TOK_ID;
4767 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4768 change = true;
4769 break;
4770 case TOK_MMACRO_PARAM:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004771 {
4772 Token *tt = NULL;
4773
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004774 change = true;
4775
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004776 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004777 nasm_nonfatal("`%s': not in a macro call", text);
4778 text = NULL;
4779 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004780 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004781
4782 if (strchr(text, ':')) {
4783 /*
4784 * seems we have a parameters range here
4785 */
4786 Token *head, **last;
4787 head = expand_mmac_params_range(mac, t, &last);
4788 if (head) {
4789 *tail = head;
4790 *last = tline;
4791 text = NULL;
4792 }
4793 break;
4794 }
4795
4796 switch (text[1]) {
4797 /*
4798 * We have to make a substitution of one of the
4799 * forms %1, %-1, %+1, %%foo, %0, %00.
4800 */
4801 case '0':
4802 if (!text[2]) {
4803 type = TOK_NUMBER;
4804 text = nasm_asprintf("%d", mac->nparam);
4805 break;
4806 }
4807 if (text[2] != '0' || text[3])
4808 goto invalid;
4809 /* a possible captured label == mac->params[0] */
4810 /* fall through */
4811 default:
4812 {
4813 unsigned long n;
4814 char *ep;
4815
4816 n = strtoul(text + 1, &ep, 10);
4817 if (unlikely(*ep))
4818 goto invalid;
4819
4820 if (n <= mac->nparam) {
4821 n = mmac_rotate(mac, n);
4822 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4823 }
4824 text = NULL;
4825 break;
4826 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004827 case '-':
4828 case '+':
4829 {
4830 int cc;
4831 unsigned long n;
4832 char *ep;
4833
4834 text = NULL;
4835
H. Peter Anvin8571f062019-09-23 16:40:03 -07004836 n = strtoul(tok_text(t) + 2, &ep, 10);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004837 if (unlikely(*ep))
4838 goto invalid;
4839
4840 if (n && n < mac->nparam) {
4841 n = mmac_rotate(mac, n);
4842 tt = mac->params[n];
4843 }
4844 cc = find_cc(tt);
4845 if (cc == -1) {
4846 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvin8571f062019-09-23 16:40:03 -07004847 tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004848 text = NULL;
4849 break;
4850 }
4851
4852 type = TOK_ID;
4853 if (text[1] == '-') {
4854 int ncc = inverse_ccs[cc];
4855 if (unlikely(ncc == -1)) {
4856 nasm_nonfatal("condition code `%s' is not invertible",
4857 conditions[cc]);
4858 break;
4859 }
4860 cc = ncc;
4861 }
4862 text = nasm_strdup(conditions[cc]);
4863 break;
4864 }
4865
4866 invalid:
4867 nasm_nonfatal("invalid macro parameter: `%s'", text);
4868 text = NULL;
4869 break;
4870 }
4871 break;
4872 }
4873
4874 case TOK_PREPROC_Q:
4875 if (mac) {
4876 type = TOK_ID;
4877 text = nasm_strdup(mac->iname);
4878 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004879 } else {
4880 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004881 }
4882 break;
4883
4884 case TOK_PREPROC_QQ:
4885 if (mac) {
4886 type = TOK_ID;
4887 text = nasm_strdup(mac->name);
4888 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004889 } else {
4890 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004891 }
4892 break;
4893
4894 case TOK_INDIRECT:
4895 {
4896 Token *tt;
4897
H. Peter Anvin8571f062019-09-23 16:40:03 -07004898 tt = tokenize(tok_text(t));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004899 tt = expand_mmac_params(tt);
4900 tt = expand_smacro(tt);
4901 /* Why dup_tlist() here? We should own tt... */
4902 dup_tlist(tt, &tail);
4903 text = NULL;
4904 change = true;
4905 break;
4906 }
4907
4908 default:
4909 change = false;
4910 break;
4911 }
4912
4913 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004914 if (!text) {
4915 delete_Token(t);
4916 } else {
4917 *tail = t;
4918 tail = &t->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004919 set_text(t, text, tok_strlen(text));
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004920 t->type = type;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004921 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004922 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004923 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004924 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004925 tail = &t->next;
4926 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004927 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004928
H. Peter Anvineba20a72002-04-30 20:53:55 +00004929 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004930
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004931 if (changed) {
4932 const struct tokseq_match t[] = {
4933 {
4934 PP_CONCAT_MASK(TOK_ID) |
4935 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4936 PP_CONCAT_MASK(TOK_ID) |
4937 PP_CONCAT_MASK(TOK_NUMBER) |
4938 PP_CONCAT_MASK(TOK_FLOAT) |
4939 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4940 },
4941 {
4942 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4943 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4944 }
4945 };
4946 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4947 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004948
H. Peter Anvin76690a12002-04-30 20:52:49 +00004949 return thead;
4950}
4951
H. Peter Anvin322bee02019-08-10 01:38:06 -07004952static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004953
H. Peter Anvin76690a12002-04-30 20:52:49 +00004954/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004955 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004956 * a macro at all, it is simply copied to the output and the pointer
4957 * advanced. tpp should be a pointer to a pointer (usually the next
4958 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004959 * to point to the first token of the expansion, and *tpp updated to
4960 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004961 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004962 * If the expansion is empty, *tpp will be unchanged but **tpp will
4963 * be advanced past the macro call.
4964 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004965 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004966 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004967static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004968{
4969 Token **params = NULL;
4970 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004971 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004972 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004973 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004974 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004975 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004976 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004977 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004978
4979 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004980 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004981
H. Peter Anvin8571f062019-09-23 16:40:03 -07004982 mname = tok_text(mstart);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004983
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004984 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004985 smacro_deadman.levels--;
4986
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004987 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004988 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004989 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004990 smacro_deadman.triggered = true;
4991 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004992 goto not_a_macro;
H. Peter Anvin8571f062019-09-23 16:40:03 -07004993 } else if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004994 head = (SMacro *)hash_findix(&smacros, mname);
H. Peter Anvin8571f062019-09-23 16:40:03 -07004995 } else if (tline->type == TOK_LOCAL_MACRO) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004996 Context *ctx = get_ctx(mname, &mname);
4997 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4998 } else {
4999 goto not_a_macro;
5000 }
5001
5002 /*
5003 * We've hit an identifier of some sort. First check whether the
5004 * identifier is a single-line macro at all, then think about
5005 * checking for parameters if necessary.
5006 */
5007 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07005008 if (unlikely(m->alias && !do_aliases))
5009 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005010 if (!mstrcmp(m->name, mname, m->casesense))
5011 break;
5012 }
5013
5014 if (!m) {
5015 goto not_a_macro;
5016 }
5017
5018 /* Parse parameters, if applicable */
5019
5020 params = NULL;
5021 nparam = 0;
5022
5023 if (m->nparam == 0) {
5024 /*
5025 * Simple case: the macro is parameterless.
5026 * Nothing to parse; the expansion code will
5027 * drop the macro name token.
5028 */
5029 } else {
5030 /*
5031 * Complicated case: at least one macro with this name
5032 * exists and takes parameters. We must find the
5033 * parameters in the call, count them, find the SMacro
5034 * that corresponds to that form of the macro call, and
5035 * substitute for the parameters when we expand. What a
5036 * pain.
5037 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005038 Token *t;
5039 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005040
5041 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005042 tline = skip_white(tline);
5043 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005044 /*
5045 * This macro wasn't called with parameters: ignore
5046 * the call. (Behaviour borrowed from gnu cpp.)
5047 */
5048 goto not_a_macro;
5049 }
5050
5051 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005052 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005053 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005054 t = tline; /* tline points to leading ( */
5055
5056 while (paren) {
5057 t = t->next;
5058
5059 if (!t) {
5060 nasm_nonfatal("macro call expects terminating `)'");
5061 goto not_a_macro;
5062 }
5063
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005064 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005065 continue;
5066
H. Peter Anvin8571f062019-09-23 16:40:03 -07005067 switch (t->text.a[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005068 case ',':
5069 if (!brackets)
5070 nparam++;
5071 break;
5072
5073 case '{':
5074 brackets++;
5075 break;
5076
5077 case '}':
5078 if (brackets > 0)
5079 brackets--;
5080 break;
5081
5082 case '(':
5083 if (!brackets)
5084 paren++;
5085 break;
5086
5087 case ')':
5088 if (!brackets)
5089 paren--;
5090 break;
5091
5092 default:
5093 break; /* Normal token */
5094 }
5095 }
5096
5097 /*
5098 * Look for a macro matching in both name and parameter count.
5099 * We already know any matches cannot be anywhere before the
5100 * current position of "m", so there is no reason to
5101 * backtrack.
5102 */
5103 while (1) {
5104 if (!m) {
5105 /*!
5106 *!macro-params-single [on] single-line macro calls with wrong parameter count
5107 *! warns about \i{single-line macros} being invoked
5108 *! with the wrong number of parameters.
5109 */
5110 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
5111 "single-line macro `%s' exists, "
5112 "but not taking %d parameter%s",
5113 mname, nparam, (nparam == 1) ? "" : "s");
5114 goto not_a_macro;
5115 }
5116
5117 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005118 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005119 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005120 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005121 break; /* Also good */
5122 }
5123 m = m->next;
5124 }
5125 }
5126
5127 if (m->in_progress)
5128 goto not_a_macro;
5129
5130 /* Expand the macro */
5131 m->in_progress = true;
5132
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005133 if (nparam) {
5134 /* Extract parameters */
5135 Token **phead, **pep;
5136 int white = 0;
5137 int brackets = 0;
5138 int paren;
5139 bool bracketed = false;
5140 bool bad_bracket = false;
5141 enum sparmflags flags;
5142
5143 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005144 paren = 1;
5145 nasm_newn(params, nparam);
5146 i = 0;
5147 flags = m->params[i].flags;
5148 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005149 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005150
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005151 while (paren) {
5152 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005153 char ch;
5154
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005155 tline = tline->next;
5156
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005157 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005158 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005159
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005160 ch = 0;
5161 skip = false;
5162
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005163
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005164 switch (tline->type) {
5165 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005166 if (tline->len == 1)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005167 ch = tline->text.a[0];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005168 break;
5169
5170 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005171 if (!(flags & SPARM_NOSTRIP)) {
5172 if (brackets || *phead)
5173 white++; /* Keep interior whitespace */
5174 skip = true;
5175 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005176 break;
5177
5178 default:
5179 break;
5180 }
5181
5182 switch (ch) {
5183 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005184 if (!brackets && !(flags & SPARM_GREEDY)) {
5185 i++;
5186 nasm_assert(i < nparam);
5187 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005188 *pep = NULL;
5189 bracketed = false;
5190 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005191 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005192 }
5193 break;
5194
5195 case '{':
5196 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005197 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
5198 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005199 }
5200 brackets++;
5201 break;
5202
5203 case '}':
5204 if (brackets > 0) {
5205 if (!--brackets)
5206 skip = bracketed;
5207 }
5208 break;
5209
5210 case '(':
5211 if (!brackets)
5212 paren++;
5213 break;
5214
5215 case ')':
5216 if (!brackets) {
5217 paren--;
5218 if (!paren) {
5219 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005220 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005221 }
5222 }
5223 break;
5224
5225 default:
5226 break; /* Normal token */
5227 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005228
5229 if (!skip) {
5230 Token *t;
5231
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005232 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005233
5234 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005235 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005236 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005237 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005238 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005239 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005240 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005241 }
5242 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005243
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005244 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005245 * Possible further processing of parameters. Note that the
5246 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005247 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005248 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005249 enum sparmflags flags = m->params[i].flags;
5250
5251 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005252 /* Evaluate this parameter as a number */
5253 struct ppscan pps;
5254 struct tokenval tokval;
5255 expr *evalresult;
5256 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005257
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005258 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
5259 pps.ntokens = -1;
5260 tokval.t_type = TOKEN_INVALID;
5261 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005262
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005263 free_tlist(eval_param);
5264 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005265
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005266 if (!evalresult) {
5267 /* Nothing meaningful to do */
5268 } else if (tokval.t_type) {
5269 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
5270 } else if (!is_simple(evalresult)) {
5271 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
5272 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005273 params[i] = make_tok_num(NULL, reloc_value(evalresult));
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005274 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005275 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005276
5277 if (flags & SPARM_STR) {
5278 /* Convert expansion to a quoted string */
5279 char *arg;
5280 Token *qs;
5281
5282 qs = expand_smacro_noreset(params[i]);
5283 arg = detoken(qs, false);
5284 free_tlist(qs);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005285 params[i] = make_tok_qstr(NULL, arg);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005286 nasm_free(arg);
5287 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005288 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005289 }
5290
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005291 /* Note: we own the expansion this returns. */
5292 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005293
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005294 tafter = tline->next; /* Skip past the macro call */
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005295 tline->next = NULL; /* Truncate list at the macro call end */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005296 tline = tafter;
5297
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005298 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005299 cond_comma = false;
5300
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005301 while (t) {
5302 enum pp_token_type type = t->type;
5303 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005304
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005305 switch (type) {
5306 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005307 delete_Token(t);
5308 t = dup_Token(tline, mstart);
5309 break;
5310
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005311 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005312 {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005313 size_t mlen = strlen(m->name);
5314 size_t len;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005315 char *p;
5316
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005317 t->type = mstart->type;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005318 if (t->type == TOK_LOCAL_MACRO) {
5319 const char *psp; /* prefix start pointer */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005320 const char *pep; /* prefix end pointer */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005321 size_t plen;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005322
H. Peter Anvin8571f062019-09-23 16:40:03 -07005323 psp = tok_text(mstart);
5324 get_ctx(psp, &pep);
5325 plen = pep - psp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005326
H. Peter Anvin8571f062019-09-23 16:40:03 -07005327 len = mlen + plen;
5328 p = nasm_malloc(len + 1);
5329 p = mempcpy(p, psp, plen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005330 } else {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005331 len = mlen;
5332 p = nasm_malloc(len + 1);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005333 }
H. Peter Anvin8571f062019-09-23 16:40:03 -07005334 p = mempcpy(p, m->name, mlen);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005335 *p = '\0';
H. Peter Anvin8571f062019-09-23 16:40:03 -07005336 set_text_free(t, p, len);
5337
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005338 t->next = tline;
5339 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005340 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005341
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005342 case TOK_COND_COMMA:
5343 delete_Token(t);
H. Peter Anvin8571f062019-09-23 16:40:03 -07005344 t = cond_comma ? make_tok_char(tline, ',') : NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005345 break;
5346
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005347 case TOK_ID:
5348 case TOK_PREPROC_ID:
H. Peter Anvin8571f062019-09-23 16:40:03 -07005349 case TOK_LOCAL_MACRO:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005350 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005351 /*
5352 * Chain this into the target line *before* expanding,
5353 * that way we pick up any arguments to the new macro call,
5354 * if applicable.
5355 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005356 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005357 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005358 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005359 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005360 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005361 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005362 default:
5363 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005364 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005365 nasm_assert(!tup && param < nparam);
5366 delete_Token(t);
5367 t = NULL;
5368 tup = tnext;
5369 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005370 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005371 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005372 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005373 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005374 }
5375
5376 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005377 Token *endt = tline;
5378
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005379 tline = t;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005380 while (!cond_comma && t && t != endt)
5381 cond_comma = t->type != TOK_WHITESPACE;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005382 }
5383
5384 if (tnext) {
5385 t = tnext;
5386 } else {
5387 t = tup;
5388 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005389 }
5390 }
5391
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005392 **tpp = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005393 for (t = tline; t != tafter; t = t->next)
5394 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005395
5396 m->in_progress = false;
5397
5398 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005399 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005400 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005401
5402 /*
5403 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005404 * and then advance to the next input token. Note that this is
5405 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005406 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005407not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005408 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005409 m = NULL;
5410done:
5411 smacro_deadman.levels++;
5412 if (unlikely(params))
5413 free_tlist_array(params, nparam);
5414 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005415}
5416
5417/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005418 * Expand all single-line macro calls made in the given line.
5419 * Return the expanded version of the line. The original is deemed
5420 * to be destroyed in the process. (In reality we'll just move
5421 * Tokens from input to output a lot of the time, rather than
5422 * actually bothering to destroy and replicate.)
5423 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005424static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005425{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005426 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005427 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5428 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005429 return expand_smacro_noreset(tline);
5430}
5431
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005432static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005433{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005434 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005435 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005436
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005437 if (!org_tline)
5438 return NULL; /* Empty input */
5439
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005440 /*
5441 * Trick: we should avoid changing the start token pointer since it can
5442 * be contained in "next" field of other token. Because of this
5443 * we allocate a copy of first token and work with it; at the end of
5444 * routine we copy it back
5445 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005446 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005447
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005448 /*
5449 * Pretend that we always end up doing expansion on the first pass;
5450 * that way %+ get processed. However, if we process %+ before the
5451 * first pass, we end up with things like MACRO %+ TAIL trying to
5452 * look up the macro "MACROTAIL", which we don't want.
5453 */
5454 expanded = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005455 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005456 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005457 {
5458 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005459 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5460 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005461 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5462 PP_CONCAT_MASK(TOK_ID) |
H. Peter Anvin8571f062019-09-23 16:40:03 -07005463 PP_CONCAT_MASK(TOK_LOCAL_MACRO) |
5464 PP_CONCAT_MASK(TOK_ENVIRON) |
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005465 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5466 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5467 }
5468 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005469 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005470
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005471 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005472 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005473
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005474 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005475 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005476
5477 /*
5478 * Now scan the entire line and look for successive TOK_IDs
5479 * that resulted after expansion (they can't be produced by
5480 * tokenize()). The successive TOK_IDs should be concatenated.
5481 * Also we look for %+ tokens and concatenate the tokens
5482 * before and after them (without white spaces in between).
5483 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005484 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5485 break; /* Done again! */
5486
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005487 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005488 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005489
H. Peter Anvin8571f062019-09-23 16:40:03 -07005490 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005491 /*
5492 * The expression expanded to empty line;
5493 * we can't return NULL because of the "trick" above.
5494 * Just set the line to a single WHITESPACE token.
H. Peter Anvin8571f062019-09-23 16:40:03 -07005495 */
5496
5497 tline = new_White(NULL);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005498 }
5499
H. Peter Anvin8571f062019-09-23 16:40:03 -07005500 steal_Token(org_tline, tline);
5501 org_tline->next = tline->next;
5502 delete_Token(tline);
5503
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005504 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005505}
5506
5507/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005508 * Similar to expand_smacro but used exclusively with macro identifiers
5509 * right before they are fetched in. The reason is that there can be
5510 * identifiers consisting of several subparts. We consider that if there
5511 * are more than one element forming the name, user wants a expansion,
5512 * otherwise it will be left as-is. Example:
5513 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005514 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005515 *
5516 * the identifier %$abc will be left as-is so that the handler for %define
5517 * will suck it and define the corresponding value. Other case:
5518 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005519 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005520 *
5521 * In this case user wants name to be expanded *before* %define starts
5522 * working, so we'll expand %$abc into something (if it has a value;
5523 * otherwise it will be left as-is) then concatenate all successive
5524 * PP_IDs into one.
5525 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005526static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005527{
5528 Token *cur, *oldnext = NULL;
5529
H. Peter Anvin734b1882002-04-30 21:01:08 +00005530 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005531 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005532
5533 cur = tline;
5534 while (cur->next &&
H. Peter Anvin8571f062019-09-23 16:40:03 -07005535 (cur->next->type == TOK_ID || cur->next->type == TOK_PREPROC_ID ||
5536 cur->next->type == TOK_LOCAL_MACRO || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005537 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005538
5539 /* If identifier consists of just one token, don't expand */
5540 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005541 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005542
H. Peter Anvine2c80182005-01-15 22:15:51 +00005543 if (cur) {
5544 oldnext = cur->next; /* Detach the tail past identifier */
5545 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005546 }
5547
H. Peter Anvin734b1882002-04-30 21:01:08 +00005548 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005549
H. Peter Anvine2c80182005-01-15 22:15:51 +00005550 if (cur) {
5551 /* expand_smacro possibly changhed tline; re-scan for EOL */
5552 cur = tline;
5553 while (cur && cur->next)
5554 cur = cur->next;
5555 if (cur)
5556 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005557 }
5558
5559 return tline;
5560}
5561
5562/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005563 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005564 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005565 * to check for an initial label - that's taken care of in
5566 * expand_mmacro - but must check numbers of parameters. Guaranteed
5567 * to be called with tline->type == TOK_ID, so the putative macro
5568 * name is easy to find.
5569 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005570static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005571{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005572 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005573 Token **params;
5574 int nparam;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005575 const char *finding = tok_text(tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005576
H. Peter Anvin8571f062019-09-23 16:40:03 -07005577 head = (MMacro *) hash_findix(&mmacros, finding);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005578
5579 /*
5580 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005581 * name which isn't already excluded by macro cycle removal.
5582 * (The cycle removal test here helps optimize the case of wrapping
5583 * instructions, and is cheap to do here.)
5584 *
5585 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005586 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005587 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005588 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005589 list_for_each(m, head) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005590 if (!mstrcmp(m->name, finding, m->casesense) &&
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005591 (m->in_progress != 1 || m->max_depth > 0))
5592 break; /* Found something that needs consideration */
5593 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005594 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005595 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005596
5597 /*
5598 * OK, we have a potential macro. Count and demarcate the
5599 * parameters.
5600 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005601 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005602
5603 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005604 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005605 * structure that handles this number.
5606 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005607 while (m) {
5608 if (m->nparam_min <= nparam
5609 && (m->plus || nparam <= m->nparam_max)) {
5610 /*
5611 * This one is right. Just check if cycle removal
5612 * prohibits us using it before we actually celebrate...
5613 */
5614 if (m->in_progress > m->max_depth) {
5615 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005616 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005617 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005618 }
5619 nasm_free(params);
5620 return NULL;
5621 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005622 /*
5623 * It's right, and we can use it. Add its default
5624 * parameters to the end of our list if necessary.
5625 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005626 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005627 int newnparam = m->nparam_min + m->ndefs;
5628 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5629 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5630 (newnparam - nparam) * sizeof(*params));
5631 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005632 }
5633 /*
5634 * If we've gone over the maximum parameter count (and
5635 * we're in Plus mode), ignore parameters beyond
5636 * nparam_max.
5637 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005638 if (m->plus && nparam > m->nparam_max)
5639 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005640
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005641 /*
5642 * If nparam was adjusted above, make sure the list is still
5643 * NULL-terminated.
5644 */
5645 params[nparam+1] = NULL;
5646
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005647 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005648 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005649 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005650 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005651 }
5652 /*
5653 * This one wasn't right: look for the next one with the
5654 * same name.
5655 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005656 list_for_each(m, m->next)
H. Peter Anvin8571f062019-09-23 16:40:03 -07005657 if (!mstrcmp(m->name, tok_text(tline), m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005658 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005659 }
5660
5661 /*
5662 * After all that, we didn't find one with the right number of
5663 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005664 *!
5665 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5666 *! warns about \i{multi-line macros} being invoked
5667 *! with the wrong number of parameters. See \k{mlmacover} for an
5668 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005669 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005670 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5671 "multi-line macro `%s' exists, but not taking %d parameter%s",
H. Peter Anvin8571f062019-09-23 16:40:03 -07005672 tok_text(tline), nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005673 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005674 return NULL;
5675}
5676
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005677
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005678#if 0
5679
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005680/*
5681 * Save MMacro invocation specific fields in
5682 * preparation for a recursive macro expansion
5683 */
5684static void push_mmacro(MMacro *m)
5685{
5686 MMacroInvocation *i;
5687
5688 i = nasm_malloc(sizeof(MMacroInvocation));
5689 i->prev = m->prev;
5690 i->params = m->params;
5691 i->iline = m->iline;
5692 i->nparam = m->nparam;
5693 i->rotate = m->rotate;
5694 i->paramlen = m->paramlen;
5695 i->unique = m->unique;
5696 i->condcnt = m->condcnt;
5697 m->prev = i;
5698}
5699
5700
5701/*
5702 * Restore MMacro invocation specific fields that were
5703 * saved during a previous recursive macro expansion
5704 */
5705static void pop_mmacro(MMacro *m)
5706{
5707 MMacroInvocation *i;
5708
5709 if (m->prev) {
5710 i = m->prev;
5711 m->prev = i->prev;
5712 m->params = i->params;
5713 m->iline = i->iline;
5714 m->nparam = i->nparam;
5715 m->rotate = i->rotate;
5716 m->paramlen = i->paramlen;
5717 m->unique = i->unique;
5718 m->condcnt = i->condcnt;
5719 nasm_free(i);
5720 }
5721}
5722
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005723#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005724
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005725/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005726 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005727 */
5728static void list_mmacro_call(const MMacro *m)
5729{
5730 const char prefix[] = " ;;; [macro] ";
5731 size_t namelen, size;
5732 char *buf, *p;
5733 unsigned int i;
5734 const Token *t;
5735
5736 namelen = strlen(m->iname);
5737 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5738
5739 for (i = 1; i <= m->nparam; i++) {
5740 int j = 0;
5741 size += 3; /* Braces and space/comma */
5742 list_for_each(t, m->params[i]) {
5743 if (j++ >= m->paramlen[i])
5744 break;
5745 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5746 }
5747 }
5748
5749 buf = p = nasm_malloc(size);
5750 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5751 p = mempcpy(p, m->iname, namelen);
5752 *p++ = ' ';
5753
5754 for (i = 1; i <= m->nparam; i++) {
5755 int j = 0;
5756 *p++ = '{';
5757 list_for_each(t, m->params[i]) {
5758 if (j++ >= m->paramlen[i])
5759 break;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005760 p = mempcpy(p, tok_text(t), t->len);
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005761 }
5762 *p++ = '}';
5763 *p++ = ',';
5764 }
5765
5766 *--p = '\0'; /* Replace last delimeter with null */
5767 lfmt->line(LIST_MACRO, -1, buf);
5768 nasm_free(buf);
5769}
5770
5771/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005772 * Expand the multi-line macro call made by the given line, if
5773 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005774 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005775 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005776static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005777{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005778 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005779 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005780 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005781 Token **params, *t, *tt;
5782 MMacro *m;
5783 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005784 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005785 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005786 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005787
5788 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005789 t = skip_white(t);
5790 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvin8571f062019-09-23 16:40:03 -07005791 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_LOCAL_MACRO))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005792 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005793 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005794 if (m) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005795 mname = tok_text(t);
H. Peter Anvinc751e862008-06-09 10:18:45 -07005796 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005797 Token *last;
5798 /*
5799 * We have an id which isn't a macro call. We'll assume
5800 * it might be a label; we'll also check to see if a
5801 * colon follows it. Then, if there's another id after
5802 * that lot, we'll check it again for macro-hood.
5803 */
5804 label = last = t;
5805 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005806 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005807 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005808 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005809 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005810 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005811 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005812 last = t, t = t->next;
5813 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005814 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005815 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005816 last->next = NULL;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005817 mname = tok_text(t);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005818 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005819 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005820
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005821 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5822 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5823 if (!mmacro_deadman.triggered) {
5824 nasm_nonfatal("interminable multiline macro recursion");
5825 mmacro_deadman.triggered = true;
5826 }
5827 return 0;
5828 }
5829
5830 mmacro_deadman.total++;
5831 mmacro_deadman.levels++;
5832
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005833 /*
5834 * Fix up the parameters: this involves stripping leading and
5835 * trailing whitespace, then stripping braces if they are
5836 * present.
5837 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005838 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005839
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005840 nasm_assert(params[nparam+1] == NULL);
5841
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005842 for (i = 1; (t = params[i]); i++) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005843 bool braced = false;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005844 int brace = 0;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005845 int white = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005846 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005847
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005848 t = skip_white(t);
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005849 if (tok_is(t, '{')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005850 t = t->next;
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005851 brace = 1;
5852 braced = true;
5853 comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005854 }
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005855
5856 params[i] = t;
5857 for (; t; t = t->next) {
5858 if (tok_white(t)) {
5859 white++;
5860 continue;
5861 }
5862
5863 if (t->type == TOK_OTHER && t->len == 1) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07005864 switch (t->text.a[0]) {
H. Peter Anvin (Intel)f7dbdb22019-09-18 21:20:52 -07005865 case ',':
5866 if (comma && !brace)
5867 goto endparam;
5868 break;
5869
5870 case '{':
5871 brace++;
5872 break;
5873
5874 case '}':
5875 brace--;
5876 if (braced && !brace) {
5877 paramlen[i] += white;
5878 goto endparam;
5879 }
5880 break;
5881
5882 default:
5883 break;
5884 }
5885 }
5886
5887 paramlen[i] += white + 1;
5888 white = 0;
5889 }
5890 endparam:
5891 ;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005892 }
5893
5894 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005895 * OK, we have a MMacro structure together with a set of
5896 * parameters. We must now go through the expansion and push
5897 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005898 * parameter tokens and macro-local tokens doesn't get done
5899 * until the single-line macro substitution process; this is
5900 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005901 * later through %rotate and give the right semantics for
5902 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005903 *
5904 * First, push an end marker on to istk->expansion, mark this
5905 * macro as in progress, and set up its invocation-specific
5906 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005907 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005908 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005909 ll->next = istk->expansion;
5910 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005911 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005912
5913 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005914 * Save the previous MMacro expansion in the case of
5915 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005916 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005917#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005918 if (m->max_depth && m->in_progress)
5919 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005920#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005921
5922 m->in_progress ++;
5923 m->params = params;
5924 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005925 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005926 m->nparam = nparam;
5927 m->rotate = 0;
5928 m->paramlen = paramlen;
5929 m->unique = unique++;
5930 m->lineno = 0;
5931 m->condcnt = 0;
5932
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005933 m->mstk = istk->mstk;
5934 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005935
5936 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005937 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005938 ll->next = istk->expansion;
5939 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005940 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005941 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005942
5943 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005944 * If we had a label, and this macro definition does not include
5945 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005946 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005947 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005948 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005949 /*
5950 * We had a label. If this macro contains an %00 parameter,
5951 * save the value as a special parameter (which is what it
5952 * is), otherwise push it as the first line of the macro
5953 * expansion.
5954 */
5955 if (m->capture_label) {
5956 params[0] = dup_Token(NULL, label);
5957 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005958 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005959 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005960 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005961 ll->finishes = NULL;
5962 ll->next = istk->expansion;
5963 istk->expansion = ll;
5964 ll->first = startline;
5965 if (!dont_prepend) {
5966 while (label->next)
5967 label = label->next;
H. Peter Anvin8571f062019-09-23 16:40:03 -07005968 label->next = tt = make_tok_char(NULL, ':');
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005969 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005970 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005971 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005972
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005973 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005974
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005975 if (list_option('m') && !m->nolist)
5976 list_mmacro_call(m);
5977
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005978 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005979}
5980
H. Peter Anvin130736c2016-02-17 20:27:41 -08005981/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005982 * This function decides if an error message should be suppressed.
5983 * It will never be called with a severity level of ERR_FATAL or
5984 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005985 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005986static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005987{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005988 /*
5989 * If we're in a dead branch of IF or something like it, ignore the error.
5990 * However, because %else etc are evaluated in the state context
5991 * of the previous branch, errors might get lost:
5992 * %if 0 ... %else trailing garbage ... %endif
5993 * So %else etc should set the ERR_PP_PRECOND flag.
5994 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005995 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005996 ((severity & ERR_PP_PRECOND) ?
5997 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005998 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005999 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02006000
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006001 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00006002}
6003
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006004static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006005stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006006{
6007 (void)s;
6008 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006009 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006010
H. Peter Anvin8571f062019-09-23 16:40:03 -07006011 return make_tok_qstr(NULL, src_get_fname());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006012}
6013
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006014static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006015stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006016{
6017 (void)s;
6018 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006019 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006020
H. Peter Anvin8571f062019-09-23 16:40:03 -07006021 return make_tok_num(NULL, src_get_linnum());
H. Peter Anvin8b262472019-02-26 14:00:54 -08006022}
6023
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006024static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006025stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006026{
6027 (void)s;
6028 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006029 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006030
H. Peter Anvin8571f062019-09-23 16:40:03 -07006031 return make_tok_num(NULL, globalbits);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006032}
6033
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006034static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07006035stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006036{
H. Peter Anvin8b262472019-02-26 14:00:54 -08006037 (void)s;
6038 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006039 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006040
6041 switch (globalbits) {
6042 case 16:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006043 return new_Token(NULL, TOK_ID, "word", 4);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006044 case 32:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006045 return new_Token(NULL, TOK_ID, "dword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006046 case 64:
H. Peter Anvin8571f062019-09-23 16:40:03 -07006047 return new_Token(NULL, TOK_ID, "qword", 5);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006048 default:
6049 panic();
6050 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08006051}
6052
H. Peter Anvin8b262472019-02-26 14:00:54 -08006053/* Add magic standard macros */
6054struct magic_macros {
6055 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006056 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006057 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006058};
6059static const struct magic_macros magic_macros[] =
6060{
H. Peter Anvind2354082019-08-27 16:38:48 -07006061 { "__?FILE?__", 0, stdmac_file },
6062 { "__?LINE?__", 0, stdmac_line },
6063 { "__?BITS?__", 0, stdmac_bits },
6064 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08006065 { NULL, 0, NULL }
6066};
6067
6068static void pp_add_magic_stdmac(void)
6069{
6070 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006071 SMacro tmpl;
6072
6073 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006074
6075 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07006076 tmpl.nparam = m->nparam;
6077 tmpl.expand = m->func;
6078 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006079 }
6080}
6081
H. Peter Anvin734b1882002-04-30 21:01:08 +00006082static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006083pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006084{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006085 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006086 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07006087
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006088 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006089 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07006090 nested_mac_count = 0;
6091 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07006092 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006093 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07006094 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006095 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07006096 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07006097
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006098 if (!use_loaded)
6099 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
6100 memset(use_loaded, 0, use_package_count * sizeof(bool));
6101
H. Peter Anvin6686de22019-08-10 05:33:14 -07006102 /* First set up the top level input file */
6103 nasm_new(istk);
6104 istk->fp = nasm_open_read(file, NF_TEXT);
6105 src_set(0, file);
6106 istk->lineinc = 1;
6107 if (!istk->fp)
6108 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
6109
6110 strlist_add(deplist, file);
6111
6112 /*
6113 * Set up the stdmac packages as a virtual include file,
6114 * indicated by a null file pointer.
6115 */
6116 nasm_new(inc);
6117 inc->next = istk;
6118 inc->fname = src_set_fname(NULL);
6119 inc->nolist = !list_option('b');
6120 istk = inc;
6121 lfmt->uplevel(LIST_INCLUDE, 0);
6122
H. Peter Anvin8b262472019-02-26 14:00:54 -08006123 pp_add_magic_stdmac();
6124
H. Peter Anvinf7606612016-07-13 14:23:48 -07006125 if (tasm_compatible_mode)
6126 pp_add_stdmac(nasm_stdmac_tasm);
6127
6128 pp_add_stdmac(nasm_stdmac_nasm);
6129 pp_add_stdmac(nasm_stdmac_version);
6130
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006131 if (extrastdmac)
6132 pp_add_stdmac(extrastdmac);
6133
H. Peter Anvinf7606612016-07-13 14:23:48 -07006134 stdmacpos = stdmacros[0];
6135 stdmacnext = &stdmacros[1];
6136
H. Peter Anvind2456592008-06-19 15:04:18 -07006137 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006138
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006139 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07006140 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07006141 * other builtins, because it is special -- it varies between
6142 * passes -- but there is really no particular reason to make it
6143 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006144 *
6145 * 0 = dependencies only
6146 * 1 = preparatory passes
6147 * 2 = final pass
6148 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07006149 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006150 switch (mode) {
6151 case PP_NORMAL:
6152 apass = pass_final() ? 2 : 1;
6153 break;
6154 case PP_DEPS:
6155 apass = 0;
6156 break;
6157 case PP_PREPROC:
6158 apass = 3;
6159 break;
6160 default:
6161 panic();
6162 }
6163
H. Peter Anvin8571f062019-09-23 16:40:03 -07006164 define_smacro("__?PASS?__", true, make_tok_num(NULL, apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006165}
6166
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006167static void pp_init(void)
6168{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006169}
6170
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006171/*
6172 * Get a line of tokens. If we popped the macro expansion/include stack,
6173 * we return a pointer to the dummy token tok_pop; at that point if
6174 * istk is NULL then we have reached end of input;
6175 */
6176static Token tok_pop; /* Dummy token placeholder */
6177
6178static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006179{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006180 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006181 Line *l = istk->expansion;
6182 Token *tline = NULL;
6183 Token *dtline;
6184
H. Peter Anvine2c80182005-01-15 22:15:51 +00006185 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006186 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00006187 * buffer or from the input file.
6188 */
6189 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006190 while (l && l->finishes) {
6191 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006192
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006193 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006194 /*
6195 * This is a macro-end marker for a macro with no
6196 * name, which means it's not really a macro at all
6197 * but a %rep block, and the `in_progress' field is
6198 * more than 1, meaning that we still need to
6199 * repeat. (1 means the natural last repetition; 0
6200 * means termination by %exitrep.) We have
6201 * therefore expanded up to the %endrep, and must
6202 * push the whole block on to the expansion buffer
6203 * again. We don't bother to remove the macro-end
6204 * marker: we'd only have to generate another one
6205 * if we did.
6206 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006207 fm->in_progress--;
6208 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006209 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006210 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006211
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006212 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006213 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006214 tail = &ll->first;
6215
6216 list_for_each(t, l->first) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006217 if (t->len) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07006218 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006219 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006220 }
6221 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006222 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006223 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006224 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006225 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006226 MMacro *m = istk->mstk.mstk;
6227
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006228 /*
6229 * Check whether a `%rep' was started and not ended
6230 * within this macro expansion. This can happen and
6231 * should be detected. It's a fatal error because
6232 * I'm too confused to work out how to recover
6233 * sensibly from it.
6234 */
6235 if (defining) {
6236 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006237 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006238 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07006239 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006240 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006241 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006242
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006243 /*
6244 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006245 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006246 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006247 istk->mstk = m->mstk;
6248 if (m->name) {
6249 /*
6250 * This was a real macro call, not a %rep, and
6251 * therefore the parameter information needs to
6252 * be freed and the iteration count/nesting
6253 * depth adjusted.
6254 */
6255
6256 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006257 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006258 * If all mmacro processing done,
6259 * clear all counters and the deadman
6260 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006261 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006262 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02006263 }
6264
Adam Majer91e72402017-07-25 10:42:01 +02006265#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006266 if (m->prev) {
6267 pop_mmacro(m);
6268 fm->in_progress --;
6269 } else
Adam Majer91e72402017-07-25 10:42:01 +02006270#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006271 {
6272 nasm_free(m->params);
6273 free_tlist(m->iline);
6274 nasm_free(m->paramlen);
6275 fm->in_progress = 0;
6276 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006277 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006278
6279 /*
6280 * FIXME It is incorrect to always free_mmacro here.
6281 * It leads to usage-after-free.
6282 *
6283 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
6284 */
6285#if 0
6286 else
6287 free_mmacro(m);
6288#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006289 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006290 istk->expansion = l->next;
6291 nasm_free(l);
6292 lfmt->downlevel(LIST_MACRO);
6293 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006294 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006295
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006296 do { /* until we get a line we can use */
6297 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006298
6299 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006300 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006301 int32_t lineno;
6302
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006303 if (istk->mstk.mstk) {
6304 istk->mstk.mstk->lineno++;
6305 if (istk->mstk.mstk->fname)
6306 lineno = istk->mstk.mstk->lineno +
6307 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006308 else
6309 lineno = 0; /* Defined at init time or builtin */
6310 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006311 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07006312 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006313
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006314 tline = l->first;
6315 istk->expansion = l->next;
6316 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006317
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006318 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07006319 if (!istk->nolist)
6320 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006321 nasm_free(line);
6322 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006323 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006324 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006325 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006326 } else {
6327 /*
6328 * The current file has ended; work down the istk
6329 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00006330 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07006331 if (i->fp)
6332 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006333 if (i->conds) {
6334 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07006335 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006336 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00006337 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07006338 if (i->next)
6339 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006340 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08006341 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006342 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006343 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006344 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006345 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006346
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006347 /*
6348 * We must expand MMacro parameters and MMacro-local labels
6349 * _before_ we plunge into directive processing, to cope
6350 * with things like `%define something %1' such as STRUC
6351 * uses. Unless we're _defining_ a MMacro, in which case
6352 * those tokens should be left alone to go into the
6353 * definition; and unless we're in a non-emitting
6354 * condition, in which case we don't want to meddle with
6355 * anything.
6356 */
6357 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006358 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006359 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006360 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006361
H. Peter Anvine2c80182005-01-15 22:15:51 +00006362 /*
6363 * Check the line to see if it's a preprocessor directive.
6364 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006365 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6366 if (dtline)
6367 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006368 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006369 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006370 * We're defining a multi-line macro. We emit nothing
6371 * at all, and just
6372 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006373 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006374 MMacro *mmac = defining->dstk.mmac;
6375
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006376 Line *l = nasm_malloc(sizeof(Line));
6377 l->next = defining->expansion;
6378 l->first = tline;
6379 l->finishes = NULL;
6380 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006381
6382 /*
6383 * Remember if this mmacro expansion contains %00:
6384 * if it does, we will have to handle leading labels
6385 * specially.
6386 */
6387 if (mmac) {
6388 const Token *t;
6389 list_for_each(t, tline) {
H. Peter Anvin8571f062019-09-23 16:40:03 -07006390 if (!memcmp(t->text.a, "%00", 4))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006391 mmac->capture_label = true;
6392 }
6393 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006394 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006395 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006396 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006397 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006398 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006399 * directive so we keep our place correctly.
6400 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006401 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006402 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006403 /*
6404 * We're in a %rep block which has been terminated, so
6405 * we're walking through to the %endrep without
6406 * emitting anything. Emit nothing at all, not even a
6407 * blank line: when we emerge from the %rep block we'll
6408 * give a line-number directive so we keep our place
6409 * correctly.
6410 */
6411 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006412 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006413 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006414 if (!expand_mmacro(tline))
6415 return tline;
6416 }
6417 }
6418}
6419
6420static char *pp_getline(void)
6421{
6422 char *line = NULL;
6423 Token *tline;
6424
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006425 while (true) {
6426 tline = pp_tokline();
6427 if (tline == &tok_pop) {
6428 /*
6429 * We popped the macro/include stack. If istk is empty,
6430 * we are at end of input, otherwise just loop back.
6431 */
6432 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006433 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006434 } else {
6435 /*
6436 * De-tokenize the line and emit it.
6437 */
6438 line = detoken(tline, true);
6439 free_tlist(tline);
6440 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006441 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006442 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006443
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006444 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006445 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006446 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006447 nasm_free(buf);
6448 }
6449
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006450 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006451}
6452
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006453static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006454{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006455 if (defining) {
6456 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006457 nasm_nonfatal("end of file while still defining macro `%s'",
6458 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006459 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006460 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006461 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006462
6463 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006464 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006465 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006466
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006467 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006468 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006469 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006470 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006471 Include *i = istk;
6472 istk = istk->next;
6473 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006474 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006475 }
6476 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006477 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006478 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006479}
6480
6481static void pp_cleanup_session(void)
6482{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006483 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006484 free_llist(predef);
6485 predef = NULL;
6486 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006487 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006488}
6489
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006490static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006491{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006492 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006493}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006494
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006495static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006496{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006497 Token *inc, *space, *name;
6498 Line *l;
6499
H. Peter Anvin734b1882002-04-30 21:01:08 +00006500 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006501 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006502 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006503
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006504 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006505 l->next = predef;
6506 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006507 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006508 predef = l;
6509}
6510
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006511static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006512{
6513 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006514 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006515 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006516
6517 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006518 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006519 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006520 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006521 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006522 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006523 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006524 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006525
H. Peter Anvin8571f062019-09-23 16:40:03 -07006526 /* We can't predefine a TOK_LOCAL_MACRO for obvious reasons... */
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006527 if (space->next->type != TOK_PREPROC_ID &&
6528 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006529 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006530
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006531 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006532 l->next = predef;
6533 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006534 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006535 predef = l;
6536}
6537
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006538static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006539{
6540 Token *def, *space;
6541 Line *l;
6542
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006543 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006544 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006545 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006546
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006547 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006548 l->next = predef;
6549 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006550 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006551 predef = l;
6552}
6553
H. Peter Anvin05990342018-06-11 13:32:42 -07006554/* Insert an early preprocessor command that doesn't need special handling */
6555static void pp_pre_command(const char *what, char *string)
6556{
6557 char *cmd;
6558 Token *def, *space;
6559 Line *l;
6560
6561 def = tokenize(string);
6562 if (what) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006563 space = new_White(def);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006564 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6565 def = new_Token(space, TOK_PREPROC_ID, cmd, nasm_last_string_len());
6566 nasm_free(cmd);
H. Peter Anvin05990342018-06-11 13:32:42 -07006567 }
6568
6569 l = nasm_malloc(sizeof(Line));
6570 l->next = predef;
6571 l->first = def;
6572 l->finishes = NULL;
6573 predef = l;
6574}
6575
H. Peter Anvinf7606612016-07-13 14:23:48 -07006576static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006577{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006578 macros_t **mp;
6579
6580 /* Find the end of the list and avoid duplicates */
6581 for (mp = stdmacros; *mp; mp++) {
6582 if (*mp == macros)
6583 return; /* Nothing to do */
6584 }
6585
6586 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6587
6588 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006589}
6590
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006591static void pp_extra_stdmac(macros_t *macros)
6592{
6593 extrastdmac = macros;
6594}
6595
H. Peter Anvin8571f062019-09-23 16:40:03 -07006596/* Create a numeric token */
6597static Token *make_tok_num(Token *next, int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006598{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006599 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006600 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
H. Peter Anvin8571f062019-09-23 16:40:03 -07006601 return new_Token(next, TOK_NUMBER, numbuf, len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006602}
6603
H. Peter Anvin8571f062019-09-23 16:40:03 -07006604/* Create a quoted string token */
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006605static Token *make_tok_qstr_len(Token *next, const char *str, size_t len)
H. Peter Anvin8b262472019-02-26 14:00:54 -08006606{
H. Peter Anvin8571f062019-09-23 16:40:03 -07006607 char *p = nasm_quote(str, &len);
6608 return new_Token_free(next, TOK_STRING, p, len);
6609}
H. Peter Anvin (Intel)18f41342019-10-16 15:02:44 -07006610static Token *make_tok_qstr(Token *next, const char *str)
6611{
6612 return make_tok_qstr_len(next, str, strlen(str));
6613}
H. Peter Anvin8571f062019-09-23 16:40:03 -07006614
6615/* Create a single-character operator token */
6616static Token *make_tok_char(Token *next, char op)
6617{
6618 Token *t = new_Token(next, TOK_OTHER, NULL, 1);
6619 t->text.a[0] = op;
H. Peter Anvin8b262472019-02-26 14:00:54 -08006620 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006621}
6622
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006623static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006624{
6625 if (!m)
6626 return;
6627
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006628 /* We need to print the mstk.mmac list in reverse order */
6629 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006630
6631 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006632 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006633 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006634 }
6635}
6636
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006637static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006638{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006639 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006640
H. Peter Anvinddb29062018-12-11 00:06:29 -08006641 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6642 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006643
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006644 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006645 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006646
H. Peter Anvinddb29062018-12-11 00:06:29 -08006647 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006648}
6649
H. Peter Anvine7469712016-02-18 02:20:59 -08006650const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006651 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006652 pp_reset,
6653 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006654 pp_cleanup_pass,
6655 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006656 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006657 pp_pre_define,
6658 pp_pre_undefine,
6659 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006660 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006661 pp_include_path,
6662 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006663 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006664};