blob: 1828c8a8a4359aef1e22dd4552e3bb4e4b87fab7 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000084typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000085typedef struct Line Line;
86typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080087typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088
89/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070090 * Note on the storage of both SMacro and MMacros: the hash table
91 * indexes them case-insensitively, and we then have to go through a
92 * linked list of potential case aliases (and, for MMacros, parameter
93 * ranges); this is to preserve the matching semantics of the earlier
94 * code. If the number of case aliases for a specific macro is a
95 * performance issue, you may want to reconsider your coding style.
96 */
97
98/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070099 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700100 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700101typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700102
103/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104 * Store the definition of a single-line macro.
105 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700106enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700107 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700108 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
109 SPARM_STR = 2, /* Convert to quoted string ($) */
110 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
111 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
112};
113
114struct smac_param {
115 char *name;
116 int namelen;
117 enum sparmflags flags;
118};
119
H. Peter Anvine2c80182005-01-15 22:15:51 +0000120struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800121 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800122 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700123 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700124 ExpandSMacro expand;
125 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700126 struct smac_param *params;
127 int nparam;
128 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800129 bool casesense;
130 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700131 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000132};
133
134/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800135 * Store the definition of a multi-line macro. This is also used to
136 * store the interiors of `%rep...%endrep' blocks, which are
137 * effectively self-re-invoking multi-line macros which simply
138 * don't have a name or bother to appear in the hash tables. %rep
139 * blocks are signified by having a NULL `name' field.
140 *
141 * In a MMacro describing a `%rep' block, the `in_progress' field
142 * isn't merely boolean, but gives the number of repeats left to
143 * run.
144 *
145 * The `next' field is used for storing MMacros in hash tables; the
146 * `next_active' field is for stacking them on istk entries.
147 *
148 * When a MMacro is being expanded, `params', `iline', `nparam',
149 * `paramlen', `rotate' and `unique' are local to the invocation.
150 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700151
152/*
153 * Expansion stack. Note that .mmac can point back to the macro itself,
154 * whereas .mstk cannot.
155 */
156struct mstk {
157 MMacro *mstk; /* Any expansion, real macro or not */
158 MMacro *mmac; /* Highest level actual mmacro */
159};
160
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800161struct MMacro {
162 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700163#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800164 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700165#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800166 char *name;
167 int nparam_min, nparam_max;
168 bool casesense;
169 bool plus; /* is the last parameter greedy? */
170 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700171 bool capture_label; /* macro definition has %00; capture label */
172 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800173 int32_t max_depth; /* maximum number of recursive expansions allowed */
174 Token *dlist; /* All defaults as one list */
175 Token **defaults; /* Parameter default pointers */
176 int ndefs; /* number of default parameters */
177 Line *expansion;
178
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700179 struct mstk mstk; /* Macro expansion stack */
180 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800181 Token **params; /* actual parameters */
182 Token *iline; /* invocation line */
183 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700184 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800185 int *paramlen;
186 uint64_t unique;
187 int lineno; /* Current line number on expansion */
188 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700189
H. Peter Anvin274cda82016-05-10 02:56:29 -0700190 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700191 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800192};
193
194
195/* Store the definition of a multi-line macro, as defined in a
196 * previous recursive macro expansion.
197 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700198#if 0
199
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800200struct MMacroInvocation {
201 MMacroInvocation *prev; /* previous invocation */
202 Token **params; /* actual parameters */
203 Token *iline; /* invocation line */
204 unsigned int nparam, rotate;
205 int *paramlen;
206 uint64_t unique;
207 uint64_t condcnt;
208};
209
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700210#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211
212/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000213 * The context stack is composed of a linked list of these.
214 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000215struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800216 Context *next;
217 char *name;
218 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700219 uint64_t number;
220 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000221};
222
223/*
224 * This is the internal form which we break input lines up into.
225 * Typically stored in linked lists.
226 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800227 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
228 * not necessarily used as-is, but is also used to encode the number
229 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000230 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800231 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700232 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000233 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800234 * tok_smac_param(0) but the one representing `y' will be
235 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000236 *
237 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
238 * which doesn't need quotes around it. Used in the pre-include
239 * mechanism as an alternative to trying to find a sensible type of
240 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000241 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000242enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
244 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800245 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700246 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800247 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300248 TOK_PASTE, /* %+ */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700249 TOK_COND_COMMA, /* %, */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300250 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800251 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300252 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000253};
254
H. Peter Anvin8b262472019-02-26 14:00:54 -0800255static inline enum pp_token_type tok_smac_param(int param)
256{
257 return TOK_SMAC_START_PARAMS + param;
258}
259static int smac_nparam(enum pp_token_type toktype)
260{
261 return toktype - TOK_SMAC_START_PARAMS;
262}
263static bool is_smac_param(enum pp_token_type toktype)
264{
265 return toktype >= TOK_SMAC_START_PARAMS;
266}
267
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400268#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400269#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400270
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400271struct tokseq_match {
272 int mask_head;
273 int mask_tail;
274};
275
H. Peter Anvine2c80182005-01-15 22:15:51 +0000276struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800277 Token *next;
278 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700279 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800280 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000281};
282
283/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800284 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000285 * these, which is essentially a container to allow several linked
286 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700287 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000288 * Note that in this module, linked lists are treated as stacks
289 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800290 * `expansion' field in MMacro structures, so that the linked list,
291 * if walked, would give the macro lines in reverse order; this
292 * means that we can walk the list when expanding a macro, and thus
293 * push the lines on to the `expansion' field in _istk_ in reverse
294 * order (so that when popped back off they are in the right
295 * order). It may seem cockeyed, and it relies on my design having
296 * an even number of steps in, but it works...
297 *
298 * Some of these structures, rather than being actual lines, are
299 * markers delimiting the end of the expansion of a given macro.
300 * This is for use in the cycle-tracking and %rep-handling code.
301 * Such structures have `finishes' non-NULL, and `first' NULL. All
302 * others have `finishes' NULL, but `first' may still be NULL if
303 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000304 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000305struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800306 Line *next;
307 MMacro *finishes;
308 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500309};
310
311/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 * To handle an arbitrary level of file inclusion, we maintain a
313 * stack (ie linked list) of these things.
314 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000315struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800316 Include *next;
317 FILE *fp;
318 Cond *conds;
319 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700320 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700321 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700322 int lineno, lineinc;
323 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324};
325
326/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700327 * File real name hash, so we don't have to re-search the include
328 * path for every pass (and potentially more than that if a file
329 * is used more than once.)
330 */
331struct hash_table FileHash;
332
333/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700334 * Counters to trap on insane macro recursion or processing.
335 * Note: for smacros these count *down*, for mmacros they count *up*.
336 */
337struct deadman {
338 int64_t total; /* Total number of macros/tokens */
339 int64_t levels; /* Descent depth across all macros */
340 bool triggered; /* Already triggered, no need for error msg */
341};
342
343static struct deadman smacro_deadman, mmacro_deadman;
344
345/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 * Conditional assembly: we maintain a separate stack of these for
347 * each level of file inclusion. (The only reason we keep the
348 * stacks separate is to ensure that a stray `%endif' in a file
349 * included from within the true branch of a `%if' won't terminate
350 * it and cause confusion: instead, rightly, it'll cause an error.)
351 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700352enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353 /*
354 * These states are for use just after %if or %elif: IF_TRUE
355 * means the condition has evaluated to truth so we are
356 * currently emitting, whereas IF_FALSE means we are not
357 * currently emitting but will start doing so if a %else comes
358 * up. In these states, all directives are admissible: %elif,
359 * %else and %endif. (And of course %if.)
360 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800361 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362 /*
363 * These states come up after a %else: ELSE_TRUE means we're
364 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
365 * any %elif or %else will cause an error.
366 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800367 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200369 * These states mean that we're not emitting now, and also that
370 * nothing until %endif will be emitted at all. COND_DONE is
371 * used when we've had our moment of emission
372 * and have now started seeing %elifs. COND_NEVER is used when
373 * the condition construct in question is contained within a
374 * non-emitting branch of a larger condition construct,
375 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000376 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800377 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000378};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700379struct Cond {
380 Cond *next;
381 enum cond_state state;
382};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800383#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000384
H. Peter Anvin70653092007-10-19 14:42:29 -0700385/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000386 * These defines are used as the possible return values for do_directive
387 */
388#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300389#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000390
Keith Kanios852f1ee2009-07-12 00:19:55 -0500391/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392 * Condition codes. Note that we use c_ prefix not C_ because C_ is
393 * used in nasm.h for the "real" condition codes. At _this_ level,
394 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
395 * ones, so we need a different enum...
396 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700397static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
399 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000400 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700402enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
404 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700405 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
406 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700408static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000409 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
410 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000411 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412};
413
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800414/*
415 * Directive names.
416 */
417/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
418static int is_condition(enum preproc_token arg)
419{
420 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
421}
422
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000423/* For TASM compatibility we need to be able to recognise TASM compatible
424 * conditional compilation directives. Using the NASM pre-processor does
425 * not work, so we look for them specifically from the following list and
426 * then jam in the equivalent NASM directive into the input stream.
427 */
428
H. Peter Anvine2c80182005-01-15 22:15:51 +0000429enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000430 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
431 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
432};
433
H. Peter Anvin476d2862007-10-02 22:04:15 -0700434static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000435 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
436 "ifndef", "include", "local"
437};
438
439static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700440static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000441static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800442static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000443
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000444static Context *cstk;
445static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300446static const struct strlist *ipath_list;
H. Peter Anvind2354082019-08-27 16:38:48 -0700447static bool do_aliases;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000448
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300449static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000450
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300451static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000452
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800453static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700454static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800455static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000456
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000457/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800458 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000459 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800460static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000461
462/*
463 * The current set of single-line macros we have defined.
464 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700465static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000466
467/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800468 * The multi-line macro we are currently defining, or the %rep
469 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000470 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800471static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000472
Charles Crayned4200be2008-07-12 16:42:33 -0700473static uint64_t nested_mac_count;
474static uint64_t nested_rep_count;
475
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000476/*
477 * The number of macro parameters to allocate space for at a time.
478 */
479#define PARAM_DELTA 16
480
481/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700482 * The standard macro set: defined in macros.c in a set of arrays.
483 * This gives our position in any macro set, while we are processing it.
484 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000485 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700486static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700487static macros_t **stdmacnext;
488static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300489static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000490
491/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700492 * Map of which %use packages have been loaded
493 */
494static bool *use_loaded;
495
496/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000497 * Tokens are allocated in blocks to improve speed
498 */
499#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000501struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000502 Blocks *next;
503 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000504};
505
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800506static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000507
508/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000509 * Forward declarations.
510 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700511static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000512static Token *expand_mmac_params(Token * tline);
513static Token *expand_smacro(Token * tline);
514static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400515static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800516static Token *make_tok_num(int64_t val);
517static Token *make_tok_qstr(const char *str);
H. Peter Anvince616072002-04-30 21:02:23 +0000518static void *new_Block(size_t size);
519static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700520static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700521 const char *text, size_t txtlen);
522static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000523static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000524
525/*
526 * Macros for safe checking of token pointers, avoid *(NULL)
527 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300528#define tok_type_(x,t) ((x) && (x)->type == (t))
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -0700529#define skip_white_(x) ((x) = (tok_type_((x), TOK_WHITESPACE) ? (x)->next : (x)))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300530#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
531#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000532
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400533/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700534 * In-place reverse a list of tokens.
535 */
536static Token *reverse_tokens(Token *t)
537{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800538 Token *prev = NULL;
539 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700540
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800541 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400542 next = t->next;
543 t->next = prev;
544 prev = t;
545 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800546 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700547
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800548 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700549}
550
551/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300552 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000553 * front of them. We do it here because I could not find any other
554 * place to do it for the moment, and it is a hack (ideally it would
555 * be nice to be able to use the NASM pre-processor to do it).
556 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000557static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000558{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000559 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400560 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000561
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400562 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000563
564 /* Binary search for the directive name */
565 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400566 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400567 q = nasm_skip_word(p);
568 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 if (len) {
570 oldchar = p[len];
571 p[len] = 0;
572 while (j - i > 1) {
573 k = (j + i) / 2;
574 m = nasm_stricmp(p, tasm_directives[k]);
575 if (m == 0) {
576 /* We have found a directive, so jam a % in front of it
577 * so that NASM will then recognise it as one if it's own.
578 */
579 p[len] = oldchar;
580 len = strlen(p);
581 oldline = line;
582 line = nasm_malloc(len + 2);
583 line[0] = '%';
584 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700585 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300586 * NASM does not recognise IFDIFI, so we convert
587 * it to %if 0. This is not used in NASM
588 * compatible code, but does need to parse for the
589 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700591 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 } else {
593 memcpy(line + 1, p, len + 1);
594 }
595 nasm_free(oldline);
596 return line;
597 } else if (m < 0) {
598 j = k;
599 } else
600 i = k;
601 }
602 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000603 }
604 return line;
605}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000606
H. Peter Anvin76690a12002-04-30 20:52:49 +0000607/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000608 * The pre-preprocessing stage... This function translates line
609 * number indications as they emerge from GNU cpp (`# lineno "file"
610 * flags') into NASM preprocessor line number indications (`%line
611 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000612 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000613static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000615 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000616 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000617
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 if (line[0] == '#' && line[1] == ' ') {
619 oldline = line;
620 fname = oldline + 2;
621 lineno = atoi(fname);
622 fname += strspn(fname, "0123456789 ");
623 if (*fname == '"')
624 fname++;
625 fnlen = strcspn(fname, "\"");
626 line = nasm_malloc(20 + fnlen);
627 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
628 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000629 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000630 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000632 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000633}
634
635/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000636 * Free a linked list of tokens.
637 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000639{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400640 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000642}
643
644/*
645 * Free a linked list of lines.
646 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000647static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000648{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400649 Line *l, *tmp;
650 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 free_tlist(l->first);
652 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000653 }
654}
655
656/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700657 * Free an array of linked lists of tokens
658 */
659static void free_tlist_array(Token **array, size_t nlists)
660{
661 Token **listp = array;
662
663 while (nlists--)
664 free_tlist(*listp++);
665
666 nasm_free(array);
667}
668
669/*
670 * Duplicate a linked list of tokens.
671 */
672static Token *dup_tlist(const Token *list, Token ***tailp)
673{
674 Token *newlist = NULL;
675 Token **tailpp = &newlist;
676 const Token *t;
677
678 list_for_each(t, list) {
679 Token *nt;
680 *tailpp = nt = dup_Token(NULL, t);
681 tailpp = &nt->next;
682 }
683
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700684 if (tailp) {
685 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700686 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700687 }
688
689 return newlist;
690}
691
692/*
693 * Duplicate a linked list of tokens with a maximum count
694 */
695static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
696{
697 Token *newlist = NULL;
698 Token **tailpp = &newlist;
699 const Token *t;
700
701 list_for_each(t, list) {
702 Token *nt;
703 if (!cnt--)
704 break;
705 *tailpp = nt = dup_Token(NULL, t);
706 tailpp = &nt->next;
707 }
708
709 if (tailp) {
710 **tailp = newlist;
711 *tailp = tailpp;
712 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700713
714 return newlist;
715}
716
717/*
718 * Duplicate a linked list of tokens in reverse order
719 */
720static Token *dup_tlist_reverse(const Token *list, Token *tail)
721{
722 const Token *t;
723
724 list_for_each(t, list)
725 tail = dup_Token(tail, t);
726
727 return tail;
728}
729
730/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800731 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000732 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800733static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000734{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800735 nasm_free(m->name);
736 free_tlist(m->dlist);
737 nasm_free(m->defaults);
738 free_llist(m->expansion);
739 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000740}
741
742/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700743 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800744 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700745static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800746{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700747 if (s->params) {
748 int i;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700749 for (i = 0; i < s->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700750 nasm_free(s->params[i].name);
751 nasm_free(s->params);
752 }
H. Peter Anvin8b262472019-02-26 14:00:54 -0800753 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700754 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700755}
756
757static void clear_smacro(SMacro *s)
758{
759 free_smacro_members(s);
760 /* Wipe everything except the next pointer */
761 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
762}
763
764/*
765 * Free an SMacro
766 */
767static void free_smacro(SMacro *s)
768{
769 free_smacro_members(s);
770 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800771}
772
773/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700774 * Free all currently defined macros, and free the hash tables
775 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700776static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700777{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800778 struct hash_iterator it;
779 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700780
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800781 hash_for_each(smt, it, np) {
782 SMacro *tmp;
783 SMacro *s = np->data;
784 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800785 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700786 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700787 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700788 hash_free(smt);
789}
790
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800791static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700792{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800793 struct hash_iterator it;
794 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700795
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800796 hash_for_each(mmt, it, np) {
797 MMacro *tmp;
798 MMacro *m = np->data;
799 nasm_free((void *)np->key);
800 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800801 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700802 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800803 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700804}
805
806static void free_macros(void)
807{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700808 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800809 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700810}
811
812/*
813 * Initialize the hash tables
814 */
815static void init_macros(void)
816{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700817}
818
819/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000820 * Pop the context stack.
821 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000823{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000824 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000825
826 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700827 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000828 nasm_free(c->name);
829 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000830}
831
H. Peter Anvin072771e2008-05-22 13:17:51 -0700832/*
833 * Search for a key in the hash index; adding it if necessary
834 * (in which case we initialize the data pointer to NULL.)
835 */
836static void **
837hash_findi_add(struct hash_table *hash, const char *str)
838{
839 struct hash_insert hi;
840 void **r;
841 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800842 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700843
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800844 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700845 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300846 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700847
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800848 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
849 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700850 return hash_add(&hi, strx, NULL);
851}
852
853/*
854 * Like hash_findi, but returns the data element rather than a pointer
855 * to it. Used only when not adding a new element, hence no third
856 * argument.
857 */
858static void *
859hash_findix(struct hash_table *hash, const char *str)
860{
861 void **p;
862
863 p = hash_findi(hash, str, NULL);
864 return p ? *p : NULL;
865}
866
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400867/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800868 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400869 * if there no more left -- return NULL
870 */
871static char *line_from_stdmac(void)
872{
873 unsigned char c;
874 const unsigned char *p = stdmacpos;
875 char *line, *q;
876 size_t len = 0;
877
878 if (!stdmacpos)
879 return NULL;
880
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700881 /*
882 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
883 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
884 */
885 while ((c = *p++) != 127) {
886 uint8_t ndir = c - 128;
887 if (ndir < 256-96)
888 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400889 else
890 len++;
891 }
892
893 line = nasm_malloc(len + 1);
894 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700895
896 while ((c = *stdmacpos++) != 127) {
897 uint8_t ndir = c - 128;
898 if (ndir < 256-96) {
899 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
900 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400901 *q++ = ' ';
902 } else {
903 *q++ = c;
904 }
905 }
906 stdmacpos = p;
907 *q = '\0';
908
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700909 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700910 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400911 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700912 if (*stdmacnext) {
913 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400914 } else if (do_predef) {
915 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400916
917 /*
918 * Nasty hack: here we push the contents of
919 * `predef' on to the top-level expansion stack,
920 * since this is the most convenient way to
921 * implement the pre-include and pre-define
922 * features.
923 */
924 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700925 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800926 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700927 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800928 l->finishes = NULL;
929
930 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400931 }
932 do_predef = false;
933 }
934 }
935
936 return line;
937}
938
H. Peter Anvin6686de22019-08-10 05:33:14 -0700939/*
940 * Read a line from a file. Return NULL on end of file.
941 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700942static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000943{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700944 int c;
945 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400946 const unsigned int delta = 512;
947 const unsigned int pad = 8;
948 unsigned int nr_cont = 0;
949 bool cont = false;
950 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700951 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000952
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400953 size = delta;
954 p = buffer = nasm_malloc(size);
955
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700956 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700957 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400958
959 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700960 case EOF:
961 if (p == buffer) {
962 nasm_free(buffer);
963 return NULL;
964 }
965 c = 0;
966 break;
967
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400968 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700969 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400970 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700971 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400972 if (cont) {
973 cont = false;
974 continue;
975 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700976 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400977 break;
978
979 case '\n':
980 if (cont) {
981 cont = false;
982 continue;
983 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700984 c = 0;
985 break;
986
987 case 032: /* ^Z = legacy MS-DOS end of file mark */
988 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400989 break;
990
991 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700992 next = fgetc(f);
993 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400994 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400995 cont = true;
996 nr_cont++;
997 continue;
998 }
999 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001001
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001002 if (p >= (buffer + size - pad)) {
1003 buffer = nasm_realloc(buffer, size + delta);
1004 p = buffer + size - pad;
1005 size += delta;
1006 }
1007
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001008 *p++ = c;
1009 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001010
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001011 lineno = src_get_linnum() + istk->lineinc +
1012 (nr_cont * istk->lineinc);
1013 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001014
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001015 return buffer;
1016}
1017
1018/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001019 * Common read routine regardless of source
1020 */
1021static char *read_line(void)
1022{
1023 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001024 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001025
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001026 if (f)
1027 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001028 else
1029 line = line_from_stdmac();
1030
1031 if (!line)
1032 return NULL;
1033
1034 if (!istk->nolist)
1035 lfmt->line(LIST_READ, src_get_linnum(), line);
1036
1037 return line;
1038}
1039
1040/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001041 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001042 * don't need to parse the value out of e.g. numeric tokens: we
1043 * simply split one string into many.
1044 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001045static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001046{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001047 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001048 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001049 Token *list = NULL;
1050 Token *t, **tail = &list;
1051
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001053 char *p = line;
1054 char *ep = NULL; /* End of token, for trimming the end */
1055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 if (*p == '%') {
1057 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001058 if (*p == '+' && !nasm_isdigit(p[1])) {
1059 p++;
1060 type = TOK_PASTE;
1061 } else if (nasm_isdigit(*p) ||
1062 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 do {
1064 p++;
1065 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001066 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 type = TOK_PREPROC_ID;
1068 } else if (*p == '{') {
1069 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001070 while (*p) {
1071 if (*p == '}')
1072 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 p[-1] = *p;
1074 p++;
1075 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001076 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001077 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001078 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 if (*p)
1080 p++;
1081 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001082 } else if (*p == '[') {
1083 int lvl = 1;
1084 line += 2; /* Skip the leading %[ */
1085 p++;
1086 while (lvl && (c = *p++)) {
1087 switch (c) {
1088 case ']':
1089 lvl--;
1090 break;
1091 case '%':
1092 if (*p == '[')
1093 lvl++;
1094 break;
1095 case '\'':
1096 case '\"':
1097 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001098 p = nasm_skip_string(p - 1);
1099 if (*p)
1100 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001101 break;
1102 default:
1103 break;
1104 }
1105 }
1106 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001107 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001108 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001109 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001110 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001111 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001112 type = TOK_INDIRECT;
1113 } else if (*p == '?') {
1114 type = TOK_PREPROC_Q; /* %? */
1115 p++;
1116 if (*p == '?') {
1117 type = TOK_PREPROC_QQ; /* %?? */
1118 p++;
1119 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001120 } else if (*p == '!') {
1121 type = TOK_PREPROC_ID;
1122 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001123 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001124 do {
1125 p++;
1126 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001127 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001128 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001129 p = nasm_skip_string(p);
1130 if (*p)
1131 p++;
1132 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001133 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001134 } else {
1135 /* %! without string or identifier */
1136 type = TOK_OTHER; /* Legacy behavior... */
1137 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001138 } else if (*p == ',') {
1139 p++;
1140 type = TOK_COND_COMMA;
H. Peter Anvin13506202018-11-28 14:55:58 -08001141 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001143 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 do {
1145 p++;
1146 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001147 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 type = TOK_PREPROC_ID;
1149 } else {
1150 type = TOK_OTHER;
1151 if (*p == '%')
1152 p++;
1153 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001154 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 type = TOK_ID;
1156 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001157 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001159 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 /*
1161 * A string token.
1162 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001164 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001165
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 if (*p) {
1167 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001168 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001169 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170 /* Handling unterminated strings by UNV */
1171 /* type = -1; */
1172 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001173 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001174 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001175 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001176 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001177 bool is_hex = false;
1178 bool is_float = false;
1179 bool has_e = false;
1180 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001181
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001183 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001185
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001186 if (*p == '$') {
1187 p++;
1188 is_hex = true;
1189 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001190
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001191 for (;;) {
1192 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001193
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001194 if (!is_hex && (c == 'e' || c == 'E')) {
1195 has_e = true;
1196 if (*p == '+' || *p == '-') {
1197 /*
1198 * e can only be followed by +/- if it is either a
1199 * prefixed hex number or a floating-point number
1200 */
1201 p++;
1202 is_float = true;
1203 }
1204 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1205 is_hex = true;
1206 } else if (c == 'P' || c == 'p') {
1207 is_float = true;
1208 if (*p == '+' || *p == '-')
1209 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001210 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001211 ; /* just advance */
1212 else if (c == '.') {
1213 /*
1214 * we need to deal with consequences of the legacy
1215 * parser, like "1.nolist" being two tokens
1216 * (TOK_NUMBER, TOK_ID) here; at least give it
1217 * a shot for now. In the future, we probably need
1218 * a flex-based scanner with proper pattern matching
1219 * to do it as well as it can be done. Nothing in
1220 * the world is going to help the person who wants
1221 * 0x123.p16 interpreted as two tokens, though.
1222 */
1223 r = p;
1224 while (*r == '_')
1225 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001226
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001227 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1228 (!is_hex && (*r == 'e' || *r == 'E')) ||
1229 (*r == 'p' || *r == 'P')) {
1230 p = r;
1231 is_float = true;
1232 } else
1233 break; /* Terminate the token */
1234 } else
1235 break;
1236 }
1237 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001238
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001239 if (p == line+1 && *line == '$') {
1240 type = TOK_OTHER; /* TOKEN_HERE */
1241 } else {
1242 if (has_e && !is_hex) {
1243 /* 1e13 is floating-point, but 1e13h is not */
1244 is_float = true;
1245 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001246
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001247 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1248 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001249 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001251 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 /*
1253 * Whitespace just before end-of-line is discarded by
1254 * pretending it's a comment; whitespace just before a
1255 * comment gets lumped into the comment.
1256 */
1257 if (!*p || *p == ';') {
1258 type = TOK_COMMENT;
1259 while (*p)
1260 p++;
1261 }
1262 } else if (*p == ';') {
1263 type = TOK_COMMENT;
1264 while (*p)
1265 p++;
1266 } else {
1267 /*
1268 * Anything else is an operator of some kind. We check
1269 * for all the double-character operators (>>, <<, //,
1270 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001271 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 */
1273 type = TOK_OTHER;
1274 if ((p[0] == '>' && p[1] == '>') ||
1275 (p[0] == '<' && p[1] == '<') ||
1276 (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++;
1286 }
1287 p++;
1288 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001289
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 /* Handling unterminated string by UNV */
1291 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001292 {
1293 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1294 t->text[p-line] = *line;
1295 tail = &t->next;
1296 }
1297 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001299 if (!ep)
1300 ep = p;
1301 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 tail = &t->next;
1303 }
1304 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001305 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001306 return list;
1307}
1308
H. Peter Anvince616072002-04-30 21:02:23 +00001309/*
1310 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001311 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001312 * deleted only all at once by the delete_Blocks function.
1313 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001315{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001316 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001318 /* first, get to the end of the linked list */
1319 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001321 /* now allocate the requested chunk */
1322 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001324 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001325 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001326 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001327}
1328
1329/*
1330 * this function deletes all managed blocks of memory
1331 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001333{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001335
H. Peter Anvin70653092007-10-19 14:42:29 -07001336 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001337 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001338 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001339 * free it.
1340 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001342 if (b->chunk)
1343 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 a = b;
1345 b = b->next;
1346 if (a != &blocks)
1347 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001348 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001349 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001351
1352/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001353 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001354 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001355 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001356static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001357 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001358{
1359 Token *t;
1360 int i;
1361
H. Peter Anvin89cee572009-07-15 09:16:54 -04001362 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1364 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1365 freeTokens[i].next = &freeTokens[i + 1];
1366 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001367 }
1368 t = freeTokens;
1369 freeTokens = t->next;
1370 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001371 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001372 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001373 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 t->text = NULL;
1375 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001376 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001378 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001379 t->text = nasm_malloc(txtlen+1);
1380 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001382 }
1383 return t;
1384}
1385
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001386static Token *dup_Token(Token *next, const Token *src)
1387{
1388 return new_Token(next, src->type, src->text, src->len);
1389}
1390
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001392{
1393 Token *next = t->next;
1394 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001395 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001396 freeTokens = t;
1397 return next;
1398}
1399
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001400/*
1401 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001402 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1403 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001404 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001405static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001406{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001407 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001408 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001409 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001410
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001411 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001412 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001413 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001414 char *v;
1415 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001416
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001417 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001418 if (nasm_isquote(*v))
1419 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001420
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001421 if (v) {
1422 char *p = getenv(v);
1423 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001424 /*!
1425 *!environment [on] nonexistent environment variable
1426 *! warns if a nonexistent environment variable
1427 *! is accessed using the \c{%!} preprocessor
1428 *! construct (see \k{getenv}.) Such environment
1429 *! variables are treated as empty (with this
1430 *! warning issued) starting in NASM 2.15;
1431 *! earlier versions of NASM would treat this as
1432 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001433 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001434 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1435 p = "";
1436 }
1437 t->text = nasm_strdup(p);
1438 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001439 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001440 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001442
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 /* Expand local macros here and not during preprocessing */
1444 if (expand_locals &&
1445 t->type == TOK_PREPROC_ID && t->text &&
1446 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001447 const char *q;
1448 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001449 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001450 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001451 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1452 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001453 nasm_free(t->text);
1454 t->text = p;
1455 }
1456 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001457 if (t->text) {
1458 if (debug_level(2)) {
1459 unsigned long t_len = t->len;
1460 unsigned long s_len = strlen(t->text);
1461 if (t_len != s_len) {
1462 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1463 t->text, t->type, s_len, t_len);
1464 t->len = s_len;
1465 }
1466 }
1467 len += t->len;
1468 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001470 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001471 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001472
H. Peter Anvin734b1882002-04-30 21:01:08 +00001473 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001474
1475 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001476 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001477 memcpy(p, t->text, t->len);
1478 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001479 } else if (t->type == TOK_WHITESPACE) {
1480 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001481 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001482 }
1483 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001484
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001485 return line;
1486}
1487
1488/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001489 * A scanner, suitable for use by the expression evaluator, which
1490 * operates on a line of Tokens. Expects a pointer to a pointer to
1491 * the first token in the line to be passed in as its private_data
1492 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001493 *
1494 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001495 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001496struct ppscan {
1497 Token *tptr;
1498 int ntokens;
1499};
1500
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001502{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001503 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001504 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001505 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001506
H. Peter Anvine2c80182005-01-15 22:15:51 +00001507 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001508 if (pps->ntokens && (tline = pps->tptr)) {
1509 pps->ntokens--;
1510 pps->tptr = tline->next;
1511 } else {
1512 pps->tptr = NULL;
1513 pps->ntokens = 0;
1514 return tokval->t_type = TOKEN_EOS;
1515 }
1516 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001517
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001518 tokval->t_charptr = tline->text;
1519
H. Peter Anvin76690a12002-04-30 20:52:49 +00001520 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001522 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001524
H. Peter Anvine2c80182005-01-15 22:15:51 +00001525 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001526 p = tokval->t_charptr = tline->text;
1527 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001528 tokval->t_charptr++;
1529 return tokval->t_type = TOKEN_ID;
1530 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001531
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001532 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001533 if (r >= p+MAX_KEYWORD)
1534 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001535 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001536 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001537 *s = '\0';
1538 /* right, so we have an identifier sitting in temp storage. now,
1539 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001540 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001541 }
1542
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001544 bool rn_error;
1545 tokval->t_integer = readnum(tline->text, &rn_error);
1546 tokval->t_charptr = tline->text;
1547 if (rn_error)
1548 return tokval->t_type = TOKEN_ERRNUM;
1549 else
1550 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001551 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001552
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001553 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001554 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001555 }
1556
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001558 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001559
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001560 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001561 tokval->t_charptr = tline->text;
1562 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001563
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001564 if (ep[0] != bq || ep[1] != '\0')
1565 return tokval->t_type = TOKEN_ERRSTR;
1566 else
1567 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001568 }
1569
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 if (tline->type == TOK_OTHER) {
1571 if (!strcmp(tline->text, "<<"))
1572 return tokval->t_type = TOKEN_SHL;
1573 if (!strcmp(tline->text, ">>"))
1574 return tokval->t_type = TOKEN_SHR;
1575 if (!strcmp(tline->text, "//"))
1576 return tokval->t_type = TOKEN_SDIV;
1577 if (!strcmp(tline->text, "%%"))
1578 return tokval->t_type = TOKEN_SMOD;
1579 if (!strcmp(tline->text, "=="))
1580 return tokval->t_type = TOKEN_EQ;
1581 if (!strcmp(tline->text, "<>"))
1582 return tokval->t_type = TOKEN_NE;
1583 if (!strcmp(tline->text, "!="))
1584 return tokval->t_type = TOKEN_NE;
1585 if (!strcmp(tline->text, "<="))
1586 return tokval->t_type = TOKEN_LE;
1587 if (!strcmp(tline->text, ">="))
1588 return tokval->t_type = TOKEN_GE;
1589 if (!strcmp(tline->text, "&&"))
1590 return tokval->t_type = TOKEN_DBL_AND;
1591 if (!strcmp(tline->text, "^^"))
1592 return tokval->t_type = TOKEN_DBL_XOR;
1593 if (!strcmp(tline->text, "||"))
1594 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001595 }
1596
1597 /*
1598 * We have no other options: just return the first character of
1599 * the token text.
1600 */
1601 return tokval->t_type = tline->text[0];
1602}
1603
1604/*
H. Peter Anvind2354082019-08-27 16:38:48 -07001605 * 1. An expression (true if nonzero 0)
1606 * 2. The keywords true, on, yes for true
1607 * 3. The keywords false, off, no for false
1608 * 4. An empty line, for true
1609 *
1610 * On error, return defval (usually the previous value)
1611 */
1612static bool pp_get_boolean_option(Token *tline, bool defval)
1613{
1614 static const char * const noyes[] = {
1615 "no", "yes",
1616 "false", "true",
1617 "off", "on"
1618 };
1619 struct ppscan pps;
1620 struct tokenval tokval;
1621 expr *evalresult;
1622
1623 skip_white_(tline);
1624 if (!tline || !tline->type)
1625 return true;
1626
1627 if (tline->type == TOK_ID) {
1628 size_t i;
1629 for (i = 0; i < ARRAY_SIZE(noyes); i++)
1630 if (!nasm_stricmp(tline->text, noyes[i]))
1631 return i & 1;
1632 }
1633
1634 pps.tptr = NULL;
1635 pps.tptr = tline;
1636 pps.ntokens = -1;
1637 tokval.t_type = TOKEN_INVALID;
1638 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
1639
1640 if (!evalresult)
1641 return true;
1642
1643 if (tokval.t_type)
1644 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
1645 if (!is_really_simple(evalresult)) {
1646 nasm_nonfatal("boolean flag expression must be a constant");
1647 return defval;
1648 }
1649
1650 return reloc_value(evalresult) != 0;
1651}
1652
1653/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001654 * Compare a string to the name of an existing macro; this is a
1655 * simple wrapper which calls either strcmp or nasm_stricmp
1656 * depending on the value of the `casesense' parameter.
1657 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001658static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001659{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001660 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001661}
1662
1663/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001664 * Compare a string to the name of an existing macro; this is a
1665 * simple wrapper which calls either strcmp or nasm_stricmp
1666 * depending on the value of the `casesense' parameter.
1667 */
1668static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1669{
1670 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1671}
1672
1673/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001674 * Return the Context structure associated with a %$ token. Return
1675 * NULL, having _already_ reported an error condition, if the
1676 * context stack isn't deep enough for the supplied number of $
1677 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001678 *
1679 * If "namep" is non-NULL, set it to the pointer to the macro name
1680 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001681 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001682static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001683{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001684 Context *ctx;
1685 int i;
1686
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001687 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001688 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001689
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001690 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001692
H. Peter Anvine2c80182005-01-15 22:15:51 +00001693 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001694 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001695 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696 }
1697
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001698 name += 2;
1699 ctx = cstk;
1700 i = 0;
1701 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001702 name++;
1703 i++;
1704 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001705 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001707 nasm_nonfatal("`%s': context stack is only"
1708 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001709 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001710 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001711
1712 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001713 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001714
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001715 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001716}
1717
1718/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001719 * Open an include file. This routine must always return a valid
1720 * file pointer if it returns - it's responsible for throwing an
1721 * ERR_FATAL and bombing out completely if not. It should also try
1722 * the include path one by one until it finds the file or reaches
1723 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001724 *
1725 * Note: for INC_PROBE the function returns NULL at all times;
1726 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001727 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001728enum incopen_mode {
1729 INC_NEEDED, /* File must exist */
1730 INC_OPTIONAL, /* Missing is OK */
1731 INC_PROBE /* Only an existence probe */
1732};
1733
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001734/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001735static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001736 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001737{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001738 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001739 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001740 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001741 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001742 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001743
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001745 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001746 if (omode == INC_PROBE) {
1747 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001748 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001749 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001750 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001751 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001752 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001753 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001754 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001756 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001757
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001758 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001759
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001760 if (!ip) {
1761 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001762 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001763 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001764
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001765 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001766 ip = ip->next;
1767 }
1768}
1769
1770/*
1771 * Open a file, or test for the presence of one (depending on omode),
1772 * considering the include path.
1773 */
1774static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001775 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001776 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001777 enum incopen_mode omode,
1778 enum file_flags fmode)
1779{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001780 struct hash_insert hi;
1781 void **hp;
1782 char *path;
1783 FILE *fp = NULL;
1784
1785 hp = hash_find(&FileHash, file, &hi);
1786 if (hp) {
1787 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001788 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001789 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001790 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001791 } else {
1792 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001793 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001794
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001795 /* Positive or negative result */
1796 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001797
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001798 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001799 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001800 */
1801 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001802 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001803 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001804
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001805 if (!path) {
1806 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001807 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001808 } else {
1809 if (!fp && omode != INC_PROBE)
1810 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001811 }
1812
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001813 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001814 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001815
1816 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001817}
1818
1819/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001820 * Opens an include or input file. Public version, for use by modules
1821 * that get a file:lineno pair and need to look at the file again
1822 * (e.g. the CodeView debug backend). Returns NULL on failure.
1823 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001824FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001825{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001826 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001827}
1828
1829/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001830 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001831 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001832 * return true if _any_ single-line macro of that name is defined.
1833 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001834 * `nparam' or no parameters is defined.
1835 *
1836 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001837 * defined, or nparam is -1, the address of the definition structure
1838 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001839 * is NULL, no action will be taken regarding its contents, and no
1840 * error will occur.
1841 *
1842 * Note that this is also called with nparam zero to resolve
1843 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001844 *
1845 * If you already know which context macro belongs to, you can pass
1846 * the context pointer as first parameter; if you won't but name begins
1847 * with %$ the context will be automatically computed. If all_contexts
1848 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001849 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001850static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001851smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07001852 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001853{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001854 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001855 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001856
H. Peter Anvin97a23472007-09-16 17:57:25 -07001857 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001858 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001859 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001860 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001861 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001863 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001864 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001865 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001866 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001867 }
H. Peter Anvind2354082019-08-27 16:38:48 -07001868
1869restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07001870 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001871
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 while (m) {
1873 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001874 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001875 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07001876 if (m->alias && !find_alias) {
1877 if (do_aliases) {
H. Peter Anvind626f352019-09-12 18:31:29 -07001878 name = m->expansion->text;
H. Peter Anvind2354082019-08-27 16:38:48 -07001879 goto restart;
1880 } else {
1881 continue;
1882 }
1883 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001885 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001887 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 }
1889 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001890 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001891
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001892 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001893}
1894
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001895/* param should be a natural number [0; INT_MAX] */
1896static int read_param_count(const char *str)
1897{
1898 int result;
1899 bool err;
1900
1901 result = readnum(str, &err);
1902 if (result < 0 || result > INT_MAX) {
1903 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001904 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1905 str, 0, INT_MAX);
1906 } else if (err)
1907 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001908 return result;
1909}
1910
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001911/*
1912 * Count and mark off the parameters in a multi-line macro call.
1913 * This is called both from within the multi-line macro expansion
1914 * code, and also to mark off the default parameters when provided
1915 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001916 *
1917 * Note that we need space in the params array for parameter 0 being
1918 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001919 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001920static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001921{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001922 int paramsize, brace;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001923 int nparam = 0;
1924 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001925
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001926 paramsize = PARAM_DELTA;
1927 params = nasm_malloc(paramsize * sizeof(*params));
1928 params[0] = NULL;
1929
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07001930 while (skip_white_(t)) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001931 /* 2 slots for captured label and NULL */
1932 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001934 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001936 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001938 brace++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001939 params[++nparam] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001940 if (brace) {
1941 while (brace && (t = t->next) != NULL) {
1942 if (tok_is_(t, "{"))
1943 brace++;
1944 else if (tok_is_(t, "}"))
1945 brace--;
1946 }
1947
1948 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 /*
1950 * Now we've found the closing brace, look further
1951 * for the comma.
1952 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001953 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 skip_white_(t);
1955 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001956 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 while (tok_isnt_(t, ","))
1958 t = t->next;
1959 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001961 } else {
1962 while (tok_isnt_(t, ","))
1963 t = t->next;
1964 }
1965 if (t) { /* got a comma/brace */
1966 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001968 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001969
1970 params[nparam+1] = NULL;
1971 *paramsp = params;
1972 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001973}
1974
1975/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001976 * Determine whether one of the various `if' conditions is true or
1977 * not.
1978 *
1979 * We must free the tline we get passed.
1980 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001981static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001982{
H. Peter Anvin70055962007-10-11 00:05:31 -07001983 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001984 Token *t, *tt, *origline;
1985 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001986 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001987 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001988 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001989 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001990 const char *dname = pp_directives[ct];
1991 bool casesense = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001992
1993 origline = tline;
1994
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001995 switch (PP_COND(ct)) {
1996 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001997 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001998 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002000 if (!tline)
2001 break;
2002 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002003 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002004 dname);
2005 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002006 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002007 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002008 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002009 tline = tline->next;
2010 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002012
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002013 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002014 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002015 while (tline) {
2016 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002017 if (!tline || (tline->type != TOK_ID &&
2018 (tline->type != TOK_PREPROC_ID ||
2019 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002020 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002021 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002022 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 }
H. Peter Anvind2354082019-08-27 16:38:48 -07002024 if (smacro_defined(NULL, tline->text, 0, NULL, true, false))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002025 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002027 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002028 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002029
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002030 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002031 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002032 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002033 while (tline) {
2034 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002035 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002036 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002037 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002038 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002039 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002040 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002041 goto fail;
2042 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002043 p = tline->text;
2044 if (tline->type == TOK_PREPROC_ID)
2045 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08002046 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002047 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002048 if (getenv(p))
2049 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002050 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002051 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002052 break;
2053
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002054 case PP_IFIDNI:
2055 casesense = false;
2056 /* fall through */
2057 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002058 tline = expand_smacro(tline);
2059 t = tt = tline;
2060 while (tok_isnt_(tt, ","))
2061 tt = tt->next;
2062 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002063 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002064 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002065 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002066 }
2067 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002068 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
2070 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002071 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002072 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002074 }
2075 if (t->type == TOK_WHITESPACE) {
2076 t = t->next;
2077 continue;
2078 }
2079 if (tt->type == TOK_WHITESPACE) {
2080 tt = tt->next;
2081 continue;
2082 }
2083 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002084 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002085 break;
2086 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002087 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002088 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002089 size_t l1 = nasm_unquote(t->text, NULL);
2090 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002091
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002092 if (l1 != l2) {
2093 j = false;
2094 break;
2095 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002096 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002097 j = false;
2098 break;
2099 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002100 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002101 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002102 break;
2103 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002104
H. Peter Anvine2c80182005-01-15 22:15:51 +00002105 t = t->next;
2106 tt = tt->next;
2107 }
2108 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002109 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002110 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002111
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002112 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002113 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002114 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002115 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002116
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002117 skip_white_(tline);
2118 tline = expand_id(tline);
2119 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002120 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 goto fail;
2122 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002123 nasm_zero(searching);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124 searching.name = nasm_strdup(tline->text);
2125 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002126 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 searching.nparam_max = INT_MAX;
2128 tline = expand_smacro(tline->next);
2129 skip_white_(tline);
2130 if (!tline) {
2131 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002132 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002133 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134 } else {
2135 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002136 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002137 }
2138 if (tline && tok_is_(tline->next, "-")) {
2139 tline = tline->next->next;
2140 if (tok_is_(tline, "*"))
2141 searching.nparam_max = INT_MAX;
2142 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002143 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002144 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002145 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002146 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002147 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002148 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002149 searching.nparam_max = searching.nparam_min;
2150 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 }
2152 }
2153 if (tline && tok_is_(tline->next, "+")) {
2154 tline = tline->next;
2155 searching.plus = true;
2156 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002157 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2158 while (mmac) {
2159 if (!strcmp(mmac->name, searching.name) &&
2160 (mmac->nparam_min <= searching.nparam_max
2161 || searching.plus)
2162 && (searching.nparam_min <= mmac->nparam_max
2163 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002164 found = true;
2165 break;
2166 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002167 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002168 }
2169 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002170 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002171 nasm_free(searching.name);
2172 j = found;
2173 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002174 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002175
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002176 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002177 needtype = TOK_ID;
2178 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002179 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 needtype = TOK_NUMBER;
2181 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002182 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002183 needtype = TOK_STRING;
2184 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002185
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002186iftype:
2187 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002188
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002189 while (tok_type_(t, TOK_WHITESPACE) ||
2190 (needtype == TOK_NUMBER &&
2191 tok_type_(t, TOK_OTHER) &&
2192 (t->text[0] == '-' || t->text[0] == '+') &&
2193 !t->text[1]))
2194 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002195
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002196 j = tok_type_(t, needtype);
2197 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002198
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002199 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002200 t = tline = expand_smacro(tline);
2201 while (tok_type_(t, TOK_WHITESPACE))
2202 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002203
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002204 j = false;
2205 if (t) {
2206 t = t->next; /* Skip the actual token */
2207 while (tok_type_(t, TOK_WHITESPACE))
2208 t = t->next;
2209 j = !t; /* Should be nothing left */
2210 }
2211 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002212
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002213 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002214 t = tline = expand_smacro(tline);
2215 while (tok_type_(t, TOK_WHITESPACE))
2216 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002217
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002218 j = !t; /* Should be empty */
2219 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002220
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002221 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002222 pps.tptr = tline = expand_smacro(tline);
2223 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002224 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002225 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002226 if (!evalresult)
2227 return -1;
2228 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002229 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002230 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002231 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002232 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002233 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002234 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002235 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002236 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002237
H. Peter Anvine2c80182005-01-15 22:15:51 +00002238 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002239 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002240 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002241 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002242
2243 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002244 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002245
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002246fail:
2247 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002248 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002249}
2250
2251/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002252 * Default smacro expansion routine: just returns a copy of the
2253 * expansion list.
2254 */
2255static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002256smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002257{
2258 (void)params;
2259 (void)nparams;
2260
2261 return dup_tlist(s->expansion, NULL);
2262}
2263
2264/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002265 * Emit a macro defintion or undef to the listing file, if
2266 * desired. This is similar to detoken(), but it handles the reverse
2267 * expansion list, does not expand %! or local variable tokens, and
2268 * does some special handling for macro parameters.
2269 */
2270static void
2271list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2272{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002273 Token *t;
2274 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002275 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002276 char *context_prefix = NULL;
2277 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002278
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002279 namelen = strlen(m->name);
2280 size = namelen + 2; /* Include room for space after name + NUL */
2281
2282 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002283 int context_depth = cstk->depth - ctx->depth + 1;
2284 context_prefix =
2285 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2286 ctx->name ? ctx->name : "",
2287 ctx->number, context_depth, "");
2288
2289 context_len = nasm_last_string_len();
2290 memset(context_prefix + context_len - context_depth,
2291 '$', context_depth);
2292 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002293 }
2294
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002295 list_for_each(t, m->expansion)
2296 size += t->text ? t->len : 1;
2297
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002298 if (m->nparam) {
2299 /*
2300 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002301 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002302 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002303 int i;
2304
2305 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002306 for (i = 0; i < m->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002307 size += m->params[i].namelen;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002308 }
2309
2310 def = nasm_malloc(size);
2311 p = def+size;
2312 *--p = '\0';
2313
2314 list_for_each(t, m->expansion) {
2315 if (!t->text) {
2316 *--p = ' ';
2317 } else {
2318 p -= t->len;
2319 memcpy(p, t->text, t->len);
2320 }
2321 }
2322
2323 *--p = ' ';
2324
2325 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002326 int i;
2327
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002328 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002329 for (i = m->nparam-1; i >= 0; i--) {
2330 enum sparmflags flags = m->params[i].flags;
2331 if (flags & SPARM_GREEDY)
2332 *--p = '+';
2333 if (m->params[i].name) {
2334 p -= m->params[i].namelen;
2335 memcpy(p, m->params[i].name, m->params[i].namelen);
2336 }
2337 if (flags & SPARM_NOSTRIP)
2338 *--p = '!';
2339 if (flags & SPARM_STR)
2340 *--p = '&';
2341 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002342 *--p = '=';
2343 *--p = ',';
2344 }
2345 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002346 }
2347
2348 p -= namelen;
2349 memcpy(p, m->name, namelen);
2350
H. Peter Anvin6686de22019-08-10 05:33:14 -07002351 if (context_prefix) {
2352 p -= context_len;
2353 memcpy(p, context_prefix, context_len);
2354 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002355 }
2356
2357 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002358 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002359}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002360
2361/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002362 * Parse smacro arguments, return argument count. If the tmpl argument
2363 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002364 * *tpp is updated to point to the pointer to the first token after the
2365 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002366 *
2367 * The text values from any argument tokens are "stolen" and the
2368 * corresponding text fields set to NULL.
2369 */
2370static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2371{
2372 int nparam = 0;
2373 enum sparmflags flags;
2374 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002375 bool err, done;
2376 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002377 Token **tn = *tpp;
2378 Token *t = *tn;
2379 Token *name;
2380
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002381 /*
2382 * DO NOT skip whitespace here, or we won't be able to distinguish:
2383 *
2384 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2385 * %define bar(a,b) ; two arguments, empty expansion
2386 *
2387 * This ambiguity was inherited from C.
2388 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002389
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002390 if (!tok_is_(t, "("))
2391 goto finish;
2392
2393 if (tmpl) {
2394 Token *tx = t;
2395 Token **txpp = &tx;
2396 int sparam;
2397
2398 /* Count parameters first */
2399 sparam = parse_smacro_template(&txpp, NULL);
2400 if (!sparam)
2401 goto finish; /* No parameters, we're done */
2402 nasm_newn(params, sparam);
2403 }
2404
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002405 /* Skip leading paren */
2406 tn = &t->next;
2407 t = *tn;
2408
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002409 name = NULL;
2410 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002411 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002412
2413 while (!done) {
2414 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002415 if (name || flags)
2416 nasm_nonfatal("`)' expected to terminate macro template");
2417 else
2418 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002419 break;
2420 }
2421
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002422 switch (t->type) {
2423 case TOK_ID:
2424 if (name)
2425 goto bad;
2426 name = t;
2427 break;
2428
2429 case TOK_OTHER:
2430 if (t->text[1])
2431 goto bad;
2432 switch (t->text[0]) {
2433 case '=':
2434 flags |= SPARM_EVAL;
2435 break;
2436 case '&':
2437 flags |= SPARM_STR;
2438 break;
2439 case '!':
2440 flags |= SPARM_NOSTRIP;
2441 break;
2442 case '+':
2443 flags |= SPARM_GREEDY;
2444 greedy = true;
2445 break;
2446 case ',':
2447 if (greedy)
2448 nasm_nonfatal("greedy parameter must be last");
2449 /* fall through */
2450 case ')':
2451 if (params) {
2452 if (name) {
2453 params[nparam].name = name->text;
2454 params[nparam].namelen = name->len;
2455 name->text = NULL;
2456 }
2457 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002458 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002459 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002460 name = NULL;
2461 flags = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002462 done = t->text[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002463 break;
2464 default:
2465 goto bad;
2466 }
2467 break;
2468
2469 case TOK_WHITESPACE:
2470 break;
2471
2472 default:
2473 bad:
2474 if (!err) {
2475 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2476 err = true;
2477 }
2478 break;
2479 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002480
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002481 tn = &t->next;
2482 t = *tn;
2483 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002484
2485finish:
2486 while (t && t->type == TOK_WHITESPACE) {
2487 tn = &t->next;
2488 t = t->next;
2489 }
2490 *tpp = tn;
2491 if (tmpl) {
2492 tmpl->nparam = nparam;
2493 tmpl->greedy = greedy;
2494 tmpl->params = params;
2495 }
2496 return nparam;
2497}
2498
2499/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002500 * Common code for defining an smacro. The tmpl argument, if not NULL,
2501 * contains any macro parameters that aren't explicit arguments;
2502 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002503 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002504static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002505 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002506{
2507 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002508 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002509 Context *ctx;
2510 bool defining_alias = false;
2511 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002512
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002513 if (tmpl) {
2514 defining_alias = tmpl->alias;
2515 nparam = tmpl->nparam;
2516 }
2517
2518 while (1) {
2519 ctx = get_ctx(mname, &mname);
2520
H. Peter Anvind2354082019-08-27 16:38:48 -07002521 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002522 /* Create a new macro */
2523 smtbl = ctx ? &ctx->localmac : &smacros;
2524 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2525 nasm_new(smac);
2526 smac->next = *smhead;
2527 *smhead = smac;
2528 break;
2529 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002530 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002531 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 /*
2533 * Some instances of the old code considered this a failure,
2534 * some others didn't. What is the right thing to do here?
2535 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002536 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002537 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002538 /*
2539 * We're redefining, so we have to take over an
2540 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002541 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002542 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002543 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002544 break;
2545 } else if (smac->in_progress) {
2546 nasm_nonfatal("macro alias loop");
2547 goto fail;
2548 } else {
2549 /* It is an alias macro; follow the alias link */
2550 SMacro *s;
2551
2552 smac->in_progress = true;
2553 s = define_smacro(smac->expansion->text, casesense,
2554 expansion, tmpl);
2555 smac->in_progress = false;
2556 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002557 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002558 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002559
2560 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002561 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002562 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002563 smac->expand = smacro_expand_default;
2564 if (tmpl) {
2565 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002566 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002567 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002568 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002569 if (tmpl->expand)
2570 smac->expand = tmpl->expand;
2571 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002572 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002573 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2574 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002575 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002576 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002577
2578fail:
2579 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002580 if (tmpl)
2581 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002582 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002583}
2584
2585/*
2586 * Undefine an smacro
2587 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002588static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002589{
2590 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002591 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002592 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002593
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002594 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002595 smtbl = ctx ? &ctx->localmac : &smacros;
2596 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002597
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002598 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 /*
2600 * We now have a macro name... go hunt for it.
2601 */
2602 sp = smhead;
2603 while ((s = *sp) != NULL) {
2604 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002605 if (s->alias && !undefalias) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002606 if (!do_aliases)
2607 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002608 if (s->in_progress) {
2609 nasm_nonfatal("macro alias loop");
2610 } else {
2611 s->in_progress = true;
2612 undef_smacro(s->expansion->text, false);
2613 s->in_progress = false;
2614 }
2615 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002616 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002617 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2618 ctx, s);
2619 *sp = s->next;
2620 free_smacro(s);
2621 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 } else {
2623 sp = &s->next;
2624 }
2625 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002626 }
2627}
2628
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002629/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002630 * Parse a mmacro specification.
2631 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002632static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002633{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002634 tline = tline->next;
2635 skip_white_(tline);
2636 tline = expand_id(tline);
2637 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002638 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002639 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002640 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002641
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002642#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002643 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002644#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002645 def->name = nasm_strdup(tline->text);
2646 def->plus = false;
2647 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002648 def->nparam_min = 0;
2649 def->nparam_max = 0;
2650
H. Peter Anvina26433d2008-07-16 14:40:01 -07002651 tline = expand_smacro(tline->next);
2652 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002653 if (!tok_type_(tline, TOK_NUMBER))
2654 nasm_nonfatal("`%s' expects a parameter count", directive);
2655 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002656 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002657 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002658 tline = tline->next->next;
2659 if (tok_is_(tline, "*")) {
2660 def->nparam_max = INT_MAX;
2661 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002662 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002664 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002665 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002666 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002667 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002668 }
2669 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002670 }
2671 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 tline = tline->next;
2673 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002674 }
2675 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 !nasm_stricmp(tline->next->text, ".nolist")) {
2677 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002678 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002679 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680
H. Peter Anvina26433d2008-07-16 14:40:01 -07002681 /*
2682 * Handle default parameters.
2683 */
2684 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 def->dlist = tline->next;
2686 tline->next = NULL;
2687 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002688 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 def->dlist = NULL;
2690 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002691 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002692 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002693
H. Peter Anvin89cee572009-07-15 09:16:54 -04002694 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002695 !def->plus) {
2696 /*
2697 *!macro-defaults [on] macros with more default than optional parameters
2698 *! warns when a macro has more default parameters than optional parameters.
2699 *! See \k{mlmacdef} for why might want to disable this warning.
2700 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002701 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002702 "too many default macro parameters in macro `%s'", def->name);
2703 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002704
H. Peter Anvina26433d2008-07-16 14:40:01 -07002705 return true;
2706}
2707
2708
2709/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002710 * Decode a size directive
2711 */
2712static int parse_size(const char *str) {
2713 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002715 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002717 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002718}
2719
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002720/*
2721 * Process a preprocessor %pragma directive. Currently there are none.
2722 * Gets passed the token list starting with the "preproc" token from
2723 * "%pragma preproc".
2724 */
2725static void do_pragma_preproc(Token *tline)
2726{
2727 /* Skip to the real stuff */
2728 tline = tline->next;
2729 skip_white_(tline);
2730 if (!tline)
2731 return;
2732
2733 (void)tline; /* Nothing else to do at present */
2734}
2735
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002736static bool is_macro_id(const Token *t)
2737{
2738 return t && (t->type == TOK_ID ||
2739 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2740}
2741
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002742static char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002743{
2744 char *id;
2745 Token *t = *tp;
2746
2747 t = t->next; /* Skip directive */
2748 skip_white_(t);
2749 t = expand_id(t);
2750
2751 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002752 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002753 return NULL;
2754 }
2755
2756 id = t->text;
2757 skip_white_(t);
2758 *tp = t;
2759 return id;
2760}
2761
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002762/* Parse a %use package name and find the package. Set *err on syntax error. */
2763static const struct use_package *
2764get_use_pkg(Token *t, const char *dname, bool *err)
2765{
2766 char *id;
2767
2768 *err = false;
2769
2770 t = t->next; /* Skip directive */
2771 skip_white_(t);
2772
2773 t = expand_smacro(t);
2774
2775 id = NULL;
2776 if (t) {
2777 if (t->type == TOK_ID) {
2778 id = t->text;
2779 } else if (t->type == TOK_STRING) {
2780 nasm_unquote_cstr(t->text, NULL);
2781 id = t->text;
2782 }
2783 }
2784
2785 if (!id) {
2786 nasm_nonfatal("`%s' expects a package name", dname);
2787 *err = true;
2788 return NULL;
2789 }
2790
2791 t = t->next;
2792 skip_white_(t);
2793 if (t)
2794 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
2795
2796 return nasm_find_use_package(id);
2797}
2798
Ed Beroset3ab3f412002-06-11 03:31:49 +00002799/**
2800 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002801 * Find out if a line contains a preprocessor directive, and deal
2802 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002803 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002804 * If a directive _is_ found, it is the responsibility of this routine
2805 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002806 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002807 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002808 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002809 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002810 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002811 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002812static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002813{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002814 enum preproc_token i;
2815 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002816 bool err;
2817 int nparam;
2818 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002819 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002820 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002821 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002822 char *p, *pp;
2823 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002824 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002825 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002826 Include *inc;
2827 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002828 Cond *cond;
2829 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002830 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002831 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002832 struct tokenval tokval;
2833 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002834 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002835 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002836 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002837 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002838
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002839 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002840 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002841
H. Peter Anvineba20a72002-04-30 20:53:55 +00002842 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002843 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002844 (tline->text[0] && (tline->text[1] == '%' ||
2845 tline->text[1] == '$' ||
2846 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002848
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002849 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002850 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002851
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002852 casesense = true;
2853 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2854 casesense = false;
2855 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002856 }
2857
2858 /*
2859 * If we're in a non-emitting branch of a condition construct,
2860 * or walking to the end of an already terminated %rep block,
2861 * we should ignore all directives except for condition
2862 * directives.
2863 */
2864 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002865 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2866 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002867 return NO_DIRECTIVE_FOUND;
2868 }
2869
2870 /*
2871 * If we're defining a macro or reading a %rep block, we should
2872 * ignore all directives except for %macro/%imacro (which nest),
2873 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2874 * If we're in a %rep block, another %rep nests, so should be let through.
2875 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002876 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002877 i != PP_ENDMACRO && i != PP_ENDM &&
2878 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2879 return NO_DIRECTIVE_FOUND;
2880 }
2881
2882 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002883 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002884 nested_mac_count++;
2885 return NO_DIRECTIVE_FOUND;
2886 } else if (nested_mac_count > 0) {
2887 if (i == PP_ENDMACRO) {
2888 nested_mac_count--;
2889 return NO_DIRECTIVE_FOUND;
2890 }
2891 }
2892 if (!defining->name) {
2893 if (i == PP_REP) {
2894 nested_rep_count++;
2895 return NO_DIRECTIVE_FOUND;
2896 } else if (nested_rep_count > 0) {
2897 if (i == PP_ENDREP) {
2898 nested_rep_count--;
2899 return NO_DIRECTIVE_FOUND;
2900 }
2901 }
2902 }
2903 }
2904
H. Peter Anvin4169a472007-09-12 01:29:43 +00002905 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002906 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002907 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002909
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002910 case PP_PRAGMA:
2911 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002912 * %pragma namespace options...
2913 *
2914 * The namespace "preproc" is reserved for the preprocessor;
2915 * all other namespaces generate a [pragma] assembly directive.
2916 *
2917 * Invalid %pragmas are ignored and may have different
2918 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002919 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002920 t = tline;
2921 tline = tline->next;
2922 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002923 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002924 while (tok_type_(tline, TOK_WHITESPACE)) {
2925 t = tline;
2926 tline = tline->next;
2927 delete_Token(t);
2928 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002929 if (tok_type_(tline, TOK_ID)) {
2930 if (!nasm_stricmp(tline->text, "preproc")) {
2931 /* Preprocessor pragma */
2932 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002933 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002934 } else {
2935 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002936
2937 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002938 for (t = tline; t->next; t = t->next)
2939 ;
2940 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002941
2942 /* Prepend "[pragma " */
2943 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2944 t = new_Token(t, TOK_ID, "pragma", 6);
2945 t = new_Token(t, TOK_OTHER, "[", 1);
2946 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002947 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002948 }
2949 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002950 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002951
H. Peter Anvine2c80182005-01-15 22:15:51 +00002952 case PP_STACKSIZE:
2953 /* Directive to tell NASM what the default stack size is. The
2954 * default is for a 16-bit stack, and this can be overriden with
2955 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 */
2957 tline = tline->next;
2958 if (tline && tline->type == TOK_WHITESPACE)
2959 tline = tline->next;
2960 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002961 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002962 }
2963 if (nasm_stricmp(tline->text, "flat") == 0) {
2964 /* All subsequent ARG directives are for a 32-bit stack */
2965 StackSize = 4;
2966 StackPointer = "ebp";
2967 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002968 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002969 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2970 /* All subsequent ARG directives are for a 64-bit stack */
2971 StackSize = 8;
2972 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002973 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002974 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 } else if (nasm_stricmp(tline->text, "large") == 0) {
2976 /* All subsequent ARG directives are for a 16-bit stack,
2977 * far function call.
2978 */
2979 StackSize = 2;
2980 StackPointer = "bp";
2981 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002982 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 } else if (nasm_stricmp(tline->text, "small") == 0) {
2984 /* All subsequent ARG directives are for a 16-bit stack,
2985 * far function call. We don't support near functions.
2986 */
2987 StackSize = 2;
2988 StackPointer = "bp";
2989 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002990 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002992 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002994 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002995
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 case PP_ARG:
2997 /* TASM like ARG directive to define arguments to functions, in
2998 * the following form:
2999 *
3000 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3001 */
3002 offset = ArgOffset;
3003 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003004 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003006
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 /* Find the argument name */
3008 tline = tline->next;
3009 if (tline && tline->type == TOK_WHITESPACE)
3010 tline = tline->next;
3011 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003012 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003013 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 }
3015 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003016
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 /* Find the argument size type */
3018 tline = tline->next;
3019 if (!tline || tline->type != TOK_OTHER
3020 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003021 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003022 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 }
3024 tline = tline->next;
3025 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003026 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003027 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003029
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003031 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003032 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003033 size = parse_size(tt->text);
3034 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003035 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003037 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 }
3039 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003040
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003041 /* Round up to even stack slots */
3042 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003043
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 /* Now define the macro for the argument */
3045 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3046 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003047 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003049
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 /* Move to the next argument in the list */
3051 tline = tline->next;
3052 if (tline && tline->type == TOK_WHITESPACE)
3053 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003054 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003055 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003056 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 case PP_LOCAL:
3059 /* TASM like LOCAL directive to define local variables for a
3060 * function, in the following form:
3061 *
3062 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3063 *
3064 * The '= LocalSize' at the end is ignored by NASM, but is
3065 * required by TASM to define the local parameter size (and used
3066 * by the TASM macro package).
3067 */
3068 offset = LocalOffset;
3069 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003070 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003072
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 /* Find the argument name */
3074 tline = tline->next;
3075 if (tline && tline->type == TOK_WHITESPACE)
3076 tline = tline->next;
3077 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003078 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003079 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003080 }
3081 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003082
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 /* Find the argument size type */
3084 tline = tline->next;
3085 if (!tline || tline->type != TOK_OTHER
3086 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003087 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003088 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 }
3090 tline = tline->next;
3091 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003092 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003093 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003095
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003097 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003098 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003099 size = parse_size(tt->text);
3100 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003101 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003103 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 }
3105 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003106
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003107 /* Round up to even stack slots */
3108 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003109
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003110 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003111
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003112 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3114 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003115 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003116
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 /* Now define the assign to setup the enter_c macro correctly */
3118 snprintf(directive, sizeof(directive),
3119 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003120 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003121
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 /* Move to the next argument in the list */
3123 tline = tline->next;
3124 if (tline && tline->type == TOK_WHITESPACE)
3125 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003126 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003127 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003128 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003129
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 case PP_CLEAR:
3131 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003132 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 free_macros();
3134 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003135 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003136
H. Peter Anvin418ca702008-05-30 10:42:30 -07003137 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003139 skip_white_(t);
3140 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003142 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003143 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003144 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003145 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003146 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003147 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003148 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003149 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003150 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003151 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003152
3153 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003155 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003156
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003157 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003158 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003159 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003160 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003161 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003162 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003163 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003164 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003165 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003166 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003167 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003169 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003170 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003171 (pp_mode == PP_DEPS)
3172 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003173 if (!inc->fp) {
3174 /* -MG given but file not found */
3175 nasm_free(inc);
3176 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003177 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 inc->lineno = src_set_linnum(0);
3179 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003180 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003182 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003183 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003184 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003185
H. Peter Anvind2456592008-06-19 15:04:18 -07003186 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003187 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003188 const struct use_package *pkg;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003189 bool err;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003190
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003191 pkg = get_use_pkg(tline, dname, &err);
3192 if (err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003193 goto done;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003194
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003195 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003196 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003197 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003198 /*
3199 * Not already included, go ahead and include it.
3200 * Treat it as an include file for the purpose of
3201 * producing a listing.
3202 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003203 use_loaded[pkg->index] = true;
3204 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003205 nasm_new(inc);
3206 inc->next = istk;
3207 inc->fname = src_set_fname(NULL);
3208 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003209 inc->nolist = !list_option('b') || istk->nolist;
3210 istk = inc;
3211 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003212 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003213 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003214 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003215 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003216 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003217 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003218 tline = tline->next;
3219 skip_white_(tline);
3220 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003221 if (tline) {
3222 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003223 nasm_nonfatal("`%s' expects a context identifier",
3224 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003225 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 }
3227 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003228 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003229 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 p = nasm_strdup(tline->text);
3231 } else {
3232 p = NULL; /* Anonymous */
3233 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003234
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003236 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003237 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003238 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239 ctx->name = p;
3240 ctx->number = unique++;
3241 cstk = ctx;
3242 } else {
3243 /* %pop or %repl */
3244 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003245 nasm_nonfatal("`%s': context stack is empty",
3246 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003247 } else if (i == PP_POP) {
3248 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003249 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003250 "expected %s",
3251 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003252 else
3253 ctx_pop();
3254 } else {
3255 /* i == PP_REPL */
3256 nasm_free(cstk->name);
3257 cstk->name = p;
3258 p = NULL;
3259 }
3260 nasm_free(p);
3261 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003262 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003263 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003264 severity = ERR_FATAL;
3265 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003267 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003268 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003269 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003270 /*!
3271 *!user [on] %warning directives
3272 *! controls output of \c{%warning} directives (see \k{pperror}).
3273 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003274 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003276
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003277issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003278 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003279 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 tline->next = expand_smacro(tline->next);
3281 tline = tline->next;
3282 skip_white_(tline);
3283 t = tline ? tline->next : NULL;
3284 skip_white_(t);
3285 if (tok_type_(tline, TOK_STRING) && !t) {
3286 /* The line contains only a quoted string */
3287 p = tline->text;
3288 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003289 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003290 } else {
3291 /* Not a quoted string, or more than a quoted string */
3292 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003293 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003294 nasm_free(p);
3295 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003296 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003297 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003298
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003299 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003300 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003301 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003302 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003303 j = if_condition(tline->next, i);
3304 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003305 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003306 cond = nasm_malloc(sizeof(Cond));
3307 cond->next = istk->conds;
3308 cond->state = j;
3309 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003310 if(istk->mstk.mstk)
3311 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003312 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003313
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003314 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003315 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003316 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003317 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003318 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003319 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003320 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003321
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003322 case COND_DONE:
3323 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003324 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003325
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003326 case COND_ELSE_TRUE:
3327 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003328 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003329 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003330 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003331 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003332
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003333 case COND_IF_FALSE:
3334 /*
3335 * IMPORTANT: In the case of %if, we will already have
3336 * called expand_mmac_params(); however, if we're
3337 * processing an %elif we must have been in a
3338 * non-emitting mode, which would have inhibited
3339 * the normal invocation of expand_mmac_params().
3340 * Therefore, we have to do it explicitly here.
3341 */
3342 j = if_condition(expand_mmac_params(tline->next), i);
3343 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003344 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003345 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003346 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003347 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003348
H. Peter Anvine2c80182005-01-15 22:15:51 +00003349 case PP_ELSE:
3350 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003351 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003352 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003353 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003354 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003355 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003356 case COND_IF_TRUE:
3357 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003358 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003359 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003360
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003361 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003362 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003363
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003364 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003365 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003366 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003367
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 case COND_ELSE_TRUE:
3369 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003370 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003371 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003372 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003374 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003375 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003376
H. Peter Anvine2c80182005-01-15 22:15:51 +00003377 case PP_ENDIF:
3378 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003379 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003380 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003381 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003382 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003383 cond = istk->conds;
3384 istk->conds = cond->next;
3385 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003386 if(istk->mstk.mstk)
3387 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003388 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389
H. Peter Anvin8b262472019-02-26 14:00:54 -08003390 case PP_RMACRO:
3391 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003392 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003393 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003394 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003395 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003396 if (i == PP_RMACRO)
3397 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003398 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003399 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003400 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003401 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003402
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003403 src_get(&defining->xline, &defining->fname);
3404
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003405 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3406 while (mmac) {
3407 if (!strcmp(mmac->name, defining->name) &&
3408 (mmac->nparam_min <= defining->nparam_max
3409 || defining->plus)
3410 && (defining->nparam_min <= mmac->nparam_max
3411 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003412 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003413 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003414 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003415 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003416 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003417 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003418 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003419
H. Peter Anvine2c80182005-01-15 22:15:51 +00003420 case PP_ENDM:
3421 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003422 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003423 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003424 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003425 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003426 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3427 defining->next = *mmhead;
3428 *mmhead = defining;
3429 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003430 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003431
H. Peter Anvin89cee572009-07-15 09:16:54 -04003432 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003433 /*
3434 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003435 * macro-end marker for a macro with a name. Then we
3436 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003437 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003438 list_for_each(l, istk->expansion)
3439 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003440 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003441
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003442 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003443 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003444 * Remove all conditional entries relative to this
3445 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003446 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003447 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3448 cond = istk->conds;
3449 istk->conds = cond->next;
3450 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003451 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003452 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003453 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003454 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003455 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003456 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003457
H. Peter Anvina26433d2008-07-16 14:40:01 -07003458 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003459 casesense = false;
3460 /* fall through */
3461 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003462 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003463 MMacro **mmac_p;
3464 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003465
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003466 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003467 spec.casesense = casesense;
3468 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003469 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003471 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3472 while (mmac_p && *mmac_p) {
3473 mmac = *mmac_p;
3474 if (mmac->casesense == spec.casesense &&
3475 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3476 mmac->nparam_min == spec.nparam_min &&
3477 mmac->nparam_max == spec.nparam_max &&
3478 mmac->plus == spec.plus) {
3479 *mmac_p = mmac->next;
3480 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003482 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003483 }
3484 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003485 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003486 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003487 }
3488
H. Peter Anvine2c80182005-01-15 22:15:51 +00003489 case PP_ROTATE:
3490 if (tline->next && tline->next->type == TOK_WHITESPACE)
3491 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003492 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003494 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 return DIRECTIVE_FOUND;
3496 }
3497 t = expand_smacro(tline->next);
3498 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003499 pps.tptr = tline = t;
3500 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 tokval.t_type = TOKEN_INVALID;
3502 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003503 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003504 free_tlist(tline);
3505 if (!evalresult)
3506 return DIRECTIVE_FOUND;
3507 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003508 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003510 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003511 return DIRECTIVE_FOUND;
3512 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003513 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003514 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003515 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003516 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003517 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003518 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003519 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003520
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003521 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003522 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003523 rotate += mmac->nparam;
3524
3525 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003526 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003527 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003528
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003530 {
3531 MMacro *tmp_defining;
3532
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003533 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 do {
3535 tline = tline->next;
3536 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003537
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 if (tok_type_(tline, TOK_ID) &&
3539 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003540 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003541 do {
3542 tline = tline->next;
3543 } while (tok_type_(tline, TOK_WHITESPACE));
3544 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003545
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003547 pps.tptr = expand_smacro(tline);
3548 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003550 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003552 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003553 if (!evalresult)
3554 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003556 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003558 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003559 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003560 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003561 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003562 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003563 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3564 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003565 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003566 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003567 /*!
3568 *!negative-rep [on] regative %rep count
3569 *! warns about negative counts given to the \c{%rep}
3570 *! preprocessor directive.
3571 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003572 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003573 "negative `%%rep' count: %"PRId64, count);
3574 count = 0;
3575 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003576 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003577 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003578 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003579 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003580 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003582 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003583 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003584 defining->nolist = nolist;
3585 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003586 defining->mstk = istk->mstk;
3587 defining->dstk.mstk = tmp_defining;
3588 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003589 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003590 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003591 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003592
H. Peter Anvine2c80182005-01-15 22:15:51 +00003593 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003594 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003595 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003596 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003598
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 /*
3600 * Now we have a "macro" defined - although it has no name
3601 * and we won't be entering it in the hash tables - we must
3602 * push a macro-end marker for it on to istk->expansion.
3603 * After that, it will take care of propagating itself (a
3604 * macro-end marker line for a macro which is really a %rep
3605 * block will cause the macro to be re-expanded, complete
3606 * with another macro-end marker to ensure the process
3607 * continues) until the whole expansion is forcibly removed
3608 * from istk->expansion by a %exitrep.
3609 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003610 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003611 l->next = istk->expansion;
3612 l->finishes = defining;
3613 l->first = NULL;
3614 istk->expansion = l;
3615
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003616 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003617
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003618 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003619 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003620 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003621
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003623 /*
3624 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003625 * macro-end marker for a macro with no name. Then we set
3626 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003627 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003628 list_for_each(l, istk->expansion)
3629 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003630 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003631
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003632 if (l)
3633 l->finishes->in_progress = 1;
3634 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003635 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003636 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003637
H. Peter Anvin8b262472019-02-26 14:00:54 -08003638 case PP_DEFINE:
3639 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003640 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003641 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003642 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003643 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003644
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003645 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003646 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003647
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003648 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003649 lastp = &tline->next;
3650 nparam = parse_smacro_template(&lastp, &tmpl);
3651 tline = *lastp;
3652 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003653
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003654 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003655 macro_start = tline;
3656 if (!is_macro_id(macro_start)) {
3657 nasm_nonfatal("`%s' expects a macro identifier to alias",
3658 dname);
3659 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003661 tt = macro_start->next;
3662 macro_start->next = NULL;
3663 tline = tline->next;
3664 skip_white_(tline);
3665 if (tline && tline->type) {
3666 nasm_warn(WARN_OTHER,
3667 "trailing garbage after aliasing identifier ignored");
3668 }
3669 free_tlist(tt);
3670 tmpl.alias = true;
3671 } else {
3672 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003673 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003674 tline = expand_smacro(tline);
3675
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003676 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003677 macro_start = NULL;
3678 t = tline;
3679 while (t) {
3680 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003681 for (i = 0; i < nparam; i++) {
3682 if ((size_t)tmpl.params[i].namelen == t->len &&
3683 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3684 t->type = tok_smac_param(i);
3685 break;
3686 }
3687 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003688 }
3689 tt = t->next;
3690 t->next = macro_start;
3691 macro_start = t;
3692 t = tt;
3693 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003694 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003695
H. Peter Anvine2c80182005-01-15 22:15:51 +00003696 /*
3697 * Good. We now have a macro name, a parameter count, and a
3698 * token list (in reverse order) for an expansion. We ought
3699 * to be OK just to create an SMacro, store it, and let
3700 * free_tlist have the rest of the line (which we have
3701 * carefully re-terminated after chopping off the expansion
3702 * from the end).
3703 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003704 define_smacro(mname, casesense, macro_start, &tmpl);
3705 break;
3706 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003707
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003709 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003710 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003711 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003712 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003713 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003714
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003715 undef_smacro(mname, i == PP_UNDEFALIAS);
3716 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003717
H. Peter Anvin8b262472019-02-26 14:00:54 -08003718 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003719 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003720 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003721
H. Peter Anvin9e200162008-06-04 17:23:14 -07003722 last = tline;
3723 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003724 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003725
3726 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003727 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003728
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003729 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003730 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003731 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003732
3733 /*
3734 * We now have a macro name, an implicit parameter count of
3735 * zero, and a string token to use as an expansion. Create
3736 * and store an SMacro.
3737 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003738 define_smacro(mname, casesense, macro_start, NULL);
3739 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003740
H. Peter Anvin8b262472019-02-26 14:00:54 -08003741 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003742 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003743 goto done;
3744
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003745 last = tline;
3746 tline = expand_smacro(tline->next);
3747 last->next = NULL;
3748
3749 t = tline;
3750 while (tok_type_(t, TOK_WHITESPACE))
3751 t = t->next;
3752 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003753 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003754 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003755 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003756 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003757 }
3758
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003759 /*
3760 * Convert the string to a token stream. Note that smacros
3761 * are stored with the token stream reversed, so we have to
3762 * reverse the output of tokenize().
3763 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003764 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003765 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003766
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003767 /*
3768 * We now have a macro name, an implicit parameter count of
3769 * zero, and a numeric token to use as an expansion. Create
3770 * and store an SMacro.
3771 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003772 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003773 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003774 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003775
H. Peter Anvin418ca702008-05-30 10:42:30 -07003776 case PP_PATHSEARCH:
3777 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003778 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003779
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003780 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003781 goto done;
3782
H. Peter Anvin418ca702008-05-30 10:42:30 -07003783 last = tline;
3784 tline = expand_smacro(tline->next);
3785 last->next = NULL;
3786
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003787 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003788 while (tok_type_(t, TOK_WHITESPACE))
3789 t = t->next;
3790
3791 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003792 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003793 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003794 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003795 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003796 }
3797 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003798 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003799 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003800 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003801 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003802
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003803 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003804 if (!found_path)
3805 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003806 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003807
3808 /*
3809 * We now have a macro name, an implicit parameter count of
3810 * zero, and a string token to use as an expansion. Create
3811 * and store an SMacro.
3812 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003813 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003814 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003815 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003816 }
3817
H. Peter Anvine2c80182005-01-15 22:15:51 +00003818 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003819 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003820 goto done;
3821
H. Peter Anvine2c80182005-01-15 22:15:51 +00003822 last = tline;
3823 tline = expand_smacro(tline->next);
3824 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003825
H. Peter Anvine2c80182005-01-15 22:15:51 +00003826 t = tline;
3827 while (tok_type_(t, TOK_WHITESPACE))
3828 t = t->next;
3829 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003830 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003831 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003832 free_tlist(tline);
3833 free_tlist(origline);
3834 return DIRECTIVE_FOUND;
3835 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003836
H. Peter Anvin8b262472019-02-26 14:00:54 -08003837 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003838
H. Peter Anvine2c80182005-01-15 22:15:51 +00003839 /*
3840 * We now have a macro name, an implicit parameter count of
3841 * zero, and a numeric token to use as an expansion. Create
3842 * and store an SMacro.
3843 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003844 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003845 free_tlist(tline);
3846 free_tlist(origline);
3847 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003848
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003849 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003850 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003851 goto done;
3852
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003853 last = tline;
3854 tline = expand_smacro(tline->next);
3855 last->next = NULL;
3856
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003857 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003858 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003859 switch (t->type) {
3860 case TOK_WHITESPACE:
3861 break;
3862 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003863 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003864 break;
3865 case TOK_OTHER:
3866 if (!strcmp(t->text, ",")) /* permit comma separators */
3867 break;
3868 /* else fall through */
3869 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003870 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003871 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003872 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003873 }
3874 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003875
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003876 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003877 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003878 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003879 memcpy(p, t->text, t->len);
3880 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003881 }
3882 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003883
3884 /*
3885 * We now have a macro name, an implicit parameter count of
3886 * zero, and a numeric token to use as an expansion. Create
3887 * and store an SMacro.
3888 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003889 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003890 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003891 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003892 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003893 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003894
H. Peter Anvine2c80182005-01-15 22:15:51 +00003895 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003896 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003897 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003898 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003899
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003900 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003901 goto done;
3902
H. Peter Anvine2c80182005-01-15 22:15:51 +00003903 last = tline;
3904 tline = expand_smacro(tline->next);
3905 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003906
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003907 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003908 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003909 while (tok_type_(t, TOK_WHITESPACE))
3910 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003911
H. Peter Anvine2c80182005-01-15 22:15:51 +00003912 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003913 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003914 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003916 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003917 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003918
H. Peter Anvin8b262472019-02-26 14:00:54 -08003919 pps.tptr = t->next;
3920 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003921 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003922 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003923 if (!evalresult) {
3924 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003925 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003926 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003927 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003929 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003930 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003931 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003932
H. Peter Anvin8b262472019-02-26 14:00:54 -08003933 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3934 pps.tptr = pps.tptr->next;
3935 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003936 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003937 } else {
3938 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003939 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003940 if (!evalresult) {
3941 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003942 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003943 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003944 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003945 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003946 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003947 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003948 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003949 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003950
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003951 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003952
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003953 /* make start and count being in range */
3954 if (start < 0)
3955 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003956 if (count < 0)
3957 count = len + count + 1 - start;
3958 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003959 count = len - start;
3960 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003961 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003962
H. Peter Anvin8b262472019-02-26 14:00:54 -08003963 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003964 macro_start->len = count;
3965 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3966 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003967
H. Peter Anvine2c80182005-01-15 22:15:51 +00003968 /*
3969 * We now have a macro name, an implicit parameter count of
3970 * zero, and a numeric token to use as an expansion. Create
3971 * and store an SMacro.
3972 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003973 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003974 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003975 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003976 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003977
H. Peter Anvin8b262472019-02-26 14:00:54 -08003978 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003979 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003980 goto done;
3981
H. Peter Anvine2c80182005-01-15 22:15:51 +00003982 last = tline;
3983 tline = expand_smacro(tline->next);
3984 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003985
H. Peter Anvin8b262472019-02-26 14:00:54 -08003986 pps.tptr = tline;
3987 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003988 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003989 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003990 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003991 if (!evalresult)
3992 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003993
H. Peter Anvine2c80182005-01-15 22:15:51 +00003994 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003995 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003996
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003998 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 free_tlist(origline);
4000 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004001 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004002
H. Peter Anvin8b262472019-02-26 14:00:54 -08004003 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004004
H. Peter Anvine2c80182005-01-15 22:15:51 +00004005 /*
4006 * We now have a macro name, an implicit parameter count of
4007 * zero, and a numeric token to use as an expansion. Create
4008 * and store an SMacro.
4009 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004010 define_smacro(mname, casesense, macro_start, NULL);
4011 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004012
H. Peter Anvind2354082019-08-27 16:38:48 -07004013 case PP_ALIASES:
4014 tline = tline->next;
4015 tline = expand_smacro(tline);
4016 do_aliases = pp_get_boolean_option(tline, do_aliases);
4017 break;
4018
H. Peter Anvine2c80182005-01-15 22:15:51 +00004019 case PP_LINE:
4020 /*
4021 * Syntax is `%line nnn[+mmm] [filename]'
4022 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004023 if (unlikely(pp_noline))
4024 goto done;
4025
H. Peter Anvine2c80182005-01-15 22:15:51 +00004026 tline = tline->next;
4027 skip_white_(tline);
4028 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004029 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004030 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004031 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004032 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004033 m = 1;
4034 tline = tline->next;
4035 if (tok_is_(tline, "+")) {
4036 tline = tline->next;
4037 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004038 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004039 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004040 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004041 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004042 tline = tline->next;
4043 }
4044 skip_white_(tline);
4045 src_set_linnum(k);
4046 istk->lineinc = m;
4047 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004048 char *fname = detoken(tline, false);
4049 src_set_fname(fname);
4050 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004051 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004052 break;
4053 }
4054
4055done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004056 free_tlist(origline);
4057 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004058}
4059
4060/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004061 * Ensure that a macro parameter contains a condition code and
4062 * nothing else. Return the condition code index if so, or -1
4063 * otherwise.
4064 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004066{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004067 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004068
H. Peter Anvin25a99342007-09-22 17:45:45 -07004069 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004070 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004071
H. Peter Anvineba20a72002-04-30 20:53:55 +00004072 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03004073 if (!t)
4074 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004075 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004076 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004077 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004078 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004079 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004080 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004081
Cyrill Gorcunov19456392012-05-02 00:18:56 +04004082 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004083}
4084
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004085/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004086 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004087 * pasting, if @handle_explicit passed then explicit pasting
4088 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004089 * The @m array can contain a series of token types which are
4090 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004091 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004092static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004093 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004094{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004095 Token *tok, *next, **prev_next, **prev_nonspace;
4096 bool pasted = false;
4097 char *buf, *p;
4098 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004099
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004100 /*
4101 * The last token before pasting. We need it
4102 * to be able to connect new handled tokens.
4103 * In other words if there were a tokens stream
4104 *
4105 * A -> B -> C -> D
4106 *
4107 * and we've joined tokens B and C, the resulting
4108 * stream should be
4109 *
4110 * A -> BC -> D
4111 */
4112 tok = *head;
4113 prev_next = NULL;
4114
4115 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
4116 prev_nonspace = head;
4117 else
4118 prev_nonspace = NULL;
4119
4120 while (tok && (next = tok->next)) {
4121
4122 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004123 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004124 /* Zap redundant whitespaces */
4125 while (tok_type_(next, TOK_WHITESPACE))
4126 next = delete_Token(next);
4127 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004128 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004129
4130 case TOK_PASTE:
4131 /* Explicit pasting */
4132 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004133 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004134 next = delete_Token(tok);
4135
4136 while (tok_type_(next, TOK_WHITESPACE))
4137 next = delete_Token(next);
4138
4139 if (!pasted)
4140 pasted = true;
4141
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004142 /* Left pasting token is start of line */
4143 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004144 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004145
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004146 /*
4147 * No ending token, this might happen in two
4148 * cases
4149 *
4150 * 1) There indeed no right token at all
4151 * 2) There is a bare "%define ID" statement,
4152 * and @ID does expand to whitespace.
4153 *
4154 * So technically we need to do a grammar analysis
4155 * in another stage of parsing, but for now lets don't
4156 * change the behaviour people used to. Simply allow
4157 * whitespace after paste token.
4158 */
4159 if (!next) {
4160 /*
4161 * Zap ending space tokens and that's all.
4162 */
4163 tok = (*prev_nonspace)->next;
4164 while (tok_type_(tok, TOK_WHITESPACE))
4165 tok = delete_Token(tok);
4166 tok = *prev_nonspace;
4167 tok->next = NULL;
4168 break;
4169 }
4170
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004171 tok = *prev_nonspace;
4172 while (tok_type_(tok, TOK_WHITESPACE))
4173 tok = delete_Token(tok);
4174 len = strlen(tok->text);
4175 len += strlen(next->text);
4176
4177 p = buf = nasm_malloc(len + 1);
4178 strcpy(p, tok->text);
4179 p = strchr(p, '\0');
4180 strcpy(p, next->text);
4181
4182 delete_Token(tok);
4183
4184 tok = tokenize(buf);
4185 nasm_free(buf);
4186
4187 *prev_nonspace = tok;
4188 while (tok && tok->next)
4189 tok = tok->next;
4190
4191 tok->next = delete_Token(next);
4192
4193 /* Restart from pasted tokens head */
4194 tok = *prev_nonspace;
4195 break;
4196
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004197 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004198 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004199 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004200 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4201 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004202
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004203 len = 0;
4204 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4205 len += strlen(next->text);
4206 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004207 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004208
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004209 /* No match or no text to process */
4210 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004211 break;
4212
4213 len += strlen(tok->text);
4214 p = buf = nasm_malloc(len + 1);
4215
Adam Majer1a069432017-07-25 11:12:35 +02004216 strcpy(p, tok->text);
4217 p = strchr(p, '\0');
4218 tok = delete_Token(tok);
4219
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004220 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004221 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4222 strcpy(p, tok->text);
4223 p = strchr(p, '\0');
4224 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004225 tok = delete_Token(tok);
4226 }
4227
4228 tok = tokenize(buf);
4229 nasm_free(buf);
4230
4231 if (prev_next)
4232 *prev_next = tok;
4233 else
4234 *head = tok;
4235
4236 /*
4237 * Connect pasted into original stream,
4238 * ie A -> new-tokens -> B
4239 */
4240 while (tok && tok->next)
4241 tok = tok->next;
4242 tok->next = next;
4243
4244 if (!pasted)
4245 pasted = true;
4246
4247 /* Restart from pasted tokens head */
4248 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004249 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004250
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004251 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004252 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004253
4254 prev_next = &tok->next;
4255
4256 if (tok->next &&
4257 !tok_type_(tok->next, TOK_WHITESPACE) &&
4258 !tok_type_(tok->next, TOK_PASTE))
4259 prev_nonspace = prev_next;
4260
4261 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004262 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004263
4264 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004265}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004266
4267/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004268 * Computes the proper rotation of mmacro parameters
4269 */
4270static int mmac_rotate(const MMacro *mac, unsigned int n)
4271{
4272 if (--n < mac->nparam)
4273 n = (n + mac->rotate) % mac->nparam;
4274
4275 return n+1;
4276}
4277
4278/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004279 * expands to a list of tokens from %{x:y}
4280 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004281static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004282{
4283 Token *t = tline, **tt, *tm, *head;
4284 char *pos;
4285 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004286
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004287 pos = strchr(tline->text, ':');
4288 nasm_assert(pos);
4289
4290 lst = atoi(pos + 1);
4291 fst = atoi(tline->text + 1);
4292
4293 /*
4294 * only macros params are accounted so
4295 * if someone passes %0 -- we reject such
4296 * value(s)
4297 */
4298 if (lst == 0 || fst == 0)
4299 goto err;
4300
4301 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004302 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4303 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004304 goto err;
4305
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004306 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4307 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004308
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004309 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004310 fst--, lst--;
4311
4312 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004313 * It will be at least one token. Note we
4314 * need to scan params until separator, otherwise
4315 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004316 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004317 j = (fst + mac->rotate) % mac->nparam;
4318 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004319 if (!tm)
4320 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004321 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004322 tt = &head->next, tm = tm->next;
4323 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004324 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004325 *tt = t, tt = &t->next, tm = tm->next;
4326 }
4327
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004328 if (fst < lst) {
4329 for (i = fst + 1; i <= lst; i++) {
4330 t = new_Token(NULL, TOK_OTHER, ",", 0);
4331 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004332 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004333 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004334 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004335 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004336 *tt = t, tt = &t->next, tm = tm->next;
4337 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004338 }
4339 } else {
4340 for (i = fst - 1; i >= lst; i--) {
4341 t = new_Token(NULL, TOK_OTHER, ",", 0);
4342 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004343 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004344 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004345 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004346 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004347 *tt = t, tt = &t->next, tm = tm->next;
4348 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004349 }
4350 }
4351
4352 *last = tt;
4353 return head;
4354
4355err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004356 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004357 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004358 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004359}
4360
H. Peter Anvin76690a12002-04-30 20:52:49 +00004361/*
4362 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004363 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004364 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004365 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004366static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004367{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004368 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004369 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004370 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004371
4372 tail = &thead;
4373 thead = NULL;
4374
H. Peter Anvine2c80182005-01-15 22:15:51 +00004375 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004376 bool change;
4377 Token *t = tline;
4378 char *text = t->text;
4379 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004380
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004381 tline = tline->next;
4382 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004383
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004384 switch (type) {
4385 case TOK_PREPROC_ID:
4386 {
4387 Token *tt = NULL;
4388
4389 change = false;
4390
4391 if (!text || !text[0])
4392 break;
4393 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4394 ((text[1] == '+' || text[1] == '-') && text[2])))
4395 break;
4396
4397 change = true;
4398
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004399 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004400 nasm_nonfatal("`%s': not in a macro call", text);
4401 text = NULL;
4402 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004403 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004404
4405 if (strchr(text, ':')) {
4406 /*
4407 * seems we have a parameters range here
4408 */
4409 Token *head, **last;
4410 head = expand_mmac_params_range(mac, t, &last);
4411 if (head) {
4412 *tail = head;
4413 *last = tline;
4414 text = NULL;
4415 }
4416 break;
4417 }
4418
4419 switch (text[1]) {
4420 /*
4421 * We have to make a substitution of one of the
4422 * forms %1, %-1, %+1, %%foo, %0, %00.
4423 */
4424 case '0':
4425 if (!text[2]) {
4426 type = TOK_NUMBER;
4427 text = nasm_asprintf("%d", mac->nparam);
4428 break;
4429 }
4430 if (text[2] != '0' || text[3])
4431 goto invalid;
4432 /* a possible captured label == mac->params[0] */
4433 /* fall through */
4434 default:
4435 {
4436 unsigned long n;
4437 char *ep;
4438
4439 n = strtoul(text + 1, &ep, 10);
4440 if (unlikely(*ep))
4441 goto invalid;
4442
4443 if (n <= mac->nparam) {
4444 n = mmac_rotate(mac, n);
4445 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4446 }
4447 text = NULL;
4448 break;
4449 }
4450 case '%':
4451 type = TOK_ID;
4452 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4453 break;
4454 case '-':
4455 case '+':
4456 {
4457 int cc;
4458 unsigned long n;
4459 char *ep;
4460
4461 text = NULL;
4462
4463 n = strtoul(t->text + 2, &ep, 10);
4464 if (unlikely(*ep))
4465 goto invalid;
4466
4467 if (n && n < mac->nparam) {
4468 n = mmac_rotate(mac, n);
4469 tt = mac->params[n];
4470 }
4471 cc = find_cc(tt);
4472 if (cc == -1) {
4473 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvind2354082019-08-27 16:38:48 -07004474 t->text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004475 text = NULL;
4476 break;
4477 }
4478
4479 type = TOK_ID;
4480 if (text[1] == '-') {
4481 int ncc = inverse_ccs[cc];
4482 if (unlikely(ncc == -1)) {
4483 nasm_nonfatal("condition code `%s' is not invertible",
4484 conditions[cc]);
4485 break;
4486 }
4487 cc = ncc;
4488 }
4489 text = nasm_strdup(conditions[cc]);
4490 break;
4491 }
4492
4493 invalid:
4494 nasm_nonfatal("invalid macro parameter: `%s'", text);
4495 text = NULL;
4496 break;
4497 }
4498 break;
4499 }
4500
4501 case TOK_PREPROC_Q:
4502 if (mac) {
4503 type = TOK_ID;
4504 text = nasm_strdup(mac->iname);
4505 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004506 } else {
4507 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004508 }
4509 break;
4510
4511 case TOK_PREPROC_QQ:
4512 if (mac) {
4513 type = TOK_ID;
4514 text = nasm_strdup(mac->name);
4515 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004516 } else {
4517 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004518 }
4519 break;
4520
4521 case TOK_INDIRECT:
4522 {
4523 Token *tt;
4524
4525 tt = tokenize(t->text);
4526 tt = expand_mmac_params(tt);
4527 tt = expand_smacro(tt);
4528 /* Why dup_tlist() here? We should own tt... */
4529 dup_tlist(tt, &tail);
4530 text = NULL;
4531 change = true;
4532 break;
4533 }
4534
4535 default:
4536 change = false;
4537 break;
4538 }
4539
4540 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004541 if (!text) {
4542 delete_Token(t);
4543 } else {
4544 *tail = t;
4545 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004546 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004547 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004548 t->type = type;
4549 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004550 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004551 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004552 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004553 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004554 tail = &t->next;
4555 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004556 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004557
H. Peter Anvineba20a72002-04-30 20:53:55 +00004558 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004559
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004560 if (changed) {
4561 const struct tokseq_match t[] = {
4562 {
4563 PP_CONCAT_MASK(TOK_ID) |
4564 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4565 PP_CONCAT_MASK(TOK_ID) |
4566 PP_CONCAT_MASK(TOK_NUMBER) |
4567 PP_CONCAT_MASK(TOK_FLOAT) |
4568 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4569 },
4570 {
4571 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4572 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4573 }
4574 };
4575 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4576 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004577
H. Peter Anvin76690a12002-04-30 20:52:49 +00004578 return thead;
4579}
4580
H. Peter Anvin322bee02019-08-10 01:38:06 -07004581static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004582
H. Peter Anvin76690a12002-04-30 20:52:49 +00004583/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004584 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004585 * a macro at all, it is simply copied to the output and the pointer
4586 * advanced. tpp should be a pointer to a pointer (usually the next
4587 * pointer of the previous token) to the first token. **tpp is updated
4588 * to point to the last token of the expansion, and *tpp updated to
4589 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004590 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004591 * If the expansion is empty, *tpp will be unchanged but **tpp will
4592 * be advanced past the macro call.
4593 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004594 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004595 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004596static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004597{
4598 Token **params = NULL;
4599 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004600 Token *tline = **tpp;
4601 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004602 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004603 int i;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004604 Token *t, *tup, *ttail;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004605 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004606 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004607
4608 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004609 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004610
4611 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004612
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004613 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004614 smacro_deadman.levels--;
4615
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004616 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004617 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004618 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004619 smacro_deadman.triggered = true;
4620 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004621 goto not_a_macro;
4622 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004623 head = (SMacro *)hash_findix(&smacros, mname);
4624 } else if (tline->type == TOK_PREPROC_ID) {
4625 Context *ctx = get_ctx(mname, &mname);
4626 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4627 } else {
4628 goto not_a_macro;
4629 }
4630
4631 /*
4632 * We've hit an identifier of some sort. First check whether the
4633 * identifier is a single-line macro at all, then think about
4634 * checking for parameters if necessary.
4635 */
4636 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004637 if (unlikely(m->alias && !do_aliases))
4638 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004639 if (!mstrcmp(m->name, mname, m->casesense))
4640 break;
4641 }
4642
4643 if (!m) {
4644 goto not_a_macro;
4645 }
4646
4647 /* Parse parameters, if applicable */
4648
4649 params = NULL;
4650 nparam = 0;
4651
4652 if (m->nparam == 0) {
4653 /*
4654 * Simple case: the macro is parameterless.
4655 * Nothing to parse; the expansion code will
4656 * drop the macro name token.
4657 */
4658 } else {
4659 /*
4660 * Complicated case: at least one macro with this name
4661 * exists and takes parameters. We must find the
4662 * parameters in the call, count them, find the SMacro
4663 * that corresponds to that form of the macro call, and
4664 * substitute for the parameters when we expand. What a
4665 * pain.
4666 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004667 Token *t;
4668 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004669
4670 tline = tline->next;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004671 skip_white_(tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004672 if (!tok_is_(tline, "(")) {
4673 /*
4674 * This macro wasn't called with parameters: ignore
4675 * the call. (Behaviour borrowed from gnu cpp.)
4676 */
4677 goto not_a_macro;
4678 }
4679
4680 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004681 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004682 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004683 t = tline; /* tline points to leading ( */
4684
4685 while (paren) {
4686 t = t->next;
4687
4688 if (!t) {
4689 nasm_nonfatal("macro call expects terminating `)'");
4690 goto not_a_macro;
4691 }
4692
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004693 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004694 continue;
4695
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004696 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004697 case ',':
4698 if (!brackets)
4699 nparam++;
4700 break;
4701
4702 case '{':
4703 brackets++;
4704 break;
4705
4706 case '}':
4707 if (brackets > 0)
4708 brackets--;
4709 break;
4710
4711 case '(':
4712 if (!brackets)
4713 paren++;
4714 break;
4715
4716 case ')':
4717 if (!brackets)
4718 paren--;
4719 break;
4720
4721 default:
4722 break; /* Normal token */
4723 }
4724 }
4725
4726 /*
4727 * Look for a macro matching in both name and parameter count.
4728 * We already know any matches cannot be anywhere before the
4729 * current position of "m", so there is no reason to
4730 * backtrack.
4731 */
4732 while (1) {
4733 if (!m) {
4734 /*!
4735 *!macro-params-single [on] single-line macro calls with wrong parameter count
4736 *! warns about \i{single-line macros} being invoked
4737 *! with the wrong number of parameters.
4738 */
4739 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4740 "single-line macro `%s' exists, "
4741 "but not taking %d parameter%s",
4742 mname, nparam, (nparam == 1) ? "" : "s");
4743 goto not_a_macro;
4744 }
4745
4746 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004747 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004748 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004749 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004750 break; /* Also good */
4751 }
4752 m = m->next;
4753 }
4754 }
4755
4756 if (m->in_progress)
4757 goto not_a_macro;
4758
4759 /* Expand the macro */
4760 m->in_progress = true;
4761
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004762 if (nparam) {
4763 /* Extract parameters */
4764 Token **phead, **pep;
4765 int white = 0;
4766 int brackets = 0;
4767 int paren;
4768 bool bracketed = false;
4769 bool bad_bracket = false;
4770 enum sparmflags flags;
4771
4772 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004773 paren = 1;
4774 nasm_newn(params, nparam);
4775 i = 0;
4776 flags = m->params[i].flags;
4777 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004778 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004779
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004780 while (paren) {
4781 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004782 char ch;
4783
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004784 tline = tline->next;
4785
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004786 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004787 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004788
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004789 ch = 0;
4790 skip = false;
4791
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004792
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004793 switch (tline->type) {
4794 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004795 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004796 ch = tline->text[0];
4797 break;
4798
4799 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004800 if (!(flags & SPARM_NOSTRIP)) {
4801 if (brackets || *phead)
4802 white++; /* Keep interior whitespace */
4803 skip = true;
4804 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004805 break;
4806
4807 default:
4808 break;
4809 }
4810
4811 switch (ch) {
4812 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004813 if (!brackets && !(flags & SPARM_GREEDY)) {
4814 i++;
4815 nasm_assert(i < nparam);
4816 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004817 *pep = NULL;
4818 bracketed = false;
4819 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004820 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004821 }
4822 break;
4823
4824 case '{':
4825 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004826 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4827 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004828 }
4829 brackets++;
4830 break;
4831
4832 case '}':
4833 if (brackets > 0) {
4834 if (!--brackets)
4835 skip = bracketed;
4836 }
4837 break;
4838
4839 case '(':
4840 if (!brackets)
4841 paren++;
4842 break;
4843
4844 case ')':
4845 if (!brackets) {
4846 paren--;
4847 if (!paren) {
4848 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004849 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004850 }
4851 }
4852 break;
4853
4854 default:
4855 break; /* Normal token */
4856 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004857
4858 if (!skip) {
4859 Token *t;
4860
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004861 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004862
4863 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004864 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004865 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004866 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004867 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004868 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004869 pep = &t->next;
4870 white = 0;
4871 }
4872 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004873
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004874 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004875 * Possible further processing of parameters. Note that the
4876 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004877 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004878 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004879 enum sparmflags flags = m->params[i].flags;
4880
4881 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004882 /* Evaluate this parameter as a number */
4883 struct ppscan pps;
4884 struct tokenval tokval;
4885 expr *evalresult;
4886 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004887
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004888 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4889 pps.ntokens = -1;
4890 tokval.t_type = TOKEN_INVALID;
4891 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004892
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004893 free_tlist(eval_param);
4894 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004895
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004896 if (!evalresult) {
4897 /* Nothing meaningful to do */
4898 } else if (tokval.t_type) {
4899 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4900 } else if (!is_simple(evalresult)) {
4901 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4902 } else {
4903 params[i] = make_tok_num(reloc_value(evalresult));
4904 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004905 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004906
4907 if (flags & SPARM_STR) {
4908 /* Convert expansion to a quoted string */
4909 char *arg;
4910 Token *qs;
4911
4912 qs = expand_smacro_noreset(params[i]);
4913 arg = detoken(qs, false);
4914 free_tlist(qs);
4915 params[i] = make_tok_qstr(arg);
4916 nasm_free(arg);
4917 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004918 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004919 }
4920
4921 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004922 tline = tline->next; /* Remove the macro call from the input */
4923 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004924
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004925 /* Note: we own the expansion this returns. */
4926 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004927
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004928 tup = NULL;
4929 ttail = NULL; /* Pointer to the last token of the expansion */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004930 cond_comma = false;
4931
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004932 while (t) {
4933 enum pp_token_type type = t->type;
4934 Token *tnext = t->next;
4935 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004936
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004937 switch (type) {
4938 case TOK_PREPROC_Q:
4939 case TOK_PREPROC_QQ:
4940 t->type = TOK_ID;
4941 nasm_free(t->text);
4942 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4943 t->len = nasm_last_string_len();
4944 t->next = tline;
4945 break;
4946
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004947 case TOK_COND_COMMA:
4948 delete_Token(t);
4949 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4950 break;
4951
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004952 case TOK_ID:
4953 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004954 /*
4955 * Chain this into the target line *before* expanding,
4956 * that way we pick up any arguments to the new macro call,
4957 * if applicable.
4958 */
4959 t->next = tline;
4960 tp = &t;
4961 expand_one_smacro(&tp);
4962 if (t == tline)
4963 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004964 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004965
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004966 default:
4967 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004968 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004969 nasm_assert(!tup && param < nparam);
4970 delete_Token(t);
4971 t = NULL;
4972 tup = tnext;
4973 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004974 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004975 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004976 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004977 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004978 }
4979
4980 if (t) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004981 if (t->type != TOK_WHITESPACE)
4982 cond_comma = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004983 tline = t;
4984 if (!ttail)
4985 ttail = t;
4986 }
4987
4988 if (tnext) {
4989 t = tnext;
4990 } else {
4991 t = tup;
4992 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004993 }
4994 }
4995
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004996 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004997 if (ttail)
4998 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004999
5000 m->in_progress = false;
5001
5002 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005003 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005004 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005005
5006 /*
5007 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005008 * and then advance to the next input token. Note that this is
5009 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005010 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005011not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005012 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005013 m = NULL;
5014done:
5015 smacro_deadman.levels++;
5016 if (unlikely(params))
5017 free_tlist_array(params, nparam);
5018 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005019}
5020
5021/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005022 * Expand all single-line macro calls made in the given line.
5023 * Return the expanded version of the line. The original is deemed
5024 * to be destroyed in the process. (In reality we'll just move
5025 * Tokens from input to output a lot of the time, rather than
5026 * actually bothering to destroy and replicate.)
5027 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005028static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005029{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005030 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005031 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5032 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005033 return expand_smacro_noreset(tline);
5034}
5035
5036static Token *expand_smacro_noreset(Token * tline)
5037{
5038 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005039 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005040 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005041
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005042 /*
5043 * Trick: we should avoid changing the start token pointer since it can
5044 * be contained in "next" field of other token. Because of this
5045 * we allocate a copy of first token and work with it; at the end of
5046 * routine we copy it back
5047 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005048 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04005049 tline = new_Token(org_tline->next, org_tline->type,
5050 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005051 nasm_free(org_tline->text);
5052 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005053 }
5054
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005055
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005056 /*
5057 * Pretend that we always end up doing expansion on the first pass;
5058 * that way %+ get processed. However, if we process %+ before the
5059 * first pass, we end up with things like MACRO %+ TAIL trying to
5060 * look up the macro "MACROTAIL", which we don't want.
5061 */
5062 expanded = true;
5063 thead = tline;
5064 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005065 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005066 {
5067 PP_CONCAT_MASK(TOK_ID) |
5068 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5069 PP_CONCAT_MASK(TOK_ID) |
5070 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5071 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5072 }
5073 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005074
5075 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005076 while ((t = *tail)) /* main token loop */
5077 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005078
5079 if (!expanded) {
5080 tline = thead;
5081 break; /* Done! */
5082 }
5083
5084 /*
5085 * Now scan the entire line and look for successive TOK_IDs
5086 * that resulted after expansion (they can't be produced by
5087 * tokenize()). The successive TOK_IDs should be concatenated.
5088 * Also we look for %+ tokens and concatenate the tokens
5089 * before and after them (without white spaces in between).
5090 */
H. Peter Anvind2354082019-08-27 16:38:48 -07005091 if (!paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true)) {
5092 tline = thead;
5093 break;
5094 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005095 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005096 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005097
H. Peter Anvine2c80182005-01-15 22:15:51 +00005098 if (org_tline) {
5099 if (thead) {
5100 *org_tline = *thead;
5101 /* since we just gave text to org_line, don't free it */
5102 thead->text = NULL;
5103 delete_Token(thead);
5104 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005105 /*
5106 * The expression expanded to empty line;
5107 * we can't return NULL because of the "trick" above.
5108 * Just set the line to a single WHITESPACE token.
5109 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005110 memset(org_tline, 0, sizeof(*org_tline));
5111 org_tline->text = NULL;
5112 org_tline->type = TOK_WHITESPACE;
5113 }
5114 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005115 }
5116
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005117 return thead;
5118}
5119
5120/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005121 * Similar to expand_smacro but used exclusively with macro identifiers
5122 * right before they are fetched in. The reason is that there can be
5123 * identifiers consisting of several subparts. We consider that if there
5124 * are more than one element forming the name, user wants a expansion,
5125 * otherwise it will be left as-is. Example:
5126 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005127 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005128 *
5129 * the identifier %$abc will be left as-is so that the handler for %define
5130 * will suck it and define the corresponding value. Other case:
5131 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005132 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005133 *
5134 * In this case user wants name to be expanded *before* %define starts
5135 * working, so we'll expand %$abc into something (if it has a value;
5136 * otherwise it will be left as-is) then concatenate all successive
5137 * PP_IDs into one.
5138 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005139static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005140{
5141 Token *cur, *oldnext = NULL;
5142
H. Peter Anvin734b1882002-04-30 21:01:08 +00005143 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005144 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005145
5146 cur = tline;
5147 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005148 (cur->next->type == TOK_ID ||
5149 cur->next->type == TOK_PREPROC_ID
5150 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005151 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005152
5153 /* If identifier consists of just one token, don't expand */
5154 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005155 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005156
H. Peter Anvine2c80182005-01-15 22:15:51 +00005157 if (cur) {
5158 oldnext = cur->next; /* Detach the tail past identifier */
5159 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005160 }
5161
H. Peter Anvin734b1882002-04-30 21:01:08 +00005162 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005163
H. Peter Anvine2c80182005-01-15 22:15:51 +00005164 if (cur) {
5165 /* expand_smacro possibly changhed tline; re-scan for EOL */
5166 cur = tline;
5167 while (cur && cur->next)
5168 cur = cur->next;
5169 if (cur)
5170 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005171 }
5172
5173 return tline;
5174}
5175
5176/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005177 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005178 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005179 * to check for an initial label - that's taken care of in
5180 * expand_mmacro - but must check numbers of parameters. Guaranteed
5181 * to be called with tline->type == TOK_ID, so the putative macro
5182 * name is easy to find.
5183 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005184static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005185{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005186 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005187 Token **params;
5188 int nparam;
5189
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005190 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005191
5192 /*
5193 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005194 * name which isn't already excluded by macro cycle removal.
5195 * (The cycle removal test here helps optimize the case of wrapping
5196 * instructions, and is cheap to do here.)
5197 *
5198 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005199 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005200 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005201 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005202 list_for_each(m, head) {
5203 if (!mstrcmp(m->name, tline->text, m->casesense) &&
5204 (m->in_progress != 1 || m->max_depth > 0))
5205 break; /* Found something that needs consideration */
5206 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005207 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005208 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005209
5210 /*
5211 * OK, we have a potential macro. Count and demarcate the
5212 * parameters.
5213 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005214 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005215
5216 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005217 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005218 * structure that handles this number.
5219 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005220 while (m) {
5221 if (m->nparam_min <= nparam
5222 && (m->plus || nparam <= m->nparam_max)) {
5223 /*
5224 * This one is right. Just check if cycle removal
5225 * prohibits us using it before we actually celebrate...
5226 */
5227 if (m->in_progress > m->max_depth) {
5228 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005229 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005230 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005231 }
5232 nasm_free(params);
5233 return NULL;
5234 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005235 /*
5236 * It's right, and we can use it. Add its default
5237 * parameters to the end of our list if necessary.
5238 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005239 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005240 int newnparam = m->nparam_min + m->ndefs;
5241 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5242 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5243 (newnparam - nparam) * sizeof(*params));
5244 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005245 }
5246 /*
5247 * If we've gone over the maximum parameter count (and
5248 * we're in Plus mode), ignore parameters beyond
5249 * nparam_max.
5250 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005251 if (m->plus && nparam > m->nparam_max)
5252 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005253
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005254 /*
5255 * If nparam was adjusted above, make sure the list is still
5256 * NULL-terminated.
5257 */
5258 params[nparam+1] = NULL;
5259
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005260 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005261 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005262 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005263 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005264 }
5265 /*
5266 * This one wasn't right: look for the next one with the
5267 * same name.
5268 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005269 list_for_each(m, m->next)
5270 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005271 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005272 }
5273
5274 /*
5275 * After all that, we didn't find one with the right number of
5276 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005277 *!
5278 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5279 *! warns about \i{multi-line macros} being invoked
5280 *! with the wrong number of parameters. See \k{mlmacover} for an
5281 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005282 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005283 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5284 "multi-line macro `%s' exists, but not taking %d parameter%s",
5285 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005286 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005287 return NULL;
5288}
5289
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005290
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005291#if 0
5292
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005293/*
5294 * Save MMacro invocation specific fields in
5295 * preparation for a recursive macro expansion
5296 */
5297static void push_mmacro(MMacro *m)
5298{
5299 MMacroInvocation *i;
5300
5301 i = nasm_malloc(sizeof(MMacroInvocation));
5302 i->prev = m->prev;
5303 i->params = m->params;
5304 i->iline = m->iline;
5305 i->nparam = m->nparam;
5306 i->rotate = m->rotate;
5307 i->paramlen = m->paramlen;
5308 i->unique = m->unique;
5309 i->condcnt = m->condcnt;
5310 m->prev = i;
5311}
5312
5313
5314/*
5315 * Restore MMacro invocation specific fields that were
5316 * saved during a previous recursive macro expansion
5317 */
5318static void pop_mmacro(MMacro *m)
5319{
5320 MMacroInvocation *i;
5321
5322 if (m->prev) {
5323 i = m->prev;
5324 m->prev = i->prev;
5325 m->params = i->params;
5326 m->iline = i->iline;
5327 m->nparam = i->nparam;
5328 m->rotate = i->rotate;
5329 m->paramlen = i->paramlen;
5330 m->unique = i->unique;
5331 m->condcnt = i->condcnt;
5332 nasm_free(i);
5333 }
5334}
5335
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005336#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005337
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005338/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005339 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005340 */
5341static void list_mmacro_call(const MMacro *m)
5342{
5343 const char prefix[] = " ;;; [macro] ";
5344 size_t namelen, size;
5345 char *buf, *p;
5346 unsigned int i;
5347 const Token *t;
5348
5349 namelen = strlen(m->iname);
5350 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5351
5352 for (i = 1; i <= m->nparam; i++) {
5353 int j = 0;
5354 size += 3; /* Braces and space/comma */
5355 list_for_each(t, m->params[i]) {
5356 if (j++ >= m->paramlen[i])
5357 break;
5358 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5359 }
5360 }
5361
5362 buf = p = nasm_malloc(size);
5363 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5364 p = mempcpy(p, m->iname, namelen);
5365 *p++ = ' ';
5366
5367 for (i = 1; i <= m->nparam; i++) {
5368 int j = 0;
5369 *p++ = '{';
5370 list_for_each(t, m->params[i]) {
5371 if (j++ >= m->paramlen[i])
5372 break;
5373 if (!t->text) {
5374 if (t->type == TOK_WHITESPACE)
5375 *p++ = ' ';
5376 } else {
5377 p = mempcpy(p, t->text, t->len);
5378 }
5379 }
5380 *p++ = '}';
5381 *p++ = ',';
5382 }
5383
5384 *--p = '\0'; /* Replace last delimeter with null */
5385 lfmt->line(LIST_MACRO, -1, buf);
5386 nasm_free(buf);
5387}
5388
5389/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005390 * Expand the multi-line macro call made by the given line, if
5391 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005392 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005393 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005394static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005395{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005396 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005397 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005398 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005399 Token **params, *t, *tt;
5400 MMacro *m;
5401 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005402 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005403 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005404 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005405
5406 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005407 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005408 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005409 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005410 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005411 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005412 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005413 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005414 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005415 Token *last;
5416 /*
5417 * We have an id which isn't a macro call. We'll assume
5418 * it might be a label; we'll also check to see if a
5419 * colon follows it. Then, if there's another id after
5420 * that lot, we'll check it again for macro-hood.
5421 */
5422 label = last = t;
5423 t = t->next;
5424 if (tok_type_(t, TOK_WHITESPACE))
5425 last = t, t = t->next;
5426 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005427 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005428 last = t, t = t->next;
5429 if (tok_type_(t, TOK_WHITESPACE))
5430 last = t, t = t->next;
5431 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005432 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005433 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005434 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005435 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005436 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005437 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005438
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005439 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5440 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5441 if (!mmacro_deadman.triggered) {
5442 nasm_nonfatal("interminable multiline macro recursion");
5443 mmacro_deadman.triggered = true;
5444 }
5445 return 0;
5446 }
5447
5448 mmacro_deadman.total++;
5449 mmacro_deadman.levels++;
5450
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005451 /*
5452 * Fix up the parameters: this involves stripping leading and
5453 * trailing whitespace, then stripping braces if they are
5454 * present.
5455 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005456 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005457
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005458 nasm_assert(params[nparam+1] == NULL);
5459
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005460 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005461 int brace = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005462 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005463
H. Peter Anvine2c80182005-01-15 22:15:51 +00005464 skip_white_(t);
5465 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005466 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005467 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005468 while (t) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005469 if (comma) {
5470 /* Check if we hit a comma that ends this argument */
5471 if (tok_is_(t, ","))
5472 break;
5473 else if (t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
5474 break;
5475 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005476 if (brace && t->type == TOK_OTHER) {
5477 if (t->text[0] == '{')
5478 brace++; /* ... or a nested opening brace */
5479 else if (t->text[0] == '}')
5480 if (!--brace)
5481 break; /* ... or a brace */
5482 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005483 t = t->next;
5484 paramlen[i]++;
5485 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005486 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005487 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005488 }
5489
5490 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005491 * OK, we have a MMacro structure together with a set of
5492 * parameters. We must now go through the expansion and push
5493 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005494 * parameter tokens and macro-local tokens doesn't get done
5495 * until the single-line macro substitution process; this is
5496 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005497 * later through %rotate and give the right semantics for
5498 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005499 *
5500 * First, push an end marker on to istk->expansion, mark this
5501 * macro as in progress, and set up its invocation-specific
5502 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005503 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005504 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005505 ll->next = istk->expansion;
5506 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005507 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005508
5509 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005510 * Save the previous MMacro expansion in the case of
5511 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005512 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005513#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005514 if (m->max_depth && m->in_progress)
5515 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005516#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005517
5518 m->in_progress ++;
5519 m->params = params;
5520 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005521 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005522 m->nparam = nparam;
5523 m->rotate = 0;
5524 m->paramlen = paramlen;
5525 m->unique = unique++;
5526 m->lineno = 0;
5527 m->condcnt = 0;
5528
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005529 m->mstk = istk->mstk;
5530 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005531
5532 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005533 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005534 ll->next = istk->expansion;
5535 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005536 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005537 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005538
5539 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005540 * If we had a label, and this macro definition does not include
5541 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005542 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005543 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005544 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005545 /*
5546 * We had a label. If this macro contains an %00 parameter,
5547 * save the value as a special parameter (which is what it
5548 * is), otherwise push it as the first line of the macro
5549 * expansion.
5550 */
5551 if (m->capture_label) {
5552 params[0] = dup_Token(NULL, label);
5553 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005554 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005555 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005556 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005557 ll->finishes = NULL;
5558 ll->next = istk->expansion;
5559 istk->expansion = ll;
5560 ll->first = startline;
5561 if (!dont_prepend) {
5562 while (label->next)
5563 label = label->next;
5564 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005565 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005566 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005567 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005568
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005569 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005570
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005571 if (list_option('m') && !m->nolist)
5572 list_mmacro_call(m);
5573
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005574 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005575}
5576
H. Peter Anvin130736c2016-02-17 20:27:41 -08005577/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005578 * This function decides if an error message should be suppressed.
5579 * It will never be called with a severity level of ERR_FATAL or
5580 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005581 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005582static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005583{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005584 /*
5585 * If we're in a dead branch of IF or something like it, ignore the error.
5586 * However, because %else etc are evaluated in the state context
5587 * of the previous branch, errors might get lost:
5588 * %if 0 ... %else trailing garbage ... %endif
5589 * So %else etc should set the ERR_PP_PRECOND flag.
5590 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005591 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005592 ((severity & ERR_PP_PRECOND) ?
5593 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005594 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005595 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005596
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005597 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005598}
5599
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005600static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005601stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005602{
5603 (void)s;
5604 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005605 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005606
5607 return make_tok_qstr(src_get_fname());
5608}
5609
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005610static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005611stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005612{
5613 (void)s;
5614 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005615 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005616
5617 return make_tok_num(src_get_linnum());
5618}
5619
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005620static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005621stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005622{
5623 (void)s;
5624 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005625 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005626
5627 return make_tok_num(globalbits);
5628}
5629
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005630static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005631stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005632{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005633 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005634 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5635 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5636 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005637
5638 (void)s;
5639 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005640 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005641
5642 switch (globalbits) {
5643 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005644 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005645 break;
5646 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005647 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005648 break;
5649 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005650 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005651 break;
5652 default:
5653 panic();
5654 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005655
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005656 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005657}
5658
H. Peter Anvin8b262472019-02-26 14:00:54 -08005659/* Add magic standard macros */
5660struct magic_macros {
5661 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005662 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005663 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005664};
5665static const struct magic_macros magic_macros[] =
5666{
H. Peter Anvind2354082019-08-27 16:38:48 -07005667 { "__?FILE?__", 0, stdmac_file },
5668 { "__?LINE?__", 0, stdmac_line },
5669 { "__?BITS?__", 0, stdmac_bits },
5670 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005671 { NULL, 0, NULL }
5672};
5673
5674static void pp_add_magic_stdmac(void)
5675{
5676 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005677 SMacro tmpl;
5678
5679 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005680
5681 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005682 tmpl.nparam = m->nparam;
5683 tmpl.expand = m->func;
5684 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005685 }
5686}
5687
H. Peter Anvin734b1882002-04-30 21:01:08 +00005688static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005689pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005690{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005691 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005692 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005693
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005694 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005695 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005696 nested_mac_count = 0;
5697 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005698 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005699 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005700 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005701 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07005702 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005703
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005704 if (!use_loaded)
5705 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5706 memset(use_loaded, 0, use_package_count * sizeof(bool));
5707
H. Peter Anvin6686de22019-08-10 05:33:14 -07005708 /* First set up the top level input file */
5709 nasm_new(istk);
5710 istk->fp = nasm_open_read(file, NF_TEXT);
5711 src_set(0, file);
5712 istk->lineinc = 1;
5713 if (!istk->fp)
5714 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5715
5716 strlist_add(deplist, file);
5717
5718 /*
5719 * Set up the stdmac packages as a virtual include file,
5720 * indicated by a null file pointer.
5721 */
5722 nasm_new(inc);
5723 inc->next = istk;
5724 inc->fname = src_set_fname(NULL);
5725 inc->nolist = !list_option('b');
5726 istk = inc;
5727 lfmt->uplevel(LIST_INCLUDE, 0);
5728
H. Peter Anvin8b262472019-02-26 14:00:54 -08005729 pp_add_magic_stdmac();
5730
H. Peter Anvinf7606612016-07-13 14:23:48 -07005731 if (tasm_compatible_mode)
5732 pp_add_stdmac(nasm_stdmac_tasm);
5733
5734 pp_add_stdmac(nasm_stdmac_nasm);
5735 pp_add_stdmac(nasm_stdmac_version);
5736
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005737 if (extrastdmac)
5738 pp_add_stdmac(extrastdmac);
5739
H. Peter Anvinf7606612016-07-13 14:23:48 -07005740 stdmacpos = stdmacros[0];
5741 stdmacnext = &stdmacros[1];
5742
H. Peter Anvind2456592008-06-19 15:04:18 -07005743 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005744
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005745 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07005746 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005747 * other builtins, because it is special -- it varies between
5748 * passes -- but there is really no particular reason to make it
5749 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005750 *
5751 * 0 = dependencies only
5752 * 1 = preparatory passes
5753 * 2 = final pass
5754 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005755 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005756 switch (mode) {
5757 case PP_NORMAL:
5758 apass = pass_final() ? 2 : 1;
5759 break;
5760 case PP_DEPS:
5761 apass = 0;
5762 break;
5763 case PP_PREPROC:
5764 apass = 3;
5765 break;
5766 default:
5767 panic();
5768 }
5769
H. Peter Anvind2354082019-08-27 16:38:48 -07005770 define_smacro("__?PASS?__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005771}
5772
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005773static void pp_init(void)
5774{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005775}
5776
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005777/*
5778 * Get a line of tokens. If we popped the macro expansion/include stack,
5779 * we return a pointer to the dummy token tok_pop; at that point if
5780 * istk is NULL then we have reached end of input;
5781 */
5782static Token tok_pop; /* Dummy token placeholder */
5783
5784static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005785{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005786 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005787 Line *l = istk->expansion;
5788 Token *tline = NULL;
5789 Token *dtline;
5790
H. Peter Anvine2c80182005-01-15 22:15:51 +00005791 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005792 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005793 * buffer or from the input file.
5794 */
5795 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005796 while (l && l->finishes) {
5797 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005798
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005799 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005800 /*
5801 * This is a macro-end marker for a macro with no
5802 * name, which means it's not really a macro at all
5803 * but a %rep block, and the `in_progress' field is
5804 * more than 1, meaning that we still need to
5805 * repeat. (1 means the natural last repetition; 0
5806 * means termination by %exitrep.) We have
5807 * therefore expanded up to the %endrep, and must
5808 * push the whole block on to the expansion buffer
5809 * again. We don't bother to remove the macro-end
5810 * marker: we'd only have to generate another one
5811 * if we did.
5812 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005813 fm->in_progress--;
5814 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005815 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005816 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005817
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005818 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005819 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005820 tail = &ll->first;
5821
5822 list_for_each(t, l->first) {
5823 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005824 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005825 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005826 }
5827 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005828 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005829 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005830 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005831 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005832 MMacro *m = istk->mstk.mstk;
5833
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005834 /*
5835 * Check whether a `%rep' was started and not ended
5836 * within this macro expansion. This can happen and
5837 * should be detected. It's a fatal error because
5838 * I'm too confused to work out how to recover
5839 * sensibly from it.
5840 */
5841 if (defining) {
5842 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005843 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005844 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005845 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005846 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005847 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005848
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005849 /*
5850 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005851 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005852 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005853 istk->mstk = m->mstk;
5854 if (m->name) {
5855 /*
5856 * This was a real macro call, not a %rep, and
5857 * therefore the parameter information needs to
5858 * be freed and the iteration count/nesting
5859 * depth adjusted.
5860 */
5861
5862 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005863 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005864 * If all mmacro processing done,
5865 * clear all counters and the deadman
5866 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005867 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005868 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005869 }
5870
Adam Majer91e72402017-07-25 10:42:01 +02005871#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005872 if (m->prev) {
5873 pop_mmacro(m);
5874 fm->in_progress --;
5875 } else
Adam Majer91e72402017-07-25 10:42:01 +02005876#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005877 {
5878 nasm_free(m->params);
5879 free_tlist(m->iline);
5880 nasm_free(m->paramlen);
5881 fm->in_progress = 0;
5882 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005883 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005884
5885 /*
5886 * FIXME It is incorrect to always free_mmacro here.
5887 * It leads to usage-after-free.
5888 *
5889 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5890 */
5891#if 0
5892 else
5893 free_mmacro(m);
5894#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005895 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005896 istk->expansion = l->next;
5897 nasm_free(l);
5898 lfmt->downlevel(LIST_MACRO);
5899 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005900 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005901
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005902 do { /* until we get a line we can use */
5903 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005904
5905 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005906 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005907 int32_t lineno;
5908
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005909 if (istk->mstk.mstk) {
5910 istk->mstk.mstk->lineno++;
5911 if (istk->mstk.mstk->fname)
5912 lineno = istk->mstk.mstk->lineno +
5913 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005914 else
5915 lineno = 0; /* Defined at init time or builtin */
5916 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005917 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005918 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005919
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005920 tline = l->first;
5921 istk->expansion = l->next;
5922 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005923
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005924 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005925 if (!istk->nolist)
5926 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005927 nasm_free(line);
5928 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005929 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005930 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005931 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005932 } else {
5933 /*
5934 * The current file has ended; work down the istk
5935 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005936 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005937 if (i->fp)
5938 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005939 if (i->conds) {
5940 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005941 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005942 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005943 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005944 if (i->next)
5945 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005946 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005947 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005948 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005949 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005950 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005951 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005952
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005953 /*
5954 * We must expand MMacro parameters and MMacro-local labels
5955 * _before_ we plunge into directive processing, to cope
5956 * with things like `%define something %1' such as STRUC
5957 * uses. Unless we're _defining_ a MMacro, in which case
5958 * those tokens should be left alone to go into the
5959 * definition; and unless we're in a non-emitting
5960 * condition, in which case we don't want to meddle with
5961 * anything.
5962 */
5963 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005964 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005965 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005966 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005967
H. Peter Anvine2c80182005-01-15 22:15:51 +00005968 /*
5969 * Check the line to see if it's a preprocessor directive.
5970 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005971 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5972 if (dtline)
5973 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005974 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005975 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005976 * We're defining a multi-line macro. We emit nothing
5977 * at all, and just
5978 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005979 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005980 MMacro *mmac = defining->dstk.mmac;
5981
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005982 Line *l = nasm_malloc(sizeof(Line));
5983 l->next = defining->expansion;
5984 l->first = tline;
5985 l->finishes = NULL;
5986 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005987
5988 /*
5989 * Remember if this mmacro expansion contains %00:
5990 * if it does, we will have to handle leading labels
5991 * specially.
5992 */
5993 if (mmac) {
5994 const Token *t;
5995 list_for_each(t, tline) {
5996 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
5997 mmac->capture_label = true;
5998 }
5999 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006000 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006001 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006002 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006003 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006004 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006005 * directive so we keep our place correctly.
6006 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006007 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006008 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006009 /*
6010 * We're in a %rep block which has been terminated, so
6011 * we're walking through to the %endrep without
6012 * emitting anything. Emit nothing at all, not even a
6013 * blank line: when we emerge from the %rep block we'll
6014 * give a line-number directive so we keep our place
6015 * correctly.
6016 */
6017 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006018 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006019 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006020 if (!expand_mmacro(tline))
6021 return tline;
6022 }
6023 }
6024}
6025
6026static char *pp_getline(void)
6027{
6028 char *line = NULL;
6029 Token *tline;
6030
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006031 while (true) {
6032 tline = pp_tokline();
6033 if (tline == &tok_pop) {
6034 /*
6035 * We popped the macro/include stack. If istk is empty,
6036 * we are at end of input, otherwise just loop back.
6037 */
6038 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006039 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006040 } else {
6041 /*
6042 * De-tokenize the line and emit it.
6043 */
6044 line = detoken(tline, true);
6045 free_tlist(tline);
6046 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006047 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006048 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006049
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006050 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006051 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006052 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006053 nasm_free(buf);
6054 }
6055
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006056 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006057}
6058
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006059static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006060{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006061 if (defining) {
6062 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006063 nasm_nonfatal("end of file while still defining macro `%s'",
6064 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006065 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006066 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006067 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006068
6069 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006070 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006071 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006072
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006073 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006074 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006075 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006076 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006077 Include *i = istk;
6078 istk = istk->next;
6079 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006080 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006081 }
6082 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006083 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006084 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006085}
6086
6087static void pp_cleanup_session(void)
6088{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006089 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006090 free_llist(predef);
6091 predef = NULL;
6092 delete_Blocks();
6093 freeTokens = NULL;
6094 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006095}
6096
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006097static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006098{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006099 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006100}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006101
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006102static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006103{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006104 Token *inc, *space, *name;
6105 Line *l;
6106
H. Peter Anvin734b1882002-04-30 21:01:08 +00006107 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
6108 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
6109 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006110
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006111 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006112 l->next = predef;
6113 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006114 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006115 predef = l;
6116}
6117
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006118static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006119{
6120 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006121 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006122 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006123
6124 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00006125 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6126 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006127 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006128 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006129 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006130 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006131 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006132
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006133 if (space->next->type != TOK_PREPROC_ID &&
6134 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006135 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006136
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006137 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006138 l->next = predef;
6139 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006140 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006141 predef = l;
6142}
6143
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006144static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006145{
6146 Token *def, *space;
6147 Line *l;
6148
H. Peter Anvin734b1882002-04-30 21:01:08 +00006149 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6150 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006151 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006152
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006153 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006154 l->next = predef;
6155 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006156 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006157 predef = l;
6158}
6159
H. Peter Anvin05990342018-06-11 13:32:42 -07006160/* Insert an early preprocessor command that doesn't need special handling */
6161static void pp_pre_command(const char *what, char *string)
6162{
6163 char *cmd;
6164 Token *def, *space;
6165 Line *l;
6166
6167 def = tokenize(string);
6168 if (what) {
6169 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6170 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
6171 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6172 }
6173
6174 l = nasm_malloc(sizeof(Line));
6175 l->next = predef;
6176 l->first = def;
6177 l->finishes = NULL;
6178 predef = l;
6179}
6180
H. Peter Anvinf7606612016-07-13 14:23:48 -07006181static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006182{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006183 macros_t **mp;
6184
6185 /* Find the end of the list and avoid duplicates */
6186 for (mp = stdmacros; *mp; mp++) {
6187 if (*mp == macros)
6188 return; /* Nothing to do */
6189 }
6190
6191 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6192
6193 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006194}
6195
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006196static void pp_extra_stdmac(macros_t *macros)
6197{
6198 extrastdmac = macros;
6199}
6200
H. Peter Anvin8b262472019-02-26 14:00:54 -08006201static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006202{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006203 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006204 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6205 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6206}
6207
6208static Token *make_tok_qstr(const char *str)
6209{
6210 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006211 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006212 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006213}
6214
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006215static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006216{
6217 if (!m)
6218 return;
6219
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006220 /* We need to print the mstk.mmac list in reverse order */
6221 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006222
6223 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006224 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006225 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006226 }
6227}
6228
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006229static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006230{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006231 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006232
H. Peter Anvinddb29062018-12-11 00:06:29 -08006233 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6234 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006235
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006236 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006237 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006238
H. Peter Anvinddb29062018-12-11 00:06:29 -08006239 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006240}
6241
H. Peter Anvine7469712016-02-18 02:20:59 -08006242const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006243 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006244 pp_reset,
6245 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006246 pp_cleanup_pass,
6247 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006248 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006249 pp_pre_define,
6250 pp_pre_undefine,
6251 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006252 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006253 pp_include_path,
6254 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006255 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006256};