blob: 42cb969ef51391c8a959646ecba08046b0bdaab4 [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 Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002742static char *get_id(Token **tp, const char *dname, const char *err)
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 Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002752 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002753 err ? err : "macro identifier");
2754 return NULL;
2755 }
2756
2757 id = t->text;
2758 skip_white_(t);
2759 *tp = t;
2760 return id;
2761}
2762
Ed Beroset3ab3f412002-06-11 03:31:49 +00002763/**
2764 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002765 * Find out if a line contains a preprocessor directive, and deal
2766 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002767 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002768 * If a directive _is_ found, it is the responsibility of this routine
2769 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002770 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002771 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002772 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002773 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002774 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002775 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002776static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002777{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002778 enum preproc_token i;
2779 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002780 bool err;
2781 int nparam;
2782 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002783 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002784 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002785 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002786 char *p, *pp;
2787 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002788 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002789 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002790 Include *inc;
2791 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002792 Cond *cond;
2793 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002794 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002795 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002796 struct tokenval tokval;
2797 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002798 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002799 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002800 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002801 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002802
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002803 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002804 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002805
H. Peter Anvineba20a72002-04-30 20:53:55 +00002806 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002807 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002808 (tline->text[0] && (tline->text[1] == '%' ||
2809 tline->text[1] == '$' ||
2810 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002812
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002813 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002814 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002815
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002816 casesense = true;
2817 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2818 casesense = false;
2819 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002820 }
2821
2822 /*
2823 * If we're in a non-emitting branch of a condition construct,
2824 * or walking to the end of an already terminated %rep block,
2825 * we should ignore all directives except for condition
2826 * directives.
2827 */
2828 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002829 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2830 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002831 return NO_DIRECTIVE_FOUND;
2832 }
2833
2834 /*
2835 * If we're defining a macro or reading a %rep block, we should
2836 * ignore all directives except for %macro/%imacro (which nest),
2837 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2838 * If we're in a %rep block, another %rep nests, so should be let through.
2839 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002840 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002841 i != PP_ENDMACRO && i != PP_ENDM &&
2842 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2843 return NO_DIRECTIVE_FOUND;
2844 }
2845
2846 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002847 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002848 nested_mac_count++;
2849 return NO_DIRECTIVE_FOUND;
2850 } else if (nested_mac_count > 0) {
2851 if (i == PP_ENDMACRO) {
2852 nested_mac_count--;
2853 return NO_DIRECTIVE_FOUND;
2854 }
2855 }
2856 if (!defining->name) {
2857 if (i == PP_REP) {
2858 nested_rep_count++;
2859 return NO_DIRECTIVE_FOUND;
2860 } else if (nested_rep_count > 0) {
2861 if (i == PP_ENDREP) {
2862 nested_rep_count--;
2863 return NO_DIRECTIVE_FOUND;
2864 }
2865 }
2866 }
2867 }
2868
H. Peter Anvin4169a472007-09-12 01:29:43 +00002869 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002870 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002871 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002872 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002873
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002874 case PP_PRAGMA:
2875 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002876 * %pragma namespace options...
2877 *
2878 * The namespace "preproc" is reserved for the preprocessor;
2879 * all other namespaces generate a [pragma] assembly directive.
2880 *
2881 * Invalid %pragmas are ignored and may have different
2882 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002883 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002884 t = tline;
2885 tline = tline->next;
2886 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002887 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002888 while (tok_type_(tline, TOK_WHITESPACE)) {
2889 t = tline;
2890 tline = tline->next;
2891 delete_Token(t);
2892 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002893 if (tok_type_(tline, TOK_ID)) {
2894 if (!nasm_stricmp(tline->text, "preproc")) {
2895 /* Preprocessor pragma */
2896 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002897 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002898 } else {
2899 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002900
2901 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002902 for (t = tline; t->next; t = t->next)
2903 ;
2904 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002905
2906 /* Prepend "[pragma " */
2907 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2908 t = new_Token(t, TOK_ID, "pragma", 6);
2909 t = new_Token(t, TOK_OTHER, "[", 1);
2910 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002911 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002912 }
2913 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002914 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002915
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 case PP_STACKSIZE:
2917 /* Directive to tell NASM what the default stack size is. The
2918 * default is for a 16-bit stack, and this can be overriden with
2919 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 */
2921 tline = tline->next;
2922 if (tline && tline->type == TOK_WHITESPACE)
2923 tline = tline->next;
2924 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002925 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002926 }
2927 if (nasm_stricmp(tline->text, "flat") == 0) {
2928 /* All subsequent ARG directives are for a 32-bit stack */
2929 StackSize = 4;
2930 StackPointer = "ebp";
2931 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002932 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002933 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2934 /* All subsequent ARG directives are for a 64-bit stack */
2935 StackSize = 8;
2936 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002937 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002938 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 } else if (nasm_stricmp(tline->text, "large") == 0) {
2940 /* All subsequent ARG directives are for a 16-bit stack,
2941 * far function call.
2942 */
2943 StackSize = 2;
2944 StackPointer = "bp";
2945 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002946 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 } else if (nasm_stricmp(tline->text, "small") == 0) {
2948 /* All subsequent ARG directives are for a 16-bit stack,
2949 * far function call. We don't support near functions.
2950 */
2951 StackSize = 2;
2952 StackPointer = "bp";
2953 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002954 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002956 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002957 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002958 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002959
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 case PP_ARG:
2961 /* TASM like ARG directive to define arguments to functions, in
2962 * the following form:
2963 *
2964 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2965 */
2966 offset = ArgOffset;
2967 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002968 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002969 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002970
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 /* Find the argument name */
2972 tline = tline->next;
2973 if (tline && tline->type == TOK_WHITESPACE)
2974 tline = tline->next;
2975 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002976 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002977 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 }
2979 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002980
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 /* Find the argument size type */
2982 tline = tline->next;
2983 if (!tline || tline->type != TOK_OTHER
2984 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002985 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002986 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 }
2988 tline = tline->next;
2989 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002990 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002991 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002993
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002995 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002997 size = parse_size(tt->text);
2998 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002999 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003001 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 }
3003 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003004
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003005 /* Round up to even stack slots */
3006 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003007
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 /* Now define the macro for the argument */
3009 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3010 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003011 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003013
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 /* Move to the next argument in the list */
3015 tline = tline->next;
3016 if (tline && tline->type == TOK_WHITESPACE)
3017 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003018 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003019 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003020 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003021
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 case PP_LOCAL:
3023 /* TASM like LOCAL directive to define local variables for a
3024 * function, in the following form:
3025 *
3026 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3027 *
3028 * The '= LocalSize' at the end is ignored by NASM, but is
3029 * required by TASM to define the local parameter size (and used
3030 * by the TASM macro package).
3031 */
3032 offset = LocalOffset;
3033 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003034 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003036
H. Peter Anvine2c80182005-01-15 22:15:51 +00003037 /* Find the argument name */
3038 tline = tline->next;
3039 if (tline && tline->type == TOK_WHITESPACE)
3040 tline = tline->next;
3041 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003042 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003043 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 }
3045 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003046
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 /* Find the argument size type */
3048 tline = tline->next;
3049 if (!tline || tline->type != TOK_OTHER
3050 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003051 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003052 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 }
3054 tline = tline->next;
3055 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003056 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003057 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003059
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003061 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003063 size = parse_size(tt->text);
3064 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003065 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003066 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003067 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 }
3069 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003070
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003071 /* Round up to even stack slots */
3072 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003073
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003074 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003075
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003076 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003077 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3078 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003079 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003080
H. Peter Anvine2c80182005-01-15 22:15:51 +00003081 /* Now define the assign to setup the enter_c macro correctly */
3082 snprintf(directive, sizeof(directive),
3083 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003084 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003085
H. Peter Anvine2c80182005-01-15 22:15:51 +00003086 /* Move to the next argument in the list */
3087 tline = tline->next;
3088 if (tline && tline->type == TOK_WHITESPACE)
3089 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003090 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003091 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003092 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003093
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 case PP_CLEAR:
3095 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003096 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003097 free_macros();
3098 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003099 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003100
H. Peter Anvin418ca702008-05-30 10:42:30 -07003101 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003102 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003103 skip_white_(t);
3104 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003105 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003106 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003107 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003108 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003109 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003110 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003111 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003112 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003113 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003114 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003115 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003116
3117 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003118 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003119 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003120
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003121 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003122 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003123 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003124 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003126 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003127 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003128 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003129 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003130 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003131 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003133 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003134 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003135 (pp_mode == PP_DEPS)
3136 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003137 if (!inc->fp) {
3138 /* -MG given but file not found */
3139 nasm_free(inc);
3140 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003141 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003142 inc->lineno = src_set_linnum(0);
3143 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003144 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003146 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003147 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003148 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003149
H. Peter Anvind2456592008-06-19 15:04:18 -07003150 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003151 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003152 const struct use_package *pkg;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003153
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003154 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003155 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07003156 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003157 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003158 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003159 nasm_unquote_cstr(tline->text, NULL);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003160 pkg = nasm_find_use_package(tline->text);
3161 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003162 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003163 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003164 /*
3165 * Not already included, go ahead and include it.
3166 * Treat it as an include file for the purpose of
3167 * producing a listing.
3168 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003169 use_loaded[pkg->index] = true;
3170 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003171 nasm_new(inc);
3172 inc->next = istk;
3173 inc->fname = src_set_fname(NULL);
3174 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003175 inc->nolist = !list_option('b') || istk->nolist;
3176 istk = inc;
3177 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003179 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003180 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003182 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003183 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 tline = tline->next;
3185 skip_white_(tline);
3186 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003187 if (tline) {
3188 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003189 nasm_nonfatal("`%s' expects a context identifier",
3190 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003191 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 }
3193 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003194 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003195 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003196 p = nasm_strdup(tline->text);
3197 } else {
3198 p = NULL; /* Anonymous */
3199 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003200
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003201 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003202 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003203 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003204 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003205 ctx->name = p;
3206 ctx->number = unique++;
3207 cstk = ctx;
3208 } else {
3209 /* %pop or %repl */
3210 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003211 nasm_nonfatal("`%s': context stack is empty",
3212 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003213 } else if (i == PP_POP) {
3214 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003215 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003216 "expected %s",
3217 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003218 else
3219 ctx_pop();
3220 } else {
3221 /* i == PP_REPL */
3222 nasm_free(cstk->name);
3223 cstk->name = p;
3224 p = NULL;
3225 }
3226 nasm_free(p);
3227 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003228 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003229 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003230 severity = ERR_FATAL;
3231 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003232 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003233 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003234 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003235 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003236 /*!
3237 *!user [on] %warning directives
3238 *! controls output of \c{%warning} directives (see \k{pperror}).
3239 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003240 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003241 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003242
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003243issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003244 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003245 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003246 tline->next = expand_smacro(tline->next);
3247 tline = tline->next;
3248 skip_white_(tline);
3249 t = tline ? tline->next : NULL;
3250 skip_white_(t);
3251 if (tok_type_(tline, TOK_STRING) && !t) {
3252 /* The line contains only a quoted string */
3253 p = tline->text;
3254 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003255 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003256 } else {
3257 /* Not a quoted string, or more than a quoted string */
3258 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003259 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003260 nasm_free(p);
3261 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003262 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003263 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003264
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003265 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003266 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003267 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003268 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003269 j = if_condition(tline->next, i);
3270 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003271 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003272 cond = nasm_malloc(sizeof(Cond));
3273 cond->next = istk->conds;
3274 cond->state = j;
3275 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003276 if(istk->mstk.mstk)
3277 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003278 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003279
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003280 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003281 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003282 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003283 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003284 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003285 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003286 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003287
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003288 case COND_DONE:
3289 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003290 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003291
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003292 case COND_ELSE_TRUE:
3293 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003294 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003295 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003296 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003297 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003298
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003299 case COND_IF_FALSE:
3300 /*
3301 * IMPORTANT: In the case of %if, we will already have
3302 * called expand_mmac_params(); however, if we're
3303 * processing an %elif we must have been in a
3304 * non-emitting mode, which would have inhibited
3305 * the normal invocation of expand_mmac_params().
3306 * Therefore, we have to do it explicitly here.
3307 */
3308 j = if_condition(expand_mmac_params(tline->next), i);
3309 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003310 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003311 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003313 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003314
H. Peter Anvine2c80182005-01-15 22:15:51 +00003315 case PP_ELSE:
3316 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003317 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003318 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003319 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003320 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003321 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003322 case COND_IF_TRUE:
3323 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003324 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003325 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003326
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003327 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003328 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003329
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003330 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003331 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003332 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003333
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003334 case COND_ELSE_TRUE:
3335 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003336 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003337 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003338 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003339 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003340 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003341 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003342
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 case PP_ENDIF:
3344 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003345 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003346 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003347 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003348 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003349 cond = istk->conds;
3350 istk->conds = cond->next;
3351 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003352 if(istk->mstk.mstk)
3353 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003354 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003355
H. Peter Anvin8b262472019-02-26 14:00:54 -08003356 case PP_RMACRO:
3357 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003358 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003359 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003360 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003361 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003362 if (i == PP_RMACRO)
3363 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003364 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003365 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003366 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003367 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003368
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003369 src_get(&defining->xline, &defining->fname);
3370
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003371 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3372 while (mmac) {
3373 if (!strcmp(mmac->name, defining->name) &&
3374 (mmac->nparam_min <= defining->nparam_max
3375 || defining->plus)
3376 && (defining->nparam_min <= mmac->nparam_max
3377 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003378 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003379 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003380 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003381 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003382 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003383 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003384 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003385
H. Peter Anvine2c80182005-01-15 22:15:51 +00003386 case PP_ENDM:
3387 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003388 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003389 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003390 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003391 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003392 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3393 defining->next = *mmhead;
3394 *mmhead = defining;
3395 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003396 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003397
H. Peter Anvin89cee572009-07-15 09:16:54 -04003398 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003399 /*
3400 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003401 * macro-end marker for a macro with a name. Then we
3402 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003403 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003404 list_for_each(l, istk->expansion)
3405 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003406 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003407
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003408 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003409 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003410 * Remove all conditional entries relative to this
3411 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003412 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003413 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3414 cond = istk->conds;
3415 istk->conds = cond->next;
3416 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003417 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003418 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003419 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003420 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003421 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003422 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003423
H. Peter Anvina26433d2008-07-16 14:40:01 -07003424 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003425 casesense = false;
3426 /* fall through */
3427 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003428 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003429 MMacro **mmac_p;
3430 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003431
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003432 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003433 spec.casesense = casesense;
3434 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003435 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003436 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003437 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3438 while (mmac_p && *mmac_p) {
3439 mmac = *mmac_p;
3440 if (mmac->casesense == spec.casesense &&
3441 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3442 mmac->nparam_min == spec.nparam_min &&
3443 mmac->nparam_max == spec.nparam_max &&
3444 mmac->plus == spec.plus) {
3445 *mmac_p = mmac->next;
3446 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003447 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003448 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003449 }
3450 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003451 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003452 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003453 }
3454
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 case PP_ROTATE:
3456 if (tline->next && tline->next->type == TOK_WHITESPACE)
3457 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003458 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003460 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 return DIRECTIVE_FOUND;
3462 }
3463 t = expand_smacro(tline->next);
3464 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003465 pps.tptr = tline = t;
3466 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 tokval.t_type = TOKEN_INVALID;
3468 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003469 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 free_tlist(tline);
3471 if (!evalresult)
3472 return DIRECTIVE_FOUND;
3473 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003474 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003475 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003476 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 return DIRECTIVE_FOUND;
3478 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003479 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003480 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003481 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003482 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003483 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003484 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003485 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003486
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003487 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003488 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003489 rotate += mmac->nparam;
3490
3491 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003492 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003493 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003496 {
3497 MMacro *tmp_defining;
3498
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003499 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 do {
3501 tline = tline->next;
3502 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003503
H. Peter Anvine2c80182005-01-15 22:15:51 +00003504 if (tok_type_(tline, TOK_ID) &&
3505 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003506 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003507 do {
3508 tline = tline->next;
3509 } while (tok_type_(tline, TOK_WHITESPACE));
3510 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003511
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003513 pps.tptr = expand_smacro(tline);
3514 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003516 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003517 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003518 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003519 if (!evalresult)
3520 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003522 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003523 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003524 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003525 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003526 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003527 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003528 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003529 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3530 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003531 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003532 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003533 /*!
3534 *!negative-rep [on] regative %rep count
3535 *! warns about negative counts given to the \c{%rep}
3536 *! preprocessor directive.
3537 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003538 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003539 "negative `%%rep' count: %"PRId64, count);
3540 count = 0;
3541 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003542 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003543 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003544 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003545 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003546 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003548 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003549 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003550 defining->nolist = nolist;
3551 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003552 defining->mstk = istk->mstk;
3553 defining->dstk.mstk = tmp_defining;
3554 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003555 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003556 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003557 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003558
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003560 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003561 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003562 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003564
H. Peter Anvine2c80182005-01-15 22:15:51 +00003565 /*
3566 * Now we have a "macro" defined - although it has no name
3567 * and we won't be entering it in the hash tables - we must
3568 * push a macro-end marker for it on to istk->expansion.
3569 * After that, it will take care of propagating itself (a
3570 * macro-end marker line for a macro which is really a %rep
3571 * block will cause the macro to be re-expanded, complete
3572 * with another macro-end marker to ensure the process
3573 * continues) until the whole expansion is forcibly removed
3574 * from istk->expansion by a %exitrep.
3575 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003576 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003577 l->next = istk->expansion;
3578 l->finishes = defining;
3579 l->first = NULL;
3580 istk->expansion = l;
3581
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003582 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003583
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003584 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003585 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003586 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003587
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003589 /*
3590 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003591 * macro-end marker for a macro with no name. Then we set
3592 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003593 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003594 list_for_each(l, istk->expansion)
3595 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003596 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003597
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003598 if (l)
3599 l->finishes->in_progress = 1;
3600 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003601 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003602 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003603
H. Peter Anvin8b262472019-02-26 14:00:54 -08003604 case PP_DEFINE:
3605 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003606 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003607 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003608 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003609 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003610
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003611 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003612 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003613
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003614 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003615 lastp = &tline->next;
3616 nparam = parse_smacro_template(&lastp, &tmpl);
3617 tline = *lastp;
3618 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003619
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003620 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003621 macro_start = tline;
3622 if (!is_macro_id(macro_start)) {
3623 nasm_nonfatal("`%s' expects a macro identifier to alias",
3624 dname);
3625 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003627 tt = macro_start->next;
3628 macro_start->next = NULL;
3629 tline = tline->next;
3630 skip_white_(tline);
3631 if (tline && tline->type) {
3632 nasm_warn(WARN_OTHER,
3633 "trailing garbage after aliasing identifier ignored");
3634 }
3635 free_tlist(tt);
3636 tmpl.alias = true;
3637 } else {
3638 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003639 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003640 tline = expand_smacro(tline);
3641
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003642 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003643 macro_start = NULL;
3644 t = tline;
3645 while (t) {
3646 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003647 for (i = 0; i < nparam; i++) {
3648 if ((size_t)tmpl.params[i].namelen == t->len &&
3649 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3650 t->type = tok_smac_param(i);
3651 break;
3652 }
3653 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003654 }
3655 tt = t->next;
3656 t->next = macro_start;
3657 macro_start = t;
3658 t = tt;
3659 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003661
H. Peter Anvine2c80182005-01-15 22:15:51 +00003662 /*
3663 * Good. We now have a macro name, a parameter count, and a
3664 * token list (in reverse order) for an expansion. We ought
3665 * to be OK just to create an SMacro, store it, and let
3666 * free_tlist have the rest of the line (which we have
3667 * carefully re-terminated after chopping off the expansion
3668 * from the end).
3669 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003670 define_smacro(mname, casesense, macro_start, &tmpl);
3671 break;
3672 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003673
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003675 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003676 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003677 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003678 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003679 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003680
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003681 undef_smacro(mname, i == PP_UNDEFALIAS);
3682 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003683
H. Peter Anvin8b262472019-02-26 14:00:54 -08003684 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003685 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003686 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003687
H. Peter Anvin9e200162008-06-04 17:23:14 -07003688 last = tline;
3689 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003690 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003691
3692 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003693 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003694
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003695 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003696 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003698
3699 /*
3700 * We now have a macro name, an implicit parameter count of
3701 * zero, and a string token to use as an expansion. Create
3702 * and store an SMacro.
3703 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003704 define_smacro(mname, casesense, macro_start, NULL);
3705 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003706
H. Peter Anvin8b262472019-02-26 14:00:54 -08003707 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003708 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003709 goto done;
3710
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003711 last = tline;
3712 tline = expand_smacro(tline->next);
3713 last->next = NULL;
3714
3715 t = tline;
3716 while (tok_type_(t, TOK_WHITESPACE))
3717 t = t->next;
3718 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003719 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003720 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003721 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003722 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003723 }
3724
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003725 /*
3726 * Convert the string to a token stream. Note that smacros
3727 * are stored with the token stream reversed, so we have to
3728 * reverse the output of tokenize().
3729 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003730 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003731 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003732
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003733 /*
3734 * We now have a macro name, an implicit parameter count of
3735 * zero, and a numeric 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);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003739 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003740 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003741
H. Peter Anvin418ca702008-05-30 10:42:30 -07003742 case PP_PATHSEARCH:
3743 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003744 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003745
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003746 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003747 goto done;
3748
H. Peter Anvin418ca702008-05-30 10:42:30 -07003749 last = tline;
3750 tline = expand_smacro(tline->next);
3751 last->next = NULL;
3752
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003753 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003754 while (tok_type_(t, TOK_WHITESPACE))
3755 t = t->next;
3756
3757 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003758 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003759 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003760 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003761 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003762 }
3763 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003764 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003765 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003766 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003767 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003768
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003769 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003770 if (!found_path)
3771 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003772 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003773
3774 /*
3775 * We now have a macro name, an implicit parameter count of
3776 * zero, and a string token to use as an expansion. Create
3777 * and store an SMacro.
3778 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003779 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003780 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003781 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003782 }
3783
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003785 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003786 goto done;
3787
H. Peter Anvine2c80182005-01-15 22:15:51 +00003788 last = tline;
3789 tline = expand_smacro(tline->next);
3790 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003791
H. Peter Anvine2c80182005-01-15 22:15:51 +00003792 t = tline;
3793 while (tok_type_(t, TOK_WHITESPACE))
3794 t = t->next;
3795 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003796 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003797 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 free_tlist(tline);
3799 free_tlist(origline);
3800 return DIRECTIVE_FOUND;
3801 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003802
H. Peter Anvin8b262472019-02-26 14:00:54 -08003803 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003804
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 /*
3806 * We now have a macro name, an implicit parameter count of
3807 * zero, and a numeric token to use as an expansion. Create
3808 * and store an SMacro.
3809 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003810 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003811 free_tlist(tline);
3812 free_tlist(origline);
3813 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003814
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003815 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003816 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003817 goto done;
3818
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003819 last = tline;
3820 tline = expand_smacro(tline->next);
3821 last->next = NULL;
3822
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003823 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003824 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003825 switch (t->type) {
3826 case TOK_WHITESPACE:
3827 break;
3828 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003829 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003830 break;
3831 case TOK_OTHER:
3832 if (!strcmp(t->text, ",")) /* permit comma separators */
3833 break;
3834 /* else fall through */
3835 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003836 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003837 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003838 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003839 }
3840 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003841
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003842 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003843 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003844 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003845 memcpy(p, t->text, t->len);
3846 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003847 }
3848 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003849
3850 /*
3851 * We now have a macro name, an implicit parameter count of
3852 * zero, and a numeric token to use as an expansion. Create
3853 * and store an SMacro.
3854 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003855 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003856 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003857 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003858 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003859 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003860
H. Peter Anvine2c80182005-01-15 22:15:51 +00003861 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003862 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003863 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003864 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003865
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003866 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003867 goto done;
3868
H. Peter Anvine2c80182005-01-15 22:15:51 +00003869 last = tline;
3870 tline = expand_smacro(tline->next);
3871 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003872
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003873 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003874 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003875 while (tok_type_(t, TOK_WHITESPACE))
3876 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003877
H. Peter Anvine2c80182005-01-15 22:15:51 +00003878 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003879 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003880 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003881 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003882 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003883 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003884
H. Peter Anvin8b262472019-02-26 14:00:54 -08003885 pps.tptr = t->next;
3886 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003887 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003888 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003889 if (!evalresult) {
3890 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003891 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003892 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003893 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003895 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003897 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003898
H. Peter Anvin8b262472019-02-26 14:00:54 -08003899 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3900 pps.tptr = pps.tptr->next;
3901 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003902 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003903 } else {
3904 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003905 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003906 if (!evalresult) {
3907 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003908 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003909 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003910 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003911 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003912 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003913 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003914 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003915 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003916
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003917 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003918
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003919 /* make start and count being in range */
3920 if (start < 0)
3921 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003922 if (count < 0)
3923 count = len + count + 1 - start;
3924 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003925 count = len - start;
3926 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003927 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003928
H. Peter Anvin8b262472019-02-26 14:00:54 -08003929 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003930 macro_start->len = count;
3931 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3932 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003933
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 /*
3935 * We now have a macro name, an implicit parameter count of
3936 * zero, and a numeric token to use as an expansion. Create
3937 * and store an SMacro.
3938 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003939 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003940 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003941 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003942 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003943
H. Peter Anvin8b262472019-02-26 14:00:54 -08003944 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003945 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003946 goto done;
3947
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948 last = tline;
3949 tline = expand_smacro(tline->next);
3950 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003951
H. Peter Anvin8b262472019-02-26 14:00:54 -08003952 pps.tptr = tline;
3953 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003955 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003957 if (!evalresult)
3958 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003959
H. Peter Anvine2c80182005-01-15 22:15:51 +00003960 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003961 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003962
H. Peter Anvine2c80182005-01-15 22:15:51 +00003963 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003964 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 free_tlist(origline);
3966 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003967 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003968
H. Peter Anvin8b262472019-02-26 14:00:54 -08003969 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003970
H. Peter Anvine2c80182005-01-15 22:15:51 +00003971 /*
3972 * We now have a macro name, an implicit parameter count of
3973 * zero, and a numeric token to use as an expansion. Create
3974 * and store an SMacro.
3975 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003976 define_smacro(mname, casesense, macro_start, NULL);
3977 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003978
H. Peter Anvind2354082019-08-27 16:38:48 -07003979 case PP_ALIASES:
3980 tline = tline->next;
3981 tline = expand_smacro(tline);
3982 do_aliases = pp_get_boolean_option(tline, do_aliases);
3983 break;
3984
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985 case PP_LINE:
3986 /*
3987 * Syntax is `%line nnn[+mmm] [filename]'
3988 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003989 if (unlikely(pp_noline))
3990 goto done;
3991
H. Peter Anvine2c80182005-01-15 22:15:51 +00003992 tline = tline->next;
3993 skip_white_(tline);
3994 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003995 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003996 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003997 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003998 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 m = 1;
4000 tline = tline->next;
4001 if (tok_is_(tline, "+")) {
4002 tline = tline->next;
4003 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004004 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004005 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004007 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004008 tline = tline->next;
4009 }
4010 skip_white_(tline);
4011 src_set_linnum(k);
4012 istk->lineinc = m;
4013 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004014 char *fname = detoken(tline, false);
4015 src_set_fname(fname);
4016 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004017 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004018 break;
4019 }
4020
4021done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004022 free_tlist(origline);
4023 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004024}
4025
4026/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004027 * Ensure that a macro parameter contains a condition code and
4028 * nothing else. Return the condition code index if so, or -1
4029 * otherwise.
4030 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004031static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004032{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004033 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004034
H. Peter Anvin25a99342007-09-22 17:45:45 -07004035 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004036 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004037
H. Peter Anvineba20a72002-04-30 20:53:55 +00004038 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03004039 if (!t)
4040 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004041 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004042 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004043 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004044 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004045 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004046 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004047
Cyrill Gorcunov19456392012-05-02 00:18:56 +04004048 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004049}
4050
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004051/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004052 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004053 * pasting, if @handle_explicit passed then explicit pasting
4054 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004055 * The @m array can contain a series of token types which are
4056 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004057 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004058static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004059 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004060{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004061 Token *tok, *next, **prev_next, **prev_nonspace;
4062 bool pasted = false;
4063 char *buf, *p;
4064 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004065
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004066 /*
4067 * The last token before pasting. We need it
4068 * to be able to connect new handled tokens.
4069 * In other words if there were a tokens stream
4070 *
4071 * A -> B -> C -> D
4072 *
4073 * and we've joined tokens B and C, the resulting
4074 * stream should be
4075 *
4076 * A -> BC -> D
4077 */
4078 tok = *head;
4079 prev_next = NULL;
4080
4081 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
4082 prev_nonspace = head;
4083 else
4084 prev_nonspace = NULL;
4085
4086 while (tok && (next = tok->next)) {
4087
4088 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004089 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004090 /* Zap redundant whitespaces */
4091 while (tok_type_(next, TOK_WHITESPACE))
4092 next = delete_Token(next);
4093 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004094 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004095
4096 case TOK_PASTE:
4097 /* Explicit pasting */
4098 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004099 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004100 next = delete_Token(tok);
4101
4102 while (tok_type_(next, TOK_WHITESPACE))
4103 next = delete_Token(next);
4104
4105 if (!pasted)
4106 pasted = true;
4107
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004108 /* Left pasting token is start of line */
4109 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004110 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004111
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004112 /*
4113 * No ending token, this might happen in two
4114 * cases
4115 *
4116 * 1) There indeed no right token at all
4117 * 2) There is a bare "%define ID" statement,
4118 * and @ID does expand to whitespace.
4119 *
4120 * So technically we need to do a grammar analysis
4121 * in another stage of parsing, but for now lets don't
4122 * change the behaviour people used to. Simply allow
4123 * whitespace after paste token.
4124 */
4125 if (!next) {
4126 /*
4127 * Zap ending space tokens and that's all.
4128 */
4129 tok = (*prev_nonspace)->next;
4130 while (tok_type_(tok, TOK_WHITESPACE))
4131 tok = delete_Token(tok);
4132 tok = *prev_nonspace;
4133 tok->next = NULL;
4134 break;
4135 }
4136
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004137 tok = *prev_nonspace;
4138 while (tok_type_(tok, TOK_WHITESPACE))
4139 tok = delete_Token(tok);
4140 len = strlen(tok->text);
4141 len += strlen(next->text);
4142
4143 p = buf = nasm_malloc(len + 1);
4144 strcpy(p, tok->text);
4145 p = strchr(p, '\0');
4146 strcpy(p, next->text);
4147
4148 delete_Token(tok);
4149
4150 tok = tokenize(buf);
4151 nasm_free(buf);
4152
4153 *prev_nonspace = tok;
4154 while (tok && tok->next)
4155 tok = tok->next;
4156
4157 tok->next = delete_Token(next);
4158
4159 /* Restart from pasted tokens head */
4160 tok = *prev_nonspace;
4161 break;
4162
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004163 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004164 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004165 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004166 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4167 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004168
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004169 len = 0;
4170 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4171 len += strlen(next->text);
4172 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004173 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004174
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004175 /* No match or no text to process */
4176 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004177 break;
4178
4179 len += strlen(tok->text);
4180 p = buf = nasm_malloc(len + 1);
4181
Adam Majer1a069432017-07-25 11:12:35 +02004182 strcpy(p, tok->text);
4183 p = strchr(p, '\0');
4184 tok = delete_Token(tok);
4185
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004186 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004187 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4188 strcpy(p, tok->text);
4189 p = strchr(p, '\0');
4190 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004191 tok = delete_Token(tok);
4192 }
4193
4194 tok = tokenize(buf);
4195 nasm_free(buf);
4196
4197 if (prev_next)
4198 *prev_next = tok;
4199 else
4200 *head = tok;
4201
4202 /*
4203 * Connect pasted into original stream,
4204 * ie A -> new-tokens -> B
4205 */
4206 while (tok && tok->next)
4207 tok = tok->next;
4208 tok->next = next;
4209
4210 if (!pasted)
4211 pasted = true;
4212
4213 /* Restart from pasted tokens head */
4214 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004215 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004216
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004217 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004218 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004219
4220 prev_next = &tok->next;
4221
4222 if (tok->next &&
4223 !tok_type_(tok->next, TOK_WHITESPACE) &&
4224 !tok_type_(tok->next, TOK_PASTE))
4225 prev_nonspace = prev_next;
4226
4227 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004228 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004229
4230 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004231}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004232
4233/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004234 * Computes the proper rotation of mmacro parameters
4235 */
4236static int mmac_rotate(const MMacro *mac, unsigned int n)
4237{
4238 if (--n < mac->nparam)
4239 n = (n + mac->rotate) % mac->nparam;
4240
4241 return n+1;
4242}
4243
4244/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 * expands to a list of tokens from %{x:y}
4246 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004247static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004248{
4249 Token *t = tline, **tt, *tm, *head;
4250 char *pos;
4251 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004252
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004253 pos = strchr(tline->text, ':');
4254 nasm_assert(pos);
4255
4256 lst = atoi(pos + 1);
4257 fst = atoi(tline->text + 1);
4258
4259 /*
4260 * only macros params are accounted so
4261 * if someone passes %0 -- we reject such
4262 * value(s)
4263 */
4264 if (lst == 0 || fst == 0)
4265 goto err;
4266
4267 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004268 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4269 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004270 goto err;
4271
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004272 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4273 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004274
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004275 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004276 fst--, lst--;
4277
4278 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004279 * It will be at least one token. Note we
4280 * need to scan params until separator, otherwise
4281 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004282 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004283 j = (fst + mac->rotate) % mac->nparam;
4284 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004285 if (!tm)
4286 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004287 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004288 tt = &head->next, tm = tm->next;
4289 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004290 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004291 *tt = t, tt = &t->next, tm = tm->next;
4292 }
4293
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004294 if (fst < lst) {
4295 for (i = fst + 1; i <= lst; i++) {
4296 t = new_Token(NULL, TOK_OTHER, ",", 0);
4297 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004298 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004299 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004300 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004301 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004302 *tt = t, tt = &t->next, tm = tm->next;
4303 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004304 }
4305 } else {
4306 for (i = fst - 1; i >= lst; i--) {
4307 t = new_Token(NULL, TOK_OTHER, ",", 0);
4308 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004309 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004310 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004311 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004312 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004313 *tt = t, tt = &t->next, tm = tm->next;
4314 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004315 }
4316 }
4317
4318 *last = tt;
4319 return head;
4320
4321err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004322 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004323 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004324 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004325}
4326
H. Peter Anvin76690a12002-04-30 20:52:49 +00004327/*
4328 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004329 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004330 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004331 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004332static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004333{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004334 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004335 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004336 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004337
4338 tail = &thead;
4339 thead = NULL;
4340
H. Peter Anvine2c80182005-01-15 22:15:51 +00004341 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004342 bool change;
4343 Token *t = tline;
4344 char *text = t->text;
4345 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004346
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004347 tline = tline->next;
4348 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004349
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004350 switch (type) {
4351 case TOK_PREPROC_ID:
4352 {
4353 Token *tt = NULL;
4354
4355 change = false;
4356
4357 if (!text || !text[0])
4358 break;
4359 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4360 ((text[1] == '+' || text[1] == '-') && text[2])))
4361 break;
4362
4363 change = true;
4364
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004365 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004366 nasm_nonfatal("`%s': not in a macro call", text);
4367 text = NULL;
4368 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004369 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004370
4371 if (strchr(text, ':')) {
4372 /*
4373 * seems we have a parameters range here
4374 */
4375 Token *head, **last;
4376 head = expand_mmac_params_range(mac, t, &last);
4377 if (head) {
4378 *tail = head;
4379 *last = tline;
4380 text = NULL;
4381 }
4382 break;
4383 }
4384
4385 switch (text[1]) {
4386 /*
4387 * We have to make a substitution of one of the
4388 * forms %1, %-1, %+1, %%foo, %0, %00.
4389 */
4390 case '0':
4391 if (!text[2]) {
4392 type = TOK_NUMBER;
4393 text = nasm_asprintf("%d", mac->nparam);
4394 break;
4395 }
4396 if (text[2] != '0' || text[3])
4397 goto invalid;
4398 /* a possible captured label == mac->params[0] */
4399 /* fall through */
4400 default:
4401 {
4402 unsigned long n;
4403 char *ep;
4404
4405 n = strtoul(text + 1, &ep, 10);
4406 if (unlikely(*ep))
4407 goto invalid;
4408
4409 if (n <= mac->nparam) {
4410 n = mmac_rotate(mac, n);
4411 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4412 }
4413 text = NULL;
4414 break;
4415 }
4416 case '%':
4417 type = TOK_ID;
4418 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4419 break;
4420 case '-':
4421 case '+':
4422 {
4423 int cc;
4424 unsigned long n;
4425 char *ep;
4426
4427 text = NULL;
4428
4429 n = strtoul(t->text + 2, &ep, 10);
4430 if (unlikely(*ep))
4431 goto invalid;
4432
4433 if (n && n < mac->nparam) {
4434 n = mmac_rotate(mac, n);
4435 tt = mac->params[n];
4436 }
4437 cc = find_cc(tt);
4438 if (cc == -1) {
4439 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvind2354082019-08-27 16:38:48 -07004440 t->text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004441 text = NULL;
4442 break;
4443 }
4444
4445 type = TOK_ID;
4446 if (text[1] == '-') {
4447 int ncc = inverse_ccs[cc];
4448 if (unlikely(ncc == -1)) {
4449 nasm_nonfatal("condition code `%s' is not invertible",
4450 conditions[cc]);
4451 break;
4452 }
4453 cc = ncc;
4454 }
4455 text = nasm_strdup(conditions[cc]);
4456 break;
4457 }
4458
4459 invalid:
4460 nasm_nonfatal("invalid macro parameter: `%s'", text);
4461 text = NULL;
4462 break;
4463 }
4464 break;
4465 }
4466
4467 case TOK_PREPROC_Q:
4468 if (mac) {
4469 type = TOK_ID;
4470 text = nasm_strdup(mac->iname);
4471 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004472 } else {
4473 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004474 }
4475 break;
4476
4477 case TOK_PREPROC_QQ:
4478 if (mac) {
4479 type = TOK_ID;
4480 text = nasm_strdup(mac->name);
4481 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004482 } else {
4483 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004484 }
4485 break;
4486
4487 case TOK_INDIRECT:
4488 {
4489 Token *tt;
4490
4491 tt = tokenize(t->text);
4492 tt = expand_mmac_params(tt);
4493 tt = expand_smacro(tt);
4494 /* Why dup_tlist() here? We should own tt... */
4495 dup_tlist(tt, &tail);
4496 text = NULL;
4497 change = true;
4498 break;
4499 }
4500
4501 default:
4502 change = false;
4503 break;
4504 }
4505
4506 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004507 if (!text) {
4508 delete_Token(t);
4509 } else {
4510 *tail = t;
4511 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004512 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004513 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004514 t->type = type;
4515 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004516 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004517 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004518 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004519 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004520 tail = &t->next;
4521 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004522 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004523
H. Peter Anvineba20a72002-04-30 20:53:55 +00004524 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004525
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004526 if (changed) {
4527 const struct tokseq_match t[] = {
4528 {
4529 PP_CONCAT_MASK(TOK_ID) |
4530 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4531 PP_CONCAT_MASK(TOK_ID) |
4532 PP_CONCAT_MASK(TOK_NUMBER) |
4533 PP_CONCAT_MASK(TOK_FLOAT) |
4534 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4535 },
4536 {
4537 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4538 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4539 }
4540 };
4541 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4542 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004543
H. Peter Anvin76690a12002-04-30 20:52:49 +00004544 return thead;
4545}
4546
H. Peter Anvin322bee02019-08-10 01:38:06 -07004547static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004548
H. Peter Anvin76690a12002-04-30 20:52:49 +00004549/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004550 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004551 * a macro at all, it is simply copied to the output and the pointer
4552 * advanced. tpp should be a pointer to a pointer (usually the next
4553 * pointer of the previous token) to the first token. **tpp is updated
4554 * to point to the last token of the expansion, and *tpp updated to
4555 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004556 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004557 * If the expansion is empty, *tpp will be unchanged but **tpp will
4558 * be advanced past the macro call.
4559 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004560 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004561 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004562static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004563{
4564 Token **params = NULL;
4565 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004566 Token *tline = **tpp;
4567 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004568 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004569 int i;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004570 Token *t, *tup, *ttail;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004571 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004572 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004573
4574 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004575 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004576
4577 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004578
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004579 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004580 smacro_deadman.levels--;
4581
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004582 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004583 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004584 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004585 smacro_deadman.triggered = true;
4586 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004587 goto not_a_macro;
4588 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004589 head = (SMacro *)hash_findix(&smacros, mname);
4590 } else if (tline->type == TOK_PREPROC_ID) {
4591 Context *ctx = get_ctx(mname, &mname);
4592 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4593 } else {
4594 goto not_a_macro;
4595 }
4596
4597 /*
4598 * We've hit an identifier of some sort. First check whether the
4599 * identifier is a single-line macro at all, then think about
4600 * checking for parameters if necessary.
4601 */
4602 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004603 if (unlikely(m->alias && !do_aliases))
4604 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004605 if (!mstrcmp(m->name, mname, m->casesense))
4606 break;
4607 }
4608
4609 if (!m) {
4610 goto not_a_macro;
4611 }
4612
4613 /* Parse parameters, if applicable */
4614
4615 params = NULL;
4616 nparam = 0;
4617
4618 if (m->nparam == 0) {
4619 /*
4620 * Simple case: the macro is parameterless.
4621 * Nothing to parse; the expansion code will
4622 * drop the macro name token.
4623 */
4624 } else {
4625 /*
4626 * Complicated case: at least one macro with this name
4627 * exists and takes parameters. We must find the
4628 * parameters in the call, count them, find the SMacro
4629 * that corresponds to that form of the macro call, and
4630 * substitute for the parameters when we expand. What a
4631 * pain.
4632 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004633 Token *t;
4634 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004635
4636 tline = tline->next;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004637 skip_white_(tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004638 if (!tok_is_(tline, "(")) {
4639 /*
4640 * This macro wasn't called with parameters: ignore
4641 * the call. (Behaviour borrowed from gnu cpp.)
4642 */
4643 goto not_a_macro;
4644 }
4645
4646 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004647 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004648 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004649 t = tline; /* tline points to leading ( */
4650
4651 while (paren) {
4652 t = t->next;
4653
4654 if (!t) {
4655 nasm_nonfatal("macro call expects terminating `)'");
4656 goto not_a_macro;
4657 }
4658
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004659 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004660 continue;
4661
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004662 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004663 case ',':
4664 if (!brackets)
4665 nparam++;
4666 break;
4667
4668 case '{':
4669 brackets++;
4670 break;
4671
4672 case '}':
4673 if (brackets > 0)
4674 brackets--;
4675 break;
4676
4677 case '(':
4678 if (!brackets)
4679 paren++;
4680 break;
4681
4682 case ')':
4683 if (!brackets)
4684 paren--;
4685 break;
4686
4687 default:
4688 break; /* Normal token */
4689 }
4690 }
4691
4692 /*
4693 * Look for a macro matching in both name and parameter count.
4694 * We already know any matches cannot be anywhere before the
4695 * current position of "m", so there is no reason to
4696 * backtrack.
4697 */
4698 while (1) {
4699 if (!m) {
4700 /*!
4701 *!macro-params-single [on] single-line macro calls with wrong parameter count
4702 *! warns about \i{single-line macros} being invoked
4703 *! with the wrong number of parameters.
4704 */
4705 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4706 "single-line macro `%s' exists, "
4707 "but not taking %d parameter%s",
4708 mname, nparam, (nparam == 1) ? "" : "s");
4709 goto not_a_macro;
4710 }
4711
4712 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004713 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004714 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004715 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004716 break; /* Also good */
4717 }
4718 m = m->next;
4719 }
4720 }
4721
4722 if (m->in_progress)
4723 goto not_a_macro;
4724
4725 /* Expand the macro */
4726 m->in_progress = true;
4727
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004728 if (nparam) {
4729 /* Extract parameters */
4730 Token **phead, **pep;
4731 int white = 0;
4732 int brackets = 0;
4733 int paren;
4734 bool bracketed = false;
4735 bool bad_bracket = false;
4736 enum sparmflags flags;
4737
4738 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004739 paren = 1;
4740 nasm_newn(params, nparam);
4741 i = 0;
4742 flags = m->params[i].flags;
4743 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004744 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004745
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004746 while (paren) {
4747 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004748 char ch;
4749
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004750 tline = tline->next;
4751
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004752 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004753 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004754
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004755 ch = 0;
4756 skip = false;
4757
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004758
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004759 switch (tline->type) {
4760 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004761 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004762 ch = tline->text[0];
4763 break;
4764
4765 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004766 if (!(flags & SPARM_NOSTRIP)) {
4767 if (brackets || *phead)
4768 white++; /* Keep interior whitespace */
4769 skip = true;
4770 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004771 break;
4772
4773 default:
4774 break;
4775 }
4776
4777 switch (ch) {
4778 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004779 if (!brackets && !(flags & SPARM_GREEDY)) {
4780 i++;
4781 nasm_assert(i < nparam);
4782 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004783 *pep = NULL;
4784 bracketed = false;
4785 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004786 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004787 }
4788 break;
4789
4790 case '{':
4791 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004792 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4793 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004794 }
4795 brackets++;
4796 break;
4797
4798 case '}':
4799 if (brackets > 0) {
4800 if (!--brackets)
4801 skip = bracketed;
4802 }
4803 break;
4804
4805 case '(':
4806 if (!brackets)
4807 paren++;
4808 break;
4809
4810 case ')':
4811 if (!brackets) {
4812 paren--;
4813 if (!paren) {
4814 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004815 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004816 }
4817 }
4818 break;
4819
4820 default:
4821 break; /* Normal token */
4822 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004823
4824 if (!skip) {
4825 Token *t;
4826
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004827 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004828
4829 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004830 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004831 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004832 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004833 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004834 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004835 pep = &t->next;
4836 white = 0;
4837 }
4838 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004839
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004840 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004841 * Possible further processing of parameters. Note that the
4842 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004843 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004844 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004845 enum sparmflags flags = m->params[i].flags;
4846
4847 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004848 /* Evaluate this parameter as a number */
4849 struct ppscan pps;
4850 struct tokenval tokval;
4851 expr *evalresult;
4852 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004853
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004854 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4855 pps.ntokens = -1;
4856 tokval.t_type = TOKEN_INVALID;
4857 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004858
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004859 free_tlist(eval_param);
4860 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004861
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004862 if (!evalresult) {
4863 /* Nothing meaningful to do */
4864 } else if (tokval.t_type) {
4865 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4866 } else if (!is_simple(evalresult)) {
4867 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4868 } else {
4869 params[i] = make_tok_num(reloc_value(evalresult));
4870 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004871 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004872
4873 if (flags & SPARM_STR) {
4874 /* Convert expansion to a quoted string */
4875 char *arg;
4876 Token *qs;
4877
4878 qs = expand_smacro_noreset(params[i]);
4879 arg = detoken(qs, false);
4880 free_tlist(qs);
4881 params[i] = make_tok_qstr(arg);
4882 nasm_free(arg);
4883 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004884 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004885 }
4886
4887 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004888 tline = tline->next; /* Remove the macro call from the input */
4889 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004890
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004891 /* Note: we own the expansion this returns. */
4892 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004893
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004894 tup = NULL;
4895 ttail = NULL; /* Pointer to the last token of the expansion */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004896 cond_comma = false;
4897
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004898 while (t) {
4899 enum pp_token_type type = t->type;
4900 Token *tnext = t->next;
4901 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004902
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004903 switch (type) {
4904 case TOK_PREPROC_Q:
4905 case TOK_PREPROC_QQ:
4906 t->type = TOK_ID;
4907 nasm_free(t->text);
4908 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4909 t->len = nasm_last_string_len();
4910 t->next = tline;
4911 break;
4912
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004913 case TOK_COND_COMMA:
4914 delete_Token(t);
4915 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4916 break;
4917
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004918 case TOK_ID:
4919 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004920 /*
4921 * Chain this into the target line *before* expanding,
4922 * that way we pick up any arguments to the new macro call,
4923 * if applicable.
4924 */
4925 t->next = tline;
4926 tp = &t;
4927 expand_one_smacro(&tp);
4928 if (t == tline)
4929 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004930 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004931
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004932 default:
4933 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004934 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004935 nasm_assert(!tup && param < nparam);
4936 delete_Token(t);
4937 t = NULL;
4938 tup = tnext;
4939 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004940 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004941 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004942 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004943 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004944 }
4945
4946 if (t) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004947 if (t->type != TOK_WHITESPACE)
4948 cond_comma = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004949 tline = t;
4950 if (!ttail)
4951 ttail = t;
4952 }
4953
4954 if (tnext) {
4955 t = tnext;
4956 } else {
4957 t = tup;
4958 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004959 }
4960 }
4961
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004962 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004963 if (ttail)
4964 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004965
4966 m->in_progress = false;
4967
4968 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004969 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004970 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004971
4972 /*
4973 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004974 * and then advance to the next input token. Note that this is
4975 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004976 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004977not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004978 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004979 m = NULL;
4980done:
4981 smacro_deadman.levels++;
4982 if (unlikely(params))
4983 free_tlist_array(params, nparam);
4984 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004985}
4986
4987/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004988 * Expand all single-line macro calls made in the given line.
4989 * Return the expanded version of the line. The original is deemed
4990 * to be destroyed in the process. (In reality we'll just move
4991 * Tokens from input to output a lot of the time, rather than
4992 * actually bothering to destroy and replicate.)
4993 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004994static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004995{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004996 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07004997 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4998 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004999 return expand_smacro_noreset(tline);
5000}
5001
5002static Token *expand_smacro_noreset(Token * tline)
5003{
5004 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005005 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005006 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005007
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005008 /*
5009 * Trick: we should avoid changing the start token pointer since it can
5010 * be contained in "next" field of other token. Because of this
5011 * we allocate a copy of first token and work with it; at the end of
5012 * routine we copy it back
5013 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005014 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04005015 tline = new_Token(org_tline->next, org_tline->type,
5016 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005017 nasm_free(org_tline->text);
5018 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005019 }
5020
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005021
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005022 /*
5023 * Pretend that we always end up doing expansion on the first pass;
5024 * that way %+ get processed. However, if we process %+ before the
5025 * first pass, we end up with things like MACRO %+ TAIL trying to
5026 * look up the macro "MACROTAIL", which we don't want.
5027 */
5028 expanded = true;
5029 thead = tline;
5030 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005031 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005032 {
5033 PP_CONCAT_MASK(TOK_ID) |
5034 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5035 PP_CONCAT_MASK(TOK_ID) |
5036 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5037 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5038 }
5039 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005040
5041 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005042 while ((t = *tail)) /* main token loop */
5043 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005044
5045 if (!expanded) {
5046 tline = thead;
5047 break; /* Done! */
5048 }
5049
5050 /*
5051 * Now scan the entire line and look for successive TOK_IDs
5052 * that resulted after expansion (they can't be produced by
5053 * tokenize()). The successive TOK_IDs should be concatenated.
5054 * Also we look for %+ tokens and concatenate the tokens
5055 * before and after them (without white spaces in between).
5056 */
H. Peter Anvind2354082019-08-27 16:38:48 -07005057 if (!paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true)) {
5058 tline = thead;
5059 break;
5060 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005061 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005062 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005063
H. Peter Anvine2c80182005-01-15 22:15:51 +00005064 if (org_tline) {
5065 if (thead) {
5066 *org_tline = *thead;
5067 /* since we just gave text to org_line, don't free it */
5068 thead->text = NULL;
5069 delete_Token(thead);
5070 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005071 /*
5072 * The expression expanded to empty line;
5073 * we can't return NULL because of the "trick" above.
5074 * Just set the line to a single WHITESPACE token.
5075 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005076 memset(org_tline, 0, sizeof(*org_tline));
5077 org_tline->text = NULL;
5078 org_tline->type = TOK_WHITESPACE;
5079 }
5080 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005081 }
5082
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005083 return thead;
5084}
5085
5086/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005087 * Similar to expand_smacro but used exclusively with macro identifiers
5088 * right before they are fetched in. The reason is that there can be
5089 * identifiers consisting of several subparts. We consider that if there
5090 * are more than one element forming the name, user wants a expansion,
5091 * otherwise it will be left as-is. Example:
5092 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005093 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005094 *
5095 * the identifier %$abc will be left as-is so that the handler for %define
5096 * will suck it and define the corresponding value. Other case:
5097 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005098 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005099 *
5100 * In this case user wants name to be expanded *before* %define starts
5101 * working, so we'll expand %$abc into something (if it has a value;
5102 * otherwise it will be left as-is) then concatenate all successive
5103 * PP_IDs into one.
5104 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005105static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005106{
5107 Token *cur, *oldnext = NULL;
5108
H. Peter Anvin734b1882002-04-30 21:01:08 +00005109 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005110 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005111
5112 cur = tline;
5113 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005114 (cur->next->type == TOK_ID ||
5115 cur->next->type == TOK_PREPROC_ID
5116 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005117 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005118
5119 /* If identifier consists of just one token, don't expand */
5120 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005121 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005122
H. Peter Anvine2c80182005-01-15 22:15:51 +00005123 if (cur) {
5124 oldnext = cur->next; /* Detach the tail past identifier */
5125 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005126 }
5127
H. Peter Anvin734b1882002-04-30 21:01:08 +00005128 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005129
H. Peter Anvine2c80182005-01-15 22:15:51 +00005130 if (cur) {
5131 /* expand_smacro possibly changhed tline; re-scan for EOL */
5132 cur = tline;
5133 while (cur && cur->next)
5134 cur = cur->next;
5135 if (cur)
5136 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005137 }
5138
5139 return tline;
5140}
5141
5142/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005143 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005144 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005145 * to check for an initial label - that's taken care of in
5146 * expand_mmacro - but must check numbers of parameters. Guaranteed
5147 * to be called with tline->type == TOK_ID, so the putative macro
5148 * name is easy to find.
5149 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005150static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005151{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005152 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005153 Token **params;
5154 int nparam;
5155
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005156 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005157
5158 /*
5159 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005160 * name which isn't already excluded by macro cycle removal.
5161 * (The cycle removal test here helps optimize the case of wrapping
5162 * instructions, and is cheap to do here.)
5163 *
5164 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005165 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005166 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005167 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005168 list_for_each(m, head) {
5169 if (!mstrcmp(m->name, tline->text, m->casesense) &&
5170 (m->in_progress != 1 || m->max_depth > 0))
5171 break; /* Found something that needs consideration */
5172 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005173 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005174 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005175
5176 /*
5177 * OK, we have a potential macro. Count and demarcate the
5178 * parameters.
5179 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005180 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005181
5182 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005183 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005184 * structure that handles this number.
5185 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005186 while (m) {
5187 if (m->nparam_min <= nparam
5188 && (m->plus || nparam <= m->nparam_max)) {
5189 /*
5190 * This one is right. Just check if cycle removal
5191 * prohibits us using it before we actually celebrate...
5192 */
5193 if (m->in_progress > m->max_depth) {
5194 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005195 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005196 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005197 }
5198 nasm_free(params);
5199 return NULL;
5200 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005201 /*
5202 * It's right, and we can use it. Add its default
5203 * parameters to the end of our list if necessary.
5204 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005205 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005206 int newnparam = m->nparam_min + m->ndefs;
5207 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5208 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5209 (newnparam - nparam) * sizeof(*params));
5210 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005211 }
5212 /*
5213 * If we've gone over the maximum parameter count (and
5214 * we're in Plus mode), ignore parameters beyond
5215 * nparam_max.
5216 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005217 if (m->plus && nparam > m->nparam_max)
5218 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005219
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005220 /*
5221 * If nparam was adjusted above, make sure the list is still
5222 * NULL-terminated.
5223 */
5224 params[nparam+1] = NULL;
5225
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005226 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005227 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005228 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005229 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005230 }
5231 /*
5232 * This one wasn't right: look for the next one with the
5233 * same name.
5234 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005235 list_for_each(m, m->next)
5236 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005237 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005238 }
5239
5240 /*
5241 * After all that, we didn't find one with the right number of
5242 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005243 *!
5244 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5245 *! warns about \i{multi-line macros} being invoked
5246 *! with the wrong number of parameters. See \k{mlmacover} for an
5247 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005248 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005249 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5250 "multi-line macro `%s' exists, but not taking %d parameter%s",
5251 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005252 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005253 return NULL;
5254}
5255
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005256
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005257#if 0
5258
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005259/*
5260 * Save MMacro invocation specific fields in
5261 * preparation for a recursive macro expansion
5262 */
5263static void push_mmacro(MMacro *m)
5264{
5265 MMacroInvocation *i;
5266
5267 i = nasm_malloc(sizeof(MMacroInvocation));
5268 i->prev = m->prev;
5269 i->params = m->params;
5270 i->iline = m->iline;
5271 i->nparam = m->nparam;
5272 i->rotate = m->rotate;
5273 i->paramlen = m->paramlen;
5274 i->unique = m->unique;
5275 i->condcnt = m->condcnt;
5276 m->prev = i;
5277}
5278
5279
5280/*
5281 * Restore MMacro invocation specific fields that were
5282 * saved during a previous recursive macro expansion
5283 */
5284static void pop_mmacro(MMacro *m)
5285{
5286 MMacroInvocation *i;
5287
5288 if (m->prev) {
5289 i = m->prev;
5290 m->prev = i->prev;
5291 m->params = i->params;
5292 m->iline = i->iline;
5293 m->nparam = i->nparam;
5294 m->rotate = i->rotate;
5295 m->paramlen = i->paramlen;
5296 m->unique = i->unique;
5297 m->condcnt = i->condcnt;
5298 nasm_free(i);
5299 }
5300}
5301
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005302#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005303
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005304/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005305 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005306 */
5307static void list_mmacro_call(const MMacro *m)
5308{
5309 const char prefix[] = " ;;; [macro] ";
5310 size_t namelen, size;
5311 char *buf, *p;
5312 unsigned int i;
5313 const Token *t;
5314
5315 namelen = strlen(m->iname);
5316 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5317
5318 for (i = 1; i <= m->nparam; i++) {
5319 int j = 0;
5320 size += 3; /* Braces and space/comma */
5321 list_for_each(t, m->params[i]) {
5322 if (j++ >= m->paramlen[i])
5323 break;
5324 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5325 }
5326 }
5327
5328 buf = p = nasm_malloc(size);
5329 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5330 p = mempcpy(p, m->iname, namelen);
5331 *p++ = ' ';
5332
5333 for (i = 1; i <= m->nparam; i++) {
5334 int j = 0;
5335 *p++ = '{';
5336 list_for_each(t, m->params[i]) {
5337 if (j++ >= m->paramlen[i])
5338 break;
5339 if (!t->text) {
5340 if (t->type == TOK_WHITESPACE)
5341 *p++ = ' ';
5342 } else {
5343 p = mempcpy(p, t->text, t->len);
5344 }
5345 }
5346 *p++ = '}';
5347 *p++ = ',';
5348 }
5349
5350 *--p = '\0'; /* Replace last delimeter with null */
5351 lfmt->line(LIST_MACRO, -1, buf);
5352 nasm_free(buf);
5353}
5354
5355/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005356 * Expand the multi-line macro call made by the given line, if
5357 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005358 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005359 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005360static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005361{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005362 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005363 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005364 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005365 Token **params, *t, *tt;
5366 MMacro *m;
5367 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005368 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005369 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005370 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005371
5372 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005373 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005374 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005375 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005376 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005377 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005378 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005379 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005380 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005381 Token *last;
5382 /*
5383 * We have an id which isn't a macro call. We'll assume
5384 * it might be a label; we'll also check to see if a
5385 * colon follows it. Then, if there's another id after
5386 * that lot, we'll check it again for macro-hood.
5387 */
5388 label = last = t;
5389 t = t->next;
5390 if (tok_type_(t, TOK_WHITESPACE))
5391 last = t, t = t->next;
5392 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005393 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005394 last = t, t = t->next;
5395 if (tok_type_(t, TOK_WHITESPACE))
5396 last = t, t = t->next;
5397 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005398 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005399 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005400 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005401 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005402 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005403 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005404
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005405 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5406 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5407 if (!mmacro_deadman.triggered) {
5408 nasm_nonfatal("interminable multiline macro recursion");
5409 mmacro_deadman.triggered = true;
5410 }
5411 return 0;
5412 }
5413
5414 mmacro_deadman.total++;
5415 mmacro_deadman.levels++;
5416
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005417 /*
5418 * Fix up the parameters: this involves stripping leading and
5419 * trailing whitespace, then stripping braces if they are
5420 * present.
5421 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005422 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005423
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005424 nasm_assert(params[nparam+1] == NULL);
5425
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005426 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005427 int brace = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005428 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005429
H. Peter Anvine2c80182005-01-15 22:15:51 +00005430 skip_white_(t);
5431 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005432 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005433 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005434 while (t) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005435 if (comma) {
5436 /* Check if we hit a comma that ends this argument */
5437 if (tok_is_(t, ","))
5438 break;
5439 else if (t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
5440 break;
5441 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005442 if (brace && t->type == TOK_OTHER) {
5443 if (t->text[0] == '{')
5444 brace++; /* ... or a nested opening brace */
5445 else if (t->text[0] == '}')
5446 if (!--brace)
5447 break; /* ... or a brace */
5448 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005449 t = t->next;
5450 paramlen[i]++;
5451 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005452 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005453 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005454 }
5455
5456 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005457 * OK, we have a MMacro structure together with a set of
5458 * parameters. We must now go through the expansion and push
5459 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005460 * parameter tokens and macro-local tokens doesn't get done
5461 * until the single-line macro substitution process; this is
5462 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005463 * later through %rotate and give the right semantics for
5464 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005465 *
5466 * First, push an end marker on to istk->expansion, mark this
5467 * macro as in progress, and set up its invocation-specific
5468 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005469 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005470 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005471 ll->next = istk->expansion;
5472 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005473 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005474
5475 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005476 * Save the previous MMacro expansion in the case of
5477 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005478 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005479#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005480 if (m->max_depth && m->in_progress)
5481 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005482#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005483
5484 m->in_progress ++;
5485 m->params = params;
5486 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005487 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005488 m->nparam = nparam;
5489 m->rotate = 0;
5490 m->paramlen = paramlen;
5491 m->unique = unique++;
5492 m->lineno = 0;
5493 m->condcnt = 0;
5494
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005495 m->mstk = istk->mstk;
5496 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005497
5498 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005499 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005500 ll->next = istk->expansion;
5501 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005502 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005503 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005504
5505 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005506 * If we had a label, and this macro definition does not include
5507 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005508 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005509 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005510 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005511 /*
5512 * We had a label. If this macro contains an %00 parameter,
5513 * save the value as a special parameter (which is what it
5514 * is), otherwise push it as the first line of the macro
5515 * expansion.
5516 */
5517 if (m->capture_label) {
5518 params[0] = dup_Token(NULL, label);
5519 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005520 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005521 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005522 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005523 ll->finishes = NULL;
5524 ll->next = istk->expansion;
5525 istk->expansion = ll;
5526 ll->first = startline;
5527 if (!dont_prepend) {
5528 while (label->next)
5529 label = label->next;
5530 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005531 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005532 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005533 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005534
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005535 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005536
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005537 if (list_option('m') && !m->nolist)
5538 list_mmacro_call(m);
5539
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005540 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005541}
5542
H. Peter Anvin130736c2016-02-17 20:27:41 -08005543/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005544 * This function decides if an error message should be suppressed.
5545 * It will never be called with a severity level of ERR_FATAL or
5546 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005547 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005548static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005549{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005550 /*
5551 * If we're in a dead branch of IF or something like it, ignore the error.
5552 * However, because %else etc are evaluated in the state context
5553 * of the previous branch, errors might get lost:
5554 * %if 0 ... %else trailing garbage ... %endif
5555 * So %else etc should set the ERR_PP_PRECOND flag.
5556 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005557 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005558 ((severity & ERR_PP_PRECOND) ?
5559 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005560 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005561 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005562
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005563 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005564}
5565
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005566static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005567stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005568{
5569 (void)s;
5570 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005571 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005572
5573 return make_tok_qstr(src_get_fname());
5574}
5575
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005576static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005577stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005578{
5579 (void)s;
5580 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005581 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005582
5583 return make_tok_num(src_get_linnum());
5584}
5585
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005586static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005587stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005588{
5589 (void)s;
5590 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005591 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005592
5593 return make_tok_num(globalbits);
5594}
5595
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005596static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005597stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005598{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005599 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005600 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5601 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5602 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005603
5604 (void)s;
5605 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005606 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005607
5608 switch (globalbits) {
5609 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005610 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005611 break;
5612 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005613 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005614 break;
5615 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005616 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005617 break;
5618 default:
5619 panic();
5620 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005621
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005622 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005623}
5624
H. Peter Anvin8b262472019-02-26 14:00:54 -08005625/* Add magic standard macros */
5626struct magic_macros {
5627 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005628 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005629 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005630};
5631static const struct magic_macros magic_macros[] =
5632{
H. Peter Anvind2354082019-08-27 16:38:48 -07005633 { "__?FILE?__", 0, stdmac_file },
5634 { "__?LINE?__", 0, stdmac_line },
5635 { "__?BITS?__", 0, stdmac_bits },
5636 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005637 { NULL, 0, NULL }
5638};
5639
5640static void pp_add_magic_stdmac(void)
5641{
5642 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005643 SMacro tmpl;
5644
5645 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005646
5647 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005648 tmpl.nparam = m->nparam;
5649 tmpl.expand = m->func;
5650 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005651 }
5652}
5653
H. Peter Anvin734b1882002-04-30 21:01:08 +00005654static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005655pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005656{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005657 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005658 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005659
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005660 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005661 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005662 nested_mac_count = 0;
5663 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005664 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005665 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005666 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005667 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07005668 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005669
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005670 if (!use_loaded)
5671 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5672 memset(use_loaded, 0, use_package_count * sizeof(bool));
5673
H. Peter Anvin6686de22019-08-10 05:33:14 -07005674 /* First set up the top level input file */
5675 nasm_new(istk);
5676 istk->fp = nasm_open_read(file, NF_TEXT);
5677 src_set(0, file);
5678 istk->lineinc = 1;
5679 if (!istk->fp)
5680 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5681
5682 strlist_add(deplist, file);
5683
5684 /*
5685 * Set up the stdmac packages as a virtual include file,
5686 * indicated by a null file pointer.
5687 */
5688 nasm_new(inc);
5689 inc->next = istk;
5690 inc->fname = src_set_fname(NULL);
5691 inc->nolist = !list_option('b');
5692 istk = inc;
5693 lfmt->uplevel(LIST_INCLUDE, 0);
5694
H. Peter Anvin8b262472019-02-26 14:00:54 -08005695 pp_add_magic_stdmac();
5696
H. Peter Anvinf7606612016-07-13 14:23:48 -07005697 if (tasm_compatible_mode)
5698 pp_add_stdmac(nasm_stdmac_tasm);
5699
5700 pp_add_stdmac(nasm_stdmac_nasm);
5701 pp_add_stdmac(nasm_stdmac_version);
5702
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005703 if (extrastdmac)
5704 pp_add_stdmac(extrastdmac);
5705
H. Peter Anvinf7606612016-07-13 14:23:48 -07005706 stdmacpos = stdmacros[0];
5707 stdmacnext = &stdmacros[1];
5708
H. Peter Anvind2456592008-06-19 15:04:18 -07005709 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005710
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005711 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07005712 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005713 * other builtins, because it is special -- it varies between
5714 * passes -- but there is really no particular reason to make it
5715 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005716 *
5717 * 0 = dependencies only
5718 * 1 = preparatory passes
5719 * 2 = final pass
5720 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005721 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005722 switch (mode) {
5723 case PP_NORMAL:
5724 apass = pass_final() ? 2 : 1;
5725 break;
5726 case PP_DEPS:
5727 apass = 0;
5728 break;
5729 case PP_PREPROC:
5730 apass = 3;
5731 break;
5732 default:
5733 panic();
5734 }
5735
H. Peter Anvind2354082019-08-27 16:38:48 -07005736 define_smacro("__?PASS?__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005737}
5738
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005739static void pp_init(void)
5740{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005741}
5742
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005743/*
5744 * Get a line of tokens. If we popped the macro expansion/include stack,
5745 * we return a pointer to the dummy token tok_pop; at that point if
5746 * istk is NULL then we have reached end of input;
5747 */
5748static Token tok_pop; /* Dummy token placeholder */
5749
5750static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005751{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005752 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005753 Line *l = istk->expansion;
5754 Token *tline = NULL;
5755 Token *dtline;
5756
H. Peter Anvine2c80182005-01-15 22:15:51 +00005757 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005758 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005759 * buffer or from the input file.
5760 */
5761 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005762 while (l && l->finishes) {
5763 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005764
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005765 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005766 /*
5767 * This is a macro-end marker for a macro with no
5768 * name, which means it's not really a macro at all
5769 * but a %rep block, and the `in_progress' field is
5770 * more than 1, meaning that we still need to
5771 * repeat. (1 means the natural last repetition; 0
5772 * means termination by %exitrep.) We have
5773 * therefore expanded up to the %endrep, and must
5774 * push the whole block on to the expansion buffer
5775 * again. We don't bother to remove the macro-end
5776 * marker: we'd only have to generate another one
5777 * if we did.
5778 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005779 fm->in_progress--;
5780 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005781 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005782 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005783
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005784 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005785 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005786 tail = &ll->first;
5787
5788 list_for_each(t, l->first) {
5789 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005790 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005791 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005792 }
5793 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005794 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005795 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005796 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005797 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005798 MMacro *m = istk->mstk.mstk;
5799
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005800 /*
5801 * Check whether a `%rep' was started and not ended
5802 * within this macro expansion. This can happen and
5803 * should be detected. It's a fatal error because
5804 * I'm too confused to work out how to recover
5805 * sensibly from it.
5806 */
5807 if (defining) {
5808 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005809 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005810 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005811 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005812 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005813 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005814
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005815 /*
5816 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005817 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005818 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005819 istk->mstk = m->mstk;
5820 if (m->name) {
5821 /*
5822 * This was a real macro call, not a %rep, and
5823 * therefore the parameter information needs to
5824 * be freed and the iteration count/nesting
5825 * depth adjusted.
5826 */
5827
5828 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005829 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005830 * If all mmacro processing done,
5831 * clear all counters and the deadman
5832 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005833 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005834 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005835 }
5836
Adam Majer91e72402017-07-25 10:42:01 +02005837#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005838 if (m->prev) {
5839 pop_mmacro(m);
5840 fm->in_progress --;
5841 } else
Adam Majer91e72402017-07-25 10:42:01 +02005842#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005843 {
5844 nasm_free(m->params);
5845 free_tlist(m->iline);
5846 nasm_free(m->paramlen);
5847 fm->in_progress = 0;
5848 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005849 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005850
5851 /*
5852 * FIXME It is incorrect to always free_mmacro here.
5853 * It leads to usage-after-free.
5854 *
5855 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5856 */
5857#if 0
5858 else
5859 free_mmacro(m);
5860#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005861 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005862 istk->expansion = l->next;
5863 nasm_free(l);
5864 lfmt->downlevel(LIST_MACRO);
5865 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005866 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005867
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005868 do { /* until we get a line we can use */
5869 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005870
5871 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005872 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005873 int32_t lineno;
5874
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005875 if (istk->mstk.mstk) {
5876 istk->mstk.mstk->lineno++;
5877 if (istk->mstk.mstk->fname)
5878 lineno = istk->mstk.mstk->lineno +
5879 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005880 else
5881 lineno = 0; /* Defined at init time or builtin */
5882 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005883 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005884 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005885
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005886 tline = l->first;
5887 istk->expansion = l->next;
5888 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005889
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005890 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005891 if (!istk->nolist)
5892 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005893 nasm_free(line);
5894 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005895 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005896 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005897 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005898 } else {
5899 /*
5900 * The current file has ended; work down the istk
5901 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005902 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005903 if (i->fp)
5904 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005905 if (i->conds) {
5906 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005907 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005908 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005909 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005910 if (i->next)
5911 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005912 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005913 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005914 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005915 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005916 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005917 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005918
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005919 /*
5920 * We must expand MMacro parameters and MMacro-local labels
5921 * _before_ we plunge into directive processing, to cope
5922 * with things like `%define something %1' such as STRUC
5923 * uses. Unless we're _defining_ a MMacro, in which case
5924 * those tokens should be left alone to go into the
5925 * definition; and unless we're in a non-emitting
5926 * condition, in which case we don't want to meddle with
5927 * anything.
5928 */
5929 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005930 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005931 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005932 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005933
H. Peter Anvine2c80182005-01-15 22:15:51 +00005934 /*
5935 * Check the line to see if it's a preprocessor directive.
5936 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005937 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5938 if (dtline)
5939 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005940 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005941 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005942 * We're defining a multi-line macro. We emit nothing
5943 * at all, and just
5944 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005945 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005946 MMacro *mmac = defining->dstk.mmac;
5947
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005948 Line *l = nasm_malloc(sizeof(Line));
5949 l->next = defining->expansion;
5950 l->first = tline;
5951 l->finishes = NULL;
5952 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005953
5954 /*
5955 * Remember if this mmacro expansion contains %00:
5956 * if it does, we will have to handle leading labels
5957 * specially.
5958 */
5959 if (mmac) {
5960 const Token *t;
5961 list_for_each(t, tline) {
5962 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
5963 mmac->capture_label = true;
5964 }
5965 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005966 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005967 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005968 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005969 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005970 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005971 * directive so we keep our place correctly.
5972 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005973 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005974 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005975 /*
5976 * We're in a %rep block which has been terminated, so
5977 * we're walking through to the %endrep without
5978 * emitting anything. Emit nothing at all, not even a
5979 * blank line: when we emerge from the %rep block we'll
5980 * give a line-number directive so we keep our place
5981 * correctly.
5982 */
5983 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005984 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005985 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005986 if (!expand_mmacro(tline))
5987 return tline;
5988 }
5989 }
5990}
5991
5992static char *pp_getline(void)
5993{
5994 char *line = NULL;
5995 Token *tline;
5996
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005997 while (true) {
5998 tline = pp_tokline();
5999 if (tline == &tok_pop) {
6000 /*
6001 * We popped the macro/include stack. If istk is empty,
6002 * we are at end of input, otherwise just loop back.
6003 */
6004 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006005 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006006 } else {
6007 /*
6008 * De-tokenize the line and emit it.
6009 */
6010 line = detoken(tline, true);
6011 free_tlist(tline);
6012 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006013 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006014 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006015
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006016 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006017 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006018 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006019 nasm_free(buf);
6020 }
6021
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006022 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006023}
6024
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006025static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006026{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006027 if (defining) {
6028 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006029 nasm_nonfatal("end of file while still defining macro `%s'",
6030 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006031 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006032 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006033 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006034
6035 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006036 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006037 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006038
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006039 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006040 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006041 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006042 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006043 Include *i = istk;
6044 istk = istk->next;
6045 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006046 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006047 }
6048 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006049 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006050 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006051}
6052
6053static void pp_cleanup_session(void)
6054{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006055 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006056 free_llist(predef);
6057 predef = NULL;
6058 delete_Blocks();
6059 freeTokens = NULL;
6060 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006061}
6062
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006063static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006064{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006065 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006066}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006067
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006068static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006069{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006070 Token *inc, *space, *name;
6071 Line *l;
6072
H. Peter Anvin734b1882002-04-30 21:01:08 +00006073 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
6074 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
6075 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006076
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006077 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006078 l->next = predef;
6079 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006080 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006081 predef = l;
6082}
6083
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006084static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006085{
6086 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006087 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006088 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006089
6090 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00006091 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6092 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006093 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006094 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006095 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006096 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006097 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006098
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006099 if (space->next->type != TOK_PREPROC_ID &&
6100 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006101 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006102
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006103 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006104 l->next = predef;
6105 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006106 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006107 predef = l;
6108}
6109
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006110static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006111{
6112 Token *def, *space;
6113 Line *l;
6114
H. Peter Anvin734b1882002-04-30 21:01:08 +00006115 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6116 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006117 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006118
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006119 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006120 l->next = predef;
6121 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006122 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006123 predef = l;
6124}
6125
H. Peter Anvin05990342018-06-11 13:32:42 -07006126/* Insert an early preprocessor command that doesn't need special handling */
6127static void pp_pre_command(const char *what, char *string)
6128{
6129 char *cmd;
6130 Token *def, *space;
6131 Line *l;
6132
6133 def = tokenize(string);
6134 if (what) {
6135 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6136 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
6137 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6138 }
6139
6140 l = nasm_malloc(sizeof(Line));
6141 l->next = predef;
6142 l->first = def;
6143 l->finishes = NULL;
6144 predef = l;
6145}
6146
H. Peter Anvinf7606612016-07-13 14:23:48 -07006147static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006148{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006149 macros_t **mp;
6150
6151 /* Find the end of the list and avoid duplicates */
6152 for (mp = stdmacros; *mp; mp++) {
6153 if (*mp == macros)
6154 return; /* Nothing to do */
6155 }
6156
6157 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6158
6159 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006160}
6161
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006162static void pp_extra_stdmac(macros_t *macros)
6163{
6164 extrastdmac = macros;
6165}
6166
H. Peter Anvin8b262472019-02-26 14:00:54 -08006167static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006168{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006169 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006170 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6171 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6172}
6173
6174static Token *make_tok_qstr(const char *str)
6175{
6176 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006177 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006178 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006179}
6180
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006181static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006182{
6183 if (!m)
6184 return;
6185
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006186 /* We need to print the mstk.mmac list in reverse order */
6187 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006188
6189 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006190 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006191 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006192 }
6193}
6194
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006195static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006196{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006197 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006198
H. Peter Anvinddb29062018-12-11 00:06:29 -08006199 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6200 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006201
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006202 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006203 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006204
H. Peter Anvinddb29062018-12-11 00:06:29 -08006205 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006206}
6207
H. Peter Anvine7469712016-02-18 02:20:59 -08006208const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006209 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006210 pp_reset,
6211 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006212 pp_cleanup_pass,
6213 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006214 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006215 pp_pre_define,
6216 pp_pre_undefine,
6217 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006218 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006219 pp_include_path,
6220 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006221 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006222};