blob: 694527dc539175028999a98d48abf379e0838e38 [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 Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800518static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800519static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000520static void *new_Block(size_t size);
521static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700522static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700523 const char *text, size_t txtlen);
524static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000525static Token *delete_Token(Token * t);
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) {
1880 name = m->name;
1881 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 Anvin76690a12002-04-30 20:52:49 +00001994
1995 origline = tline;
1996
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001997 switch (PP_COND(ct)) {
1998 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001999 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002000 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002002 if (!tline)
2003 break;
2004 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002005 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002006 dname);
2007 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002008 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002009 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002010 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 tline = tline->next;
2012 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002014
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002015 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002016 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002017 while (tline) {
2018 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 if (!tline || (tline->type != TOK_ID &&
2020 (tline->type != TOK_PREPROC_ID ||
2021 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002022 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002023 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002024 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 }
H. Peter Anvind2354082019-08-27 16:38:48 -07002026 if (smacro_defined(NULL, tline->text, 0, NULL, true, false))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002027 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002029 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002031
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002032 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002033 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002034 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002035 while (tline) {
2036 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002037 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002038 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002039 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002040 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002041 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002042 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002043 goto fail;
2044 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002045 p = tline->text;
2046 if (tline->type == TOK_PREPROC_ID)
2047 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08002048 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002049 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002050 if (getenv(p))
2051 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002052 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002053 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002054 break;
2055
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002056 case PP_IFIDNI:
2057 casesense = false;
2058 /* fall through */
2059 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002060 tline = expand_smacro(tline);
2061 t = tt = tline;
2062 while (tok_isnt_(tt, ","))
2063 tt = tt->next;
2064 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002065 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002066 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002067 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 }
2069 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002070 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002071 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
2072 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002073 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002074 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 }
2077 if (t->type == TOK_WHITESPACE) {
2078 t = t->next;
2079 continue;
2080 }
2081 if (tt->type == TOK_WHITESPACE) {
2082 tt = tt->next;
2083 continue;
2084 }
2085 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002086 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 break;
2088 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002089 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002090 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002091 size_t l1 = nasm_unquote(t->text, NULL);
2092 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002094 if (l1 != l2) {
2095 j = false;
2096 break;
2097 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002098 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 j = false;
2100 break;
2101 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002102 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002103 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 break;
2105 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002106
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 t = t->next;
2108 tt = tt->next;
2109 }
2110 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002111 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002112 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002113
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002114 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002115 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002116 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002117 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002118
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 skip_white_(tline);
2120 tline = expand_id(tline);
2121 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002122 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002123 goto fail;
2124 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002125 nasm_zero(searching);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 searching.name = nasm_strdup(tline->text);
2127 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002128 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002129 searching.nparam_max = INT_MAX;
2130 tline = expand_smacro(tline->next);
2131 skip_white_(tline);
2132 if (!tline) {
2133 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002134 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002135 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 } else {
2137 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002138 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 }
2140 if (tline && tok_is_(tline->next, "-")) {
2141 tline = tline->next->next;
2142 if (tok_is_(tline, "*"))
2143 searching.nparam_max = INT_MAX;
2144 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002145 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002146 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002148 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002149 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002150 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002151 searching.nparam_max = searching.nparam_min;
2152 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002153 }
2154 }
2155 if (tline && tok_is_(tline->next, "+")) {
2156 tline = tline->next;
2157 searching.plus = true;
2158 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002159 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2160 while (mmac) {
2161 if (!strcmp(mmac->name, searching.name) &&
2162 (mmac->nparam_min <= searching.nparam_max
2163 || searching.plus)
2164 && (searching.nparam_min <= mmac->nparam_max
2165 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002166 found = true;
2167 break;
2168 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002169 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002170 }
2171 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002172 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002173 nasm_free(searching.name);
2174 j = found;
2175 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002176 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002177
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002178 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002179 needtype = TOK_ID;
2180 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002181 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 needtype = TOK_NUMBER;
2183 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002184 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002185 needtype = TOK_STRING;
2186 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002187
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002188iftype:
2189 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002190
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002191 while (tok_type_(t, TOK_WHITESPACE) ||
2192 (needtype == TOK_NUMBER &&
2193 tok_type_(t, TOK_OTHER) &&
2194 (t->text[0] == '-' || t->text[0] == '+') &&
2195 !t->text[1]))
2196 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002197
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002198 j = tok_type_(t, needtype);
2199 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002200
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002201 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002202 t = tline = expand_smacro(tline);
2203 while (tok_type_(t, TOK_WHITESPACE))
2204 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002205
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002206 j = false;
2207 if (t) {
2208 t = t->next; /* Skip the actual token */
2209 while (tok_type_(t, TOK_WHITESPACE))
2210 t = t->next;
2211 j = !t; /* Should be nothing left */
2212 }
2213 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002214
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002215 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002216 t = tline = expand_smacro(tline);
2217 while (tok_type_(t, TOK_WHITESPACE))
2218 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002219
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002220 j = !t; /* Should be empty */
2221 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002222
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002223 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002224 pps.tptr = tline = expand_smacro(tline);
2225 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002226 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002227 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002228 if (!evalresult)
2229 return -1;
2230 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002231 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002232 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002233 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002234 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002235 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002236 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002237 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002238 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002239
H. Peter Anvine2c80182005-01-15 22:15:51 +00002240 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002241 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002242 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002243 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002244
2245 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002246 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002247
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002248fail:
2249 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002250 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002251}
2252
2253/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002254 * Default smacro expansion routine: just returns a copy of the
2255 * expansion list.
2256 */
2257static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002258smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002259{
2260 (void)params;
2261 (void)nparams;
2262
2263 return dup_tlist(s->expansion, NULL);
2264}
2265
2266/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002267 * Emit a macro defintion or undef to the listing file, if
2268 * desired. This is similar to detoken(), but it handles the reverse
2269 * expansion list, does not expand %! or local variable tokens, and
2270 * does some special handling for macro parameters.
2271 */
2272static void
2273list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2274{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002275 Token *t;
2276 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002277 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002278 char *context_prefix = NULL;
2279 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002280
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002281 namelen = strlen(m->name);
2282 size = namelen + 2; /* Include room for space after name + NUL */
2283
2284 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002285 int context_depth = cstk->depth - ctx->depth + 1;
2286 context_prefix =
2287 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2288 ctx->name ? ctx->name : "",
2289 ctx->number, context_depth, "");
2290
2291 context_len = nasm_last_string_len();
2292 memset(context_prefix + context_len - context_depth,
2293 '$', context_depth);
2294 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002295 }
2296
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002297 list_for_each(t, m->expansion)
2298 size += t->text ? t->len : 1;
2299
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002300 if (m->nparam) {
2301 /*
2302 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002303 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002304 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002305 int i;
2306
2307 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002308 for (i = 0; i < m->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002309 size += m->params[i].namelen;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002310 }
2311
2312 def = nasm_malloc(size);
2313 p = def+size;
2314 *--p = '\0';
2315
2316 list_for_each(t, m->expansion) {
2317 if (!t->text) {
2318 *--p = ' ';
2319 } else {
2320 p -= t->len;
2321 memcpy(p, t->text, t->len);
2322 }
2323 }
2324
2325 *--p = ' ';
2326
2327 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002328 int i;
2329
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002330 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002331 for (i = m->nparam-1; i >= 0; i--) {
2332 enum sparmflags flags = m->params[i].flags;
2333 if (flags & SPARM_GREEDY)
2334 *--p = '+';
2335 if (m->params[i].name) {
2336 p -= m->params[i].namelen;
2337 memcpy(p, m->params[i].name, m->params[i].namelen);
2338 }
2339 if (flags & SPARM_NOSTRIP)
2340 *--p = '!';
2341 if (flags & SPARM_STR)
2342 *--p = '&';
2343 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002344 *--p = '=';
2345 *--p = ',';
2346 }
2347 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002348 }
2349
2350 p -= namelen;
2351 memcpy(p, m->name, namelen);
2352
H. Peter Anvin6686de22019-08-10 05:33:14 -07002353 if (context_prefix) {
2354 p -= context_len;
2355 memcpy(p, context_prefix, context_len);
2356 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002357 }
2358
2359 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002360 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002361}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002362
2363/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002364 * Parse smacro arguments, return argument count. If the tmpl argument
2365 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002366 * *tpp is updated to point to the pointer to the first token after the
2367 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002368 *
2369 * The text values from any argument tokens are "stolen" and the
2370 * corresponding text fields set to NULL.
2371 */
2372static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2373{
2374 int nparam = 0;
2375 enum sparmflags flags;
2376 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002377 bool err, done;
2378 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002379 Token **tn = *tpp;
2380 Token *t = *tn;
2381 Token *name;
2382
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002383 /*
2384 * DO NOT skip whitespace here, or we won't be able to distinguish:
2385 *
2386 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2387 * %define bar(a,b) ; two arguments, empty expansion
2388 *
2389 * This ambiguity was inherited from C.
2390 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002391
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002392 if (!tok_is_(t, "("))
2393 goto finish;
2394
2395 if (tmpl) {
2396 Token *tx = t;
2397 Token **txpp = &tx;
2398 int sparam;
2399
2400 /* Count parameters first */
2401 sparam = parse_smacro_template(&txpp, NULL);
2402 if (!sparam)
2403 goto finish; /* No parameters, we're done */
2404 nasm_newn(params, sparam);
2405 }
2406
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002407 /* Skip leading paren */
2408 tn = &t->next;
2409 t = *tn;
2410
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002411 name = NULL;
2412 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002413 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002414
2415 while (!done) {
2416 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002417 if (name || flags)
2418 nasm_nonfatal("`)' expected to terminate macro template");
2419 else
2420 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002421 break;
2422 }
2423
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002424 switch (t->type) {
2425 case TOK_ID:
2426 if (name)
2427 goto bad;
2428 name = t;
2429 break;
2430
2431 case TOK_OTHER:
2432 if (t->text[1])
2433 goto bad;
2434 switch (t->text[0]) {
2435 case '=':
2436 flags |= SPARM_EVAL;
2437 break;
2438 case '&':
2439 flags |= SPARM_STR;
2440 break;
2441 case '!':
2442 flags |= SPARM_NOSTRIP;
2443 break;
2444 case '+':
2445 flags |= SPARM_GREEDY;
2446 greedy = true;
2447 break;
2448 case ',':
2449 if (greedy)
2450 nasm_nonfatal("greedy parameter must be last");
2451 /* fall through */
2452 case ')':
2453 if (params) {
2454 if (name) {
2455 params[nparam].name = name->text;
2456 params[nparam].namelen = name->len;
2457 name->text = NULL;
2458 }
2459 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002460 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002461 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002462 name = NULL;
2463 flags = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002464 done = t->text[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002465 break;
2466 default:
2467 goto bad;
2468 }
2469 break;
2470
2471 case TOK_WHITESPACE:
2472 break;
2473
2474 default:
2475 bad:
2476 if (!err) {
2477 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2478 err = true;
2479 }
2480 break;
2481 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002482
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002483 tn = &t->next;
2484 t = *tn;
2485 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002486
2487finish:
2488 while (t && t->type == TOK_WHITESPACE) {
2489 tn = &t->next;
2490 t = t->next;
2491 }
2492 *tpp = tn;
2493 if (tmpl) {
2494 tmpl->nparam = nparam;
2495 tmpl->greedy = greedy;
2496 tmpl->params = params;
2497 }
2498 return nparam;
2499}
2500
2501/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002502 * Common code for defining an smacro. The tmpl argument, if not NULL,
2503 * contains any macro parameters that aren't explicit arguments;
2504 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002505 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002506static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002507 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002508{
2509 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002510 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002511 Context *ctx;
2512 bool defining_alias = false;
2513 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002514
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002515 if (tmpl) {
2516 defining_alias = tmpl->alias;
2517 nparam = tmpl->nparam;
2518 }
2519
2520 while (1) {
2521 ctx = get_ctx(mname, &mname);
2522
H. Peter Anvind2354082019-08-27 16:38:48 -07002523 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002524 /* Create a new macro */
2525 smtbl = ctx ? &ctx->localmac : &smacros;
2526 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2527 nasm_new(smac);
2528 smac->next = *smhead;
2529 *smhead = smac;
2530 break;
2531 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002532 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002533 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002534 /*
2535 * Some instances of the old code considered this a failure,
2536 * some others didn't. What is the right thing to do here?
2537 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002538 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002539 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002540 /*
2541 * We're redefining, so we have to take over an
2542 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002543 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002544 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002545 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002546 break;
2547 } else if (smac->in_progress) {
2548 nasm_nonfatal("macro alias loop");
2549 goto fail;
2550 } else {
2551 /* It is an alias macro; follow the alias link */
2552 SMacro *s;
2553
2554 smac->in_progress = true;
2555 s = define_smacro(smac->expansion->text, casesense,
2556 expansion, tmpl);
2557 smac->in_progress = false;
2558 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002560 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002561
2562 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002563 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002564 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002565 smac->expand = smacro_expand_default;
2566 if (tmpl) {
2567 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002568 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002569 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002570 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002571 if (tmpl->expand)
2572 smac->expand = tmpl->expand;
2573 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002574 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002575 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2576 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002577 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002578 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002579
2580fail:
2581 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002582 if (tmpl)
2583 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002584 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002585}
2586
2587/*
2588 * Undefine an smacro
2589 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002590static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002591{
2592 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002593 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002594 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002595
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002596 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002597 smtbl = ctx ? &ctx->localmac : &smacros;
2598 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002599
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002600 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 /*
2602 * We now have a macro name... go hunt for it.
2603 */
2604 sp = smhead;
2605 while ((s = *sp) != NULL) {
2606 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002607 if (s->alias && !undefalias) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002608 if (!do_aliases)
2609 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002610 if (s->in_progress) {
2611 nasm_nonfatal("macro alias loop");
2612 } else {
2613 s->in_progress = true;
2614 undef_smacro(s->expansion->text, false);
2615 s->in_progress = false;
2616 }
2617 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002618 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002619 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2620 ctx, s);
2621 *sp = s->next;
2622 free_smacro(s);
2623 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 } else {
2625 sp = &s->next;
2626 }
2627 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002628 }
2629}
2630
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002631/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002632 * Parse a mmacro specification.
2633 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002634static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002635{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002636 tline = tline->next;
2637 skip_white_(tline);
2638 tline = expand_id(tline);
2639 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002640 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002641 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002642 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002643
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002644#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002645 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002646#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002647 def->name = nasm_strdup(tline->text);
2648 def->plus = false;
2649 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002650 def->nparam_min = 0;
2651 def->nparam_max = 0;
2652
H. Peter Anvina26433d2008-07-16 14:40:01 -07002653 tline = expand_smacro(tline->next);
2654 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002655 if (!tok_type_(tline, TOK_NUMBER))
2656 nasm_nonfatal("`%s' expects a parameter count", directive);
2657 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002658 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002659 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 tline = tline->next->next;
2661 if (tok_is_(tline, "*")) {
2662 def->nparam_max = INT_MAX;
2663 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002664 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002665 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002666 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002667 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002668 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002669 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 }
2671 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002672 }
2673 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 tline = tline->next;
2675 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002676 }
2677 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 !nasm_stricmp(tline->next->text, ".nolist")) {
2679 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002680 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002681 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682
H. Peter Anvina26433d2008-07-16 14:40:01 -07002683 /*
2684 * Handle default parameters.
2685 */
2686 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 def->dlist = tline->next;
2688 tline->next = NULL;
2689 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002690 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 def->dlist = NULL;
2692 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002693 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002694 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002695
H. Peter Anvin89cee572009-07-15 09:16:54 -04002696 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002697 !def->plus) {
2698 /*
2699 *!macro-defaults [on] macros with more default than optional parameters
2700 *! warns when a macro has more default parameters than optional parameters.
2701 *! See \k{mlmacdef} for why might want to disable this warning.
2702 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002703 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002704 "too many default macro parameters in macro `%s'", def->name);
2705 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002706
H. Peter Anvina26433d2008-07-16 14:40:01 -07002707 return true;
2708}
2709
2710
2711/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002712 * Decode a size directive
2713 */
2714static int parse_size(const char *str) {
2715 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002717 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002719 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002720}
2721
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002722/*
2723 * Process a preprocessor %pragma directive. Currently there are none.
2724 * Gets passed the token list starting with the "preproc" token from
2725 * "%pragma preproc".
2726 */
2727static void do_pragma_preproc(Token *tline)
2728{
2729 /* Skip to the real stuff */
2730 tline = tline->next;
2731 skip_white_(tline);
2732 if (!tline)
2733 return;
2734
2735 (void)tline; /* Nothing else to do at present */
2736}
2737
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002738static bool is_macro_id(const Token *t)
2739{
2740 return t && (t->type == TOK_ID ||
2741 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2742}
2743
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002744static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002745{
2746 char *id;
2747 Token *t = *tp;
2748
2749 t = t->next; /* Skip directive */
2750 skip_white_(t);
2751 t = expand_id(t);
2752
2753 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002754 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002755 err ? err : "macro identifier");
2756 return NULL;
2757 }
2758
2759 id = t->text;
2760 skip_white_(t);
2761 *tp = t;
2762 return id;
2763}
2764
Ed Beroset3ab3f412002-06-11 03:31:49 +00002765/**
2766 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002767 * Find out if a line contains a preprocessor directive, and deal
2768 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002769 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002770 * If a directive _is_ found, it is the responsibility of this routine
2771 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002772 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002773 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002774 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002775 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002776 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002777 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002778static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002779{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002780 enum preproc_token i;
2781 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002782 bool err;
2783 int nparam;
2784 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002785 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002786 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002787 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002788 char *p, *pp;
2789 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002790 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002791 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002792 Include *inc;
2793 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002794 Cond *cond;
2795 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002796 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002797 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002798 struct tokenval tokval;
2799 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002800 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002801 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002802 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002803 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002804
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002805 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002806 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002807
H. Peter Anvineba20a72002-04-30 20:53:55 +00002808 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002809 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002810 (tline->text[0] && (tline->text[1] == '%' ||
2811 tline->text[1] == '$' ||
2812 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002813 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002814
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002815 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002816 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002817
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002818 casesense = true;
2819 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2820 casesense = false;
2821 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002822 }
2823
2824 /*
2825 * If we're in a non-emitting branch of a condition construct,
2826 * or walking to the end of an already terminated %rep block,
2827 * we should ignore all directives except for condition
2828 * directives.
2829 */
2830 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002831 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2832 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002833 return NO_DIRECTIVE_FOUND;
2834 }
2835
2836 /*
2837 * If we're defining a macro or reading a %rep block, we should
2838 * ignore all directives except for %macro/%imacro (which nest),
2839 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2840 * If we're in a %rep block, another %rep nests, so should be let through.
2841 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002842 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002843 i != PP_ENDMACRO && i != PP_ENDM &&
2844 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2845 return NO_DIRECTIVE_FOUND;
2846 }
2847
2848 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002849 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002850 nested_mac_count++;
2851 return NO_DIRECTIVE_FOUND;
2852 } else if (nested_mac_count > 0) {
2853 if (i == PP_ENDMACRO) {
2854 nested_mac_count--;
2855 return NO_DIRECTIVE_FOUND;
2856 }
2857 }
2858 if (!defining->name) {
2859 if (i == PP_REP) {
2860 nested_rep_count++;
2861 return NO_DIRECTIVE_FOUND;
2862 } else if (nested_rep_count > 0) {
2863 if (i == PP_ENDREP) {
2864 nested_rep_count--;
2865 return NO_DIRECTIVE_FOUND;
2866 }
2867 }
2868 }
2869 }
2870
H. Peter Anvin4169a472007-09-12 01:29:43 +00002871 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002872 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002873 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002874 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002875
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002876 case PP_PRAGMA:
2877 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002878 * %pragma namespace options...
2879 *
2880 * The namespace "preproc" is reserved for the preprocessor;
2881 * all other namespaces generate a [pragma] assembly directive.
2882 *
2883 * Invalid %pragmas are ignored and may have different
2884 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002885 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002886 t = tline;
2887 tline = tline->next;
2888 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002889 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002890 while (tok_type_(tline, TOK_WHITESPACE)) {
2891 t = tline;
2892 tline = tline->next;
2893 delete_Token(t);
2894 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002895 if (tok_type_(tline, TOK_ID)) {
2896 if (!nasm_stricmp(tline->text, "preproc")) {
2897 /* Preprocessor pragma */
2898 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002899 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002900 } else {
2901 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002902
2903 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002904 for (t = tline; t->next; t = t->next)
2905 ;
2906 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002907
2908 /* Prepend "[pragma " */
2909 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2910 t = new_Token(t, TOK_ID, "pragma", 6);
2911 t = new_Token(t, TOK_OTHER, "[", 1);
2912 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002913 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002914 }
2915 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002916 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002917
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 case PP_STACKSIZE:
2919 /* Directive to tell NASM what the default stack size is. The
2920 * default is for a 16-bit stack, and this can be overriden with
2921 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 */
2923 tline = tline->next;
2924 if (tline && tline->type == TOK_WHITESPACE)
2925 tline = tline->next;
2926 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002927 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 }
2929 if (nasm_stricmp(tline->text, "flat") == 0) {
2930 /* All subsequent ARG directives are for a 32-bit stack */
2931 StackSize = 4;
2932 StackPointer = "ebp";
2933 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002934 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002935 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2936 /* All subsequent ARG directives are for a 64-bit stack */
2937 StackSize = 8;
2938 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002939 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002940 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002941 } else if (nasm_stricmp(tline->text, "large") == 0) {
2942 /* All subsequent ARG directives are for a 16-bit stack,
2943 * far function call.
2944 */
2945 StackSize = 2;
2946 StackPointer = "bp";
2947 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002948 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 } else if (nasm_stricmp(tline->text, "small") == 0) {
2950 /* All subsequent ARG directives are for a 16-bit stack,
2951 * far function call. We don't support near functions.
2952 */
2953 StackSize = 2;
2954 StackPointer = "bp";
2955 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002956 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002957 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002958 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002960 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002961
H. Peter Anvine2c80182005-01-15 22:15:51 +00002962 case PP_ARG:
2963 /* TASM like ARG directive to define arguments to functions, in
2964 * the following form:
2965 *
2966 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2967 */
2968 offset = ArgOffset;
2969 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002970 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002972
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 /* Find the argument name */
2974 tline = tline->next;
2975 if (tline && tline->type == TOK_WHITESPACE)
2976 tline = tline->next;
2977 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002978 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002979 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 }
2981 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002982
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 /* Find the argument size type */
2984 tline = tline->next;
2985 if (!tline || tline->type != TOK_OTHER
2986 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002987 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002988 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 }
2990 tline = tline->next;
2991 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002992 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002993 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002995
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002997 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002999 size = parse_size(tt->text);
3000 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003001 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003003 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 }
3005 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003006
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003007 /* Round up to even stack slots */
3008 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003009
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 /* Now define the macro for the argument */
3011 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3012 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003013 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003015
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 /* Move to the next argument in the list */
3017 tline = tline->next;
3018 if (tline && tline->type == TOK_WHITESPACE)
3019 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003020 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003021 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003022 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003023
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 case PP_LOCAL:
3025 /* TASM like LOCAL directive to define local variables for a
3026 * function, in the following form:
3027 *
3028 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3029 *
3030 * The '= LocalSize' at the end is ignored by NASM, but is
3031 * required by TASM to define the local parameter size (and used
3032 * by the TASM macro package).
3033 */
3034 offset = LocalOffset;
3035 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003036 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003038
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 /* Find the argument name */
3040 tline = tline->next;
3041 if (tline && tline->type == TOK_WHITESPACE)
3042 tline = tline->next;
3043 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003044 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003045 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 }
3047 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 /* Find the argument size type */
3050 tline = tline->next;
3051 if (!tline || tline->type != TOK_OTHER
3052 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003053 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003054 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 }
3056 tline = tline->next;
3057 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003058 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003059 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003061
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003063 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003065 size = parse_size(tt->text);
3066 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003067 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003069 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003070 }
3071 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003073 /* Round up to even stack slots */
3074 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003075
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003076 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003077
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003078 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3080 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003081 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003082
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 /* Now define the assign to setup the enter_c macro correctly */
3084 snprintf(directive, sizeof(directive),
3085 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003086 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003087
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 /* Move to the next argument in the list */
3089 tline = tline->next;
3090 if (tline && tline->type == TOK_WHITESPACE)
3091 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003092 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003093 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003094 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003095
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 case PP_CLEAR:
3097 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003098 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003099 free_macros();
3100 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003101 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003102
H. Peter Anvin418ca702008-05-30 10:42:30 -07003103 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003104 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003105 skip_white_(t);
3106 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003107 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003108 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003109 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003110 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003111 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003112 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003113 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003114 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003115 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003116 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003117 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003118
3119 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003120 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003121 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003122
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003123 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003124 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003125 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003126 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003128 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003129 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003131 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003132 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003133 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003134 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003135 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003136 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003137 (pp_mode == PP_DEPS)
3138 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003139 if (!inc->fp) {
3140 /* -MG given but file not found */
3141 nasm_free(inc);
3142 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003143 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003144 inc->lineno = src_set_linnum(0);
3145 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003146 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003147 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003148 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003150 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003151
H. Peter Anvind2456592008-06-19 15:04:18 -07003152 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003153 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003154 const struct use_package *pkg;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003155
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003156 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003157 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07003158 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003159 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003160 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003161 nasm_unquote_cstr(tline->text, NULL);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003162 pkg = nasm_find_use_package(tline->text);
3163 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003164 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003165 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003166 /*
3167 * Not already included, go ahead and include it.
3168 * Treat it as an include file for the purpose of
3169 * producing a listing.
3170 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003171 use_loaded[pkg->index] = true;
3172 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003173 nasm_new(inc);
3174 inc->next = istk;
3175 inc->fname = src_set_fname(NULL);
3176 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003177 inc->nolist = !list_option('b') || istk->nolist;
3178 istk = inc;
3179 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003181 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003182 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003185 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 tline = tline->next;
3187 skip_white_(tline);
3188 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003189 if (tline) {
3190 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003191 nasm_nonfatal("`%s' expects a context identifier",
3192 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003193 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003194 }
3195 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003196 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003197 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003198 p = nasm_strdup(tline->text);
3199 } else {
3200 p = NULL; /* Anonymous */
3201 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003202
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003203 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003204 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003205 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003206 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003207 ctx->name = p;
3208 ctx->number = unique++;
3209 cstk = ctx;
3210 } else {
3211 /* %pop or %repl */
3212 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003213 nasm_nonfatal("`%s': context stack is empty",
3214 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003215 } else if (i == PP_POP) {
3216 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003217 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003218 "expected %s",
3219 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003220 else
3221 ctx_pop();
3222 } else {
3223 /* i == PP_REPL */
3224 nasm_free(cstk->name);
3225 cstk->name = p;
3226 p = NULL;
3227 }
3228 nasm_free(p);
3229 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003230 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003231 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003232 severity = ERR_FATAL;
3233 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003235 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003236 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003237 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003238 /*!
3239 *!user [on] %warning directives
3240 *! controls output of \c{%warning} directives (see \k{pperror}).
3241 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003242 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003244
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003245issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003246 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003247 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003248 tline->next = expand_smacro(tline->next);
3249 tline = tline->next;
3250 skip_white_(tline);
3251 t = tline ? tline->next : NULL;
3252 skip_white_(t);
3253 if (tok_type_(tline, TOK_STRING) && !t) {
3254 /* The line contains only a quoted string */
3255 p = tline->text;
3256 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003257 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003258 } else {
3259 /* Not a quoted string, or more than a quoted string */
3260 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003261 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 nasm_free(p);
3263 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003264 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003265 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003266
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003267 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003268 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003269 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003270 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003271 j = if_condition(tline->next, i);
3272 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003273 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003274 cond = nasm_malloc(sizeof(Cond));
3275 cond->next = istk->conds;
3276 cond->state = j;
3277 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003278 if(istk->mstk.mstk)
3279 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003280 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003281
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003282 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003283 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003284 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003285 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003286 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003287 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003288 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003289
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003290 case COND_DONE:
3291 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003292 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003293
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003294 case COND_ELSE_TRUE:
3295 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003296 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003297 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003298 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003299 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003300
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003301 case COND_IF_FALSE:
3302 /*
3303 * IMPORTANT: In the case of %if, we will already have
3304 * called expand_mmac_params(); however, if we're
3305 * processing an %elif we must have been in a
3306 * non-emitting mode, which would have inhibited
3307 * the normal invocation of expand_mmac_params().
3308 * Therefore, we have to do it explicitly here.
3309 */
3310 j = if_condition(expand_mmac_params(tline->next), i);
3311 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003312 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003313 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003315 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003316
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 case PP_ELSE:
3318 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003319 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003320 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003321 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003322 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003323 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003324 case COND_IF_TRUE:
3325 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003326 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003327 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003328
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003329 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003330 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003331
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003332 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003333 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003334 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003335
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003336 case COND_ELSE_TRUE:
3337 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003338 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003339 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003340 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003341 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003342 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003343 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003344
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 case PP_ENDIF:
3346 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003347 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003348 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003349 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003350 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003351 cond = istk->conds;
3352 istk->conds = cond->next;
3353 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003354 if(istk->mstk.mstk)
3355 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003356 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003357
H. Peter Anvin8b262472019-02-26 14:00:54 -08003358 case PP_RMACRO:
3359 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003360 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003361 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003362 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003363 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003364 if (i == PP_RMACRO)
3365 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003366 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003367 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003368 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003369 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003370
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003371 src_get(&defining->xline, &defining->fname);
3372
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003373 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3374 while (mmac) {
3375 if (!strcmp(mmac->name, defining->name) &&
3376 (mmac->nparam_min <= defining->nparam_max
3377 || defining->plus)
3378 && (defining->nparam_min <= mmac->nparam_max
3379 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003380 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003381 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003382 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003383 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003384 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003385 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003386 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003387
H. Peter Anvine2c80182005-01-15 22:15:51 +00003388 case PP_ENDM:
3389 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003390 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003391 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003392 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003393 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003394 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3395 defining->next = *mmhead;
3396 *mmhead = defining;
3397 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003398 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003399
H. Peter Anvin89cee572009-07-15 09:16:54 -04003400 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003401 /*
3402 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003403 * macro-end marker for a macro with a name. Then we
3404 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003405 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003406 list_for_each(l, istk->expansion)
3407 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003408 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003409
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003410 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003411 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003412 * Remove all conditional entries relative to this
3413 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003414 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003415 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3416 cond = istk->conds;
3417 istk->conds = cond->next;
3418 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003419 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003420 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003421 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003422 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003423 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003424 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003425
H. Peter Anvina26433d2008-07-16 14:40:01 -07003426 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003427 casesense = false;
3428 /* fall through */
3429 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003430 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003431 MMacro **mmac_p;
3432 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003433
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003434 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003435 spec.casesense = casesense;
3436 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003437 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003438 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003439 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3440 while (mmac_p && *mmac_p) {
3441 mmac = *mmac_p;
3442 if (mmac->casesense == spec.casesense &&
3443 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3444 mmac->nparam_min == spec.nparam_min &&
3445 mmac->nparam_max == spec.nparam_max &&
3446 mmac->plus == spec.plus) {
3447 *mmac_p = mmac->next;
3448 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003449 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003450 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003451 }
3452 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003453 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003454 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003455 }
3456
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 case PP_ROTATE:
3458 if (tline->next && tline->next->type == TOK_WHITESPACE)
3459 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003460 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003462 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463 return DIRECTIVE_FOUND;
3464 }
3465 t = expand_smacro(tline->next);
3466 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003467 pps.tptr = tline = t;
3468 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003469 tokval.t_type = TOKEN_INVALID;
3470 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003471 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 free_tlist(tline);
3473 if (!evalresult)
3474 return DIRECTIVE_FOUND;
3475 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003476 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003478 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003479 return DIRECTIVE_FOUND;
3480 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003481 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003482 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003483 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003484 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003485 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003486 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003487 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003488
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003489 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003490 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003491 rotate += mmac->nparam;
3492
3493 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003494 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003495 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003496
H. Peter Anvine2c80182005-01-15 22:15:51 +00003497 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003498 {
3499 MMacro *tmp_defining;
3500
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003501 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 do {
3503 tline = tline->next;
3504 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003505
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 if (tok_type_(tline, TOK_ID) &&
3507 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003508 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 do {
3510 tline = tline->next;
3511 } while (tok_type_(tline, TOK_WHITESPACE));
3512 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003513
H. Peter Anvine2c80182005-01-15 22:15:51 +00003514 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003515 pps.tptr = expand_smacro(tline);
3516 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003518 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003519 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003520 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003521 if (!evalresult)
3522 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 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 `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003527 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003529 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003530 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003531 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3532 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003533 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003534 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003535 /*!
3536 *!negative-rep [on] regative %rep count
3537 *! warns about negative counts given to the \c{%rep}
3538 *! preprocessor directive.
3539 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003540 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003541 "negative `%%rep' count: %"PRId64, count);
3542 count = 0;
3543 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003544 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003545 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003547 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003548 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003550 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003551 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003552 defining->nolist = nolist;
3553 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003554 defining->mstk = istk->mstk;
3555 defining->dstk.mstk = tmp_defining;
3556 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003557 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003558 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003559 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003560
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003562 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003563 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003564 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003565 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003566
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 /*
3568 * Now we have a "macro" defined - although it has no name
3569 * and we won't be entering it in the hash tables - we must
3570 * push a macro-end marker for it on to istk->expansion.
3571 * After that, it will take care of propagating itself (a
3572 * macro-end marker line for a macro which is really a %rep
3573 * block will cause the macro to be re-expanded, complete
3574 * with another macro-end marker to ensure the process
3575 * continues) until the whole expansion is forcibly removed
3576 * from istk->expansion by a %exitrep.
3577 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003578 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003579 l->next = istk->expansion;
3580 l->finishes = defining;
3581 l->first = NULL;
3582 istk->expansion = l;
3583
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003584 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003585
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003586 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003587 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003588 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003589
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003591 /*
3592 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003593 * macro-end marker for a macro with no name. Then we set
3594 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003595 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003596 list_for_each(l, istk->expansion)
3597 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003598 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003599
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003600 if (l)
3601 l->finishes->in_progress = 1;
3602 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003603 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003604 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003605
H. Peter Anvin8b262472019-02-26 14:00:54 -08003606 case PP_DEFINE:
3607 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003608 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003609 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003610 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003611 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003612
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003613 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003614 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003615
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003616 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003617 lastp = &tline->next;
3618 nparam = parse_smacro_template(&lastp, &tmpl);
3619 tline = *lastp;
3620 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003621
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003622 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003623 macro_start = tline;
3624 if (!is_macro_id(macro_start)) {
3625 nasm_nonfatal("`%s' expects a macro identifier to alias",
3626 dname);
3627 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003628 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003629 tt = macro_start->next;
3630 macro_start->next = NULL;
3631 tline = tline->next;
3632 skip_white_(tline);
3633 if (tline && tline->type) {
3634 nasm_warn(WARN_OTHER,
3635 "trailing garbage after aliasing identifier ignored");
3636 }
3637 free_tlist(tt);
3638 tmpl.alias = true;
3639 } else {
3640 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003641 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003642 tline = expand_smacro(tline);
3643
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003644 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003645 macro_start = NULL;
3646 t = tline;
3647 while (t) {
3648 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003649 for (i = 0; i < nparam; i++) {
3650 if ((size_t)tmpl.params[i].namelen == t->len &&
3651 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3652 t->type = tok_smac_param(i);
3653 break;
3654 }
3655 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003656 }
3657 tt = t->next;
3658 t->next = macro_start;
3659 macro_start = t;
3660 t = tt;
3661 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003662 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003663
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 /*
3665 * Good. We now have a macro name, a parameter count, and a
3666 * token list (in reverse order) for an expansion. We ought
3667 * to be OK just to create an SMacro, store it, and let
3668 * free_tlist have the rest of the line (which we have
3669 * carefully re-terminated after chopping off the expansion
3670 * from the end).
3671 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003672 define_smacro(mname, casesense, macro_start, &tmpl);
3673 break;
3674 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003675
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003677 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003678 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003679 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003680 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003681 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003682
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003683 undef_smacro(mname, i == PP_UNDEFALIAS);
3684 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003685
H. Peter Anvin8b262472019-02-26 14:00:54 -08003686 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003687 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003688 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003689
H. Peter Anvin9e200162008-06-04 17:23:14 -07003690 last = tline;
3691 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003692 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003693
3694 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003695 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003696
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003698 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003700
3701 /*
3702 * We now have a macro name, an implicit parameter count of
3703 * zero, and a string token to use as an expansion. Create
3704 * and store an SMacro.
3705 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003706 define_smacro(mname, casesense, macro_start, NULL);
3707 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003708
H. Peter Anvin8b262472019-02-26 14:00:54 -08003709 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003710 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003711 goto done;
3712
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003713 last = tline;
3714 tline = expand_smacro(tline->next);
3715 last->next = NULL;
3716
3717 t = tline;
3718 while (tok_type_(t, TOK_WHITESPACE))
3719 t = t->next;
3720 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003721 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003722 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003723 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003724 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003725 }
3726
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003727 /*
3728 * Convert the string to a token stream. Note that smacros
3729 * are stored with the token stream reversed, so we have to
3730 * reverse the output of tokenize().
3731 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003732 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003733 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003734
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003735 /*
3736 * We now have a macro name, an implicit parameter count of
3737 * zero, and a numeric token to use as an expansion. Create
3738 * and store an SMacro.
3739 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003740 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003741 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003742 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003743
H. Peter Anvin418ca702008-05-30 10:42:30 -07003744 case PP_PATHSEARCH:
3745 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003746 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003747
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003748 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003749 goto done;
3750
H. Peter Anvin418ca702008-05-30 10:42:30 -07003751 last = tline;
3752 tline = expand_smacro(tline->next);
3753 last->next = NULL;
3754
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003755 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003756 while (tok_type_(t, TOK_WHITESPACE))
3757 t = t->next;
3758
3759 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003760 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003761 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003762 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003763 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003764 }
3765 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003766 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003767 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003768 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003769 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003770
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003771 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003772 if (!found_path)
3773 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003774 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003775
3776 /*
3777 * We now have a macro name, an implicit parameter count of
3778 * zero, and a string token to use as an expansion. Create
3779 * and store an SMacro.
3780 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003781 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003782 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003783 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003784 }
3785
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003787 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003788 goto done;
3789
H. Peter Anvine2c80182005-01-15 22:15:51 +00003790 last = tline;
3791 tline = expand_smacro(tline->next);
3792 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003793
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 t = tline;
3795 while (tok_type_(t, TOK_WHITESPACE))
3796 t = t->next;
3797 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003798 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003799 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003800 free_tlist(tline);
3801 free_tlist(origline);
3802 return DIRECTIVE_FOUND;
3803 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003804
H. Peter Anvin8b262472019-02-26 14:00:54 -08003805 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003806
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 /*
3808 * We now have a macro name, an implicit parameter count of
3809 * zero, and a numeric token to use as an expansion. Create
3810 * and store an SMacro.
3811 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003812 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003813 free_tlist(tline);
3814 free_tlist(origline);
3815 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003816
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003817 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003818 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003819 goto done;
3820
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003821 last = tline;
3822 tline = expand_smacro(tline->next);
3823 last->next = NULL;
3824
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003825 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003826 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003827 switch (t->type) {
3828 case TOK_WHITESPACE:
3829 break;
3830 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003831 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003832 break;
3833 case TOK_OTHER:
3834 if (!strcmp(t->text, ",")) /* permit comma separators */
3835 break;
3836 /* else fall through */
3837 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003838 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003839 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003840 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003841 }
3842 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003843
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003844 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003845 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003846 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003847 memcpy(p, t->text, t->len);
3848 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003849 }
3850 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003851
3852 /*
3853 * We now have a macro name, an implicit parameter count of
3854 * zero, and a numeric token to use as an expansion. Create
3855 * and store an SMacro.
3856 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003857 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003858 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003859 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003860 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003861 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003862
H. Peter Anvine2c80182005-01-15 22:15:51 +00003863 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003864 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003865 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003866 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003867
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003868 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003869 goto done;
3870
H. Peter Anvine2c80182005-01-15 22:15:51 +00003871 last = tline;
3872 tline = expand_smacro(tline->next);
3873 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003874
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003875 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003876 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003877 while (tok_type_(t, TOK_WHITESPACE))
3878 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003879
H. Peter Anvine2c80182005-01-15 22:15:51 +00003880 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003881 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003882 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003883 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003884 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003886
H. Peter Anvin8b262472019-02-26 14:00:54 -08003887 pps.tptr = t->next;
3888 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003889 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003890 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003891 if (!evalresult) {
3892 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003893 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003894 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003895 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003897 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003898 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003899 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003900
H. Peter Anvin8b262472019-02-26 14:00:54 -08003901 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3902 pps.tptr = pps.tptr->next;
3903 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003904 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003905 } else {
3906 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003907 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003908 if (!evalresult) {
3909 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003910 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003911 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003912 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003913 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003914 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003915 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003916 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003917 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003918
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003919 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003920
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003921 /* make start and count being in range */
3922 if (start < 0)
3923 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003924 if (count < 0)
3925 count = len + count + 1 - start;
3926 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003927 count = len - start;
3928 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003929 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003930
H. Peter Anvin8b262472019-02-26 14:00:54 -08003931 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003932 macro_start->len = count;
3933 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3934 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003935
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 /*
3937 * We now have a macro name, an implicit parameter count of
3938 * zero, and a numeric token to use as an expansion. Create
3939 * and store an SMacro.
3940 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003941 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003942 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003943 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003944 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003945
H. Peter Anvin8b262472019-02-26 14:00:54 -08003946 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003947 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003948 goto done;
3949
H. Peter Anvine2c80182005-01-15 22:15:51 +00003950 last = tline;
3951 tline = expand_smacro(tline->next);
3952 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003953
H. Peter Anvin8b262472019-02-26 14:00:54 -08003954 pps.tptr = tline;
3955 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003957 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003958 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003959 if (!evalresult)
3960 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003961
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003963 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003964
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003966 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003967 free_tlist(origline);
3968 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003969 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003970
H. Peter Anvin8b262472019-02-26 14:00:54 -08003971 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003972
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 /*
3974 * We now have a macro name, an implicit parameter count of
3975 * zero, and a numeric token to use as an expansion. Create
3976 * and store an SMacro.
3977 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003978 define_smacro(mname, casesense, macro_start, NULL);
3979 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003980
H. Peter Anvind2354082019-08-27 16:38:48 -07003981 case PP_ALIASES:
3982 tline = tline->next;
3983 tline = expand_smacro(tline);
3984 do_aliases = pp_get_boolean_option(tline, do_aliases);
3985 break;
3986
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 case PP_LINE:
3988 /*
3989 * Syntax is `%line nnn[+mmm] [filename]'
3990 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003991 if (unlikely(pp_noline))
3992 goto done;
3993
H. Peter Anvine2c80182005-01-15 22:15:51 +00003994 tline = tline->next;
3995 skip_white_(tline);
3996 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003997 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003998 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004000 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004001 m = 1;
4002 tline = tline->next;
4003 if (tok_is_(tline, "+")) {
4004 tline = tline->next;
4005 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004006 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004007 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004008 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004009 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004010 tline = tline->next;
4011 }
4012 skip_white_(tline);
4013 src_set_linnum(k);
4014 istk->lineinc = m;
4015 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004016 char *fname = detoken(tline, false);
4017 src_set_fname(fname);
4018 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004019 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004020 break;
4021 }
4022
4023done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004024 free_tlist(origline);
4025 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004026}
4027
4028/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004029 * Ensure that a macro parameter contains a condition code and
4030 * nothing else. Return the condition code index if so, or -1
4031 * otherwise.
4032 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004034{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004035 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004036
H. Peter Anvin25a99342007-09-22 17:45:45 -07004037 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004038 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004039
H. Peter Anvineba20a72002-04-30 20:53:55 +00004040 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03004041 if (!t)
4042 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004043 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004044 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004045 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004046 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004047 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004048 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004049
Cyrill Gorcunov19456392012-05-02 00:18:56 +04004050 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004051}
4052
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004053/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004054 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004055 * pasting, if @handle_explicit passed then explicit pasting
4056 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004057 * The @m array can contain a series of token types which are
4058 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004059 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004060static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004061 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004062{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004063 Token *tok, *next, **prev_next, **prev_nonspace;
4064 bool pasted = false;
4065 char *buf, *p;
4066 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004067
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004068 /*
4069 * The last token before pasting. We need it
4070 * to be able to connect new handled tokens.
4071 * In other words if there were a tokens stream
4072 *
4073 * A -> B -> C -> D
4074 *
4075 * and we've joined tokens B and C, the resulting
4076 * stream should be
4077 *
4078 * A -> BC -> D
4079 */
4080 tok = *head;
4081 prev_next = NULL;
4082
4083 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
4084 prev_nonspace = head;
4085 else
4086 prev_nonspace = NULL;
4087
4088 while (tok && (next = tok->next)) {
4089
4090 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004091 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004092 /* Zap redundant whitespaces */
4093 while (tok_type_(next, TOK_WHITESPACE))
4094 next = delete_Token(next);
4095 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004096 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004097
4098 case TOK_PASTE:
4099 /* Explicit pasting */
4100 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004101 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004102 next = delete_Token(tok);
4103
4104 while (tok_type_(next, TOK_WHITESPACE))
4105 next = delete_Token(next);
4106
4107 if (!pasted)
4108 pasted = true;
4109
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004110 /* Left pasting token is start of line */
4111 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004112 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004113
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004114 /*
4115 * No ending token, this might happen in two
4116 * cases
4117 *
4118 * 1) There indeed no right token at all
4119 * 2) There is a bare "%define ID" statement,
4120 * and @ID does expand to whitespace.
4121 *
4122 * So technically we need to do a grammar analysis
4123 * in another stage of parsing, but for now lets don't
4124 * change the behaviour people used to. Simply allow
4125 * whitespace after paste token.
4126 */
4127 if (!next) {
4128 /*
4129 * Zap ending space tokens and that's all.
4130 */
4131 tok = (*prev_nonspace)->next;
4132 while (tok_type_(tok, TOK_WHITESPACE))
4133 tok = delete_Token(tok);
4134 tok = *prev_nonspace;
4135 tok->next = NULL;
4136 break;
4137 }
4138
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004139 tok = *prev_nonspace;
4140 while (tok_type_(tok, TOK_WHITESPACE))
4141 tok = delete_Token(tok);
4142 len = strlen(tok->text);
4143 len += strlen(next->text);
4144
4145 p = buf = nasm_malloc(len + 1);
4146 strcpy(p, tok->text);
4147 p = strchr(p, '\0');
4148 strcpy(p, next->text);
4149
4150 delete_Token(tok);
4151
4152 tok = tokenize(buf);
4153 nasm_free(buf);
4154
4155 *prev_nonspace = tok;
4156 while (tok && tok->next)
4157 tok = tok->next;
4158
4159 tok->next = delete_Token(next);
4160
4161 /* Restart from pasted tokens head */
4162 tok = *prev_nonspace;
4163 break;
4164
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004165 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004166 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004167 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004168 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4169 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004170
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004171 len = 0;
4172 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4173 len += strlen(next->text);
4174 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004175 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004176
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004177 /* No match or no text to process */
4178 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004179 break;
4180
4181 len += strlen(tok->text);
4182 p = buf = nasm_malloc(len + 1);
4183
Adam Majer1a069432017-07-25 11:12:35 +02004184 strcpy(p, tok->text);
4185 p = strchr(p, '\0');
4186 tok = delete_Token(tok);
4187
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004188 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004189 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4190 strcpy(p, tok->text);
4191 p = strchr(p, '\0');
4192 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004193 tok = delete_Token(tok);
4194 }
4195
4196 tok = tokenize(buf);
4197 nasm_free(buf);
4198
4199 if (prev_next)
4200 *prev_next = tok;
4201 else
4202 *head = tok;
4203
4204 /*
4205 * Connect pasted into original stream,
4206 * ie A -> new-tokens -> B
4207 */
4208 while (tok && tok->next)
4209 tok = tok->next;
4210 tok->next = next;
4211
4212 if (!pasted)
4213 pasted = true;
4214
4215 /* Restart from pasted tokens head */
4216 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004217 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004218
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004219 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004220 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004221
4222 prev_next = &tok->next;
4223
4224 if (tok->next &&
4225 !tok_type_(tok->next, TOK_WHITESPACE) &&
4226 !tok_type_(tok->next, TOK_PASTE))
4227 prev_nonspace = prev_next;
4228
4229 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004230 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004231
4232 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004233}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004234
4235/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004236 * Computes the proper rotation of mmacro parameters
4237 */
4238static int mmac_rotate(const MMacro *mac, unsigned int n)
4239{
4240 if (--n < mac->nparam)
4241 n = (n + mac->rotate) % mac->nparam;
4242
4243 return n+1;
4244}
4245
4246/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004247 * expands to a list of tokens from %{x:y}
4248 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004249static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004250{
4251 Token *t = tline, **tt, *tm, *head;
4252 char *pos;
4253 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004254
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004255 pos = strchr(tline->text, ':');
4256 nasm_assert(pos);
4257
4258 lst = atoi(pos + 1);
4259 fst = atoi(tline->text + 1);
4260
4261 /*
4262 * only macros params are accounted so
4263 * if someone passes %0 -- we reject such
4264 * value(s)
4265 */
4266 if (lst == 0 || fst == 0)
4267 goto err;
4268
4269 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004270 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4271 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004272 goto err;
4273
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004274 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4275 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004276
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004277 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004278 fst--, lst--;
4279
4280 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004281 * It will be at least one token. Note we
4282 * need to scan params until separator, otherwise
4283 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004284 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004285 j = (fst + mac->rotate) % mac->nparam;
4286 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004287 if (!tm)
4288 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004289 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004290 tt = &head->next, tm = tm->next;
4291 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004292 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004293 *tt = t, tt = &t->next, tm = tm->next;
4294 }
4295
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004296 if (fst < lst) {
4297 for (i = fst + 1; i <= lst; i++) {
4298 t = new_Token(NULL, TOK_OTHER, ",", 0);
4299 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004300 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004301 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004302 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004303 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004304 *tt = t, tt = &t->next, tm = tm->next;
4305 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004306 }
4307 } else {
4308 for (i = fst - 1; i >= lst; i--) {
4309 t = new_Token(NULL, TOK_OTHER, ",", 0);
4310 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004311 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004312 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004313 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004314 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004315 *tt = t, tt = &t->next, tm = tm->next;
4316 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004317 }
4318 }
4319
4320 *last = tt;
4321 return head;
4322
4323err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004324 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004325 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004326 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004327}
4328
H. Peter Anvin76690a12002-04-30 20:52:49 +00004329/*
4330 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004331 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004332 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004333 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004334static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004335{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004336 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004337 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004338 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004339
4340 tail = &thead;
4341 thead = NULL;
4342
H. Peter Anvine2c80182005-01-15 22:15:51 +00004343 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004344 bool change;
4345 Token *t = tline;
4346 char *text = t->text;
4347 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004348
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004349 tline = tline->next;
4350 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004351
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004352 switch (type) {
4353 case TOK_PREPROC_ID:
4354 {
4355 Token *tt = NULL;
4356
4357 change = false;
4358
4359 if (!text || !text[0])
4360 break;
4361 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4362 ((text[1] == '+' || text[1] == '-') && text[2])))
4363 break;
4364
4365 change = true;
4366
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004367 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004368 nasm_nonfatal("`%s': not in a macro call", text);
4369 text = NULL;
4370 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004371 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004372
4373 if (strchr(text, ':')) {
4374 /*
4375 * seems we have a parameters range here
4376 */
4377 Token *head, **last;
4378 head = expand_mmac_params_range(mac, t, &last);
4379 if (head) {
4380 *tail = head;
4381 *last = tline;
4382 text = NULL;
4383 }
4384 break;
4385 }
4386
4387 switch (text[1]) {
4388 /*
4389 * We have to make a substitution of one of the
4390 * forms %1, %-1, %+1, %%foo, %0, %00.
4391 */
4392 case '0':
4393 if (!text[2]) {
4394 type = TOK_NUMBER;
4395 text = nasm_asprintf("%d", mac->nparam);
4396 break;
4397 }
4398 if (text[2] != '0' || text[3])
4399 goto invalid;
4400 /* a possible captured label == mac->params[0] */
4401 /* fall through */
4402 default:
4403 {
4404 unsigned long n;
4405 char *ep;
4406
4407 n = strtoul(text + 1, &ep, 10);
4408 if (unlikely(*ep))
4409 goto invalid;
4410
4411 if (n <= mac->nparam) {
4412 n = mmac_rotate(mac, n);
4413 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4414 }
4415 text = NULL;
4416 break;
4417 }
4418 case '%':
4419 type = TOK_ID;
4420 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4421 break;
4422 case '-':
4423 case '+':
4424 {
4425 int cc;
4426 unsigned long n;
4427 char *ep;
4428
4429 text = NULL;
4430
4431 n = strtoul(t->text + 2, &ep, 10);
4432 if (unlikely(*ep))
4433 goto invalid;
4434
4435 if (n && n < mac->nparam) {
4436 n = mmac_rotate(mac, n);
4437 tt = mac->params[n];
4438 }
4439 cc = find_cc(tt);
4440 if (cc == -1) {
4441 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvind2354082019-08-27 16:38:48 -07004442 t->text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004443 text = NULL;
4444 break;
4445 }
4446
4447 type = TOK_ID;
4448 if (text[1] == '-') {
4449 int ncc = inverse_ccs[cc];
4450 if (unlikely(ncc == -1)) {
4451 nasm_nonfatal("condition code `%s' is not invertible",
4452 conditions[cc]);
4453 break;
4454 }
4455 cc = ncc;
4456 }
4457 text = nasm_strdup(conditions[cc]);
4458 break;
4459 }
4460
4461 invalid:
4462 nasm_nonfatal("invalid macro parameter: `%s'", text);
4463 text = NULL;
4464 break;
4465 }
4466 break;
4467 }
4468
4469 case TOK_PREPROC_Q:
4470 if (mac) {
4471 type = TOK_ID;
4472 text = nasm_strdup(mac->iname);
4473 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004474 } else {
4475 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004476 }
4477 break;
4478
4479 case TOK_PREPROC_QQ:
4480 if (mac) {
4481 type = TOK_ID;
4482 text = nasm_strdup(mac->name);
4483 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004484 } else {
4485 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004486 }
4487 break;
4488
4489 case TOK_INDIRECT:
4490 {
4491 Token *tt;
4492
4493 tt = tokenize(t->text);
4494 tt = expand_mmac_params(tt);
4495 tt = expand_smacro(tt);
4496 /* Why dup_tlist() here? We should own tt... */
4497 dup_tlist(tt, &tail);
4498 text = NULL;
4499 change = true;
4500 break;
4501 }
4502
4503 default:
4504 change = false;
4505 break;
4506 }
4507
4508 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004509 if (!text) {
4510 delete_Token(t);
4511 } else {
4512 *tail = t;
4513 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004514 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004515 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004516 t->type = type;
4517 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004519 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004521 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004522 tail = &t->next;
4523 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004524 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004525
H. Peter Anvineba20a72002-04-30 20:53:55 +00004526 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004527
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004528 if (changed) {
4529 const struct tokseq_match t[] = {
4530 {
4531 PP_CONCAT_MASK(TOK_ID) |
4532 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4533 PP_CONCAT_MASK(TOK_ID) |
4534 PP_CONCAT_MASK(TOK_NUMBER) |
4535 PP_CONCAT_MASK(TOK_FLOAT) |
4536 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4537 },
4538 {
4539 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4540 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4541 }
4542 };
4543 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4544 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004545
H. Peter Anvin76690a12002-04-30 20:52:49 +00004546 return thead;
4547}
4548
H. Peter Anvin322bee02019-08-10 01:38:06 -07004549static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004550
H. Peter Anvin76690a12002-04-30 20:52:49 +00004551/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004552 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004553 * a macro at all, it is simply copied to the output and the pointer
4554 * advanced. tpp should be a pointer to a pointer (usually the next
4555 * pointer of the previous token) to the first token. **tpp is updated
4556 * to point to the last token of the expansion, and *tpp updated to
4557 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004558 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004559 * If the expansion is empty, *tpp will be unchanged but **tpp will
4560 * be advanced past the macro call.
4561 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004562 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004563 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004564static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004565{
4566 Token **params = NULL;
4567 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004568 Token *tline = **tpp;
4569 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004570 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004571 int i;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004572 Token *t, *tup, *ttail;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004573 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004574 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004575
4576 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004577 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004578
4579 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004580
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004581 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004582 smacro_deadman.levels--;
4583
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004584 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004585 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004586 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004587 smacro_deadman.triggered = true;
4588 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004589 goto not_a_macro;
4590 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004591 head = (SMacro *)hash_findix(&smacros, mname);
4592 } else if (tline->type == TOK_PREPROC_ID) {
4593 Context *ctx = get_ctx(mname, &mname);
4594 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4595 } else {
4596 goto not_a_macro;
4597 }
4598
4599 /*
4600 * We've hit an identifier of some sort. First check whether the
4601 * identifier is a single-line macro at all, then think about
4602 * checking for parameters if necessary.
4603 */
4604 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004605 if (unlikely(m->alias && !do_aliases))
4606 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004607 if (!mstrcmp(m->name, mname, m->casesense))
4608 break;
4609 }
4610
4611 if (!m) {
4612 goto not_a_macro;
4613 }
4614
4615 /* Parse parameters, if applicable */
4616
4617 params = NULL;
4618 nparam = 0;
4619
4620 if (m->nparam == 0) {
4621 /*
4622 * Simple case: the macro is parameterless.
4623 * Nothing to parse; the expansion code will
4624 * drop the macro name token.
4625 */
4626 } else {
4627 /*
4628 * Complicated case: at least one macro with this name
4629 * exists and takes parameters. We must find the
4630 * parameters in the call, count them, find the SMacro
4631 * that corresponds to that form of the macro call, and
4632 * substitute for the parameters when we expand. What a
4633 * pain.
4634 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004635 Token *t;
4636 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004637
4638 tline = tline->next;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004639 skip_white_(tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004640 if (!tok_is_(tline, "(")) {
4641 /*
4642 * This macro wasn't called with parameters: ignore
4643 * the call. (Behaviour borrowed from gnu cpp.)
4644 */
4645 goto not_a_macro;
4646 }
4647
4648 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004649 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004650 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004651 t = tline; /* tline points to leading ( */
4652
4653 while (paren) {
4654 t = t->next;
4655
4656 if (!t) {
4657 nasm_nonfatal("macro call expects terminating `)'");
4658 goto not_a_macro;
4659 }
4660
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004661 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004662 continue;
4663
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004664 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004665 case ',':
4666 if (!brackets)
4667 nparam++;
4668 break;
4669
4670 case '{':
4671 brackets++;
4672 break;
4673
4674 case '}':
4675 if (brackets > 0)
4676 brackets--;
4677 break;
4678
4679 case '(':
4680 if (!brackets)
4681 paren++;
4682 break;
4683
4684 case ')':
4685 if (!brackets)
4686 paren--;
4687 break;
4688
4689 default:
4690 break; /* Normal token */
4691 }
4692 }
4693
4694 /*
4695 * Look for a macro matching in both name and parameter count.
4696 * We already know any matches cannot be anywhere before the
4697 * current position of "m", so there is no reason to
4698 * backtrack.
4699 */
4700 while (1) {
4701 if (!m) {
4702 /*!
4703 *!macro-params-single [on] single-line macro calls with wrong parameter count
4704 *! warns about \i{single-line macros} being invoked
4705 *! with the wrong number of parameters.
4706 */
4707 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4708 "single-line macro `%s' exists, "
4709 "but not taking %d parameter%s",
4710 mname, nparam, (nparam == 1) ? "" : "s");
4711 goto not_a_macro;
4712 }
4713
4714 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004715 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004716 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004717 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004718 break; /* Also good */
4719 }
4720 m = m->next;
4721 }
4722 }
4723
4724 if (m->in_progress)
4725 goto not_a_macro;
4726
4727 /* Expand the macro */
4728 m->in_progress = true;
4729
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004730 if (nparam) {
4731 /* Extract parameters */
4732 Token **phead, **pep;
4733 int white = 0;
4734 int brackets = 0;
4735 int paren;
4736 bool bracketed = false;
4737 bool bad_bracket = false;
4738 enum sparmflags flags;
4739
4740 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004741 paren = 1;
4742 nasm_newn(params, nparam);
4743 i = 0;
4744 flags = m->params[i].flags;
4745 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004746 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004747
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004748 while (paren) {
4749 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004750 char ch;
4751
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004752 tline = tline->next;
4753
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004754 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004755 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004756
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004757 ch = 0;
4758 skip = false;
4759
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004760
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004761 switch (tline->type) {
4762 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004763 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004764 ch = tline->text[0];
4765 break;
4766
4767 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004768 if (!(flags & SPARM_NOSTRIP)) {
4769 if (brackets || *phead)
4770 white++; /* Keep interior whitespace */
4771 skip = true;
4772 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004773 break;
4774
4775 default:
4776 break;
4777 }
4778
4779 switch (ch) {
4780 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004781 if (!brackets && !(flags & SPARM_GREEDY)) {
4782 i++;
4783 nasm_assert(i < nparam);
4784 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004785 *pep = NULL;
4786 bracketed = false;
4787 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004788 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004789 }
4790 break;
4791
4792 case '{':
4793 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004794 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4795 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004796 }
4797 brackets++;
4798 break;
4799
4800 case '}':
4801 if (brackets > 0) {
4802 if (!--brackets)
4803 skip = bracketed;
4804 }
4805 break;
4806
4807 case '(':
4808 if (!brackets)
4809 paren++;
4810 break;
4811
4812 case ')':
4813 if (!brackets) {
4814 paren--;
4815 if (!paren) {
4816 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004817 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004818 }
4819 }
4820 break;
4821
4822 default:
4823 break; /* Normal token */
4824 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004825
4826 if (!skip) {
4827 Token *t;
4828
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004829 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004830
4831 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004832 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004833 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004834 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004835 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004836 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004837 pep = &t->next;
4838 white = 0;
4839 }
4840 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004841
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004842 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004843 * Possible further processing of parameters. Note that the
4844 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004845 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004846 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004847 enum sparmflags flags = m->params[i].flags;
4848
4849 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004850 /* Evaluate this parameter as a number */
4851 struct ppscan pps;
4852 struct tokenval tokval;
4853 expr *evalresult;
4854 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004855
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004856 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4857 pps.ntokens = -1;
4858 tokval.t_type = TOKEN_INVALID;
4859 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004860
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004861 free_tlist(eval_param);
4862 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004863
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004864 if (!evalresult) {
4865 /* Nothing meaningful to do */
4866 } else if (tokval.t_type) {
4867 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4868 } else if (!is_simple(evalresult)) {
4869 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4870 } else {
4871 params[i] = make_tok_num(reloc_value(evalresult));
4872 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004873 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004874
4875 if (flags & SPARM_STR) {
4876 /* Convert expansion to a quoted string */
4877 char *arg;
4878 Token *qs;
4879
4880 qs = expand_smacro_noreset(params[i]);
4881 arg = detoken(qs, false);
4882 free_tlist(qs);
4883 params[i] = make_tok_qstr(arg);
4884 nasm_free(arg);
4885 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004886 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004887 }
4888
4889 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004890 tline = tline->next; /* Remove the macro call from the input */
4891 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004892
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004893 /* Note: we own the expansion this returns. */
4894 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004895
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004896 tup = NULL;
4897 ttail = NULL; /* Pointer to the last token of the expansion */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004898 cond_comma = false;
4899
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004900 while (t) {
4901 enum pp_token_type type = t->type;
4902 Token *tnext = t->next;
4903 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004904
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004905 switch (type) {
4906 case TOK_PREPROC_Q:
4907 case TOK_PREPROC_QQ:
4908 t->type = TOK_ID;
4909 nasm_free(t->text);
4910 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4911 t->len = nasm_last_string_len();
4912 t->next = tline;
4913 break;
4914
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004915 case TOK_COND_COMMA:
4916 delete_Token(t);
4917 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4918 break;
4919
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004920 case TOK_ID:
4921 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004922 /*
4923 * Chain this into the target line *before* expanding,
4924 * that way we pick up any arguments to the new macro call,
4925 * if applicable.
4926 */
4927 t->next = tline;
4928 tp = &t;
4929 expand_one_smacro(&tp);
4930 if (t == tline)
4931 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004932 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004933
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004934 default:
4935 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004936 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004937 nasm_assert(!tup && param < nparam);
4938 delete_Token(t);
4939 t = NULL;
4940 tup = tnext;
4941 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004942 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004943 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004944 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004945 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004946 }
4947
4948 if (t) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004949 if (t->type != TOK_WHITESPACE)
4950 cond_comma = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004951 tline = t;
4952 if (!ttail)
4953 ttail = t;
4954 }
4955
4956 if (tnext) {
4957 t = tnext;
4958 } else {
4959 t = tup;
4960 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004961 }
4962 }
4963
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004964 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004965 if (ttail)
4966 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004967
4968 m->in_progress = false;
4969
4970 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004971 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004972 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004973
4974 /*
4975 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004976 * and then advance to the next input token. Note that this is
4977 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004978 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004979not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004980 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004981 m = NULL;
4982done:
4983 smacro_deadman.levels++;
4984 if (unlikely(params))
4985 free_tlist_array(params, nparam);
4986 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004987}
4988
4989/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004990 * Expand all single-line macro calls made in the given line.
4991 * Return the expanded version of the line. The original is deemed
4992 * to be destroyed in the process. (In reality we'll just move
4993 * Tokens from input to output a lot of the time, rather than
4994 * actually bothering to destroy and replicate.)
4995 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004996static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004997{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004998 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07004999 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5000 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005001 return expand_smacro_noreset(tline);
5002}
5003
5004static Token *expand_smacro_noreset(Token * tline)
5005{
5006 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005007 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005008 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005009
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005010 /*
5011 * Trick: we should avoid changing the start token pointer since it can
5012 * be contained in "next" field of other token. Because of this
5013 * we allocate a copy of first token and work with it; at the end of
5014 * routine we copy it back
5015 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005016 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04005017 tline = new_Token(org_tline->next, org_tline->type,
5018 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005019 nasm_free(org_tline->text);
5020 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005021 }
5022
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005023
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005024 /*
5025 * Pretend that we always end up doing expansion on the first pass;
5026 * that way %+ get processed. However, if we process %+ before the
5027 * first pass, we end up with things like MACRO %+ TAIL trying to
5028 * look up the macro "MACROTAIL", which we don't want.
5029 */
5030 expanded = true;
5031 thead = tline;
5032 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005033 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005034 {
5035 PP_CONCAT_MASK(TOK_ID) |
5036 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5037 PP_CONCAT_MASK(TOK_ID) |
5038 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5039 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5040 }
5041 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005042
5043 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005044 while ((t = *tail)) /* main token loop */
5045 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005046
5047 if (!expanded) {
5048 tline = thead;
5049 break; /* Done! */
5050 }
5051
5052 /*
5053 * Now scan the entire line and look for successive TOK_IDs
5054 * that resulted after expansion (they can't be produced by
5055 * tokenize()). The successive TOK_IDs should be concatenated.
5056 * Also we look for %+ tokens and concatenate the tokens
5057 * before and after them (without white spaces in between).
5058 */
H. Peter Anvind2354082019-08-27 16:38:48 -07005059 if (!paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true)) {
5060 tline = thead;
5061 break;
5062 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005063 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005064 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005065
H. Peter Anvine2c80182005-01-15 22:15:51 +00005066 if (org_tline) {
5067 if (thead) {
5068 *org_tline = *thead;
5069 /* since we just gave text to org_line, don't free it */
5070 thead->text = NULL;
5071 delete_Token(thead);
5072 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005073 /*
5074 * The expression expanded to empty line;
5075 * we can't return NULL because of the "trick" above.
5076 * Just set the line to a single WHITESPACE token.
5077 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005078 memset(org_tline, 0, sizeof(*org_tline));
5079 org_tline->text = NULL;
5080 org_tline->type = TOK_WHITESPACE;
5081 }
5082 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005083 }
5084
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005085 return thead;
5086}
5087
5088/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005089 * Similar to expand_smacro but used exclusively with macro identifiers
5090 * right before they are fetched in. The reason is that there can be
5091 * identifiers consisting of several subparts. We consider that if there
5092 * are more than one element forming the name, user wants a expansion,
5093 * otherwise it will be left as-is. Example:
5094 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005095 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005096 *
5097 * the identifier %$abc will be left as-is so that the handler for %define
5098 * will suck it and define the corresponding value. Other case:
5099 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005100 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005101 *
5102 * In this case user wants name to be expanded *before* %define starts
5103 * working, so we'll expand %$abc into something (if it has a value;
5104 * otherwise it will be left as-is) then concatenate all successive
5105 * PP_IDs into one.
5106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005107static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005108{
5109 Token *cur, *oldnext = NULL;
5110
H. Peter Anvin734b1882002-04-30 21:01:08 +00005111 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005112 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005113
5114 cur = tline;
5115 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005116 (cur->next->type == TOK_ID ||
5117 cur->next->type == TOK_PREPROC_ID
5118 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005119 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005120
5121 /* If identifier consists of just one token, don't expand */
5122 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005123 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005124
H. Peter Anvine2c80182005-01-15 22:15:51 +00005125 if (cur) {
5126 oldnext = cur->next; /* Detach the tail past identifier */
5127 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005128 }
5129
H. Peter Anvin734b1882002-04-30 21:01:08 +00005130 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005131
H. Peter Anvine2c80182005-01-15 22:15:51 +00005132 if (cur) {
5133 /* expand_smacro possibly changhed tline; re-scan for EOL */
5134 cur = tline;
5135 while (cur && cur->next)
5136 cur = cur->next;
5137 if (cur)
5138 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005139 }
5140
5141 return tline;
5142}
5143
5144/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005145 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005146 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005147 * to check for an initial label - that's taken care of in
5148 * expand_mmacro - but must check numbers of parameters. Guaranteed
5149 * to be called with tline->type == TOK_ID, so the putative macro
5150 * name is easy to find.
5151 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005152static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005153{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005154 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005155 Token **params;
5156 int nparam;
5157
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005158 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005159
5160 /*
5161 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005162 * name which isn't already excluded by macro cycle removal.
5163 * (The cycle removal test here helps optimize the case of wrapping
5164 * instructions, and is cheap to do here.)
5165 *
5166 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005167 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005168 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005169 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005170 list_for_each(m, head) {
5171 if (!mstrcmp(m->name, tline->text, m->casesense) &&
5172 (m->in_progress != 1 || m->max_depth > 0))
5173 break; /* Found something that needs consideration */
5174 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005175 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005176 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005177
5178 /*
5179 * OK, we have a potential macro. Count and demarcate the
5180 * parameters.
5181 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005182 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005183
5184 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005185 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005186 * structure that handles this number.
5187 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005188 while (m) {
5189 if (m->nparam_min <= nparam
5190 && (m->plus || nparam <= m->nparam_max)) {
5191 /*
5192 * This one is right. Just check if cycle removal
5193 * prohibits us using it before we actually celebrate...
5194 */
5195 if (m->in_progress > m->max_depth) {
5196 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005197 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005198 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005199 }
5200 nasm_free(params);
5201 return NULL;
5202 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005203 /*
5204 * It's right, and we can use it. Add its default
5205 * parameters to the end of our list if necessary.
5206 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005207 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005208 int newnparam = m->nparam_min + m->ndefs;
5209 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5210 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5211 (newnparam - nparam) * sizeof(*params));
5212 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005213 }
5214 /*
5215 * If we've gone over the maximum parameter count (and
5216 * we're in Plus mode), ignore parameters beyond
5217 * nparam_max.
5218 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005219 if (m->plus && nparam > m->nparam_max)
5220 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005221
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005222 /*
5223 * If nparam was adjusted above, make sure the list is still
5224 * NULL-terminated.
5225 */
5226 params[nparam+1] = NULL;
5227
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005228 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005229 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005230 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005231 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005232 }
5233 /*
5234 * This one wasn't right: look for the next one with the
5235 * same name.
5236 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005237 list_for_each(m, m->next)
5238 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005239 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005240 }
5241
5242 /*
5243 * After all that, we didn't find one with the right number of
5244 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005245 *!
5246 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5247 *! warns about \i{multi-line macros} being invoked
5248 *! with the wrong number of parameters. See \k{mlmacover} for an
5249 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005250 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005251 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5252 "multi-line macro `%s' exists, but not taking %d parameter%s",
5253 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005254 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005255 return NULL;
5256}
5257
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005258
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005259#if 0
5260
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005261/*
5262 * Save MMacro invocation specific fields in
5263 * preparation for a recursive macro expansion
5264 */
5265static void push_mmacro(MMacro *m)
5266{
5267 MMacroInvocation *i;
5268
5269 i = nasm_malloc(sizeof(MMacroInvocation));
5270 i->prev = m->prev;
5271 i->params = m->params;
5272 i->iline = m->iline;
5273 i->nparam = m->nparam;
5274 i->rotate = m->rotate;
5275 i->paramlen = m->paramlen;
5276 i->unique = m->unique;
5277 i->condcnt = m->condcnt;
5278 m->prev = i;
5279}
5280
5281
5282/*
5283 * Restore MMacro invocation specific fields that were
5284 * saved during a previous recursive macro expansion
5285 */
5286static void pop_mmacro(MMacro *m)
5287{
5288 MMacroInvocation *i;
5289
5290 if (m->prev) {
5291 i = m->prev;
5292 m->prev = i->prev;
5293 m->params = i->params;
5294 m->iline = i->iline;
5295 m->nparam = i->nparam;
5296 m->rotate = i->rotate;
5297 m->paramlen = i->paramlen;
5298 m->unique = i->unique;
5299 m->condcnt = i->condcnt;
5300 nasm_free(i);
5301 }
5302}
5303
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005304#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005305
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005306/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005307 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005308 */
5309static void list_mmacro_call(const MMacro *m)
5310{
5311 const char prefix[] = " ;;; [macro] ";
5312 size_t namelen, size;
5313 char *buf, *p;
5314 unsigned int i;
5315 const Token *t;
5316
5317 namelen = strlen(m->iname);
5318 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5319
5320 for (i = 1; i <= m->nparam; i++) {
5321 int j = 0;
5322 size += 3; /* Braces and space/comma */
5323 list_for_each(t, m->params[i]) {
5324 if (j++ >= m->paramlen[i])
5325 break;
5326 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5327 }
5328 }
5329
5330 buf = p = nasm_malloc(size);
5331 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5332 p = mempcpy(p, m->iname, namelen);
5333 *p++ = ' ';
5334
5335 for (i = 1; i <= m->nparam; i++) {
5336 int j = 0;
5337 *p++ = '{';
5338 list_for_each(t, m->params[i]) {
5339 if (j++ >= m->paramlen[i])
5340 break;
5341 if (!t->text) {
5342 if (t->type == TOK_WHITESPACE)
5343 *p++ = ' ';
5344 } else {
5345 p = mempcpy(p, t->text, t->len);
5346 }
5347 }
5348 *p++ = '}';
5349 *p++ = ',';
5350 }
5351
5352 *--p = '\0'; /* Replace last delimeter with null */
5353 lfmt->line(LIST_MACRO, -1, buf);
5354 nasm_free(buf);
5355}
5356
5357/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005358 * Expand the multi-line macro call made by the given line, if
5359 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005360 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005361 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005362static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005363{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005364 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005365 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005366 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005367 Token **params, *t, *tt;
5368 MMacro *m;
5369 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005370 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005371 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005372 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005373
5374 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005375 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005376 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005377 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005378 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005379 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005380 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005381 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005382 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005383 Token *last;
5384 /*
5385 * We have an id which isn't a macro call. We'll assume
5386 * it might be a label; we'll also check to see if a
5387 * colon follows it. Then, if there's another id after
5388 * that lot, we'll check it again for macro-hood.
5389 */
5390 label = last = t;
5391 t = t->next;
5392 if (tok_type_(t, TOK_WHITESPACE))
5393 last = t, t = t->next;
5394 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005395 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005396 last = t, t = t->next;
5397 if (tok_type_(t, TOK_WHITESPACE))
5398 last = t, t = t->next;
5399 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005400 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005401 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005402 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005403 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005404 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005405 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005406
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005407 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5408 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5409 if (!mmacro_deadman.triggered) {
5410 nasm_nonfatal("interminable multiline macro recursion");
5411 mmacro_deadman.triggered = true;
5412 }
5413 return 0;
5414 }
5415
5416 mmacro_deadman.total++;
5417 mmacro_deadman.levels++;
5418
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005419 /*
5420 * Fix up the parameters: this involves stripping leading and
5421 * trailing whitespace, then stripping braces if they are
5422 * present.
5423 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005424 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005425
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005426 nasm_assert(params[nparam+1] == NULL);
5427
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005428 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005429 int brace = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005430 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005431
H. Peter Anvine2c80182005-01-15 22:15:51 +00005432 skip_white_(t);
5433 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005434 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005435 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005436 while (t) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005437 if (comma) {
5438 /* Check if we hit a comma that ends this argument */
5439 if (tok_is_(t, ","))
5440 break;
5441 else if (t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
5442 break;
5443 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005444 if (brace && t->type == TOK_OTHER) {
5445 if (t->text[0] == '{')
5446 brace++; /* ... or a nested opening brace */
5447 else if (t->text[0] == '}')
5448 if (!--brace)
5449 break; /* ... or a brace */
5450 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005451 t = t->next;
5452 paramlen[i]++;
5453 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005454 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005455 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005456 }
5457
5458 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005459 * OK, we have a MMacro structure together with a set of
5460 * parameters. We must now go through the expansion and push
5461 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005462 * parameter tokens and macro-local tokens doesn't get done
5463 * until the single-line macro substitution process; this is
5464 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005465 * later through %rotate and give the right semantics for
5466 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005467 *
5468 * First, push an end marker on to istk->expansion, mark this
5469 * macro as in progress, and set up its invocation-specific
5470 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005471 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005472 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005473 ll->next = istk->expansion;
5474 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005475 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005476
5477 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005478 * Save the previous MMacro expansion in the case of
5479 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005480 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005481#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005482 if (m->max_depth && m->in_progress)
5483 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005484#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005485
5486 m->in_progress ++;
5487 m->params = params;
5488 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005489 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005490 m->nparam = nparam;
5491 m->rotate = 0;
5492 m->paramlen = paramlen;
5493 m->unique = unique++;
5494 m->lineno = 0;
5495 m->condcnt = 0;
5496
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005497 m->mstk = istk->mstk;
5498 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005499
5500 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005501 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005502 ll->next = istk->expansion;
5503 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005504 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005505 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005506
5507 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005508 * If we had a label, and this macro definition does not include
5509 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005510 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005511 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005512 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005513 /*
5514 * We had a label. If this macro contains an %00 parameter,
5515 * save the value as a special parameter (which is what it
5516 * is), otherwise push it as the first line of the macro
5517 * expansion.
5518 */
5519 if (m->capture_label) {
5520 params[0] = dup_Token(NULL, label);
5521 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005522 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005523 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005524 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005525 ll->finishes = NULL;
5526 ll->next = istk->expansion;
5527 istk->expansion = ll;
5528 ll->first = startline;
5529 if (!dont_prepend) {
5530 while (label->next)
5531 label = label->next;
5532 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005533 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005534 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005535 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005536
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005537 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005538
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005539 if (list_option('m') && !m->nolist)
5540 list_mmacro_call(m);
5541
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005542 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005543}
5544
H. Peter Anvin130736c2016-02-17 20:27:41 -08005545/*
5546 * This function adds macro names to error messages, and suppresses
5547 * them if necessary.
5548 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005549static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005550{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005551 /*
5552 * If we're in a dead branch of IF or something like it, ignore the error.
5553 * However, because %else etc are evaluated in the state context
5554 * of the previous branch, errors might get lost:
5555 * %if 0 ... %else trailing garbage ... %endif
5556 * So %else etc should set the ERR_PP_PRECOND flag.
5557 */
5558 if ((severity & ERR_MASK) < ERR_FATAL &&
5559 istk && istk->conds &&
5560 ((severity & ERR_PP_PRECOND) ?
5561 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005562 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005563 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005564
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005565 /* This doesn't make sense with the macro stack unwinding */
5566 if (0) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005567 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005568
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005569 /* get %macro name */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005570 if (!(severity & ERR_NOFILE) && istk && istk->mstk.mmac) {
5571 MMacro *mmac = istk->mstk.mmac;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005572 char *buf;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005573
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005574 nasm_set_verror(real_verror);
5575 buf = nasm_vasprintf(fmt, arg);
5576 nasm_error(severity, "(%s:%"PRId32") %s",
5577 mmac->name, mmac->lineno - delta, buf);
5578 nasm_set_verror(pp_verror);
5579 nasm_free(buf);
5580 return;
5581 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005582 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005583 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005584}
5585
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005586static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005587stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005588{
5589 (void)s;
5590 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005591 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005592
5593 return make_tok_qstr(src_get_fname());
5594}
5595
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005596static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005597stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005598{
5599 (void)s;
5600 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005601 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005602
5603 return make_tok_num(src_get_linnum());
5604}
5605
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005606static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005607stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005608{
5609 (void)s;
5610 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005611 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005612
5613 return make_tok_num(globalbits);
5614}
5615
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005616static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005617stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005618{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005619 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005620 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5621 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5622 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005623
5624 (void)s;
5625 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005626 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005627
5628 switch (globalbits) {
5629 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005630 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005631 break;
5632 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005633 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005634 break;
5635 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005636 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005637 break;
5638 default:
5639 panic();
5640 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005641
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005642 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005643}
5644
H. Peter Anvin8b262472019-02-26 14:00:54 -08005645/* Add magic standard macros */
5646struct magic_macros {
5647 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005648 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005649 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005650};
5651static const struct magic_macros magic_macros[] =
5652{
H. Peter Anvind2354082019-08-27 16:38:48 -07005653 { "__?FILE?__", 0, stdmac_file },
5654 { "__?LINE?__", 0, stdmac_line },
5655 { "__?BITS?__", 0, stdmac_bits },
5656 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005657 { NULL, 0, NULL }
5658};
5659
5660static void pp_add_magic_stdmac(void)
5661{
5662 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005663 SMacro tmpl;
5664
5665 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005666
5667 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005668 tmpl.nparam = m->nparam;
5669 tmpl.expand = m->func;
5670 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005671 }
5672}
5673
H. Peter Anvin734b1882002-04-30 21:01:08 +00005674static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005675pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005676{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005677 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005678 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005679
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005680 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005681 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005682 nested_mac_count = 0;
5683 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005684 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005685 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005686 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005687 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07005688 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005689
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005690 if (!use_loaded)
5691 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5692 memset(use_loaded, 0, use_package_count * sizeof(bool));
5693
H. Peter Anvin6686de22019-08-10 05:33:14 -07005694 /* First set up the top level input file */
5695 nasm_new(istk);
5696 istk->fp = nasm_open_read(file, NF_TEXT);
5697 src_set(0, file);
5698 istk->lineinc = 1;
5699 if (!istk->fp)
5700 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5701
5702 strlist_add(deplist, file);
5703
5704 /*
5705 * Set up the stdmac packages as a virtual include file,
5706 * indicated by a null file pointer.
5707 */
5708 nasm_new(inc);
5709 inc->next = istk;
5710 inc->fname = src_set_fname(NULL);
5711 inc->nolist = !list_option('b');
5712 istk = inc;
5713 lfmt->uplevel(LIST_INCLUDE, 0);
5714
H. Peter Anvin8b262472019-02-26 14:00:54 -08005715 pp_add_magic_stdmac();
5716
H. Peter Anvinf7606612016-07-13 14:23:48 -07005717 if (tasm_compatible_mode)
5718 pp_add_stdmac(nasm_stdmac_tasm);
5719
5720 pp_add_stdmac(nasm_stdmac_nasm);
5721 pp_add_stdmac(nasm_stdmac_version);
5722
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005723 if (extrastdmac)
5724 pp_add_stdmac(extrastdmac);
5725
H. Peter Anvinf7606612016-07-13 14:23:48 -07005726 stdmacpos = stdmacros[0];
5727 stdmacnext = &stdmacros[1];
5728
H. Peter Anvind2456592008-06-19 15:04:18 -07005729 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005730
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005731 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07005732 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005733 * other builtins, because it is special -- it varies between
5734 * passes -- but there is really no particular reason to make it
5735 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005736 *
5737 * 0 = dependencies only
5738 * 1 = preparatory passes
5739 * 2 = final pass
5740 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005741 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005742 switch (mode) {
5743 case PP_NORMAL:
5744 apass = pass_final() ? 2 : 1;
5745 break;
5746 case PP_DEPS:
5747 apass = 0;
5748 break;
5749 case PP_PREPROC:
5750 apass = 3;
5751 break;
5752 default:
5753 panic();
5754 }
5755
H. Peter Anvind2354082019-08-27 16:38:48 -07005756 define_smacro("__?PASS?__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005757}
5758
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005759static void pp_init(void)
5760{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005761}
5762
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005763/*
5764 * Get a line of tokens. If we popped the macro expansion/include stack,
5765 * we return a pointer to the dummy token tok_pop; at that point if
5766 * istk is NULL then we have reached end of input;
5767 */
5768static Token tok_pop; /* Dummy token placeholder */
5769
5770static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005771{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005772 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005773 Line *l = istk->expansion;
5774 Token *tline = NULL;
5775 Token *dtline;
5776
H. Peter Anvine2c80182005-01-15 22:15:51 +00005777 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005778 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005779 * buffer or from the input file.
5780 */
5781 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005782 while (l && l->finishes) {
5783 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005784
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005785 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005786 /*
5787 * This is a macro-end marker for a macro with no
5788 * name, which means it's not really a macro at all
5789 * but a %rep block, and the `in_progress' field is
5790 * more than 1, meaning that we still need to
5791 * repeat. (1 means the natural last repetition; 0
5792 * means termination by %exitrep.) We have
5793 * therefore expanded up to the %endrep, and must
5794 * push the whole block on to the expansion buffer
5795 * again. We don't bother to remove the macro-end
5796 * marker: we'd only have to generate another one
5797 * if we did.
5798 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005799 fm->in_progress--;
5800 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005801 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005802 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005803
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005804 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005805 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005806 tail = &ll->first;
5807
5808 list_for_each(t, l->first) {
5809 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005810 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005811 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005812 }
5813 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005814 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005815 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005816 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005817 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005818 MMacro *m = istk->mstk.mstk;
5819
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005820 /*
5821 * Check whether a `%rep' was started and not ended
5822 * within this macro expansion. This can happen and
5823 * should be detected. It's a fatal error because
5824 * I'm too confused to work out how to recover
5825 * sensibly from it.
5826 */
5827 if (defining) {
5828 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005829 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005830 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005831 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005832 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005833 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005834
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005835 /*
5836 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005837 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005838 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005839 istk->mstk = m->mstk;
5840 if (m->name) {
5841 /*
5842 * This was a real macro call, not a %rep, and
5843 * therefore the parameter information needs to
5844 * be freed and the iteration count/nesting
5845 * depth adjusted.
5846 */
5847
5848 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005849 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005850 * If all mmacro processing done,
5851 * clear all counters and the deadman
5852 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005853 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005854 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005855 }
5856
Adam Majer91e72402017-07-25 10:42:01 +02005857#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005858 if (m->prev) {
5859 pop_mmacro(m);
5860 fm->in_progress --;
5861 } else
Adam Majer91e72402017-07-25 10:42:01 +02005862#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005863 {
5864 nasm_free(m->params);
5865 free_tlist(m->iline);
5866 nasm_free(m->paramlen);
5867 fm->in_progress = 0;
5868 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005869 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005870
5871 /*
5872 * FIXME It is incorrect to always free_mmacro here.
5873 * It leads to usage-after-free.
5874 *
5875 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5876 */
5877#if 0
5878 else
5879 free_mmacro(m);
5880#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005881 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005882 istk->expansion = l->next;
5883 nasm_free(l);
5884 lfmt->downlevel(LIST_MACRO);
5885 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005886 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005887
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005888 do { /* until we get a line we can use */
5889 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005890
5891 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005892 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005893 int32_t lineno;
5894
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005895 if (istk->mstk.mstk) {
5896 istk->mstk.mstk->lineno++;
5897 if (istk->mstk.mstk->fname)
5898 lineno = istk->mstk.mstk->lineno +
5899 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005900 else
5901 lineno = 0; /* Defined at init time or builtin */
5902 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005903 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005904 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005905
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005906 tline = l->first;
5907 istk->expansion = l->next;
5908 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005909
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005910 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005911 if (!istk->nolist)
5912 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005913 nasm_free(line);
5914 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005915 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005916 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005917 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005918 } else {
5919 /*
5920 * The current file has ended; work down the istk
5921 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005922 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005923 if (i->fp)
5924 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005925 if (i->conds) {
5926 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005927 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005928 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005929 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005930 if (i->next)
5931 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005932 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005933 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005934 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005935 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005936 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005937 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005938
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005939 /*
5940 * We must expand MMacro parameters and MMacro-local labels
5941 * _before_ we plunge into directive processing, to cope
5942 * with things like `%define something %1' such as STRUC
5943 * uses. Unless we're _defining_ a MMacro, in which case
5944 * those tokens should be left alone to go into the
5945 * definition; and unless we're in a non-emitting
5946 * condition, in which case we don't want to meddle with
5947 * anything.
5948 */
5949 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005950 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005951 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005952 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005953
H. Peter Anvine2c80182005-01-15 22:15:51 +00005954 /*
5955 * Check the line to see if it's a preprocessor directive.
5956 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005957 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5958 if (dtline)
5959 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005960 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005961 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005962 * We're defining a multi-line macro. We emit nothing
5963 * at all, and just
5964 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005965 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005966 MMacro *mmac = defining->dstk.mmac;
5967
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005968 Line *l = nasm_malloc(sizeof(Line));
5969 l->next = defining->expansion;
5970 l->first = tline;
5971 l->finishes = NULL;
5972 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005973
5974 /*
5975 * Remember if this mmacro expansion contains %00:
5976 * if it does, we will have to handle leading labels
5977 * specially.
5978 */
5979 if (mmac) {
5980 const Token *t;
5981 list_for_each(t, tline) {
5982 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
5983 mmac->capture_label = true;
5984 }
5985 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005986 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005987 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005988 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005989 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005990 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005991 * directive so we keep our place correctly.
5992 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005993 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005994 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005995 /*
5996 * We're in a %rep block which has been terminated, so
5997 * we're walking through to the %endrep without
5998 * emitting anything. Emit nothing at all, not even a
5999 * blank line: when we emerge from the %rep block we'll
6000 * give a line-number directive so we keep our place
6001 * correctly.
6002 */
6003 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006004 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006005 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006006 if (!expand_mmacro(tline))
6007 return tline;
6008 }
6009 }
6010}
6011
6012static char *pp_getline(void)
6013{
6014 char *line = NULL;
6015 Token *tline;
6016
6017 real_verror = nasm_set_verror(pp_verror);
6018
6019 while (true) {
6020 tline = pp_tokline();
6021 if (tline == &tok_pop) {
6022 /*
6023 * We popped the macro/include stack. If istk is empty,
6024 * we are at end of input, otherwise just loop back.
6025 */
6026 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006027 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006028 } else {
6029 /*
6030 * De-tokenize the line and emit it.
6031 */
6032 line = detoken(tline, true);
6033 free_tlist(tline);
6034 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006035 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006036 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006037
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006038 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006039 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006040 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006041 nasm_free(buf);
6042 }
6043
H. Peter Anvin130736c2016-02-17 20:27:41 -08006044 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006045 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006046}
6047
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006048static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006049{
H. Peter Anvin130736c2016-02-17 20:27:41 -08006050 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08006051
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006052 if (defining) {
6053 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006054 nasm_nonfatal("end of file while still defining macro `%s'",
6055 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006056 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006057 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006058 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006059
6060 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006061 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006062 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006063
6064 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08006065
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006066 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006067 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006068 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006069 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006070 Include *i = istk;
6071 istk = istk->next;
6072 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006073 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006074 }
6075 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006076 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006077 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006078}
6079
6080static void pp_cleanup_session(void)
6081{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006082 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006083 free_llist(predef);
6084 predef = NULL;
6085 delete_Blocks();
6086 freeTokens = NULL;
6087 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006088}
6089
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006090static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006091{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006092 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006093}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006094
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006095static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006096{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006097 Token *inc, *space, *name;
6098 Line *l;
6099
H. Peter Anvin734b1882002-04-30 21:01:08 +00006100 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
6101 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
6102 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006103
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006104 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006105 l->next = predef;
6106 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006107 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006108 predef = l;
6109}
6110
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006111static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006112{
6113 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006114 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006115 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006116
H. Peter Anvin130736c2016-02-17 20:27:41 -08006117 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08006118
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006119 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00006120 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6121 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006122 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006123 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006124 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006125 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006126 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006127
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006128 if (space->next->type != TOK_PREPROC_ID &&
6129 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006130 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006131
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006132 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006133 l->next = predef;
6134 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006135 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006136 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08006137
6138 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006139}
6140
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006141static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006142{
6143 Token *def, *space;
6144 Line *l;
6145
H. Peter Anvin734b1882002-04-30 21:01:08 +00006146 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6147 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006148 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006149
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006150 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006151 l->next = predef;
6152 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006153 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006154 predef = l;
6155}
6156
H. Peter Anvin05990342018-06-11 13:32:42 -07006157/* Insert an early preprocessor command that doesn't need special handling */
6158static void pp_pre_command(const char *what, char *string)
6159{
6160 char *cmd;
6161 Token *def, *space;
6162 Line *l;
6163
6164 def = tokenize(string);
6165 if (what) {
6166 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6167 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
6168 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6169 }
6170
6171 l = nasm_malloc(sizeof(Line));
6172 l->next = predef;
6173 l->first = def;
6174 l->finishes = NULL;
6175 predef = l;
6176}
6177
H. Peter Anvinf7606612016-07-13 14:23:48 -07006178static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006179{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006180 macros_t **mp;
6181
6182 /* Find the end of the list and avoid duplicates */
6183 for (mp = stdmacros; *mp; mp++) {
6184 if (*mp == macros)
6185 return; /* Nothing to do */
6186 }
6187
6188 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6189
6190 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006191}
6192
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006193static void pp_extra_stdmac(macros_t *macros)
6194{
6195 extrastdmac = macros;
6196}
6197
H. Peter Anvin8b262472019-02-26 14:00:54 -08006198static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006199{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006200 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006201 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6202 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6203}
6204
6205static Token *make_tok_qstr(const char *str)
6206{
6207 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006208 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006209 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006210}
6211
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006212static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006213{
6214 if (!m)
6215 return;
6216
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006217 /* We need to print the mstk.mmac list in reverse order */
6218 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006219
6220 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006221 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006222 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006223 }
6224}
6225
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006226static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006227{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006228 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006229
H. Peter Anvinddb29062018-12-11 00:06:29 -08006230 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6231 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006232
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006233 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006234 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006235
H. Peter Anvinddb29062018-12-11 00:06:29 -08006236 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006237}
6238
H. Peter Anvine7469712016-02-18 02:20:59 -08006239const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006240 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006241 pp_reset,
6242 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006243 pp_cleanup_pass,
6244 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006245 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006246 pp_pre_define,
6247 pp_pre_undefine,
6248 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006249 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006250 pp_include_path,
6251 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006252};