blob: 6c623327be9260cb0a3c65e24a4a90fad3ac2501 [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;
H. Peter Anvince616072002-04-30 21:02:23 +000084typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000085typedef struct Line Line;
86typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080087typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088
89/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070090 * Note on the storage of both SMacro and MMacros: the hash table
91 * indexes them case-insensitively, and we then have to go through a
92 * linked list of potential case aliases (and, for MMacros, parameter
93 * ranges); this is to preserve the matching semantics of the earlier
94 * code. If the number of case aliases for a specific macro is a
95 * performance issue, you may want to reconsider your coding style.
96 */
97
98/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070099 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700100 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700101typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700102
103/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104 * Store the definition of a single-line macro.
105 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700106enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700107 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700108 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
109 SPARM_STR = 2, /* Convert to quoted string ($) */
110 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
111 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
112};
113
114struct smac_param {
115 char *name;
116 int namelen;
117 enum sparmflags flags;
118};
119
H. Peter Anvine2c80182005-01-15 22:15:51 +0000120struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800121 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800122 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700123 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700124 ExpandSMacro expand;
125 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700126 struct smac_param *params;
127 int nparam;
128 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800129 bool casesense;
130 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700131 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132};
133
134/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800135 * Store the definition of a multi-line macro. This is also used to
136 * store the interiors of `%rep...%endrep' blocks, which are
137 * effectively self-re-invoking multi-line macros which simply
138 * don't have a name or bother to appear in the hash tables. %rep
139 * blocks are signified by having a NULL `name' field.
140 *
141 * In a MMacro describing a `%rep' block, the `in_progress' field
142 * isn't merely boolean, but gives the number of repeats left to
143 * run.
144 *
145 * The `next' field is used for storing MMacros in hash tables; the
146 * `next_active' field is for stacking them on istk entries.
147 *
148 * When a MMacro is being expanded, `params', `iline', `nparam',
149 * `paramlen', `rotate' and `unique' are local to the invocation.
150 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700151
152/*
153 * Expansion stack. Note that .mmac can point back to the macro itself,
154 * whereas .mstk cannot.
155 */
156struct mstk {
157 MMacro *mstk; /* Any expansion, real macro or not */
158 MMacro *mmac; /* Highest level actual mmacro */
159};
160
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800161struct MMacro {
162 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700163#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800164 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700165#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800166 char *name;
167 int nparam_min, nparam_max;
168 bool casesense;
169 bool plus; /* is the last parameter greedy? */
170 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700171 bool capture_label; /* macro definition has %00; capture label */
172 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800173 int32_t max_depth; /* maximum number of recursive expansions allowed */
174 Token *dlist; /* All defaults as one list */
175 Token **defaults; /* Parameter default pointers */
176 int ndefs; /* number of default parameters */
177 Line *expansion;
178
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700179 struct mstk mstk; /* Macro expansion stack */
180 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800181 Token **params; /* actual parameters */
182 Token *iline; /* invocation line */
183 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700184 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800185 int *paramlen;
186 uint64_t unique;
187 int lineno; /* Current line number on expansion */
188 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700189
H. Peter Anvin274cda82016-05-10 02:56:29 -0700190 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700191 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800192};
193
194
195/* Store the definition of a multi-line macro, as defined in a
196 * previous recursive macro expansion.
197 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700198#if 0
199
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800200struct MMacroInvocation {
201 MMacroInvocation *prev; /* previous invocation */
202 Token **params; /* actual parameters */
203 Token *iline; /* invocation line */
204 unsigned int nparam, rotate;
205 int *paramlen;
206 uint64_t unique;
207 uint64_t condcnt;
208};
209
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700210#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211
212/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000213 * The context stack is composed of a linked list of these.
214 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800216 Context *next;
217 char *name;
218 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700219 uint64_t number;
220 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000221};
222
223/*
224 * This is the internal form which we break input lines up into.
225 * Typically stored in linked lists.
226 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800227 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
228 * not necessarily used as-is, but is also used to encode the number
229 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800231 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700232 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000233 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800234 * tok_smac_param(0) but the one representing `y' will be
235 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000236 *
237 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
238 * which doesn't need quotes around it. Used in the pre-include
239 * mechanism as an alternative to trying to find a sensible type of
240 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000241 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000242enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
244 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800245 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700246 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800247 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300248 TOK_PASTE, /* %+ */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700249 TOK_COND_COMMA, /* %, */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300250 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800251 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300252 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000253};
254
H. Peter Anvin8b262472019-02-26 14:00:54 -0800255static inline enum pp_token_type tok_smac_param(int param)
256{
257 return TOK_SMAC_START_PARAMS + param;
258}
259static int smac_nparam(enum pp_token_type toktype)
260{
261 return toktype - TOK_SMAC_START_PARAMS;
262}
263static bool is_smac_param(enum pp_token_type toktype)
264{
265 return toktype >= TOK_SMAC_START_PARAMS;
266}
267
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400268#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400269#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400270
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400271struct tokseq_match {
272 int mask_head;
273 int mask_tail;
274};
275
H. Peter Anvine2c80182005-01-15 22:15:51 +0000276struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800277 Token *next;
278 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700279 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800280 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000281};
282
283/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800284 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 * these, which is essentially a container to allow several linked
286 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700287 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000288 * Note that in this module, linked lists are treated as stacks
289 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800290 * `expansion' field in MMacro structures, so that the linked list,
291 * if walked, would give the macro lines in reverse order; this
292 * means that we can walk the list when expanding a macro, and thus
293 * push the lines on to the `expansion' field in _istk_ in reverse
294 * order (so that when popped back off they are in the right
295 * order). It may seem cockeyed, and it relies on my design having
296 * an even number of steps in, but it works...
297 *
298 * Some of these structures, rather than being actual lines, are
299 * markers delimiting the end of the expansion of a given macro.
300 * This is for use in the cycle-tracking and %rep-handling code.
301 * Such structures have `finishes' non-NULL, and `first' NULL. All
302 * others have `finishes' NULL, but `first' may still be NULL if
303 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000304 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000305struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800306 Line *next;
307 MMacro *finishes;
308 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500309};
310
311/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 * To handle an arbitrary level of file inclusion, we maintain a
313 * stack (ie linked list) of these things.
314 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000315struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800316 Include *next;
317 FILE *fp;
318 Cond *conds;
319 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700320 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700321 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700322 int lineno, lineinc;
323 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324};
325
326/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700327 * File real name hash, so we don't have to re-search the include
328 * path for every pass (and potentially more than that if a file
329 * is used more than once.)
330 */
331struct hash_table FileHash;
332
333/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700334 * Counters to trap on insane macro recursion or processing.
335 * Note: for smacros these count *down*, for mmacros they count *up*.
336 */
337struct deadman {
338 int64_t total; /* Total number of macros/tokens */
339 int64_t levels; /* Descent depth across all macros */
340 bool triggered; /* Already triggered, no need for error msg */
341};
342
343static struct deadman smacro_deadman, mmacro_deadman;
344
345/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 * Conditional assembly: we maintain a separate stack of these for
347 * each level of file inclusion. (The only reason we keep the
348 * stacks separate is to ensure that a stray `%endif' in a file
349 * included from within the true branch of a `%if' won't terminate
350 * it and cause confusion: instead, rightly, it'll cause an error.)
351 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700352enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353 /*
354 * These states are for use just after %if or %elif: IF_TRUE
355 * means the condition has evaluated to truth so we are
356 * currently emitting, whereas IF_FALSE means we are not
357 * currently emitting but will start doing so if a %else comes
358 * up. In these states, all directives are admissible: %elif,
359 * %else and %endif. (And of course %if.)
360 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800361 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362 /*
363 * These states come up after a %else: ELSE_TRUE means we're
364 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
365 * any %elif or %else will cause an error.
366 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800367 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200369 * These states mean that we're not emitting now, and also that
370 * nothing until %endif will be emitted at all. COND_DONE is
371 * used when we've had our moment of emission
372 * and have now started seeing %elifs. COND_NEVER is used when
373 * the condition construct in question is contained within a
374 * non-emitting branch of a larger condition construct,
375 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000376 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800377 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000378};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700379struct Cond {
380 Cond *next;
381 enum cond_state state;
382};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800383#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000384
H. Peter Anvin70653092007-10-19 14:42:29 -0700385/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000386 * These defines are used as the possible return values for do_directive
387 */
388#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000390
Keith Kanios852f1ee2009-07-12 00:19:55 -0500391/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392 * Condition codes. Note that we use c_ prefix not C_ because C_ is
393 * used in nasm.h for the "real" condition codes. At _this_ level,
394 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
395 * ones, so we need a different enum...
396 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700397static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
399 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000400 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700402enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
404 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 -0700405 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
406 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700408static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
410 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 +0000411 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412};
413
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800414/*
415 * Directive names.
416 */
417/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
418static int is_condition(enum preproc_token arg)
419{
420 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
421}
422
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000423/* For TASM compatibility we need to be able to recognise TASM compatible
424 * conditional compilation directives. Using the NASM pre-processor does
425 * not work, so we look for them specifically from the following list and
426 * then jam in the equivalent NASM directive into the input stream.
427 */
428
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000430 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
431 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
432};
433
H. Peter Anvin476d2862007-10-02 22:04:15 -0700434static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000435 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
436 "ifndef", "include", "local"
437};
438
439static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700440static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000441static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800442static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000443
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000444static Context *cstk;
445static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300446static const struct strlist *ipath_list;
H. Peter Anvind2354082019-08-27 16:38:48 -0700447static bool do_aliases;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000448
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300449static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000450
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300451static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000452
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800453static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700454static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800455static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000456
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000457/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800458 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000459 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800460static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000461
462/*
463 * The current set of single-line macros we have defined.
464 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700465static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000466
467/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800468 * The multi-line macro we are currently defining, or the %rep
469 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000470 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800471static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000472
Charles Crayned4200be2008-07-12 16:42:33 -0700473static uint64_t nested_mac_count;
474static uint64_t nested_rep_count;
475
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000476/*
477 * The number of macro parameters to allocate space for at a time.
478 */
479#define PARAM_DELTA 16
480
481/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700482 * The standard macro set: defined in macros.c in a set of arrays.
483 * This gives our position in any macro set, while we are processing it.
484 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000485 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700486static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700487static macros_t **stdmacnext;
488static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300489static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000490
491/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700492 * Map of which %use packages have been loaded
493 */
494static bool *use_loaded;
495
496/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000497 * Tokens are allocated in blocks to improve speed
498 */
499#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000501struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 Blocks *next;
503 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000504};
505
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800506static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000507
508/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000509 * Forward declarations.
510 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700511static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000512static Token *expand_mmac_params(Token * tline);
513static Token *expand_smacro(Token * tline);
514static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400515static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800516static Token *make_tok_num(int64_t val);
517static Token *make_tok_qstr(const char *str);
H. Peter Anvince616072002-04-30 21:02:23 +0000518static void *new_Block(size_t size);
519static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700520static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700521 const char *text, size_t txtlen);
522static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000523static Token *delete_Token(Token * t);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700524static const struct use_package *
525get_use_pkg(Token *t, const char *dname, bool *err);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000526
527/*
528 * Macros for safe checking of token pointers, avoid *(NULL)
529 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300530#define tok_type_(x,t) ((x) && (x)->type == (t))
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -0700531#define skip_white_(x) ((x) = (tok_type_((x), TOK_WHITESPACE) ? (x)->next : (x)))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300532#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
533#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000534
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400535/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700536 * In-place reverse a list of tokens.
537 */
538static Token *reverse_tokens(Token *t)
539{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800540 Token *prev = NULL;
541 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700542
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800543 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400544 next = t->next;
545 t->next = prev;
546 prev = t;
547 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800548 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700549
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800550 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700551}
552
553/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300554 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000555 * front of them. We do it here because I could not find any other
556 * place to do it for the moment, and it is a hack (ideally it would
557 * be nice to be able to use the NASM pre-processor to do it).
558 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000559static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000560{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000561 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400562 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000563
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400564 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000565
566 /* Binary search for the directive name */
567 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400568 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400569 q = nasm_skip_word(p);
570 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 if (len) {
572 oldchar = p[len];
573 p[len] = 0;
574 while (j - i > 1) {
575 k = (j + i) / 2;
576 m = nasm_stricmp(p, tasm_directives[k]);
577 if (m == 0) {
578 /* We have found a directive, so jam a % in front of it
579 * so that NASM will then recognise it as one if it's own.
580 */
581 p[len] = oldchar;
582 len = strlen(p);
583 oldline = line;
584 line = nasm_malloc(len + 2);
585 line[0] = '%';
586 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700587 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300588 * NASM does not recognise IFDIFI, so we convert
589 * it to %if 0. This is not used in NASM
590 * compatible code, but does need to parse for the
591 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700593 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 } else {
595 memcpy(line + 1, p, len + 1);
596 }
597 nasm_free(oldline);
598 return line;
599 } else if (m < 0) {
600 j = k;
601 } else
602 i = k;
603 }
604 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000605 }
606 return line;
607}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000608
H. Peter Anvin76690a12002-04-30 20:52:49 +0000609/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000610 * The pre-preprocessing stage... This function translates line
611 * number indications as they emerge from GNU cpp (`# lineno "file"
612 * flags') into NASM preprocessor line number indications (`%line
613 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000614 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000615static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000616{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000617 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000618 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000619
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 if (line[0] == '#' && line[1] == ' ') {
621 oldline = line;
622 fname = oldline + 2;
623 lineno = atoi(fname);
624 fname += strspn(fname, "0123456789 ");
625 if (*fname == '"')
626 fname++;
627 fnlen = strcspn(fname, "\"");
628 line = nasm_malloc(20 + fnlen);
629 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
630 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000631 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000632 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000634 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000635}
636
637/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000638 * Free a linked list of tokens.
639 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000641{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400642 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000644}
645
646/*
647 * Free a linked list of lines.
648 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000650{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400651 Line *l, *tmp;
652 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 free_tlist(l->first);
654 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000655 }
656}
657
658/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700659 * Free an array of linked lists of tokens
660 */
661static void free_tlist_array(Token **array, size_t nlists)
662{
663 Token **listp = array;
664
665 while (nlists--)
666 free_tlist(*listp++);
667
668 nasm_free(array);
669}
670
671/*
672 * Duplicate a linked list of tokens.
673 */
674static Token *dup_tlist(const Token *list, Token ***tailp)
675{
676 Token *newlist = NULL;
677 Token **tailpp = &newlist;
678 const Token *t;
679
680 list_for_each(t, list) {
681 Token *nt;
682 *tailpp = nt = dup_Token(NULL, t);
683 tailpp = &nt->next;
684 }
685
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700686 if (tailp) {
687 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700688 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700689 }
690
691 return newlist;
692}
693
694/*
695 * Duplicate a linked list of tokens with a maximum count
696 */
697static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
698{
699 Token *newlist = NULL;
700 Token **tailpp = &newlist;
701 const Token *t;
702
703 list_for_each(t, list) {
704 Token *nt;
705 if (!cnt--)
706 break;
707 *tailpp = nt = dup_Token(NULL, t);
708 tailpp = &nt->next;
709 }
710
711 if (tailp) {
712 **tailp = newlist;
713 *tailp = tailpp;
714 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700715
716 return newlist;
717}
718
719/*
720 * Duplicate a linked list of tokens in reverse order
721 */
722static Token *dup_tlist_reverse(const Token *list, Token *tail)
723{
724 const Token *t;
725
726 list_for_each(t, list)
727 tail = dup_Token(tail, t);
728
729 return tail;
730}
731
732/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800733 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000734 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800735static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000736{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800737 nasm_free(m->name);
738 free_tlist(m->dlist);
739 nasm_free(m->defaults);
740 free_llist(m->expansion);
741 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000742}
743
744/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700745 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800746 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700747static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800748{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700749 if (s->params) {
750 int i;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700751 for (i = 0; i < s->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700752 nasm_free(s->params[i].name);
753 nasm_free(s->params);
754 }
H. Peter Anvin8b262472019-02-26 14:00:54 -0800755 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700756 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700757}
758
759static void clear_smacro(SMacro *s)
760{
761 free_smacro_members(s);
762 /* Wipe everything except the next pointer */
763 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
764}
765
766/*
767 * Free an SMacro
768 */
769static void free_smacro(SMacro *s)
770{
771 free_smacro_members(s);
772 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800773}
774
775/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700776 * Free all currently defined macros, and free the hash tables
777 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700778static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700779{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800780 struct hash_iterator it;
781 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700782
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800783 hash_for_each(smt, it, np) {
784 SMacro *tmp;
785 SMacro *s = np->data;
786 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800787 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700788 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700789 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700790 hash_free(smt);
791}
792
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800793static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700794{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800795 struct hash_iterator it;
796 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700797
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800798 hash_for_each(mmt, it, np) {
799 MMacro *tmp;
800 MMacro *m = np->data;
801 nasm_free((void *)np->key);
802 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800803 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700804 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800805 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700806}
807
808static void free_macros(void)
809{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700810 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800811 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700812}
813
814/*
815 * Initialize the hash tables
816 */
817static void init_macros(void)
818{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700819}
820
821/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000822 * Pop the context stack.
823 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000825{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000826 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000827
828 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700829 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000830 nasm_free(c->name);
831 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000832}
833
H. Peter Anvin072771e2008-05-22 13:17:51 -0700834/*
835 * Search for a key in the hash index; adding it if necessary
836 * (in which case we initialize the data pointer to NULL.)
837 */
838static void **
839hash_findi_add(struct hash_table *hash, const char *str)
840{
841 struct hash_insert hi;
842 void **r;
843 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800844 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700845
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800846 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700847 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300848 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700849
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800850 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
851 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700852 return hash_add(&hi, strx, NULL);
853}
854
855/*
856 * Like hash_findi, but returns the data element rather than a pointer
857 * to it. Used only when not adding a new element, hence no third
858 * argument.
859 */
860static void *
861hash_findix(struct hash_table *hash, const char *str)
862{
863 void **p;
864
865 p = hash_findi(hash, str, NULL);
866 return p ? *p : NULL;
867}
868
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400869/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800870 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400871 * if there no more left -- return NULL
872 */
873static char *line_from_stdmac(void)
874{
875 unsigned char c;
876 const unsigned char *p = stdmacpos;
877 char *line, *q;
878 size_t len = 0;
879
880 if (!stdmacpos)
881 return NULL;
882
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700883 /*
884 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
885 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
886 */
887 while ((c = *p++) != 127) {
888 uint8_t ndir = c - 128;
889 if (ndir < 256-96)
890 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400891 else
892 len++;
893 }
894
895 line = nasm_malloc(len + 1);
896 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700897
898 while ((c = *stdmacpos++) != 127) {
899 uint8_t ndir = c - 128;
900 if (ndir < 256-96) {
901 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
902 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400903 *q++ = ' ';
904 } else {
905 *q++ = c;
906 }
907 }
908 stdmacpos = p;
909 *q = '\0';
910
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700911 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700912 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400913 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700914 if (*stdmacnext) {
915 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400916 } else if (do_predef) {
917 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400918
919 /*
920 * Nasty hack: here we push the contents of
921 * `predef' on to the top-level expansion stack,
922 * since this is the most convenient way to
923 * implement the pre-include and pre-define
924 * features.
925 */
926 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700927 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800928 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700929 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800930 l->finishes = NULL;
931
932 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400933 }
934 do_predef = false;
935 }
936 }
937
938 return line;
939}
940
H. Peter Anvin6686de22019-08-10 05:33:14 -0700941/*
942 * Read a line from a file. Return NULL on end of file.
943 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700944static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000945{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700946 int c;
947 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400948 const unsigned int delta = 512;
949 const unsigned int pad = 8;
950 unsigned int nr_cont = 0;
951 bool cont = false;
952 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700953 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000954
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400955 size = delta;
956 p = buffer = nasm_malloc(size);
957
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700958 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700959 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400960
961 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700962 case EOF:
963 if (p == buffer) {
964 nasm_free(buffer);
965 return NULL;
966 }
967 c = 0;
968 break;
969
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400970 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700971 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400972 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700973 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400974 if (cont) {
975 cont = false;
976 continue;
977 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700978 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400979 break;
980
981 case '\n':
982 if (cont) {
983 cont = false;
984 continue;
985 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700986 c = 0;
987 break;
988
989 case 032: /* ^Z = legacy MS-DOS end of file mark */
990 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400991 break;
992
993 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700994 next = fgetc(f);
995 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400996 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400997 cont = true;
998 nr_cont++;
999 continue;
1000 }
1001 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001003
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001004 if (p >= (buffer + size - pad)) {
1005 buffer = nasm_realloc(buffer, size + delta);
1006 p = buffer + size - pad;
1007 size += delta;
1008 }
1009
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001010 *p++ = c;
1011 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001012
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001013 lineno = src_get_linnum() + istk->lineinc +
1014 (nr_cont * istk->lineinc);
1015 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001016
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001017 return buffer;
1018}
1019
1020/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001021 * Common read routine regardless of source
1022 */
1023static char *read_line(void)
1024{
1025 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001026 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001027
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001028 if (f)
1029 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001030 else
1031 line = line_from_stdmac();
1032
1033 if (!line)
1034 return NULL;
1035
1036 if (!istk->nolist)
1037 lfmt->line(LIST_READ, src_get_linnum(), line);
1038
1039 return line;
1040}
1041
1042/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001043 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001044 * don't need to parse the value out of e.g. numeric tokens: we
1045 * simply split one string into many.
1046 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001047static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001048{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001049 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001050 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001051 Token *list = NULL;
1052 Token *t, **tail = &list;
1053
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001055 char *p = line;
1056 char *ep = NULL; /* End of token, for trimming the end */
1057
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 if (*p == '%') {
1059 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001060 if (*p == '+' && !nasm_isdigit(p[1])) {
1061 p++;
1062 type = TOK_PASTE;
1063 } else if (nasm_isdigit(*p) ||
1064 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 do {
1066 p++;
1067 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001068 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 type = TOK_PREPROC_ID;
1070 } else if (*p == '{') {
1071 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001072 while (*p) {
1073 if (*p == '}')
1074 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 p[-1] = *p;
1076 p++;
1077 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001078 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001079 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001080 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 if (*p)
1082 p++;
1083 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001084 } else if (*p == '[') {
1085 int lvl = 1;
1086 line += 2; /* Skip the leading %[ */
1087 p++;
1088 while (lvl && (c = *p++)) {
1089 switch (c) {
1090 case ']':
1091 lvl--;
1092 break;
1093 case '%':
1094 if (*p == '[')
1095 lvl++;
1096 break;
1097 case '\'':
1098 case '\"':
1099 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001100 p = nasm_skip_string(p - 1);
1101 if (*p)
1102 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001103 break;
1104 default:
1105 break;
1106 }
1107 }
1108 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001109 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001110 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001111 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001112 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001113 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001114 type = TOK_INDIRECT;
1115 } else if (*p == '?') {
1116 type = TOK_PREPROC_Q; /* %? */
1117 p++;
1118 if (*p == '?') {
1119 type = TOK_PREPROC_QQ; /* %?? */
1120 p++;
1121 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001122 } else if (*p == '!') {
1123 type = TOK_PREPROC_ID;
1124 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001125 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001126 do {
1127 p++;
1128 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001129 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001130 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001131 p = nasm_skip_string(p);
1132 if (*p)
1133 p++;
1134 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001135 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001136 } else {
1137 /* %! without string or identifier */
1138 type = TOK_OTHER; /* Legacy behavior... */
1139 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001140 } else if (*p == ',') {
1141 p++;
1142 type = TOK_COND_COMMA;
H. Peter Anvin13506202018-11-28 14:55:58 -08001143 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001145 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 do {
1147 p++;
1148 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001149 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 type = TOK_PREPROC_ID;
1151 } else {
1152 type = TOK_OTHER;
1153 if (*p == '%')
1154 p++;
1155 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001156 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 type = TOK_ID;
1158 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001159 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001161 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 /*
1163 * A string token.
1164 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001166 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001167
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 if (*p) {
1169 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001170 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001171 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 /* Handling unterminated strings by UNV */
1173 /* type = -1; */
1174 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001175 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001176 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001177 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001178 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001179 bool is_hex = false;
1180 bool is_float = false;
1181 bool has_e = false;
1182 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001183
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001185 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001187
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001188 if (*p == '$') {
1189 p++;
1190 is_hex = true;
1191 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001192
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001193 for (;;) {
1194 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001195
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001196 if (!is_hex && (c == 'e' || c == 'E')) {
1197 has_e = true;
1198 if (*p == '+' || *p == '-') {
1199 /*
1200 * e can only be followed by +/- if it is either a
1201 * prefixed hex number or a floating-point number
1202 */
1203 p++;
1204 is_float = true;
1205 }
1206 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1207 is_hex = true;
1208 } else if (c == 'P' || c == 'p') {
1209 is_float = true;
1210 if (*p == '+' || *p == '-')
1211 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001212 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001213 ; /* just advance */
1214 else if (c == '.') {
1215 /*
1216 * we need to deal with consequences of the legacy
1217 * parser, like "1.nolist" being two tokens
1218 * (TOK_NUMBER, TOK_ID) here; at least give it
1219 * a shot for now. In the future, we probably need
1220 * a flex-based scanner with proper pattern matching
1221 * to do it as well as it can be done. Nothing in
1222 * the world is going to help the person who wants
1223 * 0x123.p16 interpreted as two tokens, though.
1224 */
1225 r = p;
1226 while (*r == '_')
1227 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001228
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001229 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1230 (!is_hex && (*r == 'e' || *r == 'E')) ||
1231 (*r == 'p' || *r == 'P')) {
1232 p = r;
1233 is_float = true;
1234 } else
1235 break; /* Terminate the token */
1236 } else
1237 break;
1238 }
1239 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001240
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001241 if (p == line+1 && *line == '$') {
1242 type = TOK_OTHER; /* TOKEN_HERE */
1243 } else {
1244 if (has_e && !is_hex) {
1245 /* 1e13 is floating-point, but 1e13h is not */
1246 is_float = true;
1247 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001248
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001249 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1250 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001251 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001253 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 /*
1255 * Whitespace just before end-of-line is discarded by
1256 * pretending it's a comment; whitespace just before a
1257 * comment gets lumped into the comment.
1258 */
1259 if (!*p || *p == ';') {
1260 type = TOK_COMMENT;
1261 while (*p)
1262 p++;
1263 }
1264 } else if (*p == ';') {
1265 type = TOK_COMMENT;
1266 while (*p)
1267 p++;
1268 } else {
1269 /*
1270 * Anything else is an operator of some kind. We check
1271 * for all the double-character operators (>>, <<, //,
1272 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001273 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 */
1275 type = TOK_OTHER;
1276 if ((p[0] == '>' && p[1] == '>') ||
1277 (p[0] == '<' && p[1] == '<') ||
1278 (p[0] == '/' && p[1] == '/') ||
1279 (p[0] == '<' && p[1] == '=') ||
1280 (p[0] == '>' && p[1] == '=') ||
1281 (p[0] == '=' && p[1] == '=') ||
1282 (p[0] == '!' && p[1] == '=') ||
1283 (p[0] == '<' && p[1] == '>') ||
1284 (p[0] == '&' && p[1] == '&') ||
1285 (p[0] == '|' && p[1] == '|') ||
1286 (p[0] == '^' && p[1] == '^')) {
1287 p++;
1288 }
1289 p++;
1290 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001291
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 /* Handling unterminated string by UNV */
1293 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001294 {
1295 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1296 t->text[p-line] = *line;
1297 tail = &t->next;
1298 }
1299 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001301 if (!ep)
1302 ep = p;
1303 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 tail = &t->next;
1305 }
1306 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001307 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308 return list;
1309}
1310
H. Peter Anvince616072002-04-30 21:02:23 +00001311/*
1312 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001313 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001314 * deleted only all at once by the delete_Blocks function.
1315 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001317{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001318 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001320 /* first, get to the end of the linked list */
1321 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001323 /* now allocate the requested chunk */
1324 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001326 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001327 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001328 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001329}
1330
1331/*
1332 * this function deletes all managed blocks of memory
1333 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001335{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001337
H. Peter Anvin70653092007-10-19 14:42:29 -07001338 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001339 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001340 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001341 * free it.
1342 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001344 if (b->chunk)
1345 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 a = b;
1347 b = b->next;
1348 if (a != &blocks)
1349 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001350 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001351 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001353
1354/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001355 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001356 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001357 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001358static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001359 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001360{
1361 Token *t;
1362 int i;
1363
H. Peter Anvin89cee572009-07-15 09:16:54 -04001364 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1366 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1367 freeTokens[i].next = &freeTokens[i + 1];
1368 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001369 }
1370 t = freeTokens;
1371 freeTokens = t->next;
1372 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001373 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001374 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001375 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 t->text = NULL;
1377 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001378 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001380 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001381 t->text = nasm_malloc(txtlen+1);
1382 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001384 }
1385 return t;
1386}
1387
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001388static Token *dup_Token(Token *next, const Token *src)
1389{
1390 return new_Token(next, src->type, src->text, src->len);
1391}
1392
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001394{
1395 Token *next = t->next;
1396 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001397 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001398 freeTokens = t;
1399 return next;
1400}
1401
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001402/*
1403 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001404 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1405 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001406 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001407static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001408{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001409 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001410 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001411 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001412
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001413 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001414 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001415 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001416 char *v;
1417 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001418
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001419 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001420 if (nasm_isquote(*v))
1421 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001422
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001423 if (v) {
1424 char *p = getenv(v);
1425 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001426 /*!
1427 *!environment [on] nonexistent environment variable
1428 *! warns if a nonexistent environment variable
1429 *! is accessed using the \c{%!} preprocessor
1430 *! construct (see \k{getenv}.) Such environment
1431 *! variables are treated as empty (with this
1432 *! warning issued) starting in NASM 2.15;
1433 *! earlier versions of NASM would treat this as
1434 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001435 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001436 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1437 p = "";
1438 }
1439 t->text = nasm_strdup(p);
1440 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001441 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001442 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001444
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 /* Expand local macros here and not during preprocessing */
1446 if (expand_locals &&
1447 t->type == TOK_PREPROC_ID && t->text &&
1448 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001449 const char *q;
1450 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001451 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001453 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1454 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 nasm_free(t->text);
1456 t->text = p;
1457 }
1458 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001459 if (t->text) {
1460 if (debug_level(2)) {
1461 unsigned long t_len = t->len;
1462 unsigned long s_len = strlen(t->text);
1463 if (t_len != s_len) {
1464 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1465 t->text, t->type, s_len, t_len);
1466 t->len = s_len;
1467 }
1468 }
1469 len += t->len;
1470 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001472 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001473 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001474
H. Peter Anvin734b1882002-04-30 21:01:08 +00001475 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001476
1477 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001478 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001479 memcpy(p, t->text, t->len);
1480 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001481 } else if (t->type == TOK_WHITESPACE) {
1482 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001483 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001484 }
1485 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001486
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001487 return line;
1488}
1489
1490/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001491 * A scanner, suitable for use by the expression evaluator, which
1492 * operates on a line of Tokens. Expects a pointer to a pointer to
1493 * the first token in the line to be passed in as its private_data
1494 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001495 *
1496 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001497 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001498struct ppscan {
1499 Token *tptr;
1500 int ntokens;
1501};
1502
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001504{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001505 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001506 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001507 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001508
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001510 if (pps->ntokens && (tline = pps->tptr)) {
1511 pps->ntokens--;
1512 pps->tptr = tline->next;
1513 } else {
1514 pps->tptr = NULL;
1515 pps->ntokens = 0;
1516 return tokval->t_type = TOKEN_EOS;
1517 }
1518 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001519
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001520 tokval->t_charptr = tline->text;
1521
H. Peter Anvin76690a12002-04-30 20:52:49 +00001522 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001524 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001526
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001528 p = tokval->t_charptr = tline->text;
1529 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 tokval->t_charptr++;
1531 return tokval->t_type = TOKEN_ID;
1532 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001533
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001534 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001535 if (r >= p+MAX_KEYWORD)
1536 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001537 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001538 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001539 *s = '\0';
1540 /* right, so we have an identifier sitting in temp storage. now,
1541 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001542 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001543 }
1544
H. Peter Anvine2c80182005-01-15 22:15:51 +00001545 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001546 bool rn_error;
1547 tokval->t_integer = readnum(tline->text, &rn_error);
1548 tokval->t_charptr = tline->text;
1549 if (rn_error)
1550 return tokval->t_type = TOKEN_ERRNUM;
1551 else
1552 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001553 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001554
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001555 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001556 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001557 }
1558
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001560 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001561
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001562 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001563 tokval->t_charptr = tline->text;
1564 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001565
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001566 if (ep[0] != bq || ep[1] != '\0')
1567 return tokval->t_type = TOKEN_ERRSTR;
1568 else
1569 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001570 }
1571
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 if (tline->type == TOK_OTHER) {
1573 if (!strcmp(tline->text, "<<"))
1574 return tokval->t_type = TOKEN_SHL;
1575 if (!strcmp(tline->text, ">>"))
1576 return tokval->t_type = TOKEN_SHR;
1577 if (!strcmp(tline->text, "//"))
1578 return tokval->t_type = TOKEN_SDIV;
1579 if (!strcmp(tline->text, "%%"))
1580 return tokval->t_type = TOKEN_SMOD;
1581 if (!strcmp(tline->text, "=="))
1582 return tokval->t_type = TOKEN_EQ;
1583 if (!strcmp(tline->text, "<>"))
1584 return tokval->t_type = TOKEN_NE;
1585 if (!strcmp(tline->text, "!="))
1586 return tokval->t_type = TOKEN_NE;
1587 if (!strcmp(tline->text, "<="))
1588 return tokval->t_type = TOKEN_LE;
1589 if (!strcmp(tline->text, ">="))
1590 return tokval->t_type = TOKEN_GE;
1591 if (!strcmp(tline->text, "&&"))
1592 return tokval->t_type = TOKEN_DBL_AND;
1593 if (!strcmp(tline->text, "^^"))
1594 return tokval->t_type = TOKEN_DBL_XOR;
1595 if (!strcmp(tline->text, "||"))
1596 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001597 }
1598
1599 /*
1600 * We have no other options: just return the first character of
1601 * the token text.
1602 */
1603 return tokval->t_type = tline->text[0];
1604}
1605
1606/*
H. Peter Anvind2354082019-08-27 16:38:48 -07001607 * 1. An expression (true if nonzero 0)
1608 * 2. The keywords true, on, yes for true
1609 * 3. The keywords false, off, no for false
1610 * 4. An empty line, for true
1611 *
1612 * On error, return defval (usually the previous value)
1613 */
1614static bool pp_get_boolean_option(Token *tline, bool defval)
1615{
1616 static const char * const noyes[] = {
1617 "no", "yes",
1618 "false", "true",
1619 "off", "on"
1620 };
1621 struct ppscan pps;
1622 struct tokenval tokval;
1623 expr *evalresult;
1624
1625 skip_white_(tline);
1626 if (!tline || !tline->type)
1627 return true;
1628
1629 if (tline->type == TOK_ID) {
1630 size_t i;
1631 for (i = 0; i < ARRAY_SIZE(noyes); i++)
1632 if (!nasm_stricmp(tline->text, noyes[i]))
1633 return i & 1;
1634 }
1635
1636 pps.tptr = NULL;
1637 pps.tptr = tline;
1638 pps.ntokens = -1;
1639 tokval.t_type = TOKEN_INVALID;
1640 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
1641
1642 if (!evalresult)
1643 return true;
1644
1645 if (tokval.t_type)
1646 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
1647 if (!is_really_simple(evalresult)) {
1648 nasm_nonfatal("boolean flag expression must be a constant");
1649 return defval;
1650 }
1651
1652 return reloc_value(evalresult) != 0;
1653}
1654
1655/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001656 * Compare a string to the name of an existing macro; this is a
1657 * simple wrapper which calls either strcmp or nasm_stricmp
1658 * depending on the value of the `casesense' parameter.
1659 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001660static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001661{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001662 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001663}
1664
1665/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001666 * Compare a string to the name of an existing macro; this is a
1667 * simple wrapper which calls either strcmp or nasm_stricmp
1668 * depending on the value of the `casesense' parameter.
1669 */
1670static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1671{
1672 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1673}
1674
1675/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001676 * Return the Context structure associated with a %$ token. Return
1677 * NULL, having _already_ reported an error condition, if the
1678 * context stack isn't deep enough for the supplied number of $
1679 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001680 *
1681 * If "namep" is non-NULL, set it to the pointer to the macro name
1682 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001683 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001684static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001685{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001686 Context *ctx;
1687 int i;
1688
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001689 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001690 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001691
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001692 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001694
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001696 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001698 }
1699
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001700 name += 2;
1701 ctx = cstk;
1702 i = 0;
1703 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001704 name++;
1705 i++;
1706 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001707 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001709 nasm_nonfatal("`%s': context stack is only"
1710 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001712 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001713
1714 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001715 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001716
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001717 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001718}
1719
1720/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001721 * Open an include file. This routine must always return a valid
1722 * file pointer if it returns - it's responsible for throwing an
1723 * ERR_FATAL and bombing out completely if not. It should also try
1724 * the include path one by one until it finds the file or reaches
1725 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001726 *
1727 * Note: for INC_PROBE the function returns NULL at all times;
1728 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001729 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001730enum incopen_mode {
1731 INC_NEEDED, /* File must exist */
1732 INC_OPTIONAL, /* Missing is OK */
1733 INC_PROBE /* Only an existence probe */
1734};
1735
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001736/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001737static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001738 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001739{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001740 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001741 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001742 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001743 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001744 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001745
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001747 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001748 if (omode == INC_PROBE) {
1749 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001750 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001751 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001752 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001753 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001754 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001755 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001756 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001758 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001759
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001760 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001761
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001762 if (!ip) {
1763 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001764 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001765 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001766
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001767 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001768 ip = ip->next;
1769 }
1770}
1771
1772/*
1773 * Open a file, or test for the presence of one (depending on omode),
1774 * considering the include path.
1775 */
1776static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001777 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001778 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001779 enum incopen_mode omode,
1780 enum file_flags fmode)
1781{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001782 struct hash_insert hi;
1783 void **hp;
1784 char *path;
1785 FILE *fp = NULL;
1786
1787 hp = hash_find(&FileHash, file, &hi);
1788 if (hp) {
1789 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001790 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001791 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001792 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001793 } else {
1794 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001795 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001796
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001797 /* Positive or negative result */
1798 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001799
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001800 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001801 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001802 */
1803 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001804 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001805 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001806
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001807 if (!path) {
1808 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001809 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001810 } else {
1811 if (!fp && omode != INC_PROBE)
1812 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001813 }
1814
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001815 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001816 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001817
1818 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001819}
1820
1821/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001822 * Opens an include or input file. Public version, for use by modules
1823 * that get a file:lineno pair and need to look at the file again
1824 * (e.g. the CodeView debug backend). Returns NULL on failure.
1825 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001826FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001827{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001828 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001829}
1830
1831/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001832 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001833 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001834 * return true if _any_ single-line macro of that name is defined.
1835 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001836 * `nparam' or no parameters is defined.
1837 *
1838 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001839 * defined, or nparam is -1, the address of the definition structure
1840 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001841 * is NULL, no action will be taken regarding its contents, and no
1842 * error will occur.
1843 *
1844 * Note that this is also called with nparam zero to resolve
1845 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001846 *
1847 * If you already know which context macro belongs to, you can pass
1848 * the context pointer as first parameter; if you won't but name begins
1849 * with %$ the context will be automatically computed. If all_contexts
1850 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001851 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001852static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001853smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07001854 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001855{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001856 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001857 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001858
H. Peter Anvin97a23472007-09-16 17:57:25 -07001859 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001860 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001861 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001862 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001863 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001865 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001866 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001867 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001868 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001869 }
H. Peter Anvind2354082019-08-27 16:38:48 -07001870
1871restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07001872 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001873
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 while (m) {
1875 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001876 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001877 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07001878 if (m->alias && !find_alias) {
1879 if (do_aliases) {
H. Peter Anvind626f352019-09-12 18:31:29 -07001880 name = m->expansion->text;
H. Peter Anvind2354082019-08-27 16:38:48 -07001881 goto restart;
1882 } else {
1883 continue;
1884 }
1885 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001887 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001889 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 }
1891 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001892 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001893
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001894 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001895}
1896
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001897/* param should be a natural number [0; INT_MAX] */
1898static int read_param_count(const char *str)
1899{
1900 int result;
1901 bool err;
1902
1903 result = readnum(str, &err);
1904 if (result < 0 || result > INT_MAX) {
1905 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001906 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1907 str, 0, INT_MAX);
1908 } else if (err)
1909 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001910 return result;
1911}
1912
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001913/*
1914 * Count and mark off the parameters in a multi-line macro call.
1915 * This is called both from within the multi-line macro expansion
1916 * code, and also to mark off the default parameters when provided
1917 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001918 *
1919 * Note that we need space in the params array for parameter 0 being
1920 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001921 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001922static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001923{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001924 int paramsize, brace;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001925 int nparam = 0;
1926 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001927
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001928 paramsize = PARAM_DELTA;
1929 params = nasm_malloc(paramsize * sizeof(*params));
1930 params[0] = NULL;
1931
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07001932 while (skip_white_(t)) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001933 /* 2 slots for captured label and NULL */
1934 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001936 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001938 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001940 brace++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001941 params[++nparam] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001942 if (brace) {
1943 while (brace && (t = t->next) != NULL) {
1944 if (tok_is_(t, "{"))
1945 brace++;
1946 else if (tok_is_(t, "}"))
1947 brace--;
1948 }
1949
1950 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 /*
1952 * Now we've found the closing brace, look further
1953 * for the comma.
1954 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001955 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 skip_white_(t);
1957 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001958 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 while (tok_isnt_(t, ","))
1960 t = t->next;
1961 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001963 } else {
1964 while (tok_isnt_(t, ","))
1965 t = t->next;
1966 }
1967 if (t) { /* got a comma/brace */
1968 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001970 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001971
1972 params[nparam+1] = NULL;
1973 *paramsp = params;
1974 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001975}
1976
1977/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001978 * Determine whether one of the various `if' conditions is true or
1979 * not.
1980 *
1981 * We must free the tline we get passed.
1982 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001983static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001984{
H. Peter Anvin70055962007-10-11 00:05:31 -07001985 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001986 Token *t, *tt, *origline;
1987 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001988 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001989 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001990 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001991 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001992 const char *dname = pp_directives[ct];
1993 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07001994 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001995
1996 origline = tline;
1997
H. Peter Anvindd88aa92019-09-12 19:39:48 -07001998 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001999 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002000 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002001 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002003 if (!tline)
2004 break;
2005 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002006 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002007 dname);
2008 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002009 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002010 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002011 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002012 tline = tline->next;
2013 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002015
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002016 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002017 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002018 while (tline) {
2019 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002020 if (!tline || (tline->type != TOK_ID &&
2021 (tline->type != TOK_PREPROC_ID ||
2022 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002023 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002024 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 }
H. Peter Anvind2354082019-08-27 16:38:48 -07002027 if (smacro_defined(NULL, tline->text, 0, NULL, true, false))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002028 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002030 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002031 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002032
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002033 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002034 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002035 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002036 while (tline) {
2037 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002038 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002039 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002040 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002041 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002042 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002043 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002044 goto fail;
2045 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002046 p = tline->text;
2047 if (tline->type == TOK_PREPROC_ID)
2048 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08002049 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002050 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002051 if (getenv(p))
2052 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002053 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002054 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002055 break;
2056
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002057 case PP_IFIDNI:
2058 casesense = false;
2059 /* fall through */
2060 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 tline = expand_smacro(tline);
2062 t = tt = tline;
2063 while (tok_isnt_(tt, ","))
2064 tt = tt->next;
2065 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002066 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002067 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002068 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 }
2070 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002071 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
2073 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002074 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002075 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002076 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 }
2078 if (t->type == TOK_WHITESPACE) {
2079 t = t->next;
2080 continue;
2081 }
2082 if (tt->type == TOK_WHITESPACE) {
2083 tt = tt->next;
2084 continue;
2085 }
2086 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002087 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002088 break;
2089 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002090 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002091 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 size_t l1 = nasm_unquote(t->text, NULL);
2093 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002094
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002095 if (l1 != l2) {
2096 j = false;
2097 break;
2098 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002099 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 j = false;
2101 break;
2102 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002103 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002104 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002105 break;
2106 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002107
H. Peter Anvine2c80182005-01-15 22:15:51 +00002108 t = t->next;
2109 tt = tt->next;
2110 }
2111 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002112 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002114
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002115 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002116 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002117 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002118 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002119
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 skip_white_(tline);
2121 tline = expand_id(tline);
2122 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002123 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124 goto fail;
2125 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002126 nasm_zero(searching);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 searching.name = nasm_strdup(tline->text);
2128 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002129 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002130 searching.nparam_max = INT_MAX;
2131 tline = expand_smacro(tline->next);
2132 skip_white_(tline);
2133 if (!tline) {
2134 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002135 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002136 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137 } else {
2138 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002139 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002140 }
2141 if (tline && tok_is_(tline->next, "-")) {
2142 tline = tline->next->next;
2143 if (tok_is_(tline, "*"))
2144 searching.nparam_max = INT_MAX;
2145 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002146 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002147 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002148 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002149 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002150 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002151 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002152 searching.nparam_max = searching.nparam_min;
2153 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002154 }
2155 }
2156 if (tline && tok_is_(tline->next, "+")) {
2157 tline = tline->next;
2158 searching.plus = true;
2159 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002160 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2161 while (mmac) {
2162 if (!strcmp(mmac->name, searching.name) &&
2163 (mmac->nparam_min <= searching.nparam_max
2164 || searching.plus)
2165 && (searching.nparam_min <= mmac->nparam_max
2166 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002167 found = true;
2168 break;
2169 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002170 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002171 }
2172 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002173 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002174 nasm_free(searching.name);
2175 j = found;
2176 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002177 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002178
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002179 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 needtype = TOK_ID;
2181 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002182 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002183 needtype = TOK_NUMBER;
2184 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002185 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002186 needtype = TOK_STRING;
2187 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002188
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002189iftype:
2190 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002191
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002192 while (tok_type_(t, TOK_WHITESPACE) ||
2193 (needtype == TOK_NUMBER &&
2194 tok_type_(t, TOK_OTHER) &&
2195 (t->text[0] == '-' || t->text[0] == '+') &&
2196 !t->text[1]))
2197 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002198
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002199 j = tok_type_(t, needtype);
2200 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002201
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002202 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002203 t = tline = expand_smacro(tline);
2204 while (tok_type_(t, TOK_WHITESPACE))
2205 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002206
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002207 j = false;
2208 if (t) {
2209 t = t->next; /* Skip the actual token */
2210 while (tok_type_(t, TOK_WHITESPACE))
2211 t = t->next;
2212 j = !t; /* Should be nothing left */
2213 }
2214 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002215
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002216 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002217 t = tline = expand_smacro(tline);
2218 while (tok_type_(t, TOK_WHITESPACE))
2219 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002220
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002221 j = !t; /* Should be empty */
2222 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002223
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002224 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002225 pps.tptr = tline = expand_smacro(tline);
2226 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002227 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002228 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002229 if (!evalresult)
2230 return -1;
2231 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002232 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002233 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002234 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002235 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002236 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002237 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002238 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002239 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002240
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002241 case PP_IFUSING:
2242 case PP_IFUSABLE:
2243 {
2244 const struct use_package *pkg;
2245 bool err;
2246
2247 pkg = get_use_pkg(tline, dname, &err);
2248 if (err)
2249 goto fail;
2250
2251 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2252 break;
2253 }
2254
H. Peter Anvine2c80182005-01-15 22:15:51 +00002255 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002256 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002257 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002258 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002259
2260 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002261 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002262
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002263fail:
2264 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002265 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002266}
2267
2268/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002269 * Default smacro expansion routine: just returns a copy of the
2270 * expansion list.
2271 */
2272static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002273smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002274{
2275 (void)params;
2276 (void)nparams;
2277
2278 return dup_tlist(s->expansion, NULL);
2279}
2280
2281/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002282 * Emit a macro defintion or undef to the listing file, if
2283 * desired. This is similar to detoken(), but it handles the reverse
2284 * expansion list, does not expand %! or local variable tokens, and
2285 * does some special handling for macro parameters.
2286 */
2287static void
2288list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2289{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002290 Token *t;
2291 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002292 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002293 char *context_prefix = NULL;
2294 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002295
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002296 namelen = strlen(m->name);
2297 size = namelen + 2; /* Include room for space after name + NUL */
2298
2299 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002300 int context_depth = cstk->depth - ctx->depth + 1;
2301 context_prefix =
2302 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2303 ctx->name ? ctx->name : "",
2304 ctx->number, context_depth, "");
2305
2306 context_len = nasm_last_string_len();
2307 memset(context_prefix + context_len - context_depth,
2308 '$', context_depth);
2309 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002310 }
2311
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002312 list_for_each(t, m->expansion)
2313 size += t->text ? t->len : 1;
2314
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002315 if (m->nparam) {
2316 /*
2317 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002318 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002319 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002320 int i;
2321
2322 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002323 for (i = 0; i < m->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002324 size += m->params[i].namelen;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002325 }
2326
2327 def = nasm_malloc(size);
2328 p = def+size;
2329 *--p = '\0';
2330
2331 list_for_each(t, m->expansion) {
2332 if (!t->text) {
2333 *--p = ' ';
2334 } else {
2335 p -= t->len;
2336 memcpy(p, t->text, t->len);
2337 }
2338 }
2339
2340 *--p = ' ';
2341
2342 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002343 int i;
2344
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002345 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002346 for (i = m->nparam-1; i >= 0; i--) {
2347 enum sparmflags flags = m->params[i].flags;
2348 if (flags & SPARM_GREEDY)
2349 *--p = '+';
2350 if (m->params[i].name) {
2351 p -= m->params[i].namelen;
2352 memcpy(p, m->params[i].name, m->params[i].namelen);
2353 }
2354 if (flags & SPARM_NOSTRIP)
2355 *--p = '!';
2356 if (flags & SPARM_STR)
2357 *--p = '&';
2358 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002359 *--p = '=';
2360 *--p = ',';
2361 }
2362 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002363 }
2364
2365 p -= namelen;
2366 memcpy(p, m->name, namelen);
2367
H. Peter Anvin6686de22019-08-10 05:33:14 -07002368 if (context_prefix) {
2369 p -= context_len;
2370 memcpy(p, context_prefix, context_len);
2371 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002372 }
2373
2374 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002375 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002376}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002377
2378/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002379 * Parse smacro arguments, return argument count. If the tmpl argument
2380 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002381 * *tpp is updated to point to the pointer to the first token after the
2382 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002383 *
2384 * The text values from any argument tokens are "stolen" and the
2385 * corresponding text fields set to NULL.
2386 */
2387static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2388{
2389 int nparam = 0;
2390 enum sparmflags flags;
2391 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002392 bool err, done;
2393 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002394 Token **tn = *tpp;
2395 Token *t = *tn;
2396 Token *name;
2397
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002398 /*
2399 * DO NOT skip whitespace here, or we won't be able to distinguish:
2400 *
2401 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2402 * %define bar(a,b) ; two arguments, empty expansion
2403 *
2404 * This ambiguity was inherited from C.
2405 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002406
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002407 if (!tok_is_(t, "("))
2408 goto finish;
2409
2410 if (tmpl) {
2411 Token *tx = t;
2412 Token **txpp = &tx;
2413 int sparam;
2414
2415 /* Count parameters first */
2416 sparam = parse_smacro_template(&txpp, NULL);
2417 if (!sparam)
2418 goto finish; /* No parameters, we're done */
2419 nasm_newn(params, sparam);
2420 }
2421
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002422 /* Skip leading paren */
2423 tn = &t->next;
2424 t = *tn;
2425
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002426 name = NULL;
2427 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002428 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002429
2430 while (!done) {
2431 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002432 if (name || flags)
2433 nasm_nonfatal("`)' expected to terminate macro template");
2434 else
2435 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002436 break;
2437 }
2438
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002439 switch (t->type) {
2440 case TOK_ID:
2441 if (name)
2442 goto bad;
2443 name = t;
2444 break;
2445
2446 case TOK_OTHER:
2447 if (t->text[1])
2448 goto bad;
2449 switch (t->text[0]) {
2450 case '=':
2451 flags |= SPARM_EVAL;
2452 break;
2453 case '&':
2454 flags |= SPARM_STR;
2455 break;
2456 case '!':
2457 flags |= SPARM_NOSTRIP;
2458 break;
2459 case '+':
2460 flags |= SPARM_GREEDY;
2461 greedy = true;
2462 break;
2463 case ',':
2464 if (greedy)
2465 nasm_nonfatal("greedy parameter must be last");
2466 /* fall through */
2467 case ')':
2468 if (params) {
2469 if (name) {
2470 params[nparam].name = name->text;
2471 params[nparam].namelen = name->len;
2472 name->text = NULL;
2473 }
2474 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002475 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002476 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002477 name = NULL;
2478 flags = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002479 done = t->text[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002480 break;
2481 default:
2482 goto bad;
2483 }
2484 break;
2485
2486 case TOK_WHITESPACE:
2487 break;
2488
2489 default:
2490 bad:
2491 if (!err) {
2492 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2493 err = true;
2494 }
2495 break;
2496 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002497
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002498 tn = &t->next;
2499 t = *tn;
2500 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002501
2502finish:
2503 while (t && t->type == TOK_WHITESPACE) {
2504 tn = &t->next;
2505 t = t->next;
2506 }
2507 *tpp = tn;
2508 if (tmpl) {
2509 tmpl->nparam = nparam;
2510 tmpl->greedy = greedy;
2511 tmpl->params = params;
2512 }
2513 return nparam;
2514}
2515
2516/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002517 * Common code for defining an smacro. The tmpl argument, if not NULL,
2518 * contains any macro parameters that aren't explicit arguments;
2519 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002520 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002521static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002522 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002523{
2524 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002525 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002526 Context *ctx;
2527 bool defining_alias = false;
2528 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002529
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002530 if (tmpl) {
2531 defining_alias = tmpl->alias;
2532 nparam = tmpl->nparam;
2533 }
2534
2535 while (1) {
2536 ctx = get_ctx(mname, &mname);
2537
H. Peter Anvind2354082019-08-27 16:38:48 -07002538 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002539 /* Create a new macro */
2540 smtbl = ctx ? &ctx->localmac : &smacros;
2541 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2542 nasm_new(smac);
2543 smac->next = *smhead;
2544 *smhead = smac;
2545 break;
2546 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002547 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002548 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002549 /*
2550 * Some instances of the old code considered this a failure,
2551 * some others didn't. What is the right thing to do here?
2552 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002553 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002554 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 /*
2556 * We're redefining, so we have to take over an
2557 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002558 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002560 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002561 break;
2562 } else if (smac->in_progress) {
2563 nasm_nonfatal("macro alias loop");
2564 goto fail;
2565 } else {
2566 /* It is an alias macro; follow the alias link */
2567 SMacro *s;
2568
2569 smac->in_progress = true;
2570 s = define_smacro(smac->expansion->text, casesense,
2571 expansion, tmpl);
2572 smac->in_progress = false;
2573 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002575 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002576
2577 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002578 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002579 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002580 smac->expand = smacro_expand_default;
2581 if (tmpl) {
2582 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002583 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002584 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002585 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002586 if (tmpl->expand)
2587 smac->expand = tmpl->expand;
2588 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002589 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002590 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2591 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002592 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002593 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002594
2595fail:
2596 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002597 if (tmpl)
2598 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002599 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002600}
2601
2602/*
2603 * Undefine an smacro
2604 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002605static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002606{
2607 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002608 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002609 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002610
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002611 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002612 smtbl = ctx ? &ctx->localmac : &smacros;
2613 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002614
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002615 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 /*
2617 * We now have a macro name... go hunt for it.
2618 */
2619 sp = smhead;
2620 while ((s = *sp) != NULL) {
2621 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002622 if (s->alias && !undefalias) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002623 if (!do_aliases)
2624 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002625 if (s->in_progress) {
2626 nasm_nonfatal("macro alias loop");
2627 } else {
2628 s->in_progress = true;
2629 undef_smacro(s->expansion->text, false);
2630 s->in_progress = false;
2631 }
2632 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002633 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002634 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2635 ctx, s);
2636 *sp = s->next;
2637 free_smacro(s);
2638 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 } else {
2640 sp = &s->next;
2641 }
2642 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002643 }
2644}
2645
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002646/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002647 * Parse a mmacro specification.
2648 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002649static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002650{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002651 tline = tline->next;
2652 skip_white_(tline);
2653 tline = expand_id(tline);
2654 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002655 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002657 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002658
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002659#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002660 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002661#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002662 def->name = nasm_strdup(tline->text);
2663 def->plus = false;
2664 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002665 def->nparam_min = 0;
2666 def->nparam_max = 0;
2667
H. Peter Anvina26433d2008-07-16 14:40:01 -07002668 tline = expand_smacro(tline->next);
2669 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002670 if (!tok_type_(tline, TOK_NUMBER))
2671 nasm_nonfatal("`%s' expects a parameter count", directive);
2672 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002673 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002674 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 tline = tline->next->next;
2676 if (tok_is_(tline, "*")) {
2677 def->nparam_max = INT_MAX;
2678 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002679 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002681 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002683 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002684 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 }
2686 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002687 }
2688 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 tline = tline->next;
2690 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002691 }
2692 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002693 !nasm_stricmp(tline->next->text, ".nolist")) {
2694 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002695 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002696 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697
H. Peter Anvina26433d2008-07-16 14:40:01 -07002698 /*
2699 * Handle default parameters.
2700 */
2701 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002702 def->dlist = tline->next;
2703 tline->next = NULL;
2704 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002705 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 def->dlist = NULL;
2707 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002708 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002709 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002710
H. Peter Anvin89cee572009-07-15 09:16:54 -04002711 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002712 !def->plus) {
2713 /*
2714 *!macro-defaults [on] macros with more default than optional parameters
2715 *! warns when a macro has more default parameters than optional parameters.
2716 *! See \k{mlmacdef} for why might want to disable this warning.
2717 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002718 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002719 "too many default macro parameters in macro `%s'", def->name);
2720 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002721
H. Peter Anvina26433d2008-07-16 14:40:01 -07002722 return true;
2723}
2724
2725
2726/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002727 * Decode a size directive
2728 */
2729static int parse_size(const char *str) {
2730 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002731 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002732 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002734 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002735}
2736
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002737/*
2738 * Process a preprocessor %pragma directive. Currently there are none.
2739 * Gets passed the token list starting with the "preproc" token from
2740 * "%pragma preproc".
2741 */
2742static void do_pragma_preproc(Token *tline)
2743{
2744 /* Skip to the real stuff */
2745 tline = tline->next;
2746 skip_white_(tline);
2747 if (!tline)
2748 return;
2749
2750 (void)tline; /* Nothing else to do at present */
2751}
2752
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002753static bool is_macro_id(const Token *t)
2754{
2755 return t && (t->type == TOK_ID ||
2756 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2757}
2758
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002759static char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002760{
2761 char *id;
2762 Token *t = *tp;
2763
2764 t = t->next; /* Skip directive */
2765 skip_white_(t);
2766 t = expand_id(t);
2767
2768 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002769 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002770 return NULL;
2771 }
2772
2773 id = t->text;
2774 skip_white_(t);
2775 *tp = t;
2776 return id;
2777}
2778
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002779/* Parse a %use package name and find the package. Set *err on syntax error. */
2780static const struct use_package *
2781get_use_pkg(Token *t, const char *dname, bool *err)
2782{
2783 char *id;
2784
2785 *err = false;
2786
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002787 skip_white_(t);
2788
2789 t = expand_smacro(t);
2790
2791 id = NULL;
2792 if (t) {
2793 if (t->type == TOK_ID) {
2794 id = t->text;
2795 } else if (t->type == TOK_STRING) {
2796 nasm_unquote_cstr(t->text, NULL);
2797 id = t->text;
2798 }
2799 }
2800
2801 if (!id) {
2802 nasm_nonfatal("`%s' expects a package name", dname);
2803 *err = true;
2804 return NULL;
2805 }
2806
2807 t = t->next;
2808 skip_white_(t);
2809 if (t)
2810 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
2811
2812 return nasm_find_use_package(id);
2813}
2814
Ed Beroset3ab3f412002-06-11 03:31:49 +00002815/**
2816 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002817 * Find out if a line contains a preprocessor directive, and deal
2818 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002819 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002820 * If a directive _is_ found, it is the responsibility of this routine
2821 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002822 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002823 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002824 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002825 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002826 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002827 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002828static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002829{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002830 enum preproc_token i;
2831 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002832 bool err;
2833 int nparam;
2834 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002835 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002836 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002837 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002838 char *p, *pp;
2839 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002840 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002841 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002842 Include *inc;
2843 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002844 Cond *cond;
2845 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002846 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002847 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002848 struct tokenval tokval;
2849 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002850 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002851 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002852 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002853 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002854
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002855 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002856 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002857
H. Peter Anvineba20a72002-04-30 20:53:55 +00002858 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002859 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002860 (tline->text[0] && (tline->text[1] == '%' ||
2861 tline->text[1] == '$' ||
2862 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002864
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002865 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002866 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002867
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002868 casesense = true;
2869 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2870 casesense = false;
2871 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002872 }
2873
2874 /*
2875 * If we're in a non-emitting branch of a condition construct,
2876 * or walking to the end of an already terminated %rep block,
2877 * we should ignore all directives except for condition
2878 * directives.
2879 */
2880 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002881 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2882 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002883 return NO_DIRECTIVE_FOUND;
2884 }
2885
2886 /*
2887 * If we're defining a macro or reading a %rep block, we should
2888 * ignore all directives except for %macro/%imacro (which nest),
2889 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2890 * If we're in a %rep block, another %rep nests, so should be let through.
2891 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002892 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002893 i != PP_ENDMACRO && i != PP_ENDM &&
2894 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2895 return NO_DIRECTIVE_FOUND;
2896 }
2897
2898 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002899 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 nested_mac_count++;
2901 return NO_DIRECTIVE_FOUND;
2902 } else if (nested_mac_count > 0) {
2903 if (i == PP_ENDMACRO) {
2904 nested_mac_count--;
2905 return NO_DIRECTIVE_FOUND;
2906 }
2907 }
2908 if (!defining->name) {
2909 if (i == PP_REP) {
2910 nested_rep_count++;
2911 return NO_DIRECTIVE_FOUND;
2912 } else if (nested_rep_count > 0) {
2913 if (i == PP_ENDREP) {
2914 nested_rep_count--;
2915 return NO_DIRECTIVE_FOUND;
2916 }
2917 }
2918 }
2919 }
2920
H. Peter Anvin4169a472007-09-12 01:29:43 +00002921 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002922 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002923 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002925
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002926 case PP_PRAGMA:
2927 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002928 * %pragma namespace options...
2929 *
2930 * The namespace "preproc" is reserved for the preprocessor;
2931 * all other namespaces generate a [pragma] assembly directive.
2932 *
2933 * Invalid %pragmas are ignored and may have different
2934 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002935 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002936 t = tline;
2937 tline = tline->next;
2938 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002939 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002940 while (tok_type_(tline, TOK_WHITESPACE)) {
2941 t = tline;
2942 tline = tline->next;
2943 delete_Token(t);
2944 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002945 if (tok_type_(tline, TOK_ID)) {
2946 if (!nasm_stricmp(tline->text, "preproc")) {
2947 /* Preprocessor pragma */
2948 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002949 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002950 } else {
2951 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002952
2953 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002954 for (t = tline; t->next; t = t->next)
2955 ;
2956 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002957
2958 /* Prepend "[pragma " */
2959 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2960 t = new_Token(t, TOK_ID, "pragma", 6);
2961 t = new_Token(t, TOK_OTHER, "[", 1);
2962 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002963 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002964 }
2965 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002966 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002967
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 case PP_STACKSIZE:
2969 /* Directive to tell NASM what the default stack size is. The
2970 * default is for a 16-bit stack, and this can be overriden with
2971 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 */
2973 tline = tline->next;
2974 if (tline && tline->type == TOK_WHITESPACE)
2975 tline = tline->next;
2976 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002977 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 }
2979 if (nasm_stricmp(tline->text, "flat") == 0) {
2980 /* All subsequent ARG directives are for a 32-bit stack */
2981 StackSize = 4;
2982 StackPointer = "ebp";
2983 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002984 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002985 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2986 /* All subsequent ARG directives are for a 64-bit stack */
2987 StackSize = 8;
2988 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002989 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002990 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 } else if (nasm_stricmp(tline->text, "large") == 0) {
2992 /* All subsequent ARG directives are for a 16-bit stack,
2993 * far function call.
2994 */
2995 StackSize = 2;
2996 StackPointer = "bp";
2997 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002998 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 } else if (nasm_stricmp(tline->text, "small") == 0) {
3000 /* All subsequent ARG directives are for a 16-bit stack,
3001 * far function call. We don't support near functions.
3002 */
3003 StackSize = 2;
3004 StackPointer = "bp";
3005 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003006 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003008 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003010 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003011
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 case PP_ARG:
3013 /* TASM like ARG directive to define arguments to functions, in
3014 * the following form:
3015 *
3016 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3017 */
3018 offset = ArgOffset;
3019 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003020 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003022
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 /* Find the argument name */
3024 tline = tline->next;
3025 if (tline && tline->type == TOK_WHITESPACE)
3026 tline = tline->next;
3027 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003028 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003029 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 }
3031 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003032
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 /* Find the argument size type */
3034 tline = tline->next;
3035 if (!tline || tline->type != TOK_OTHER
3036 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003037 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003038 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 }
3040 tline = tline->next;
3041 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003042 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003043 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003045
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003047 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003049 size = parse_size(tt->text);
3050 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003051 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003052 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003053 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003054 }
3055 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003056
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003057 /* Round up to even stack slots */
3058 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003059
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 /* Now define the macro for the argument */
3061 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3062 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003063 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003065
H. Peter Anvine2c80182005-01-15 22:15:51 +00003066 /* Move to the next argument in the list */
3067 tline = tline->next;
3068 if (tline && tline->type == TOK_WHITESPACE)
3069 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003070 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003071 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003072 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003073
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 case PP_LOCAL:
3075 /* TASM like LOCAL directive to define local variables for a
3076 * function, in the following form:
3077 *
3078 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3079 *
3080 * The '= LocalSize' at the end is ignored by NASM, but is
3081 * required by TASM to define the local parameter size (and used
3082 * by the TASM macro package).
3083 */
3084 offset = LocalOffset;
3085 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003086 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003088
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 /* Find the argument name */
3090 tline = tline->next;
3091 if (tline && tline->type == TOK_WHITESPACE)
3092 tline = tline->next;
3093 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003094 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003095 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 }
3097 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003098
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 /* Find the argument size type */
3100 tline = tline->next;
3101 if (!tline || tline->type != TOK_OTHER
3102 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003103 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003104 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 }
3106 tline = tline->next;
3107 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003108 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003109 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003111
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003113 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003115 size = parse_size(tt->text);
3116 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003117 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003119 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 }
3121 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003122
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003123 /* Round up to even stack slots */
3124 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003125
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003126 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003127
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003128 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3130 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003131 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003132
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 /* Now define the assign to setup the enter_c macro correctly */
3134 snprintf(directive, sizeof(directive),
3135 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003136 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003137
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 /* Move to the next argument in the list */
3139 tline = tline->next;
3140 if (tline && tline->type == TOK_WHITESPACE)
3141 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003142 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003143 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003144 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003145
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 case PP_CLEAR:
3147 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003148 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 free_macros();
3150 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003151 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003152
H. Peter Anvin418ca702008-05-30 10:42:30 -07003153 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003155 skip_white_(t);
3156 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003158 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003159 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003160 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003161 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003162 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003163 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003164 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003165 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003166 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003167 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003168
3169 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003171 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003172
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003173 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003175 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003176 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003177 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003178 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003179 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003181 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003182 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003183 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003185 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003186 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003187 (pp_mode == PP_DEPS)
3188 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003189 if (!inc->fp) {
3190 /* -MG given but file not found */
3191 nasm_free(inc);
3192 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003193 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003194 inc->lineno = src_set_linnum(0);
3195 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003196 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003198 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003199 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003200 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003201
H. Peter Anvind2456592008-06-19 15:04:18 -07003202 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003203 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003204 const struct use_package *pkg;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003205 bool err;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003206
H. Peter Anvindd88aa92019-09-12 19:39:48 -07003207 pkg = get_use_pkg(tline->next, dname, &err);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003208 if (err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003209 goto done;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003210
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003211 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003212 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003213 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003214 /*
3215 * Not already included, go ahead and include it.
3216 * Treat it as an include file for the purpose of
3217 * producing a listing.
3218 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003219 use_loaded[pkg->index] = true;
3220 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003221 nasm_new(inc);
3222 inc->next = istk;
3223 inc->fname = src_set_fname(NULL);
3224 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003225 inc->nolist = !list_option('b') || istk->nolist;
3226 istk = inc;
3227 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003229 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003230 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003232 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003233 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 tline = tline->next;
3235 skip_white_(tline);
3236 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003237 if (tline) {
3238 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003239 nasm_nonfatal("`%s' expects a context identifier",
3240 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003241 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003242 }
3243 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003244 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003245 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003246 p = nasm_strdup(tline->text);
3247 } else {
3248 p = NULL; /* Anonymous */
3249 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003250
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003251 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003252 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003253 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003254 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003255 ctx->name = p;
3256 ctx->number = unique++;
3257 cstk = ctx;
3258 } else {
3259 /* %pop or %repl */
3260 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003261 nasm_nonfatal("`%s': context stack is empty",
3262 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003263 } else if (i == PP_POP) {
3264 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003265 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003266 "expected %s",
3267 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003268 else
3269 ctx_pop();
3270 } else {
3271 /* i == PP_REPL */
3272 nasm_free(cstk->name);
3273 cstk->name = p;
3274 p = NULL;
3275 }
3276 nasm_free(p);
3277 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003278 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003279 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 severity = ERR_FATAL;
3281 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003282 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003283 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003284 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003285 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003286 /*!
3287 *!user [on] %warning directives
3288 *! controls output of \c{%warning} directives (see \k{pperror}).
3289 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003290 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003291 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003292
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003293issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003294 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003295 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003296 tline->next = expand_smacro(tline->next);
3297 tline = tline->next;
3298 skip_white_(tline);
3299 t = tline ? tline->next : NULL;
3300 skip_white_(t);
3301 if (tok_type_(tline, TOK_STRING) && !t) {
3302 /* The line contains only a quoted string */
3303 p = tline->text;
3304 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003305 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003306 } else {
3307 /* Not a quoted string, or more than a quoted string */
3308 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003309 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003310 nasm_free(p);
3311 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003312 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003313 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003314
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003315 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003316 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003317 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003318 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003319 j = if_condition(tline->next, i);
3320 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003321 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003322 cond = nasm_malloc(sizeof(Cond));
3323 cond->next = istk->conds;
3324 cond->state = j;
3325 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003326 if(istk->mstk.mstk)
3327 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003328 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003329
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003330 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003331 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003332 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003333 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003334 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003335 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003336 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003337
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003338 case COND_DONE:
3339 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003340 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003341
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003342 case COND_ELSE_TRUE:
3343 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003344 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003345 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003346 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003347 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003348
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003349 case COND_IF_FALSE:
3350 /*
3351 * IMPORTANT: In the case of %if, we will already have
3352 * called expand_mmac_params(); however, if we're
3353 * processing an %elif we must have been in a
3354 * non-emitting mode, which would have inhibited
3355 * the normal invocation of expand_mmac_params().
3356 * Therefore, we have to do it explicitly here.
3357 */
3358 j = if_condition(expand_mmac_params(tline->next), i);
3359 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003360 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003361 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003362 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003363 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003364
H. Peter Anvine2c80182005-01-15 22:15:51 +00003365 case PP_ELSE:
3366 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003367 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003368 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003369 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003370 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003371 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003372 case COND_IF_TRUE:
3373 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003374 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003375 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003376
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003377 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003378 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003379
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003380 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003381 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003382 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003383
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003384 case COND_ELSE_TRUE:
3385 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003386 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003387 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003388 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003390 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003391 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003392
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 case PP_ENDIF:
3394 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003395 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003396 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003397 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003398 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003399 cond = istk->conds;
3400 istk->conds = cond->next;
3401 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003402 if(istk->mstk.mstk)
3403 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003404 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003405
H. Peter Anvin8b262472019-02-26 14:00:54 -08003406 case PP_RMACRO:
3407 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003408 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003409 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003410 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003411 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003412 if (i == PP_RMACRO)
3413 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003414 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003415 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003416 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003417 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003418
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003419 src_get(&defining->xline, &defining->fname);
3420
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003421 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3422 while (mmac) {
3423 if (!strcmp(mmac->name, defining->name) &&
3424 (mmac->nparam_min <= defining->nparam_max
3425 || defining->plus)
3426 && (defining->nparam_min <= mmac->nparam_max
3427 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003428 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003429 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003430 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003431 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003432 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003433 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003434 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003435
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436 case PP_ENDM:
3437 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003438 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003439 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003440 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003441 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003442 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3443 defining->next = *mmhead;
3444 *mmhead = defining;
3445 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003446 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003447
H. Peter Anvin89cee572009-07-15 09:16:54 -04003448 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003449 /*
3450 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003451 * macro-end marker for a macro with a name. Then we
3452 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003453 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003454 list_for_each(l, istk->expansion)
3455 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003456 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003457
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003458 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003459 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003460 * Remove all conditional entries relative to this
3461 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003462 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003463 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3464 cond = istk->conds;
3465 istk->conds = cond->next;
3466 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003467 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003468 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003469 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003470 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003471 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003472 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003473
H. Peter Anvina26433d2008-07-16 14:40:01 -07003474 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003475 casesense = false;
3476 /* fall through */
3477 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003478 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003479 MMacro **mmac_p;
3480 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003481
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003482 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003483 spec.casesense = casesense;
3484 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003485 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003487 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3488 while (mmac_p && *mmac_p) {
3489 mmac = *mmac_p;
3490 if (mmac->casesense == spec.casesense &&
3491 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3492 mmac->nparam_min == spec.nparam_min &&
3493 mmac->nparam_max == spec.nparam_max &&
3494 mmac->plus == spec.plus) {
3495 *mmac_p = mmac->next;
3496 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003497 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003498 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003499 }
3500 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003501 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003502 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003503 }
3504
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 case PP_ROTATE:
3506 if (tline->next && tline->next->type == TOK_WHITESPACE)
3507 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003508 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003510 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 return DIRECTIVE_FOUND;
3512 }
3513 t = expand_smacro(tline->next);
3514 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003515 pps.tptr = tline = t;
3516 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 tokval.t_type = TOKEN_INVALID;
3518 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003519 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 free_tlist(tline);
3521 if (!evalresult)
3522 return DIRECTIVE_FOUND;
3523 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003524 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003526 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003527 return DIRECTIVE_FOUND;
3528 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003529 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003530 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003531 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003532 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003533 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003534 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003535 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003536
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003537 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003538 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003539 rotate += mmac->nparam;
3540
3541 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003542 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003543 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003544
H. Peter Anvine2c80182005-01-15 22:15:51 +00003545 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003546 {
3547 MMacro *tmp_defining;
3548
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003549 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 do {
3551 tline = tline->next;
3552 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003553
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 if (tok_type_(tline, TOK_ID) &&
3555 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003556 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 do {
3558 tline = tline->next;
3559 } while (tok_type_(tline, TOK_WHITESPACE));
3560 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003561
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003563 pps.tptr = expand_smacro(tline);
3564 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003565 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003566 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003568 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003569 if (!evalresult)
3570 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003572 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003574 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003575 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003577 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003578 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003579 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3580 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003581 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003582 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003583 /*!
3584 *!negative-rep [on] regative %rep count
3585 *! warns about negative counts given to the \c{%rep}
3586 *! preprocessor directive.
3587 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003588 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003589 "negative `%%rep' count: %"PRId64, count);
3590 count = 0;
3591 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003592 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003593 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003595 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003596 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003598 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003599 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003600 defining->nolist = nolist;
3601 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003602 defining->mstk = istk->mstk;
3603 defining->dstk.mstk = tmp_defining;
3604 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003605 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003606 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003607 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003608
H. Peter Anvine2c80182005-01-15 22:15:51 +00003609 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003610 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003611 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003612 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003614
H. Peter Anvine2c80182005-01-15 22:15:51 +00003615 /*
3616 * Now we have a "macro" defined - although it has no name
3617 * and we won't be entering it in the hash tables - we must
3618 * push a macro-end marker for it on to istk->expansion.
3619 * After that, it will take care of propagating itself (a
3620 * macro-end marker line for a macro which is really a %rep
3621 * block will cause the macro to be re-expanded, complete
3622 * with another macro-end marker to ensure the process
3623 * continues) until the whole expansion is forcibly removed
3624 * from istk->expansion by a %exitrep.
3625 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003626 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003627 l->next = istk->expansion;
3628 l->finishes = defining;
3629 l->first = NULL;
3630 istk->expansion = l;
3631
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003632 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003633
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003634 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003635 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003636 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003637
H. Peter Anvine2c80182005-01-15 22:15:51 +00003638 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003639 /*
3640 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003641 * macro-end marker for a macro with no name. Then we set
3642 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003643 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003644 list_for_each(l, istk->expansion)
3645 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003646 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003647
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003648 if (l)
3649 l->finishes->in_progress = 1;
3650 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003651 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003652 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003653
H. Peter Anvin8b262472019-02-26 14:00:54 -08003654 case PP_DEFINE:
3655 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003656 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003657 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003658 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003659 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003660
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003661 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003662 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003663
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003664 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003665 lastp = &tline->next;
3666 nparam = parse_smacro_template(&lastp, &tmpl);
3667 tline = *lastp;
3668 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003669
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003670 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003671 macro_start = tline;
3672 if (!is_macro_id(macro_start)) {
3673 nasm_nonfatal("`%s' expects a macro identifier to alias",
3674 dname);
3675 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003677 tt = macro_start->next;
3678 macro_start->next = NULL;
3679 tline = tline->next;
3680 skip_white_(tline);
3681 if (tline && tline->type) {
3682 nasm_warn(WARN_OTHER,
3683 "trailing garbage after aliasing identifier ignored");
3684 }
3685 free_tlist(tt);
3686 tmpl.alias = true;
3687 } else {
3688 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003689 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003690 tline = expand_smacro(tline);
3691
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003692 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003693 macro_start = NULL;
3694 t = tline;
3695 while (t) {
3696 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003697 for (i = 0; i < nparam; i++) {
3698 if ((size_t)tmpl.params[i].namelen == t->len &&
3699 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3700 t->type = tok_smac_param(i);
3701 break;
3702 }
3703 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003704 }
3705 tt = t->next;
3706 t->next = macro_start;
3707 macro_start = t;
3708 t = tt;
3709 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003710 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003711
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 /*
3713 * Good. We now have a macro name, a parameter count, and a
3714 * token list (in reverse order) for an expansion. We ought
3715 * to be OK just to create an SMacro, store it, and let
3716 * free_tlist have the rest of the line (which we have
3717 * carefully re-terminated after chopping off the expansion
3718 * from the end).
3719 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003720 define_smacro(mname, casesense, macro_start, &tmpl);
3721 break;
3722 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003723
H. Peter Anvine2c80182005-01-15 22:15:51 +00003724 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003725 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003726 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003727 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003728 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003729 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003730
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003731 undef_smacro(mname, i == PP_UNDEFALIAS);
3732 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003733
H. Peter Anvin8b262472019-02-26 14:00:54 -08003734 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003735 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003736 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003737
H. Peter Anvin9e200162008-06-04 17:23:14 -07003738 last = tline;
3739 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003740 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003741
3742 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003743 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003744
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003745 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003746 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003747 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003748
3749 /*
3750 * We now have a macro name, an implicit parameter count of
3751 * zero, and a string token to use as an expansion. Create
3752 * and store an SMacro.
3753 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003754 define_smacro(mname, casesense, macro_start, NULL);
3755 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003756
H. Peter Anvin8b262472019-02-26 14:00:54 -08003757 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003758 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003759 goto done;
3760
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003761 last = tline;
3762 tline = expand_smacro(tline->next);
3763 last->next = NULL;
3764
3765 t = tline;
3766 while (tok_type_(t, TOK_WHITESPACE))
3767 t = t->next;
3768 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003769 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003770 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003771 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003772 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003773 }
3774
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003775 /*
3776 * Convert the string to a token stream. Note that smacros
3777 * are stored with the token stream reversed, so we have to
3778 * reverse the output of tokenize().
3779 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003780 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003781 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003782
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003783 /*
3784 * We now have a macro name, an implicit parameter count of
3785 * zero, and a numeric token to use as an expansion. Create
3786 * and store an SMacro.
3787 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003788 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003789 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003790 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003791
H. Peter Anvin418ca702008-05-30 10:42:30 -07003792 case PP_PATHSEARCH:
3793 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003794 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003795
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003796 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003797 goto done;
3798
H. Peter Anvin418ca702008-05-30 10:42:30 -07003799 last = tline;
3800 tline = expand_smacro(tline->next);
3801 last->next = NULL;
3802
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003803 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003804 while (tok_type_(t, TOK_WHITESPACE))
3805 t = t->next;
3806
3807 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003808 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003809 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003810 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003811 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003812 }
3813 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003814 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003815 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003816 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003817 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003818
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003819 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003820 if (!found_path)
3821 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003822 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003823
3824 /*
3825 * We now have a macro name, an implicit parameter count of
3826 * zero, and a string token to use as an expansion. Create
3827 * and store an SMacro.
3828 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003829 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003830 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003831 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003832 }
3833
H. Peter Anvine2c80182005-01-15 22:15:51 +00003834 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003835 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003836 goto done;
3837
H. Peter Anvine2c80182005-01-15 22:15:51 +00003838 last = tline;
3839 tline = expand_smacro(tline->next);
3840 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003841
H. Peter Anvine2c80182005-01-15 22:15:51 +00003842 t = tline;
3843 while (tok_type_(t, TOK_WHITESPACE))
3844 t = t->next;
3845 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003846 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003847 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003848 free_tlist(tline);
3849 free_tlist(origline);
3850 return DIRECTIVE_FOUND;
3851 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003852
H. Peter Anvin8b262472019-02-26 14:00:54 -08003853 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003854
H. Peter Anvine2c80182005-01-15 22:15:51 +00003855 /*
3856 * We now have a macro name, an implicit parameter count of
3857 * zero, and a numeric token to use as an expansion. Create
3858 * and store an SMacro.
3859 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003860 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003861 free_tlist(tline);
3862 free_tlist(origline);
3863 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003864
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003865 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003866 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003867 goto done;
3868
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003869 last = tline;
3870 tline = expand_smacro(tline->next);
3871 last->next = NULL;
3872
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003873 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003874 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003875 switch (t->type) {
3876 case TOK_WHITESPACE:
3877 break;
3878 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003879 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003880 break;
3881 case TOK_OTHER:
3882 if (!strcmp(t->text, ",")) /* permit comma separators */
3883 break;
3884 /* else fall through */
3885 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003886 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003887 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003888 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003889 }
3890 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003891
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003892 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003893 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003894 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003895 memcpy(p, t->text, t->len);
3896 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003897 }
3898 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003899
3900 /*
3901 * We now have a macro name, an implicit parameter count of
3902 * zero, and a numeric token to use as an expansion. Create
3903 * and store an SMacro.
3904 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003905 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003906 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003907 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003908 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003909 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003910
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003912 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003913 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003914 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003915
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003916 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003917 goto done;
3918
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 last = tline;
3920 tline = expand_smacro(tline->next);
3921 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003922
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003923 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003924 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003925 while (tok_type_(t, TOK_WHITESPACE))
3926 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003927
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003929 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003930 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003931 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003932 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003933 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003934
H. Peter Anvin8b262472019-02-26 14:00:54 -08003935 pps.tptr = t->next;
3936 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003938 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 if (!evalresult) {
3940 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003941 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003942 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003943 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003944 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003945 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003946 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003947 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003948
H. Peter Anvin8b262472019-02-26 14:00:54 -08003949 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3950 pps.tptr = pps.tptr->next;
3951 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003952 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003953 } else {
3954 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003955 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003956 if (!evalresult) {
3957 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003958 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003959 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003960 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003961 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003962 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003963 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003964 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003965 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003966
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003967 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003968
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003969 /* make start and count being in range */
3970 if (start < 0)
3971 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003972 if (count < 0)
3973 count = len + count + 1 - start;
3974 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003975 count = len - start;
3976 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003977 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003978
H. Peter Anvin8b262472019-02-26 14:00:54 -08003979 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003980 macro_start->len = count;
3981 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3982 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003983
H. Peter Anvine2c80182005-01-15 22:15:51 +00003984 /*
3985 * We now have a macro name, an implicit parameter count of
3986 * zero, and a numeric token to use as an expansion. Create
3987 * and store an SMacro.
3988 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003989 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003990 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003991 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003992 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003993
H. Peter Anvin8b262472019-02-26 14:00:54 -08003994 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003995 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003996 goto done;
3997
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 last = tline;
3999 tline = expand_smacro(tline->next);
4000 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004001
H. Peter Anvin8b262472019-02-26 14:00:54 -08004002 pps.tptr = tline;
4003 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004005 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004007 if (!evalresult)
4008 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004009
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004011 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004012
H. Peter Anvine2c80182005-01-15 22:15:51 +00004013 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004014 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004015 free_tlist(origline);
4016 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004017 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004018
H. Peter Anvin8b262472019-02-26 14:00:54 -08004019 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004020
H. Peter Anvine2c80182005-01-15 22:15:51 +00004021 /*
4022 * We now have a macro name, an implicit parameter count of
4023 * zero, and a numeric token to use as an expansion. Create
4024 * and store an SMacro.
4025 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004026 define_smacro(mname, casesense, macro_start, NULL);
4027 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004028
H. Peter Anvind2354082019-08-27 16:38:48 -07004029 case PP_ALIASES:
4030 tline = tline->next;
4031 tline = expand_smacro(tline);
4032 do_aliases = pp_get_boolean_option(tline, do_aliases);
4033 break;
4034
H. Peter Anvine2c80182005-01-15 22:15:51 +00004035 case PP_LINE:
4036 /*
4037 * Syntax is `%line nnn[+mmm] [filename]'
4038 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004039 if (unlikely(pp_noline))
4040 goto done;
4041
H. Peter Anvine2c80182005-01-15 22:15:51 +00004042 tline = tline->next;
4043 skip_white_(tline);
4044 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004045 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004046 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004047 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004048 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004049 m = 1;
4050 tline = tline->next;
4051 if (tok_is_(tline, "+")) {
4052 tline = tline->next;
4053 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004054 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004055 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004056 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004057 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004058 tline = tline->next;
4059 }
4060 skip_white_(tline);
4061 src_set_linnum(k);
4062 istk->lineinc = m;
4063 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004064 char *fname = detoken(tline, false);
4065 src_set_fname(fname);
4066 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004067 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004068 break;
4069 }
4070
4071done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004072 free_tlist(origline);
4073 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004074}
4075
4076/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004077 * Ensure that a macro parameter contains a condition code and
4078 * nothing else. Return the condition code index if so, or -1
4079 * otherwise.
4080 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004081static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004082{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004083 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004084
H. Peter Anvin25a99342007-09-22 17:45:45 -07004085 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004086 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004087
H. Peter Anvineba20a72002-04-30 20:53:55 +00004088 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03004089 if (!t)
4090 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004091 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004092 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004093 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004094 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004095 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004096 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004097
Cyrill Gorcunov19456392012-05-02 00:18:56 +04004098 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004099}
4100
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004101/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004102 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004103 * pasting, if @handle_explicit passed then explicit pasting
4104 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004105 * The @m array can contain a series of token types which are
4106 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004107 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004108static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004109 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004110{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004111 Token *tok, *next, **prev_next, **prev_nonspace;
4112 bool pasted = false;
4113 char *buf, *p;
4114 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004115
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004116 /*
4117 * The last token before pasting. We need it
4118 * to be able to connect new handled tokens.
4119 * In other words if there were a tokens stream
4120 *
4121 * A -> B -> C -> D
4122 *
4123 * and we've joined tokens B and C, the resulting
4124 * stream should be
4125 *
4126 * A -> BC -> D
4127 */
4128 tok = *head;
4129 prev_next = NULL;
4130
4131 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
4132 prev_nonspace = head;
4133 else
4134 prev_nonspace = NULL;
4135
4136 while (tok && (next = tok->next)) {
4137
4138 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004139 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004140 /* Zap redundant whitespaces */
4141 while (tok_type_(next, TOK_WHITESPACE))
4142 next = delete_Token(next);
4143 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004144 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004145
4146 case TOK_PASTE:
4147 /* Explicit pasting */
4148 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004149 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004150 next = delete_Token(tok);
4151
4152 while (tok_type_(next, TOK_WHITESPACE))
4153 next = delete_Token(next);
4154
4155 if (!pasted)
4156 pasted = true;
4157
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004158 /* Left pasting token is start of line */
4159 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004160 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004161
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004162 /*
4163 * No ending token, this might happen in two
4164 * cases
4165 *
4166 * 1) There indeed no right token at all
4167 * 2) There is a bare "%define ID" statement,
4168 * and @ID does expand to whitespace.
4169 *
4170 * So technically we need to do a grammar analysis
4171 * in another stage of parsing, but for now lets don't
4172 * change the behaviour people used to. Simply allow
4173 * whitespace after paste token.
4174 */
4175 if (!next) {
4176 /*
4177 * Zap ending space tokens and that's all.
4178 */
4179 tok = (*prev_nonspace)->next;
4180 while (tok_type_(tok, TOK_WHITESPACE))
4181 tok = delete_Token(tok);
4182 tok = *prev_nonspace;
4183 tok->next = NULL;
4184 break;
4185 }
4186
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004187 tok = *prev_nonspace;
4188 while (tok_type_(tok, TOK_WHITESPACE))
4189 tok = delete_Token(tok);
4190 len = strlen(tok->text);
4191 len += strlen(next->text);
4192
4193 p = buf = nasm_malloc(len + 1);
4194 strcpy(p, tok->text);
4195 p = strchr(p, '\0');
4196 strcpy(p, next->text);
4197
4198 delete_Token(tok);
4199
4200 tok = tokenize(buf);
4201 nasm_free(buf);
4202
4203 *prev_nonspace = tok;
4204 while (tok && tok->next)
4205 tok = tok->next;
4206
4207 tok->next = delete_Token(next);
4208
4209 /* Restart from pasted tokens head */
4210 tok = *prev_nonspace;
4211 break;
4212
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004213 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004214 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004215 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004216 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4217 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004218
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004219 len = 0;
4220 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4221 len += strlen(next->text);
4222 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004223 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004224
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004225 /* No match or no text to process */
4226 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004227 break;
4228
4229 len += strlen(tok->text);
4230 p = buf = nasm_malloc(len + 1);
4231
Adam Majer1a069432017-07-25 11:12:35 +02004232 strcpy(p, tok->text);
4233 p = strchr(p, '\0');
4234 tok = delete_Token(tok);
4235
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004236 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004237 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4238 strcpy(p, tok->text);
4239 p = strchr(p, '\0');
4240 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004241 tok = delete_Token(tok);
4242 }
4243
4244 tok = tokenize(buf);
4245 nasm_free(buf);
4246
4247 if (prev_next)
4248 *prev_next = tok;
4249 else
4250 *head = tok;
4251
4252 /*
4253 * Connect pasted into original stream,
4254 * ie A -> new-tokens -> B
4255 */
4256 while (tok && tok->next)
4257 tok = tok->next;
4258 tok->next = next;
4259
4260 if (!pasted)
4261 pasted = true;
4262
4263 /* Restart from pasted tokens head */
4264 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004265 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004266
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004267 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004268 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004269
4270 prev_next = &tok->next;
4271
4272 if (tok->next &&
4273 !tok_type_(tok->next, TOK_WHITESPACE) &&
4274 !tok_type_(tok->next, TOK_PASTE))
4275 prev_nonspace = prev_next;
4276
4277 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004278 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004279
4280 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004281}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004282
4283/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004284 * Computes the proper rotation of mmacro parameters
4285 */
4286static int mmac_rotate(const MMacro *mac, unsigned int n)
4287{
4288 if (--n < mac->nparam)
4289 n = (n + mac->rotate) % mac->nparam;
4290
4291 return n+1;
4292}
4293
4294/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004295 * expands to a list of tokens from %{x:y}
4296 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004297static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004298{
4299 Token *t = tline, **tt, *tm, *head;
4300 char *pos;
4301 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004302
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004303 pos = strchr(tline->text, ':');
4304 nasm_assert(pos);
4305
4306 lst = atoi(pos + 1);
4307 fst = atoi(tline->text + 1);
4308
4309 /*
4310 * only macros params are accounted so
4311 * if someone passes %0 -- we reject such
4312 * value(s)
4313 */
4314 if (lst == 0 || fst == 0)
4315 goto err;
4316
4317 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004318 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4319 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004320 goto err;
4321
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004322 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4323 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004324
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004325 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004326 fst--, lst--;
4327
4328 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004329 * It will be at least one token. Note we
4330 * need to scan params until separator, otherwise
4331 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004332 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004333 j = (fst + mac->rotate) % mac->nparam;
4334 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004335 if (!tm)
4336 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004337 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004338 tt = &head->next, tm = tm->next;
4339 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004340 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004341 *tt = t, tt = &t->next, tm = tm->next;
4342 }
4343
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004344 if (fst < lst) {
4345 for (i = fst + 1; i <= lst; i++) {
4346 t = new_Token(NULL, TOK_OTHER, ",", 0);
4347 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004348 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004349 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004350 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004351 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004352 *tt = t, tt = &t->next, tm = tm->next;
4353 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004354 }
4355 } else {
4356 for (i = fst - 1; i >= lst; i--) {
4357 t = new_Token(NULL, TOK_OTHER, ",", 0);
4358 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004359 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004360 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004361 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004362 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004363 *tt = t, tt = &t->next, tm = tm->next;
4364 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004365 }
4366 }
4367
4368 *last = tt;
4369 return head;
4370
4371err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004372 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004373 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004374 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004375}
4376
H. Peter Anvin76690a12002-04-30 20:52:49 +00004377/*
4378 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004379 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004380 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004381 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004382static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004383{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004384 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004385 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004386 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004387
4388 tail = &thead;
4389 thead = NULL;
4390
H. Peter Anvine2c80182005-01-15 22:15:51 +00004391 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004392 bool change;
4393 Token *t = tline;
4394 char *text = t->text;
4395 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004396
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004397 tline = tline->next;
4398 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004399
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004400 switch (type) {
4401 case TOK_PREPROC_ID:
4402 {
4403 Token *tt = NULL;
4404
4405 change = false;
4406
4407 if (!text || !text[0])
4408 break;
4409 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4410 ((text[1] == '+' || text[1] == '-') && text[2])))
4411 break;
4412
4413 change = true;
4414
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004415 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004416 nasm_nonfatal("`%s': not in a macro call", text);
4417 text = NULL;
4418 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004419 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004420
4421 if (strchr(text, ':')) {
4422 /*
4423 * seems we have a parameters range here
4424 */
4425 Token *head, **last;
4426 head = expand_mmac_params_range(mac, t, &last);
4427 if (head) {
4428 *tail = head;
4429 *last = tline;
4430 text = NULL;
4431 }
4432 break;
4433 }
4434
4435 switch (text[1]) {
4436 /*
4437 * We have to make a substitution of one of the
4438 * forms %1, %-1, %+1, %%foo, %0, %00.
4439 */
4440 case '0':
4441 if (!text[2]) {
4442 type = TOK_NUMBER;
4443 text = nasm_asprintf("%d", mac->nparam);
4444 break;
4445 }
4446 if (text[2] != '0' || text[3])
4447 goto invalid;
4448 /* a possible captured label == mac->params[0] */
4449 /* fall through */
4450 default:
4451 {
4452 unsigned long n;
4453 char *ep;
4454
4455 n = strtoul(text + 1, &ep, 10);
4456 if (unlikely(*ep))
4457 goto invalid;
4458
4459 if (n <= mac->nparam) {
4460 n = mmac_rotate(mac, n);
4461 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4462 }
4463 text = NULL;
4464 break;
4465 }
4466 case '%':
4467 type = TOK_ID;
4468 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4469 break;
4470 case '-':
4471 case '+':
4472 {
4473 int cc;
4474 unsigned long n;
4475 char *ep;
4476
4477 text = NULL;
4478
4479 n = strtoul(t->text + 2, &ep, 10);
4480 if (unlikely(*ep))
4481 goto invalid;
4482
4483 if (n && n < mac->nparam) {
4484 n = mmac_rotate(mac, n);
4485 tt = mac->params[n];
4486 }
4487 cc = find_cc(tt);
4488 if (cc == -1) {
4489 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvind2354082019-08-27 16:38:48 -07004490 t->text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004491 text = NULL;
4492 break;
4493 }
4494
4495 type = TOK_ID;
4496 if (text[1] == '-') {
4497 int ncc = inverse_ccs[cc];
4498 if (unlikely(ncc == -1)) {
4499 nasm_nonfatal("condition code `%s' is not invertible",
4500 conditions[cc]);
4501 break;
4502 }
4503 cc = ncc;
4504 }
4505 text = nasm_strdup(conditions[cc]);
4506 break;
4507 }
4508
4509 invalid:
4510 nasm_nonfatal("invalid macro parameter: `%s'", text);
4511 text = NULL;
4512 break;
4513 }
4514 break;
4515 }
4516
4517 case TOK_PREPROC_Q:
4518 if (mac) {
4519 type = TOK_ID;
4520 text = nasm_strdup(mac->iname);
4521 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004522 } else {
4523 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004524 }
4525 break;
4526
4527 case TOK_PREPROC_QQ:
4528 if (mac) {
4529 type = TOK_ID;
4530 text = nasm_strdup(mac->name);
4531 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004532 } else {
4533 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004534 }
4535 break;
4536
4537 case TOK_INDIRECT:
4538 {
4539 Token *tt;
4540
4541 tt = tokenize(t->text);
4542 tt = expand_mmac_params(tt);
4543 tt = expand_smacro(tt);
4544 /* Why dup_tlist() here? We should own tt... */
4545 dup_tlist(tt, &tail);
4546 text = NULL;
4547 change = true;
4548 break;
4549 }
4550
4551 default:
4552 change = false;
4553 break;
4554 }
4555
4556 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004557 if (!text) {
4558 delete_Token(t);
4559 } else {
4560 *tail = t;
4561 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004562 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004563 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004564 t->type = type;
4565 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004566 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004567 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004568 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004569 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004570 tail = &t->next;
4571 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004572 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004573
H. Peter Anvineba20a72002-04-30 20:53:55 +00004574 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004575
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004576 if (changed) {
4577 const struct tokseq_match t[] = {
4578 {
4579 PP_CONCAT_MASK(TOK_ID) |
4580 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4581 PP_CONCAT_MASK(TOK_ID) |
4582 PP_CONCAT_MASK(TOK_NUMBER) |
4583 PP_CONCAT_MASK(TOK_FLOAT) |
4584 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4585 },
4586 {
4587 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4588 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4589 }
4590 };
4591 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4592 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004593
H. Peter Anvin76690a12002-04-30 20:52:49 +00004594 return thead;
4595}
4596
H. Peter Anvin322bee02019-08-10 01:38:06 -07004597static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004598
H. Peter Anvin76690a12002-04-30 20:52:49 +00004599/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004600 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004601 * a macro at all, it is simply copied to the output and the pointer
4602 * advanced. tpp should be a pointer to a pointer (usually the next
4603 * pointer of the previous token) to the first token. **tpp is updated
4604 * to point to the last token of the expansion, and *tpp updated to
4605 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004606 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004607 * If the expansion is empty, *tpp will be unchanged but **tpp will
4608 * be advanced past the macro call.
4609 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004610 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004611 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004612static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004613{
4614 Token **params = NULL;
4615 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004616 Token *tline = **tpp;
4617 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004618 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004619 int i;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004620 Token *t, *tup, *ttail;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004621 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004622 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004623
4624 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004625 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004626
4627 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004628
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004629 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004630 smacro_deadman.levels--;
4631
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004632 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004633 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004634 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004635 smacro_deadman.triggered = true;
4636 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004637 goto not_a_macro;
4638 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004639 head = (SMacro *)hash_findix(&smacros, mname);
4640 } else if (tline->type == TOK_PREPROC_ID) {
4641 Context *ctx = get_ctx(mname, &mname);
4642 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4643 } else {
4644 goto not_a_macro;
4645 }
4646
4647 /*
4648 * We've hit an identifier of some sort. First check whether the
4649 * identifier is a single-line macro at all, then think about
4650 * checking for parameters if necessary.
4651 */
4652 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004653 if (unlikely(m->alias && !do_aliases))
4654 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004655 if (!mstrcmp(m->name, mname, m->casesense))
4656 break;
4657 }
4658
4659 if (!m) {
4660 goto not_a_macro;
4661 }
4662
4663 /* Parse parameters, if applicable */
4664
4665 params = NULL;
4666 nparam = 0;
4667
4668 if (m->nparam == 0) {
4669 /*
4670 * Simple case: the macro is parameterless.
4671 * Nothing to parse; the expansion code will
4672 * drop the macro name token.
4673 */
4674 } else {
4675 /*
4676 * Complicated case: at least one macro with this name
4677 * exists and takes parameters. We must find the
4678 * parameters in the call, count them, find the SMacro
4679 * that corresponds to that form of the macro call, and
4680 * substitute for the parameters when we expand. What a
4681 * pain.
4682 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004683 Token *t;
4684 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004685
4686 tline = tline->next;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004687 skip_white_(tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004688 if (!tok_is_(tline, "(")) {
4689 /*
4690 * This macro wasn't called with parameters: ignore
4691 * the call. (Behaviour borrowed from gnu cpp.)
4692 */
4693 goto not_a_macro;
4694 }
4695
4696 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004697 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004698 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004699 t = tline; /* tline points to leading ( */
4700
4701 while (paren) {
4702 t = t->next;
4703
4704 if (!t) {
4705 nasm_nonfatal("macro call expects terminating `)'");
4706 goto not_a_macro;
4707 }
4708
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004709 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004710 continue;
4711
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004712 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004713 case ',':
4714 if (!brackets)
4715 nparam++;
4716 break;
4717
4718 case '{':
4719 brackets++;
4720 break;
4721
4722 case '}':
4723 if (brackets > 0)
4724 brackets--;
4725 break;
4726
4727 case '(':
4728 if (!brackets)
4729 paren++;
4730 break;
4731
4732 case ')':
4733 if (!brackets)
4734 paren--;
4735 break;
4736
4737 default:
4738 break; /* Normal token */
4739 }
4740 }
4741
4742 /*
4743 * Look for a macro matching in both name and parameter count.
4744 * We already know any matches cannot be anywhere before the
4745 * current position of "m", so there is no reason to
4746 * backtrack.
4747 */
4748 while (1) {
4749 if (!m) {
4750 /*!
4751 *!macro-params-single [on] single-line macro calls with wrong parameter count
4752 *! warns about \i{single-line macros} being invoked
4753 *! with the wrong number of parameters.
4754 */
4755 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4756 "single-line macro `%s' exists, "
4757 "but not taking %d parameter%s",
4758 mname, nparam, (nparam == 1) ? "" : "s");
4759 goto not_a_macro;
4760 }
4761
4762 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004763 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004764 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004765 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004766 break; /* Also good */
4767 }
4768 m = m->next;
4769 }
4770 }
4771
4772 if (m->in_progress)
4773 goto not_a_macro;
4774
4775 /* Expand the macro */
4776 m->in_progress = true;
4777
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004778 if (nparam) {
4779 /* Extract parameters */
4780 Token **phead, **pep;
4781 int white = 0;
4782 int brackets = 0;
4783 int paren;
4784 bool bracketed = false;
4785 bool bad_bracket = false;
4786 enum sparmflags flags;
4787
4788 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004789 paren = 1;
4790 nasm_newn(params, nparam);
4791 i = 0;
4792 flags = m->params[i].flags;
4793 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004794 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004795
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004796 while (paren) {
4797 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004798 char ch;
4799
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004800 tline = tline->next;
4801
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004802 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004803 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004804
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004805 ch = 0;
4806 skip = false;
4807
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004808
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004809 switch (tline->type) {
4810 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004811 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004812 ch = tline->text[0];
4813 break;
4814
4815 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004816 if (!(flags & SPARM_NOSTRIP)) {
4817 if (brackets || *phead)
4818 white++; /* Keep interior whitespace */
4819 skip = true;
4820 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004821 break;
4822
4823 default:
4824 break;
4825 }
4826
4827 switch (ch) {
4828 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004829 if (!brackets && !(flags & SPARM_GREEDY)) {
4830 i++;
4831 nasm_assert(i < nparam);
4832 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004833 *pep = NULL;
4834 bracketed = false;
4835 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004836 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004837 }
4838 break;
4839
4840 case '{':
4841 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004842 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4843 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004844 }
4845 brackets++;
4846 break;
4847
4848 case '}':
4849 if (brackets > 0) {
4850 if (!--brackets)
4851 skip = bracketed;
4852 }
4853 break;
4854
4855 case '(':
4856 if (!brackets)
4857 paren++;
4858 break;
4859
4860 case ')':
4861 if (!brackets) {
4862 paren--;
4863 if (!paren) {
4864 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004865 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004866 }
4867 }
4868 break;
4869
4870 default:
4871 break; /* Normal token */
4872 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004873
4874 if (!skip) {
4875 Token *t;
4876
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004877 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004878
4879 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004880 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004881 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004882 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004883 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004884 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004885 pep = &t->next;
4886 white = 0;
4887 }
4888 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004889
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004890 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004891 * Possible further processing of parameters. Note that the
4892 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004893 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004894 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004895 enum sparmflags flags = m->params[i].flags;
4896
4897 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004898 /* Evaluate this parameter as a number */
4899 struct ppscan pps;
4900 struct tokenval tokval;
4901 expr *evalresult;
4902 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004903
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004904 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4905 pps.ntokens = -1;
4906 tokval.t_type = TOKEN_INVALID;
4907 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004908
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004909 free_tlist(eval_param);
4910 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004911
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004912 if (!evalresult) {
4913 /* Nothing meaningful to do */
4914 } else if (tokval.t_type) {
4915 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4916 } else if (!is_simple(evalresult)) {
4917 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4918 } else {
4919 params[i] = make_tok_num(reloc_value(evalresult));
4920 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004921 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004922
4923 if (flags & SPARM_STR) {
4924 /* Convert expansion to a quoted string */
4925 char *arg;
4926 Token *qs;
4927
4928 qs = expand_smacro_noreset(params[i]);
4929 arg = detoken(qs, false);
4930 free_tlist(qs);
4931 params[i] = make_tok_qstr(arg);
4932 nasm_free(arg);
4933 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004934 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004935 }
4936
4937 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004938 tline = tline->next; /* Remove the macro call from the input */
4939 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004940
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004941 /* Note: we own the expansion this returns. */
4942 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004943
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004944 tup = NULL;
4945 ttail = NULL; /* Pointer to the last token of the expansion */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004946 cond_comma = false;
4947
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004948 while (t) {
4949 enum pp_token_type type = t->type;
4950 Token *tnext = t->next;
4951 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004952
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004953 switch (type) {
4954 case TOK_PREPROC_Q:
4955 case TOK_PREPROC_QQ:
4956 t->type = TOK_ID;
4957 nasm_free(t->text);
4958 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4959 t->len = nasm_last_string_len();
4960 t->next = tline;
4961 break;
4962
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004963 case TOK_COND_COMMA:
4964 delete_Token(t);
4965 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4966 break;
4967
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004968 case TOK_ID:
4969 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004970 /*
4971 * Chain this into the target line *before* expanding,
4972 * that way we pick up any arguments to the new macro call,
4973 * if applicable.
4974 */
4975 t->next = tline;
4976 tp = &t;
4977 expand_one_smacro(&tp);
4978 if (t == tline)
4979 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004980 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004981
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004982 default:
4983 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004984 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004985 nasm_assert(!tup && param < nparam);
4986 delete_Token(t);
4987 t = NULL;
4988 tup = tnext;
4989 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004990 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004991 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004992 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004993 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004994 }
4995
4996 if (t) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004997 if (t->type != TOK_WHITESPACE)
4998 cond_comma = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004999 tline = t;
5000 if (!ttail)
5001 ttail = t;
5002 }
5003
5004 if (tnext) {
5005 t = tnext;
5006 } else {
5007 t = tup;
5008 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005009 }
5010 }
5011
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005012 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005013 if (ttail)
5014 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005015
5016 m->in_progress = false;
5017
5018 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005019 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005020 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005021
5022 /*
5023 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005024 * and then advance to the next input token. Note that this is
5025 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005026 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005027not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005028 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005029 m = NULL;
5030done:
5031 smacro_deadman.levels++;
5032 if (unlikely(params))
5033 free_tlist_array(params, nparam);
5034 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005035}
5036
5037/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005038 * Expand all single-line macro calls made in the given line.
5039 * Return the expanded version of the line. The original is deemed
5040 * to be destroyed in the process. (In reality we'll just move
5041 * Tokens from input to output a lot of the time, rather than
5042 * actually bothering to destroy and replicate.)
5043 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005044static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005045{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005046 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005047 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5048 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005049 return expand_smacro_noreset(tline);
5050}
5051
5052static Token *expand_smacro_noreset(Token * tline)
5053{
5054 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005055 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005056 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005057
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005058 /*
5059 * Trick: we should avoid changing the start token pointer since it can
5060 * be contained in "next" field of other token. Because of this
5061 * we allocate a copy of first token and work with it; at the end of
5062 * routine we copy it back
5063 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005064 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04005065 tline = new_Token(org_tline->next, org_tline->type,
5066 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005067 nasm_free(org_tline->text);
5068 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005069 }
5070
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005071
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005072 /*
5073 * Pretend that we always end up doing expansion on the first pass;
5074 * that way %+ get processed. However, if we process %+ before the
5075 * first pass, we end up with things like MACRO %+ TAIL trying to
5076 * look up the macro "MACROTAIL", which we don't want.
5077 */
5078 expanded = true;
5079 thead = tline;
5080 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005081 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005082 {
5083 PP_CONCAT_MASK(TOK_ID) |
5084 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5085 PP_CONCAT_MASK(TOK_ID) |
5086 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5087 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5088 }
5089 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005090
5091 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005092 while ((t = *tail)) /* main token loop */
5093 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005094
5095 if (!expanded) {
5096 tline = thead;
5097 break; /* Done! */
5098 }
5099
5100 /*
5101 * Now scan the entire line and look for successive TOK_IDs
5102 * that resulted after expansion (they can't be produced by
5103 * tokenize()). The successive TOK_IDs should be concatenated.
5104 * Also we look for %+ tokens and concatenate the tokens
5105 * before and after them (without white spaces in between).
5106 */
H. Peter Anvind2354082019-08-27 16:38:48 -07005107 if (!paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true)) {
5108 tline = thead;
5109 break;
5110 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005111 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005112 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005113
H. Peter Anvine2c80182005-01-15 22:15:51 +00005114 if (org_tline) {
5115 if (thead) {
5116 *org_tline = *thead;
5117 /* since we just gave text to org_line, don't free it */
5118 thead->text = NULL;
5119 delete_Token(thead);
5120 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005121 /*
5122 * The expression expanded to empty line;
5123 * we can't return NULL because of the "trick" above.
5124 * Just set the line to a single WHITESPACE token.
5125 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 memset(org_tline, 0, sizeof(*org_tline));
5127 org_tline->text = NULL;
5128 org_tline->type = TOK_WHITESPACE;
5129 }
5130 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005131 }
5132
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005133 return thead;
5134}
5135
5136/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005137 * Similar to expand_smacro but used exclusively with macro identifiers
5138 * right before they are fetched in. The reason is that there can be
5139 * identifiers consisting of several subparts. We consider that if there
5140 * are more than one element forming the name, user wants a expansion,
5141 * otherwise it will be left as-is. Example:
5142 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005143 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005144 *
5145 * the identifier %$abc will be left as-is so that the handler for %define
5146 * will suck it and define the corresponding value. Other case:
5147 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005148 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005149 *
5150 * In this case user wants name to be expanded *before* %define starts
5151 * working, so we'll expand %$abc into something (if it has a value;
5152 * otherwise it will be left as-is) then concatenate all successive
5153 * PP_IDs into one.
5154 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005155static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005156{
5157 Token *cur, *oldnext = NULL;
5158
H. Peter Anvin734b1882002-04-30 21:01:08 +00005159 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005160 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005161
5162 cur = tline;
5163 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005164 (cur->next->type == TOK_ID ||
5165 cur->next->type == TOK_PREPROC_ID
5166 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005167 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005168
5169 /* If identifier consists of just one token, don't expand */
5170 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005171 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005172
H. Peter Anvine2c80182005-01-15 22:15:51 +00005173 if (cur) {
5174 oldnext = cur->next; /* Detach the tail past identifier */
5175 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005176 }
5177
H. Peter Anvin734b1882002-04-30 21:01:08 +00005178 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005179
H. Peter Anvine2c80182005-01-15 22:15:51 +00005180 if (cur) {
5181 /* expand_smacro possibly changhed tline; re-scan for EOL */
5182 cur = tline;
5183 while (cur && cur->next)
5184 cur = cur->next;
5185 if (cur)
5186 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005187 }
5188
5189 return tline;
5190}
5191
5192/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005193 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005194 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005195 * to check for an initial label - that's taken care of in
5196 * expand_mmacro - but must check numbers of parameters. Guaranteed
5197 * to be called with tline->type == TOK_ID, so the putative macro
5198 * name is easy to find.
5199 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005200static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005201{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005202 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005203 Token **params;
5204 int nparam;
5205
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005206 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005207
5208 /*
5209 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005210 * name which isn't already excluded by macro cycle removal.
5211 * (The cycle removal test here helps optimize the case of wrapping
5212 * instructions, and is cheap to do here.)
5213 *
5214 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005215 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005216 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005217 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005218 list_for_each(m, head) {
5219 if (!mstrcmp(m->name, tline->text, m->casesense) &&
5220 (m->in_progress != 1 || m->max_depth > 0))
5221 break; /* Found something that needs consideration */
5222 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005223 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005224 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005225
5226 /*
5227 * OK, we have a potential macro. Count and demarcate the
5228 * parameters.
5229 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005230 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005231
5232 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005233 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005234 * structure that handles this number.
5235 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005236 while (m) {
5237 if (m->nparam_min <= nparam
5238 && (m->plus || nparam <= m->nparam_max)) {
5239 /*
5240 * This one is right. Just check if cycle removal
5241 * prohibits us using it before we actually celebrate...
5242 */
5243 if (m->in_progress > m->max_depth) {
5244 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005245 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005246 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005247 }
5248 nasm_free(params);
5249 return NULL;
5250 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005251 /*
5252 * It's right, and we can use it. Add its default
5253 * parameters to the end of our list if necessary.
5254 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005255 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005256 int newnparam = m->nparam_min + m->ndefs;
5257 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5258 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5259 (newnparam - nparam) * sizeof(*params));
5260 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005261 }
5262 /*
5263 * If we've gone over the maximum parameter count (and
5264 * we're in Plus mode), ignore parameters beyond
5265 * nparam_max.
5266 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005267 if (m->plus && nparam > m->nparam_max)
5268 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005269
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005270 /*
5271 * If nparam was adjusted above, make sure the list is still
5272 * NULL-terminated.
5273 */
5274 params[nparam+1] = NULL;
5275
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005276 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005277 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005278 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005279 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005280 }
5281 /*
5282 * This one wasn't right: look for the next one with the
5283 * same name.
5284 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005285 list_for_each(m, m->next)
5286 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005287 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005288 }
5289
5290 /*
5291 * After all that, we didn't find one with the right number of
5292 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005293 *!
5294 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5295 *! warns about \i{multi-line macros} being invoked
5296 *! with the wrong number of parameters. See \k{mlmacover} for an
5297 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005298 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005299 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5300 "multi-line macro `%s' exists, but not taking %d parameter%s",
5301 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005302 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005303 return NULL;
5304}
5305
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005306
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005307#if 0
5308
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005309/*
5310 * Save MMacro invocation specific fields in
5311 * preparation for a recursive macro expansion
5312 */
5313static void push_mmacro(MMacro *m)
5314{
5315 MMacroInvocation *i;
5316
5317 i = nasm_malloc(sizeof(MMacroInvocation));
5318 i->prev = m->prev;
5319 i->params = m->params;
5320 i->iline = m->iline;
5321 i->nparam = m->nparam;
5322 i->rotate = m->rotate;
5323 i->paramlen = m->paramlen;
5324 i->unique = m->unique;
5325 i->condcnt = m->condcnt;
5326 m->prev = i;
5327}
5328
5329
5330/*
5331 * Restore MMacro invocation specific fields that were
5332 * saved during a previous recursive macro expansion
5333 */
5334static void pop_mmacro(MMacro *m)
5335{
5336 MMacroInvocation *i;
5337
5338 if (m->prev) {
5339 i = m->prev;
5340 m->prev = i->prev;
5341 m->params = i->params;
5342 m->iline = i->iline;
5343 m->nparam = i->nparam;
5344 m->rotate = i->rotate;
5345 m->paramlen = i->paramlen;
5346 m->unique = i->unique;
5347 m->condcnt = i->condcnt;
5348 nasm_free(i);
5349 }
5350}
5351
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005352#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005353
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005354/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005355 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005356 */
5357static void list_mmacro_call(const MMacro *m)
5358{
5359 const char prefix[] = " ;;; [macro] ";
5360 size_t namelen, size;
5361 char *buf, *p;
5362 unsigned int i;
5363 const Token *t;
5364
5365 namelen = strlen(m->iname);
5366 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5367
5368 for (i = 1; i <= m->nparam; i++) {
5369 int j = 0;
5370 size += 3; /* Braces and space/comma */
5371 list_for_each(t, m->params[i]) {
5372 if (j++ >= m->paramlen[i])
5373 break;
5374 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5375 }
5376 }
5377
5378 buf = p = nasm_malloc(size);
5379 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5380 p = mempcpy(p, m->iname, namelen);
5381 *p++ = ' ';
5382
5383 for (i = 1; i <= m->nparam; i++) {
5384 int j = 0;
5385 *p++ = '{';
5386 list_for_each(t, m->params[i]) {
5387 if (j++ >= m->paramlen[i])
5388 break;
5389 if (!t->text) {
5390 if (t->type == TOK_WHITESPACE)
5391 *p++ = ' ';
5392 } else {
5393 p = mempcpy(p, t->text, t->len);
5394 }
5395 }
5396 *p++ = '}';
5397 *p++ = ',';
5398 }
5399
5400 *--p = '\0'; /* Replace last delimeter with null */
5401 lfmt->line(LIST_MACRO, -1, buf);
5402 nasm_free(buf);
5403}
5404
5405/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005406 * Expand the multi-line macro call made by the given line, if
5407 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005408 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005409 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005410static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005411{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005412 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005413 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005414 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005415 Token **params, *t, *tt;
5416 MMacro *m;
5417 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005418 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005419 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005420 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005421
5422 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005423 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005424 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005425 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005426 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005427 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005428 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005429 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005430 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005431 Token *last;
5432 /*
5433 * We have an id which isn't a macro call. We'll assume
5434 * it might be a label; we'll also check to see if a
5435 * colon follows it. Then, if there's another id after
5436 * that lot, we'll check it again for macro-hood.
5437 */
5438 label = last = t;
5439 t = t->next;
5440 if (tok_type_(t, TOK_WHITESPACE))
5441 last = t, t = t->next;
5442 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005443 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005444 last = t, t = t->next;
5445 if (tok_type_(t, TOK_WHITESPACE))
5446 last = t, t = t->next;
5447 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005448 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005449 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005450 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005451 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005452 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005453 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005454
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005455 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5456 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5457 if (!mmacro_deadman.triggered) {
5458 nasm_nonfatal("interminable multiline macro recursion");
5459 mmacro_deadman.triggered = true;
5460 }
5461 return 0;
5462 }
5463
5464 mmacro_deadman.total++;
5465 mmacro_deadman.levels++;
5466
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005467 /*
5468 * Fix up the parameters: this involves stripping leading and
5469 * trailing whitespace, then stripping braces if they are
5470 * present.
5471 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005472 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005473
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005474 nasm_assert(params[nparam+1] == NULL);
5475
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005476 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005477 int brace = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005478 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005479
H. Peter Anvine2c80182005-01-15 22:15:51 +00005480 skip_white_(t);
5481 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005482 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005483 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005484 while (t) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005485 if (comma) {
5486 /* Check if we hit a comma that ends this argument */
5487 if (tok_is_(t, ","))
5488 break;
5489 else if (t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
5490 break;
5491 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005492 if (brace && t->type == TOK_OTHER) {
5493 if (t->text[0] == '{')
5494 brace++; /* ... or a nested opening brace */
5495 else if (t->text[0] == '}')
5496 if (!--brace)
5497 break; /* ... or a brace */
5498 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005499 t = t->next;
5500 paramlen[i]++;
5501 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005502 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005503 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005504 }
5505
5506 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005507 * OK, we have a MMacro structure together with a set of
5508 * parameters. We must now go through the expansion and push
5509 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005510 * parameter tokens and macro-local tokens doesn't get done
5511 * until the single-line macro substitution process; this is
5512 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005513 * later through %rotate and give the right semantics for
5514 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005515 *
5516 * First, push an end marker on to istk->expansion, mark this
5517 * macro as in progress, and set up its invocation-specific
5518 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005519 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005520 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005521 ll->next = istk->expansion;
5522 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005523 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005524
5525 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005526 * Save the previous MMacro expansion in the case of
5527 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005528 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005529#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005530 if (m->max_depth && m->in_progress)
5531 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005532#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005533
5534 m->in_progress ++;
5535 m->params = params;
5536 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005537 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005538 m->nparam = nparam;
5539 m->rotate = 0;
5540 m->paramlen = paramlen;
5541 m->unique = unique++;
5542 m->lineno = 0;
5543 m->condcnt = 0;
5544
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005545 m->mstk = istk->mstk;
5546 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005547
5548 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005549 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005550 ll->next = istk->expansion;
5551 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005552 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005553 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005554
5555 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005556 * If we had a label, and this macro definition does not include
5557 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005558 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005559 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005560 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005561 /*
5562 * We had a label. If this macro contains an %00 parameter,
5563 * save the value as a special parameter (which is what it
5564 * is), otherwise push it as the first line of the macro
5565 * expansion.
5566 */
5567 if (m->capture_label) {
5568 params[0] = dup_Token(NULL, label);
5569 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005570 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005571 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005572 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005573 ll->finishes = NULL;
5574 ll->next = istk->expansion;
5575 istk->expansion = ll;
5576 ll->first = startline;
5577 if (!dont_prepend) {
5578 while (label->next)
5579 label = label->next;
5580 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005581 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005582 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005583 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005584
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005585 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005586
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005587 if (list_option('m') && !m->nolist)
5588 list_mmacro_call(m);
5589
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005590 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005591}
5592
H. Peter Anvin130736c2016-02-17 20:27:41 -08005593/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005594 * This function decides if an error message should be suppressed.
5595 * It will never be called with a severity level of ERR_FATAL or
5596 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005597 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005598static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005599{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005600 /*
5601 * If we're in a dead branch of IF or something like it, ignore the error.
5602 * However, because %else etc are evaluated in the state context
5603 * of the previous branch, errors might get lost:
5604 * %if 0 ... %else trailing garbage ... %endif
5605 * So %else etc should set the ERR_PP_PRECOND flag.
5606 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005607 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005608 ((severity & ERR_PP_PRECOND) ?
5609 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005610 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005611 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005612
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005613 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005614}
5615
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005616static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005617stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005618{
5619 (void)s;
5620 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005621 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005622
5623 return make_tok_qstr(src_get_fname());
5624}
5625
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005626static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005627stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005628{
5629 (void)s;
5630 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005631 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005632
5633 return make_tok_num(src_get_linnum());
5634}
5635
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005636static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005637stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005638{
5639 (void)s;
5640 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005641 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005642
5643 return make_tok_num(globalbits);
5644}
5645
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005646static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005647stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005648{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005649 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005650 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5651 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5652 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005653
5654 (void)s;
5655 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005656 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005657
5658 switch (globalbits) {
5659 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005660 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005661 break;
5662 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005663 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005664 break;
5665 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005666 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005667 break;
5668 default:
5669 panic();
5670 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005671
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005672 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005673}
5674
H. Peter Anvin8b262472019-02-26 14:00:54 -08005675/* Add magic standard macros */
5676struct magic_macros {
5677 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005678 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005679 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005680};
5681static const struct magic_macros magic_macros[] =
5682{
H. Peter Anvind2354082019-08-27 16:38:48 -07005683 { "__?FILE?__", 0, stdmac_file },
5684 { "__?LINE?__", 0, stdmac_line },
5685 { "__?BITS?__", 0, stdmac_bits },
5686 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005687 { NULL, 0, NULL }
5688};
5689
5690static void pp_add_magic_stdmac(void)
5691{
5692 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005693 SMacro tmpl;
5694
5695 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005696
5697 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005698 tmpl.nparam = m->nparam;
5699 tmpl.expand = m->func;
5700 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005701 }
5702}
5703
H. Peter Anvin734b1882002-04-30 21:01:08 +00005704static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005705pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005706{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005707 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005708 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005709
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005710 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005711 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005712 nested_mac_count = 0;
5713 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005714 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005715 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005716 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005717 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07005718 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005719
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005720 if (!use_loaded)
5721 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5722 memset(use_loaded, 0, use_package_count * sizeof(bool));
5723
H. Peter Anvin6686de22019-08-10 05:33:14 -07005724 /* First set up the top level input file */
5725 nasm_new(istk);
5726 istk->fp = nasm_open_read(file, NF_TEXT);
5727 src_set(0, file);
5728 istk->lineinc = 1;
5729 if (!istk->fp)
5730 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5731
5732 strlist_add(deplist, file);
5733
5734 /*
5735 * Set up the stdmac packages as a virtual include file,
5736 * indicated by a null file pointer.
5737 */
5738 nasm_new(inc);
5739 inc->next = istk;
5740 inc->fname = src_set_fname(NULL);
5741 inc->nolist = !list_option('b');
5742 istk = inc;
5743 lfmt->uplevel(LIST_INCLUDE, 0);
5744
H. Peter Anvin8b262472019-02-26 14:00:54 -08005745 pp_add_magic_stdmac();
5746
H. Peter Anvinf7606612016-07-13 14:23:48 -07005747 if (tasm_compatible_mode)
5748 pp_add_stdmac(nasm_stdmac_tasm);
5749
5750 pp_add_stdmac(nasm_stdmac_nasm);
5751 pp_add_stdmac(nasm_stdmac_version);
5752
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005753 if (extrastdmac)
5754 pp_add_stdmac(extrastdmac);
5755
H. Peter Anvinf7606612016-07-13 14:23:48 -07005756 stdmacpos = stdmacros[0];
5757 stdmacnext = &stdmacros[1];
5758
H. Peter Anvind2456592008-06-19 15:04:18 -07005759 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005760
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005761 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07005762 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005763 * other builtins, because it is special -- it varies between
5764 * passes -- but there is really no particular reason to make it
5765 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005766 *
5767 * 0 = dependencies only
5768 * 1 = preparatory passes
5769 * 2 = final pass
5770 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005771 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005772 switch (mode) {
5773 case PP_NORMAL:
5774 apass = pass_final() ? 2 : 1;
5775 break;
5776 case PP_DEPS:
5777 apass = 0;
5778 break;
5779 case PP_PREPROC:
5780 apass = 3;
5781 break;
5782 default:
5783 panic();
5784 }
5785
H. Peter Anvind2354082019-08-27 16:38:48 -07005786 define_smacro("__?PASS?__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005787}
5788
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005789static void pp_init(void)
5790{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005791}
5792
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005793/*
5794 * Get a line of tokens. If we popped the macro expansion/include stack,
5795 * we return a pointer to the dummy token tok_pop; at that point if
5796 * istk is NULL then we have reached end of input;
5797 */
5798static Token tok_pop; /* Dummy token placeholder */
5799
5800static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005801{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005802 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005803 Line *l = istk->expansion;
5804 Token *tline = NULL;
5805 Token *dtline;
5806
H. Peter Anvine2c80182005-01-15 22:15:51 +00005807 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005808 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005809 * buffer or from the input file.
5810 */
5811 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005812 while (l && l->finishes) {
5813 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005814
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005815 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005816 /*
5817 * This is a macro-end marker for a macro with no
5818 * name, which means it's not really a macro at all
5819 * but a %rep block, and the `in_progress' field is
5820 * more than 1, meaning that we still need to
5821 * repeat. (1 means the natural last repetition; 0
5822 * means termination by %exitrep.) We have
5823 * therefore expanded up to the %endrep, and must
5824 * push the whole block on to the expansion buffer
5825 * again. We don't bother to remove the macro-end
5826 * marker: we'd only have to generate another one
5827 * if we did.
5828 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005829 fm->in_progress--;
5830 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005831 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005832 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005833
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005834 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005835 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005836 tail = &ll->first;
5837
5838 list_for_each(t, l->first) {
5839 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005840 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005841 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005842 }
5843 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005844 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005845 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005846 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005847 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005848 MMacro *m = istk->mstk.mstk;
5849
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005850 /*
5851 * Check whether a `%rep' was started and not ended
5852 * within this macro expansion. This can happen and
5853 * should be detected. It's a fatal error because
5854 * I'm too confused to work out how to recover
5855 * sensibly from it.
5856 */
5857 if (defining) {
5858 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005859 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005860 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005861 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005862 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005863 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005864
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005865 /*
5866 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005867 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005868 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005869 istk->mstk = m->mstk;
5870 if (m->name) {
5871 /*
5872 * This was a real macro call, not a %rep, and
5873 * therefore the parameter information needs to
5874 * be freed and the iteration count/nesting
5875 * depth adjusted.
5876 */
5877
5878 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005879 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005880 * If all mmacro processing done,
5881 * clear all counters and the deadman
5882 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005883 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005884 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005885 }
5886
Adam Majer91e72402017-07-25 10:42:01 +02005887#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005888 if (m->prev) {
5889 pop_mmacro(m);
5890 fm->in_progress --;
5891 } else
Adam Majer91e72402017-07-25 10:42:01 +02005892#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005893 {
5894 nasm_free(m->params);
5895 free_tlist(m->iline);
5896 nasm_free(m->paramlen);
5897 fm->in_progress = 0;
5898 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005899 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005900
5901 /*
5902 * FIXME It is incorrect to always free_mmacro here.
5903 * It leads to usage-after-free.
5904 *
5905 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5906 */
5907#if 0
5908 else
5909 free_mmacro(m);
5910#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005911 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005912 istk->expansion = l->next;
5913 nasm_free(l);
5914 lfmt->downlevel(LIST_MACRO);
5915 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005916 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005917
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005918 do { /* until we get a line we can use */
5919 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005920
5921 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005922 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005923 int32_t lineno;
5924
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005925 if (istk->mstk.mstk) {
5926 istk->mstk.mstk->lineno++;
5927 if (istk->mstk.mstk->fname)
5928 lineno = istk->mstk.mstk->lineno +
5929 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005930 else
5931 lineno = 0; /* Defined at init time or builtin */
5932 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005933 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005934 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005935
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005936 tline = l->first;
5937 istk->expansion = l->next;
5938 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005939
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005940 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005941 if (!istk->nolist)
5942 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005943 nasm_free(line);
5944 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005945 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005946 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005947 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005948 } else {
5949 /*
5950 * The current file has ended; work down the istk
5951 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005952 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005953 if (i->fp)
5954 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005955 if (i->conds) {
5956 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005957 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005958 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005959 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005960 if (i->next)
5961 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005962 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005963 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005964 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005965 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005966 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005967 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005968
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005969 /*
5970 * We must expand MMacro parameters and MMacro-local labels
5971 * _before_ we plunge into directive processing, to cope
5972 * with things like `%define something %1' such as STRUC
5973 * uses. Unless we're _defining_ a MMacro, in which case
5974 * those tokens should be left alone to go into the
5975 * definition; and unless we're in a non-emitting
5976 * condition, in which case we don't want to meddle with
5977 * anything.
5978 */
5979 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005980 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005981 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005982 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005983
H. Peter Anvine2c80182005-01-15 22:15:51 +00005984 /*
5985 * Check the line to see if it's a preprocessor directive.
5986 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005987 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5988 if (dtline)
5989 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005990 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005991 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005992 * We're defining a multi-line macro. We emit nothing
5993 * at all, and just
5994 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005995 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005996 MMacro *mmac = defining->dstk.mmac;
5997
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005998 Line *l = nasm_malloc(sizeof(Line));
5999 l->next = defining->expansion;
6000 l->first = tline;
6001 l->finishes = NULL;
6002 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006003
6004 /*
6005 * Remember if this mmacro expansion contains %00:
6006 * if it does, we will have to handle leading labels
6007 * specially.
6008 */
6009 if (mmac) {
6010 const Token *t;
6011 list_for_each(t, tline) {
6012 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
6013 mmac->capture_label = true;
6014 }
6015 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006016 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006017 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006018 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006019 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006020 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006021 * directive so we keep our place correctly.
6022 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006023 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006024 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006025 /*
6026 * We're in a %rep block which has been terminated, so
6027 * we're walking through to the %endrep without
6028 * emitting anything. Emit nothing at all, not even a
6029 * blank line: when we emerge from the %rep block we'll
6030 * give a line-number directive so we keep our place
6031 * correctly.
6032 */
6033 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006034 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006035 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006036 if (!expand_mmacro(tline))
6037 return tline;
6038 }
6039 }
6040}
6041
6042static char *pp_getline(void)
6043{
6044 char *line = NULL;
6045 Token *tline;
6046
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006047 while (true) {
6048 tline = pp_tokline();
6049 if (tline == &tok_pop) {
6050 /*
6051 * We popped the macro/include stack. If istk is empty,
6052 * we are at end of input, otherwise just loop back.
6053 */
6054 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006055 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006056 } else {
6057 /*
6058 * De-tokenize the line and emit it.
6059 */
6060 line = detoken(tline, true);
6061 free_tlist(tline);
6062 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006063 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006064 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006065
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006066 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006067 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006068 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006069 nasm_free(buf);
6070 }
6071
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006072 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006073}
6074
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006075static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006076{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006077 if (defining) {
6078 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006079 nasm_nonfatal("end of file while still defining macro `%s'",
6080 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006081 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006082 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006083 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006084
6085 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006086 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006087 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006088
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006089 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006090 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006091 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006092 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006093 Include *i = istk;
6094 istk = istk->next;
6095 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006096 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006097 }
6098 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006099 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006100 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006101}
6102
6103static void pp_cleanup_session(void)
6104{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006105 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006106 free_llist(predef);
6107 predef = NULL;
6108 delete_Blocks();
6109 freeTokens = NULL;
6110 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006111}
6112
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006113static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006114{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006115 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006116}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006117
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006118static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006119{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006120 Token *inc, *space, *name;
6121 Line *l;
6122
H. Peter Anvin734b1882002-04-30 21:01:08 +00006123 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
6124 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
6125 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006126
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006127 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006128 l->next = predef;
6129 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006130 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006131 predef = l;
6132}
6133
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006134static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006135{
6136 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006137 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006138 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006139
6140 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00006141 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6142 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006143 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006144 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006145 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006146 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006147 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006148
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006149 if (space->next->type != TOK_PREPROC_ID &&
6150 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006151 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006152
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006153 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006154 l->next = predef;
6155 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006156 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006157 predef = l;
6158}
6159
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006160static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006161{
6162 Token *def, *space;
6163 Line *l;
6164
H. Peter Anvin734b1882002-04-30 21:01:08 +00006165 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6166 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006167 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006168
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006169 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006170 l->next = predef;
6171 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006172 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006173 predef = l;
6174}
6175
H. Peter Anvin05990342018-06-11 13:32:42 -07006176/* Insert an early preprocessor command that doesn't need special handling */
6177static void pp_pre_command(const char *what, char *string)
6178{
6179 char *cmd;
6180 Token *def, *space;
6181 Line *l;
6182
6183 def = tokenize(string);
6184 if (what) {
6185 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6186 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
6187 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6188 }
6189
6190 l = nasm_malloc(sizeof(Line));
6191 l->next = predef;
6192 l->first = def;
6193 l->finishes = NULL;
6194 predef = l;
6195}
6196
H. Peter Anvinf7606612016-07-13 14:23:48 -07006197static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006198{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006199 macros_t **mp;
6200
6201 /* Find the end of the list and avoid duplicates */
6202 for (mp = stdmacros; *mp; mp++) {
6203 if (*mp == macros)
6204 return; /* Nothing to do */
6205 }
6206
6207 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6208
6209 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006210}
6211
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006212static void pp_extra_stdmac(macros_t *macros)
6213{
6214 extrastdmac = macros;
6215}
6216
H. Peter Anvin8b262472019-02-26 14:00:54 -08006217static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006218{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006219 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006220 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6221 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6222}
6223
6224static Token *make_tok_qstr(const char *str)
6225{
6226 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006227 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006228 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006229}
6230
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006231static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006232{
6233 if (!m)
6234 return;
6235
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006236 /* We need to print the mstk.mmac list in reverse order */
6237 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006238
6239 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006240 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006241 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006242 }
6243}
6244
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006245static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006246{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006247 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006248
H. Peter Anvinddb29062018-12-11 00:06:29 -08006249 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6250 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006251
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006252 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006253 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006254
H. Peter Anvinddb29062018-12-11 00:06:29 -08006255 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006256}
6257
H. Peter Anvine7469712016-02-18 02:20:59 -08006258const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006259 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006260 pp_reset,
6261 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006262 pp_cleanup_pass,
6263 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006264 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006265 pp_pre_define,
6266 pp_pre_undefine,
6267 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006268 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006269 pp_include_path,
6270 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006271 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006272};