blob: cb96cf833e8b50511f4bae99e253c9ac122a7e28 [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 Anvind7ed89e2002-04-30 20:52:08 +0000447
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300448static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000449
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300450static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000451
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800452static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700453static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800454static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000455
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000456/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800457 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000458 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800459static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000460
461/*
462 * The current set of single-line macros we have defined.
463 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700464static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000465
466/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800467 * The multi-line macro we are currently defining, or the %rep
468 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800470static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000471
Charles Crayned4200be2008-07-12 16:42:33 -0700472static uint64_t nested_mac_count;
473static uint64_t nested_rep_count;
474
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000475/*
476 * The number of macro parameters to allocate space for at a time.
477 */
478#define PARAM_DELTA 16
479
480/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700481 * The standard macro set: defined in macros.c in a set of arrays.
482 * This gives our position in any macro set, while we are processing it.
483 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000484 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700485static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700486static macros_t **stdmacnext;
487static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300488static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000489
490/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700491 * Map of which %use packages have been loaded
492 */
493static bool *use_loaded;
494
495/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000496 * Tokens are allocated in blocks to improve speed
497 */
498#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800499static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000500struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501 Blocks *next;
502 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000503};
504
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800505static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000506
507/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000508 * Forward declarations.
509 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700510static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000511static Token *expand_mmac_params(Token * tline);
512static Token *expand_smacro(Token * tline);
513static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400514static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800515static Token *make_tok_num(int64_t val);
516static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800517static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800518static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000519static void *new_Block(size_t size);
520static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700521static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700522 const char *text, size_t txtlen);
523static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000524static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525
526/*
527 * Macros for safe checking of token pointers, avoid *(NULL)
528 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300529#define tok_type_(x,t) ((x) && (x)->type == (t))
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -0700530#define skip_white_(x) ((x) = (tok_type_((x), TOK_WHITESPACE) ? (x)->next : (x)))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300531#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
532#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000533
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400534/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700535 * In-place reverse a list of tokens.
536 */
537static Token *reverse_tokens(Token *t)
538{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800539 Token *prev = NULL;
540 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700541
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800542 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400543 next = t->next;
544 t->next = prev;
545 prev = t;
546 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800547 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700548
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800549 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700550}
551
552/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300553 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000554 * front of them. We do it here because I could not find any other
555 * place to do it for the moment, and it is a hack (ideally it would
556 * be nice to be able to use the NASM pre-processor to do it).
557 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000558static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000559{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000560 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400561 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000562
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400563 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000564
565 /* Binary search for the directive name */
566 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400567 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400568 q = nasm_skip_word(p);
569 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 if (len) {
571 oldchar = p[len];
572 p[len] = 0;
573 while (j - i > 1) {
574 k = (j + i) / 2;
575 m = nasm_stricmp(p, tasm_directives[k]);
576 if (m == 0) {
577 /* We have found a directive, so jam a % in front of it
578 * so that NASM will then recognise it as one if it's own.
579 */
580 p[len] = oldchar;
581 len = strlen(p);
582 oldline = line;
583 line = nasm_malloc(len + 2);
584 line[0] = '%';
585 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700586 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300587 * NASM does not recognise IFDIFI, so we convert
588 * it to %if 0. This is not used in NASM
589 * compatible code, but does need to parse for the
590 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700592 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 } else {
594 memcpy(line + 1, p, len + 1);
595 }
596 nasm_free(oldline);
597 return line;
598 } else if (m < 0) {
599 j = k;
600 } else
601 i = k;
602 }
603 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000604 }
605 return line;
606}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000607
H. Peter Anvin76690a12002-04-30 20:52:49 +0000608/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000609 * The pre-preprocessing stage... This function translates line
610 * number indications as they emerge from GNU cpp (`# lineno "file"
611 * flags') into NASM preprocessor line number indications (`%line
612 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000613 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000614static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000615{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000616 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000617 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000618
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 if (line[0] == '#' && line[1] == ' ') {
620 oldline = line;
621 fname = oldline + 2;
622 lineno = atoi(fname);
623 fname += strspn(fname, "0123456789 ");
624 if (*fname == '"')
625 fname++;
626 fnlen = strcspn(fname, "\"");
627 line = nasm_malloc(20 + fnlen);
628 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
629 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000630 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000631 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000633 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000634}
635
636/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000637 * Free a linked list of tokens.
638 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000640{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400641 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000643}
644
645/*
646 * Free a linked list of lines.
647 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000649{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400650 Line *l, *tmp;
651 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 free_tlist(l->first);
653 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000654 }
655}
656
657/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700658 * Free an array of linked lists of tokens
659 */
660static void free_tlist_array(Token **array, size_t nlists)
661{
662 Token **listp = array;
663
664 while (nlists--)
665 free_tlist(*listp++);
666
667 nasm_free(array);
668}
669
670/*
671 * Duplicate a linked list of tokens.
672 */
673static Token *dup_tlist(const Token *list, Token ***tailp)
674{
675 Token *newlist = NULL;
676 Token **tailpp = &newlist;
677 const Token *t;
678
679 list_for_each(t, list) {
680 Token *nt;
681 *tailpp = nt = dup_Token(NULL, t);
682 tailpp = &nt->next;
683 }
684
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700685 if (tailp) {
686 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700687 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700688 }
689
690 return newlist;
691}
692
693/*
694 * Duplicate a linked list of tokens with a maximum count
695 */
696static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
697{
698 Token *newlist = NULL;
699 Token **tailpp = &newlist;
700 const Token *t;
701
702 list_for_each(t, list) {
703 Token *nt;
704 if (!cnt--)
705 break;
706 *tailpp = nt = dup_Token(NULL, t);
707 tailpp = &nt->next;
708 }
709
710 if (tailp) {
711 **tailp = newlist;
712 *tailp = tailpp;
713 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700714
715 return newlist;
716}
717
718/*
719 * Duplicate a linked list of tokens in reverse order
720 */
721static Token *dup_tlist_reverse(const Token *list, Token *tail)
722{
723 const Token *t;
724
725 list_for_each(t, list)
726 tail = dup_Token(tail, t);
727
728 return tail;
729}
730
731/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800732 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000733 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800734static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000735{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800736 nasm_free(m->name);
737 free_tlist(m->dlist);
738 nasm_free(m->defaults);
739 free_llist(m->expansion);
740 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000741}
742
743/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700744 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800745 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700746static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800747{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700748 if (s->params) {
749 int i;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700750 for (i = 0; i < s->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700751 nasm_free(s->params[i].name);
752 nasm_free(s->params);
753 }
H. Peter Anvin8b262472019-02-26 14:00:54 -0800754 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700755 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700756}
757
758static void clear_smacro(SMacro *s)
759{
760 free_smacro_members(s);
761 /* Wipe everything except the next pointer */
762 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
763}
764
765/*
766 * Free an SMacro
767 */
768static void free_smacro(SMacro *s)
769{
770 free_smacro_members(s);
771 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800772}
773
774/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700775 * Free all currently defined macros, and free the hash tables
776 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700777static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700778{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800779 struct hash_iterator it;
780 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700781
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800782 hash_for_each(smt, it, np) {
783 SMacro *tmp;
784 SMacro *s = np->data;
785 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800786 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700787 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700788 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700789 hash_free(smt);
790}
791
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800792static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700793{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800794 struct hash_iterator it;
795 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700796
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800797 hash_for_each(mmt, it, np) {
798 MMacro *tmp;
799 MMacro *m = np->data;
800 nasm_free((void *)np->key);
801 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800802 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700803 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800804 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700805}
806
807static void free_macros(void)
808{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700809 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800810 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700811}
812
813/*
814 * Initialize the hash tables
815 */
816static void init_macros(void)
817{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700818}
819
820/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000821 * Pop the context stack.
822 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000824{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000825 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000826
827 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700828 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000829 nasm_free(c->name);
830 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000831}
832
H. Peter Anvin072771e2008-05-22 13:17:51 -0700833/*
834 * Search for a key in the hash index; adding it if necessary
835 * (in which case we initialize the data pointer to NULL.)
836 */
837static void **
838hash_findi_add(struct hash_table *hash, const char *str)
839{
840 struct hash_insert hi;
841 void **r;
842 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800843 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700844
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800845 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700846 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300847 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700848
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800849 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
850 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700851 return hash_add(&hi, strx, NULL);
852}
853
854/*
855 * Like hash_findi, but returns the data element rather than a pointer
856 * to it. Used only when not adding a new element, hence no third
857 * argument.
858 */
859static void *
860hash_findix(struct hash_table *hash, const char *str)
861{
862 void **p;
863
864 p = hash_findi(hash, str, NULL);
865 return p ? *p : NULL;
866}
867
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400868/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800869 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400870 * if there no more left -- return NULL
871 */
872static char *line_from_stdmac(void)
873{
874 unsigned char c;
875 const unsigned char *p = stdmacpos;
876 char *line, *q;
877 size_t len = 0;
878
879 if (!stdmacpos)
880 return NULL;
881
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700882 /*
883 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
884 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
885 */
886 while ((c = *p++) != 127) {
887 uint8_t ndir = c - 128;
888 if (ndir < 256-96)
889 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400890 else
891 len++;
892 }
893
894 line = nasm_malloc(len + 1);
895 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700896
897 while ((c = *stdmacpos++) != 127) {
898 uint8_t ndir = c - 128;
899 if (ndir < 256-96) {
900 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
901 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400902 *q++ = ' ';
903 } else {
904 *q++ = c;
905 }
906 }
907 stdmacpos = p;
908 *q = '\0';
909
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700910 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700911 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400912 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700913 if (*stdmacnext) {
914 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400915 } else if (do_predef) {
916 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400917
918 /*
919 * Nasty hack: here we push the contents of
920 * `predef' on to the top-level expansion stack,
921 * since this is the most convenient way to
922 * implement the pre-include and pre-define
923 * features.
924 */
925 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700926 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800927 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700928 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800929 l->finishes = NULL;
930
931 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400932 }
933 do_predef = false;
934 }
935 }
936
937 return line;
938}
939
H. Peter Anvin6686de22019-08-10 05:33:14 -0700940/*
941 * Read a line from a file. Return NULL on end of file.
942 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700943static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000944{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700945 int c;
946 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400947 const unsigned int delta = 512;
948 const unsigned int pad = 8;
949 unsigned int nr_cont = 0;
950 bool cont = false;
951 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700952 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000953
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400954 size = delta;
955 p = buffer = nasm_malloc(size);
956
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700957 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700958 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400959
960 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700961 case EOF:
962 if (p == buffer) {
963 nasm_free(buffer);
964 return NULL;
965 }
966 c = 0;
967 break;
968
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400969 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700970 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400971 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700972 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400973 if (cont) {
974 cont = false;
975 continue;
976 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700977 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400978 break;
979
980 case '\n':
981 if (cont) {
982 cont = false;
983 continue;
984 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700985 c = 0;
986 break;
987
988 case 032: /* ^Z = legacy MS-DOS end of file mark */
989 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400990 break;
991
992 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700993 next = fgetc(f);
994 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400995 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400996 cont = true;
997 nr_cont++;
998 continue;
999 }
1000 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001002
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001003 if (p >= (buffer + size - pad)) {
1004 buffer = nasm_realloc(buffer, size + delta);
1005 p = buffer + size - pad;
1006 size += delta;
1007 }
1008
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001009 *p++ = c;
1010 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001011
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001012 lineno = src_get_linnum() + istk->lineinc +
1013 (nr_cont * istk->lineinc);
1014 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001015
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001016 return buffer;
1017}
1018
1019/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001020 * Common read routine regardless of source
1021 */
1022static char *read_line(void)
1023{
1024 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001025 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001026
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001027 if (f)
1028 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001029 else
1030 line = line_from_stdmac();
1031
1032 if (!line)
1033 return NULL;
1034
1035 if (!istk->nolist)
1036 lfmt->line(LIST_READ, src_get_linnum(), line);
1037
1038 return line;
1039}
1040
1041/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001042 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001043 * don't need to parse the value out of e.g. numeric tokens: we
1044 * simply split one string into many.
1045 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001046static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001047{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001048 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001049 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001050 Token *list = NULL;
1051 Token *t, **tail = &list;
1052
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001054 char *p = line;
1055 char *ep = NULL; /* End of token, for trimming the end */
1056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 if (*p == '%') {
1058 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001059 if (*p == '+' && !nasm_isdigit(p[1])) {
1060 p++;
1061 type = TOK_PASTE;
1062 } else if (nasm_isdigit(*p) ||
1063 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 do {
1065 p++;
1066 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001067 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 type = TOK_PREPROC_ID;
1069 } else if (*p == '{') {
1070 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001071 while (*p) {
1072 if (*p == '}')
1073 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 p[-1] = *p;
1075 p++;
1076 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001077 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001078 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001079 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 if (*p)
1081 p++;
1082 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001083 } else if (*p == '[') {
1084 int lvl = 1;
1085 line += 2; /* Skip the leading %[ */
1086 p++;
1087 while (lvl && (c = *p++)) {
1088 switch (c) {
1089 case ']':
1090 lvl--;
1091 break;
1092 case '%':
1093 if (*p == '[')
1094 lvl++;
1095 break;
1096 case '\'':
1097 case '\"':
1098 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001099 p = nasm_skip_string(p - 1);
1100 if (*p)
1101 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001102 break;
1103 default:
1104 break;
1105 }
1106 }
1107 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001108 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001109 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001110 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001111 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001112 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001113 type = TOK_INDIRECT;
1114 } else if (*p == '?') {
1115 type = TOK_PREPROC_Q; /* %? */
1116 p++;
1117 if (*p == '?') {
1118 type = TOK_PREPROC_QQ; /* %?? */
1119 p++;
1120 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001121 } else if (*p == '!') {
1122 type = TOK_PREPROC_ID;
1123 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001124 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001125 do {
1126 p++;
1127 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001128 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001129 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001130 p = nasm_skip_string(p);
1131 if (*p)
1132 p++;
1133 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001134 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001135 } else {
1136 /* %! without string or identifier */
1137 type = TOK_OTHER; /* Legacy behavior... */
1138 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001139 } else if (*p == ',') {
1140 p++;
1141 type = TOK_COND_COMMA;
H. Peter Anvin13506202018-11-28 14:55:58 -08001142 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001144 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 do {
1146 p++;
1147 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001148 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 type = TOK_PREPROC_ID;
1150 } else {
1151 type = TOK_OTHER;
1152 if (*p == '%')
1153 p++;
1154 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001155 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 type = TOK_ID;
1157 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001158 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001160 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 /*
1162 * A string token.
1163 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001165 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001166
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 if (*p) {
1168 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001169 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001170 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 /* Handling unterminated strings by UNV */
1172 /* type = -1; */
1173 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001174 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001175 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001176 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001177 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001178 bool is_hex = false;
1179 bool is_float = false;
1180 bool has_e = false;
1181 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001182
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001184 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001186
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001187 if (*p == '$') {
1188 p++;
1189 is_hex = true;
1190 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001191
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001192 for (;;) {
1193 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001194
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001195 if (!is_hex && (c == 'e' || c == 'E')) {
1196 has_e = true;
1197 if (*p == '+' || *p == '-') {
1198 /*
1199 * e can only be followed by +/- if it is either a
1200 * prefixed hex number or a floating-point number
1201 */
1202 p++;
1203 is_float = true;
1204 }
1205 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1206 is_hex = true;
1207 } else if (c == 'P' || c == 'p') {
1208 is_float = true;
1209 if (*p == '+' || *p == '-')
1210 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001211 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001212 ; /* just advance */
1213 else if (c == '.') {
1214 /*
1215 * we need to deal with consequences of the legacy
1216 * parser, like "1.nolist" being two tokens
1217 * (TOK_NUMBER, TOK_ID) here; at least give it
1218 * a shot for now. In the future, we probably need
1219 * a flex-based scanner with proper pattern matching
1220 * to do it as well as it can be done. Nothing in
1221 * the world is going to help the person who wants
1222 * 0x123.p16 interpreted as two tokens, though.
1223 */
1224 r = p;
1225 while (*r == '_')
1226 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001227
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001228 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1229 (!is_hex && (*r == 'e' || *r == 'E')) ||
1230 (*r == 'p' || *r == 'P')) {
1231 p = r;
1232 is_float = true;
1233 } else
1234 break; /* Terminate the token */
1235 } else
1236 break;
1237 }
1238 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001239
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001240 if (p == line+1 && *line == '$') {
1241 type = TOK_OTHER; /* TOKEN_HERE */
1242 } else {
1243 if (has_e && !is_hex) {
1244 /* 1e13 is floating-point, but 1e13h is not */
1245 is_float = true;
1246 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001247
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001248 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1249 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001250 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001252 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 /*
1254 * Whitespace just before end-of-line is discarded by
1255 * pretending it's a comment; whitespace just before a
1256 * comment gets lumped into the comment.
1257 */
1258 if (!*p || *p == ';') {
1259 type = TOK_COMMENT;
1260 while (*p)
1261 p++;
1262 }
1263 } else if (*p == ';') {
1264 type = TOK_COMMENT;
1265 while (*p)
1266 p++;
1267 } else {
1268 /*
1269 * Anything else is an operator of some kind. We check
1270 * for all the double-character operators (>>, <<, //,
1271 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001272 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 */
1274 type = TOK_OTHER;
1275 if ((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[0] == '^' && p[1] == '^')) {
1286 p++;
1287 }
1288 p++;
1289 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001290
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 /* Handling unterminated string by UNV */
1292 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001293 {
1294 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1295 t->text[p-line] = *line;
1296 tail = &t->next;
1297 }
1298 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001300 if (!ep)
1301 ep = p;
1302 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 tail = &t->next;
1304 }
1305 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001306 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001307 return list;
1308}
1309
H. Peter Anvince616072002-04-30 21:02:23 +00001310/*
1311 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001312 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001313 * deleted only all at once by the delete_Blocks function.
1314 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001316{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001317 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001319 /* first, get to the end of the linked list */
1320 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001322 /* now allocate the requested chunk */
1323 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001325 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001326 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001327 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001328}
1329
1330/*
1331 * this function deletes all managed blocks of memory
1332 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001334{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001336
H. Peter Anvin70653092007-10-19 14:42:29 -07001337 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001338 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001339 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001340 * free it.
1341 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001343 if (b->chunk)
1344 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 a = b;
1346 b = b->next;
1347 if (a != &blocks)
1348 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001349 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001350 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001352
1353/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001354 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001355 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001356 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001357static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001358 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001359{
1360 Token *t;
1361 int i;
1362
H. Peter Anvin89cee572009-07-15 09:16:54 -04001363 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001364 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1365 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1366 freeTokens[i].next = &freeTokens[i + 1];
1367 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001368 }
1369 t = freeTokens;
1370 freeTokens = t->next;
1371 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001372 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001373 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001374 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 t->text = NULL;
1376 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001377 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001379 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001380 t->text = nasm_malloc(txtlen+1);
1381 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001383 }
1384 return t;
1385}
1386
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001387static Token *dup_Token(Token *next, const Token *src)
1388{
1389 return new_Token(next, src->type, src->text, src->len);
1390}
1391
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001393{
1394 Token *next = t->next;
1395 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001396 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001397 freeTokens = t;
1398 return next;
1399}
1400
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001401/*
1402 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001403 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1404 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001405 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001406static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001407{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001408 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001409 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001410 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001411
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001412 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001413 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001414 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001415 char *v;
1416 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001417
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001418 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001419 if (nasm_isquote(*v))
1420 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001421
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001422 if (v) {
1423 char *p = getenv(v);
1424 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001425 /*!
1426 *!environment [on] nonexistent environment variable
1427 *! warns if a nonexistent environment variable
1428 *! is accessed using the \c{%!} preprocessor
1429 *! construct (see \k{getenv}.) Such environment
1430 *! variables are treated as empty (with this
1431 *! warning issued) starting in NASM 2.15;
1432 *! earlier versions of NASM would treat this as
1433 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001434 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001435 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1436 p = "";
1437 }
1438 t->text = nasm_strdup(p);
1439 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001440 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001441 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001443
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 /* Expand local macros here and not during preprocessing */
1445 if (expand_locals &&
1446 t->type == TOK_PREPROC_ID && t->text &&
1447 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001448 const char *q;
1449 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001450 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001452 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1453 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 nasm_free(t->text);
1455 t->text = p;
1456 }
1457 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001458 if (t->text) {
1459 if (debug_level(2)) {
1460 unsigned long t_len = t->len;
1461 unsigned long s_len = strlen(t->text);
1462 if (t_len != s_len) {
1463 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1464 t->text, t->type, s_len, t_len);
1465 t->len = s_len;
1466 }
1467 }
1468 len += t->len;
1469 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001471 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001472 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001473
H. Peter Anvin734b1882002-04-30 21:01:08 +00001474 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001475
1476 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001477 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001478 memcpy(p, t->text, t->len);
1479 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001480 } else if (t->type == TOK_WHITESPACE) {
1481 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001483 }
1484 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001485
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001486 return line;
1487}
1488
1489/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001490 * A scanner, suitable for use by the expression evaluator, which
1491 * operates on a line of Tokens. Expects a pointer to a pointer to
1492 * the first token in the line to be passed in as its private_data
1493 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001494 *
1495 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001496 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001497struct ppscan {
1498 Token *tptr;
1499 int ntokens;
1500};
1501
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001503{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001504 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001505 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001506 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001507
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001509 if (pps->ntokens && (tline = pps->tptr)) {
1510 pps->ntokens--;
1511 pps->tptr = tline->next;
1512 } else {
1513 pps->tptr = NULL;
1514 pps->ntokens = 0;
1515 return tokval->t_type = TOKEN_EOS;
1516 }
1517 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001518
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001519 tokval->t_charptr = tline->text;
1520
H. Peter Anvin76690a12002-04-30 20:52:49 +00001521 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001522 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001523 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001525
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001527 p = tokval->t_charptr = tline->text;
1528 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 tokval->t_charptr++;
1530 return tokval->t_type = TOKEN_ID;
1531 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001532
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001533 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001534 if (r >= p+MAX_KEYWORD)
1535 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001536 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001537 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001538 *s = '\0';
1539 /* right, so we have an identifier sitting in temp storage. now,
1540 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001541 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001542 }
1543
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001545 bool rn_error;
1546 tokval->t_integer = readnum(tline->text, &rn_error);
1547 tokval->t_charptr = tline->text;
1548 if (rn_error)
1549 return tokval->t_type = TOKEN_ERRNUM;
1550 else
1551 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001552 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001553
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001554 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001555 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001556 }
1557
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001559 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001560
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001561 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001562 tokval->t_charptr = tline->text;
1563 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001565 if (ep[0] != bq || ep[1] != '\0')
1566 return tokval->t_type = TOKEN_ERRSTR;
1567 else
1568 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001569 }
1570
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 if (tline->type == TOK_OTHER) {
1572 if (!strcmp(tline->text, "<<"))
1573 return tokval->t_type = TOKEN_SHL;
1574 if (!strcmp(tline->text, ">>"))
1575 return tokval->t_type = TOKEN_SHR;
1576 if (!strcmp(tline->text, "//"))
1577 return tokval->t_type = TOKEN_SDIV;
1578 if (!strcmp(tline->text, "%%"))
1579 return tokval->t_type = TOKEN_SMOD;
1580 if (!strcmp(tline->text, "=="))
1581 return tokval->t_type = TOKEN_EQ;
1582 if (!strcmp(tline->text, "<>"))
1583 return tokval->t_type = TOKEN_NE;
1584 if (!strcmp(tline->text, "!="))
1585 return tokval->t_type = TOKEN_NE;
1586 if (!strcmp(tline->text, "<="))
1587 return tokval->t_type = TOKEN_LE;
1588 if (!strcmp(tline->text, ">="))
1589 return tokval->t_type = TOKEN_GE;
1590 if (!strcmp(tline->text, "&&"))
1591 return tokval->t_type = TOKEN_DBL_AND;
1592 if (!strcmp(tline->text, "^^"))
1593 return tokval->t_type = TOKEN_DBL_XOR;
1594 if (!strcmp(tline->text, "||"))
1595 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001596 }
1597
1598 /*
1599 * We have no other options: just return the first character of
1600 * the token text.
1601 */
1602 return tokval->t_type = tline->text[0];
1603}
1604
1605/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001606 * Compare a string to the name of an existing macro; this is a
1607 * simple wrapper which calls either strcmp or nasm_stricmp
1608 * depending on the value of the `casesense' parameter.
1609 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001610static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001611{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001612 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001613}
1614
1615/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001616 * Compare a string to the name of an existing macro; this is a
1617 * simple wrapper which calls either strcmp or nasm_stricmp
1618 * depending on the value of the `casesense' parameter.
1619 */
1620static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1621{
1622 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1623}
1624
1625/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001626 * Return the Context structure associated with a %$ token. Return
1627 * NULL, having _already_ reported an error condition, if the
1628 * context stack isn't deep enough for the supplied number of $
1629 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001630 *
1631 * If "namep" is non-NULL, set it to the pointer to the macro name
1632 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001633 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001634static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001635{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001636 Context *ctx;
1637 int i;
1638
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001639 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001640 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001641
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001642 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001644
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001646 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001648 }
1649
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001650 name += 2;
1651 ctx = cstk;
1652 i = 0;
1653 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001654 name++;
1655 i++;
1656 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001657 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001659 nasm_nonfatal("`%s': context stack is only"
1660 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001662 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001663
1664 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001665 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001666
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001667 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001668}
1669
1670/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001671 * Open an include file. This routine must always return a valid
1672 * file pointer if it returns - it's responsible for throwing an
1673 * ERR_FATAL and bombing out completely if not. It should also try
1674 * the include path one by one until it finds the file or reaches
1675 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001676 *
1677 * Note: for INC_PROBE the function returns NULL at all times;
1678 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001679 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001680enum incopen_mode {
1681 INC_NEEDED, /* File must exist */
1682 INC_OPTIONAL, /* Missing is OK */
1683 INC_PROBE /* Only an existence probe */
1684};
1685
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001686/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001687static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001688 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001689{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001690 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001691 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001692 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001693 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001694 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001695
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001697 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001698 if (omode == INC_PROBE) {
1699 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001700 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001701 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001702 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001703 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001704 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001705 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001706 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001708 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001709
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001710 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001711
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001712 if (!ip) {
1713 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001714 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001715 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001716
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001717 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001718 ip = ip->next;
1719 }
1720}
1721
1722/*
1723 * Open a file, or test for the presence of one (depending on omode),
1724 * considering the include path.
1725 */
1726static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001727 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001728 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001729 enum incopen_mode omode,
1730 enum file_flags fmode)
1731{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001732 struct hash_insert hi;
1733 void **hp;
1734 char *path;
1735 FILE *fp = NULL;
1736
1737 hp = hash_find(&FileHash, file, &hi);
1738 if (hp) {
1739 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001740 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001741 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001742 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001743 } else {
1744 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001745 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001746
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001747 /* Positive or negative result */
1748 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001749
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001750 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001751 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001752 */
1753 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001754 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001755 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001756
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001757 if (!path) {
1758 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001759 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001760 } else {
1761 if (!fp && omode != INC_PROBE)
1762 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001763 }
1764
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001765 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001766 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001767
1768 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001769}
1770
1771/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001772 * Opens an include or input file. Public version, for use by modules
1773 * that get a file:lineno pair and need to look at the file again
1774 * (e.g. the CodeView debug backend). Returns NULL on failure.
1775 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001776FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001777{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001778 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001779}
1780
1781/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001782 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001783 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001784 * return true if _any_ single-line macro of that name is defined.
1785 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001786 * `nparam' or no parameters is defined.
1787 *
1788 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001789 * defined, or nparam is -1, the address of the definition structure
1790 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001791 * is NULL, no action will be taken regarding its contents, and no
1792 * error will occur.
1793 *
1794 * Note that this is also called with nparam zero to resolve
1795 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001796 *
1797 * If you already know which context macro belongs to, you can pass
1798 * the context pointer as first parameter; if you won't but name begins
1799 * with %$ the context will be automatically computed. If all_contexts
1800 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001801 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001802static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001803smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001804 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001805{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001806 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001807 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001808
H. Peter Anvin97a23472007-09-16 17:57:25 -07001809 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001810 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001811 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001812 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001813 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001815 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001816 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001817 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001818 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001819 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001820 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001821
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 while (m) {
1823 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001824 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001825 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001827 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001829 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 }
1831 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001832 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001833
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001834 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001835}
1836
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001837/* param should be a natural number [0; INT_MAX] */
1838static int read_param_count(const char *str)
1839{
1840 int result;
1841 bool err;
1842
1843 result = readnum(str, &err);
1844 if (result < 0 || result > INT_MAX) {
1845 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001846 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1847 str, 0, INT_MAX);
1848 } else if (err)
1849 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001850 return result;
1851}
1852
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001853/*
1854 * Count and mark off the parameters in a multi-line macro call.
1855 * This is called both from within the multi-line macro expansion
1856 * code, and also to mark off the default parameters when provided
1857 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001858 *
1859 * Note that we need space in the params array for parameter 0 being
1860 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001861 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001862static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001863{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001864 int paramsize, brace;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001865 int nparam = 0;
1866 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001867
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001868 paramsize = PARAM_DELTA;
1869 params = nasm_malloc(paramsize * sizeof(*params));
1870 params[0] = NULL;
1871
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07001872 while (skip_white_(t)) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001873 /* 2 slots for captured label and NULL */
1874 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001876 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001878 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001880 brace++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001881 params[++nparam] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001882 if (brace) {
1883 while (brace && (t = t->next) != NULL) {
1884 if (tok_is_(t, "{"))
1885 brace++;
1886 else if (tok_is_(t, "}"))
1887 brace--;
1888 }
1889
1890 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 /*
1892 * Now we've found the closing brace, look further
1893 * for the comma.
1894 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001895 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 skip_white_(t);
1897 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001898 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 while (tok_isnt_(t, ","))
1900 t = t->next;
1901 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001903 } else {
1904 while (tok_isnt_(t, ","))
1905 t = t->next;
1906 }
1907 if (t) { /* got a comma/brace */
1908 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001910 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001911
1912 params[nparam+1] = NULL;
1913 *paramsp = params;
1914 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001915}
1916
1917/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001918 * Determine whether one of the various `if' conditions is true or
1919 * not.
1920 *
1921 * We must free the tline we get passed.
1922 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001923static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001924{
H. Peter Anvin70055962007-10-11 00:05:31 -07001925 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001926 Token *t, *tt, *origline;
1927 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001928 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001929 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001930 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001931 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001932 const char *dname = pp_directives[ct];
1933 bool casesense = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001934
1935 origline = tline;
1936
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001937 switch (PP_COND(ct)) {
1938 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001939 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001940 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001942 if (!tline)
1943 break;
1944 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001945 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001946 dname);
1947 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001949 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001950 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 tline = tline->next;
1952 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001953 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001954
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001955 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001956 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001957 while (tline) {
1958 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 if (!tline || (tline->type != TOK_ID &&
1960 (tline->type != TOK_PREPROC_ID ||
1961 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001962 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001963 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001966 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001967 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001969 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001970 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001971
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001972 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001973 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001974 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001975 while (tline) {
1976 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001977 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001978 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001979 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001980 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001981 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001982 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001983 goto fail;
1984 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001985 p = tline->text;
1986 if (tline->type == TOK_PREPROC_ID)
1987 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001988 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001989 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001990 if (getenv(p))
1991 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001992 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001993 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001994 break;
1995
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001996 case PP_IFIDNI:
1997 casesense = false;
1998 /* fall through */
1999 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 tline = expand_smacro(tline);
2001 t = tt = tline;
2002 while (tok_isnt_(tt, ","))
2003 tt = tt->next;
2004 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002005 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002006 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002008 }
2009 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002010 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
2012 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002013 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002014 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002015 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002016 }
2017 if (t->type == TOK_WHITESPACE) {
2018 t = t->next;
2019 continue;
2020 }
2021 if (tt->type == TOK_WHITESPACE) {
2022 tt = tt->next;
2023 continue;
2024 }
2025 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002026 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 break;
2028 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002029 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002030 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002031 size_t l1 = nasm_unquote(t->text, NULL);
2032 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002033
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002034 if (l1 != l2) {
2035 j = false;
2036 break;
2037 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002038 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039 j = false;
2040 break;
2041 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002042 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002043 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 break;
2045 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002046
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 t = t->next;
2048 tt = tt->next;
2049 }
2050 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002051 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002052 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002053
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002054 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002055 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002057 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002058
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 skip_white_(tline);
2060 tline = expand_id(tline);
2061 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002062 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002063 goto fail;
2064 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002065 nasm_zero(searching);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002066 searching.name = nasm_strdup(tline->text);
2067 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002068 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002069 searching.nparam_max = INT_MAX;
2070 tline = expand_smacro(tline->next);
2071 skip_white_(tline);
2072 if (!tline) {
2073 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002074 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002075 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002076 } else {
2077 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002078 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002079 }
2080 if (tline && tok_is_(tline->next, "-")) {
2081 tline = tline->next->next;
2082 if (tok_is_(tline, "*"))
2083 searching.nparam_max = INT_MAX;
2084 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002085 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002086 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002088 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002089 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002090 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002091 searching.nparam_max = searching.nparam_min;
2092 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 }
2094 }
2095 if (tline && tok_is_(tline->next, "+")) {
2096 tline = tline->next;
2097 searching.plus = true;
2098 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002099 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2100 while (mmac) {
2101 if (!strcmp(mmac->name, searching.name) &&
2102 (mmac->nparam_min <= searching.nparam_max
2103 || searching.plus)
2104 && (searching.nparam_min <= mmac->nparam_max
2105 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 found = true;
2107 break;
2108 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002109 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002110 }
2111 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002112 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002113 nasm_free(searching.name);
2114 j = found;
2115 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002116 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002117
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002118 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002119 needtype = TOK_ID;
2120 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002121 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 needtype = TOK_NUMBER;
2123 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002124 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 needtype = TOK_STRING;
2126 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002127
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002128iftype:
2129 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002130
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002131 while (tok_type_(t, TOK_WHITESPACE) ||
2132 (needtype == TOK_NUMBER &&
2133 tok_type_(t, TOK_OTHER) &&
2134 (t->text[0] == '-' || t->text[0] == '+') &&
2135 !t->text[1]))
2136 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002137
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002138 j = tok_type_(t, needtype);
2139 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002140
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002141 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002142 t = tline = expand_smacro(tline);
2143 while (tok_type_(t, TOK_WHITESPACE))
2144 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002145
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002146 j = false;
2147 if (t) {
2148 t = t->next; /* Skip the actual token */
2149 while (tok_type_(t, TOK_WHITESPACE))
2150 t = t->next;
2151 j = !t; /* Should be nothing left */
2152 }
2153 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002154
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002155 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 t = tline = expand_smacro(tline);
2157 while (tok_type_(t, TOK_WHITESPACE))
2158 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002159
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002160 j = !t; /* Should be empty */
2161 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002162
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002163 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002164 pps.tptr = tline = expand_smacro(tline);
2165 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002166 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002167 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 if (!evalresult)
2169 return -1;
2170 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002171 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002173 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002174 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002175 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002177 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002179
H. Peter Anvine2c80182005-01-15 22:15:51 +00002180 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002181 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002183 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002184
2185 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002186 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002187
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002188fail:
2189 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002190 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002191}
2192
2193/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002194 * Default smacro expansion routine: just returns a copy of the
2195 * expansion list.
2196 */
2197static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002198smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002199{
2200 (void)params;
2201 (void)nparams;
2202
2203 return dup_tlist(s->expansion, NULL);
2204}
2205
2206/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002207 * Emit a macro defintion or undef to the listing file, if
2208 * desired. This is similar to detoken(), but it handles the reverse
2209 * expansion list, does not expand %! or local variable tokens, and
2210 * does some special handling for macro parameters.
2211 */
2212static void
2213list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2214{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002215 Token *t;
2216 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002217 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002218 char *context_prefix = NULL;
2219 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002220
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002221 namelen = strlen(m->name);
2222 size = namelen + 2; /* Include room for space after name + NUL */
2223
2224 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002225 int context_depth = cstk->depth - ctx->depth + 1;
2226 context_prefix =
2227 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2228 ctx->name ? ctx->name : "",
2229 ctx->number, context_depth, "");
2230
2231 context_len = nasm_last_string_len();
2232 memset(context_prefix + context_len - context_depth,
2233 '$', context_depth);
2234 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002235 }
2236
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002237 list_for_each(t, m->expansion)
2238 size += t->text ? t->len : 1;
2239
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002240 if (m->nparam) {
2241 /*
2242 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002243 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002244 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002245 int i;
2246
2247 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002248 for (i = 0; i < m->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002249 size += m->params[i].namelen;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002250 }
2251
2252 def = nasm_malloc(size);
2253 p = def+size;
2254 *--p = '\0';
2255
2256 list_for_each(t, m->expansion) {
2257 if (!t->text) {
2258 *--p = ' ';
2259 } else {
2260 p -= t->len;
2261 memcpy(p, t->text, t->len);
2262 }
2263 }
2264
2265 *--p = ' ';
2266
2267 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002268 int i;
2269
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002270 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002271 for (i = m->nparam-1; i >= 0; i--) {
2272 enum sparmflags flags = m->params[i].flags;
2273 if (flags & SPARM_GREEDY)
2274 *--p = '+';
2275 if (m->params[i].name) {
2276 p -= m->params[i].namelen;
2277 memcpy(p, m->params[i].name, m->params[i].namelen);
2278 }
2279 if (flags & SPARM_NOSTRIP)
2280 *--p = '!';
2281 if (flags & SPARM_STR)
2282 *--p = '&';
2283 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002284 *--p = '=';
2285 *--p = ',';
2286 }
2287 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002288 }
2289
2290 p -= namelen;
2291 memcpy(p, m->name, namelen);
2292
H. Peter Anvin6686de22019-08-10 05:33:14 -07002293 if (context_prefix) {
2294 p -= context_len;
2295 memcpy(p, context_prefix, context_len);
2296 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002297 }
2298
2299 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002300 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002301}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002302
2303/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002304 * Parse smacro arguments, return argument count. If the tmpl argument
2305 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002306 * *tpp is updated to point to the pointer to the first token after the
2307 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002308 *
2309 * The text values from any argument tokens are "stolen" and the
2310 * corresponding text fields set to NULL.
2311 */
2312static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2313{
2314 int nparam = 0;
2315 enum sparmflags flags;
2316 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002317 bool err, done;
2318 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002319 Token **tn = *tpp;
2320 Token *t = *tn;
2321 Token *name;
2322
2323 while (t && t->type == TOK_WHITESPACE) {
2324 tn = &t->next;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002325 t = *tn;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002326 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002327
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002328 if (!tok_is_(t, "("))
2329 goto finish;
2330
2331 if (tmpl) {
2332 Token *tx = t;
2333 Token **txpp = &tx;
2334 int sparam;
2335
2336 /* Count parameters first */
2337 sparam = parse_smacro_template(&txpp, NULL);
2338 if (!sparam)
2339 goto finish; /* No parameters, we're done */
2340 nasm_newn(params, sparam);
2341 }
2342
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002343 /* Skip leading paren */
2344 tn = &t->next;
2345 t = *tn;
2346
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002347 name = NULL;
2348 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002349 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002350
2351 while (!done) {
2352 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002353 if (name || flags)
2354 nasm_nonfatal("`)' expected to terminate macro template");
2355 else
2356 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002357 break;
2358 }
2359
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002360 switch (t->type) {
2361 case TOK_ID:
2362 if (name)
2363 goto bad;
2364 name = t;
2365 break;
2366
2367 case TOK_OTHER:
2368 if (t->text[1])
2369 goto bad;
2370 switch (t->text[0]) {
2371 case '=':
2372 flags |= SPARM_EVAL;
2373 break;
2374 case '&':
2375 flags |= SPARM_STR;
2376 break;
2377 case '!':
2378 flags |= SPARM_NOSTRIP;
2379 break;
2380 case '+':
2381 flags |= SPARM_GREEDY;
2382 greedy = true;
2383 break;
2384 case ',':
2385 if (greedy)
2386 nasm_nonfatal("greedy parameter must be last");
2387 /* fall through */
2388 case ')':
2389 if (params) {
2390 if (name) {
2391 params[nparam].name = name->text;
2392 params[nparam].namelen = name->len;
2393 name->text = NULL;
2394 }
2395 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002396 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002397 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002398 name = NULL;
2399 flags = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002400 done = t->text[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002401 break;
2402 default:
2403 goto bad;
2404 }
2405 break;
2406
2407 case TOK_WHITESPACE:
2408 break;
2409
2410 default:
2411 bad:
2412 if (!err) {
2413 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2414 err = true;
2415 }
2416 break;
2417 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002418
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002419 tn = &t->next;
2420 t = *tn;
2421 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002422
2423finish:
2424 while (t && t->type == TOK_WHITESPACE) {
2425 tn = &t->next;
2426 t = t->next;
2427 }
2428 *tpp = tn;
2429 if (tmpl) {
2430 tmpl->nparam = nparam;
2431 tmpl->greedy = greedy;
2432 tmpl->params = params;
2433 }
2434 return nparam;
2435}
2436
2437/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002438 * Common code for defining an smacro. The tmpl argument, if not NULL,
2439 * contains any macro parameters that aren't explicit arguments;
2440 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002441 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002442static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002443 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002444{
2445 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002446 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002447 Context *ctx;
2448 bool defining_alias = false;
2449 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002450
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002451 if (tmpl) {
2452 defining_alias = tmpl->alias;
2453 nparam = tmpl->nparam;
2454 }
2455
2456 while (1) {
2457 ctx = get_ctx(mname, &mname);
2458
2459 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2460 /* Create a new macro */
2461 smtbl = ctx ? &ctx->localmac : &smacros;
2462 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2463 nasm_new(smac);
2464 smac->next = *smhead;
2465 *smhead = smac;
2466 break;
2467 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002468 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002469 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 /*
2471 * Some instances of the old code considered this a failure,
2472 * some others didn't. What is the right thing to do here?
2473 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002474 goto fail;
2475 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 /*
2477 * We're redefining, so we have to take over an
2478 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002479 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002480 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002481 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002482 break;
2483 } else if (smac->in_progress) {
2484 nasm_nonfatal("macro alias loop");
2485 goto fail;
2486 } else {
2487 /* It is an alias macro; follow the alias link */
2488 SMacro *s;
2489
2490 smac->in_progress = true;
2491 s = define_smacro(smac->expansion->text, casesense,
2492 expansion, tmpl);
2493 smac->in_progress = false;
2494 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002495 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002496 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002497
2498 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002499 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002500 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002501 smac->expand = smacro_expand_default;
2502 if (tmpl) {
2503 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002504 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002505 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002506 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002507 if (tmpl->expand)
2508 smac->expand = tmpl->expand;
2509 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002510 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002511 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2512 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002513 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002514 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002515
2516fail:
2517 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002518 if (tmpl)
2519 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002520 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002521}
2522
2523/*
2524 * Undefine an smacro
2525 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002526static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002527{
2528 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002529 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002530 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002531
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002532 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002533 smtbl = ctx ? &ctx->localmac : &smacros;
2534 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002535
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002536 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 /*
2538 * We now have a macro name... go hunt for it.
2539 */
2540 sp = smhead;
2541 while ((s = *sp) != NULL) {
2542 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002543 if (s->alias && !undefalias) {
2544 if (s->in_progress) {
2545 nasm_nonfatal("macro alias loop");
2546 } else {
2547 s->in_progress = true;
2548 undef_smacro(s->expansion->text, false);
2549 s->in_progress = false;
2550 }
2551 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002552 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002553 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2554 ctx, s);
2555 *sp = s->next;
2556 free_smacro(s);
2557 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 } else {
2559 sp = &s->next;
2560 }
2561 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002562 }
2563}
2564
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002565/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002566 * Parse a mmacro specification.
2567 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002568static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002569{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002570 tline = tline->next;
2571 skip_white_(tline);
2572 tline = expand_id(tline);
2573 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002574 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002576 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002577
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002578#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002579 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002580#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002581 def->name = nasm_strdup(tline->text);
2582 def->plus = false;
2583 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002584 def->nparam_min = 0;
2585 def->nparam_max = 0;
2586
H. Peter Anvina26433d2008-07-16 14:40:01 -07002587 tline = expand_smacro(tline->next);
2588 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002589 if (!tok_type_(tline, TOK_NUMBER))
2590 nasm_nonfatal("`%s' expects a parameter count", directive);
2591 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002592 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002593 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 tline = tline->next->next;
2595 if (tok_is_(tline, "*")) {
2596 def->nparam_max = INT_MAX;
2597 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002598 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002600 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002602 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002603 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 }
2605 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002606 }
2607 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 tline = tline->next;
2609 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002610 }
2611 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 !nasm_stricmp(tline->next->text, ".nolist")) {
2613 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002614 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002615 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616
H. Peter Anvina26433d2008-07-16 14:40:01 -07002617 /*
2618 * Handle default parameters.
2619 */
2620 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 def->dlist = tline->next;
2622 tline->next = NULL;
2623 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002624 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 def->dlist = NULL;
2626 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002627 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002628 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002629
H. Peter Anvin89cee572009-07-15 09:16:54 -04002630 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002631 !def->plus) {
2632 /*
2633 *!macro-defaults [on] macros with more default than optional parameters
2634 *! warns when a macro has more default parameters than optional parameters.
2635 *! See \k{mlmacdef} for why might want to disable this warning.
2636 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002637 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002638 "too many default macro parameters in macro `%s'", def->name);
2639 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002640
H. Peter Anvina26433d2008-07-16 14:40:01 -07002641 return true;
2642}
2643
2644
2645/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002646 * Decode a size directive
2647 */
2648static int parse_size(const char *str) {
2649 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002651 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002653 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002654}
2655
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002656/*
2657 * Process a preprocessor %pragma directive. Currently there are none.
2658 * Gets passed the token list starting with the "preproc" token from
2659 * "%pragma preproc".
2660 */
2661static void do_pragma_preproc(Token *tline)
2662{
2663 /* Skip to the real stuff */
2664 tline = tline->next;
2665 skip_white_(tline);
2666 if (!tline)
2667 return;
2668
2669 (void)tline; /* Nothing else to do at present */
2670}
2671
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002672static bool is_macro_id(const Token *t)
2673{
2674 return t && (t->type == TOK_ID ||
2675 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2676}
2677
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002678static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002679{
2680 char *id;
2681 Token *t = *tp;
2682
2683 t = t->next; /* Skip directive */
2684 skip_white_(t);
2685 t = expand_id(t);
2686
2687 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002688 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002689 err ? err : "macro identifier");
2690 return NULL;
2691 }
2692
2693 id = t->text;
2694 skip_white_(t);
2695 *tp = t;
2696 return id;
2697}
2698
Ed Beroset3ab3f412002-06-11 03:31:49 +00002699/**
2700 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002701 * Find out if a line contains a preprocessor directive, and deal
2702 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002703 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002704 * If a directive _is_ found, it is the responsibility of this routine
2705 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002706 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002707 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002708 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002709 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002710 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002711 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002712static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002713{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002714 enum preproc_token i;
2715 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002716 bool err;
2717 int nparam;
2718 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002719 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002720 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002721 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002722 char *p, *pp;
2723 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002724 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002725 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002726 Include *inc;
2727 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002728 Cond *cond;
2729 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002730 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002731 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002732 struct tokenval tokval;
2733 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002734 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002735 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002736 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002737 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002738
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002739 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002740 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002741
H. Peter Anvineba20a72002-04-30 20:53:55 +00002742 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002743 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002744 (tline->text[0] && (tline->text[1] == '%' ||
2745 tline->text[1] == '$' ||
2746 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002748
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002749 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002750 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002751
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002752 casesense = true;
2753 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2754 casesense = false;
2755 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002756 }
2757
2758 /*
2759 * If we're in a non-emitting branch of a condition construct,
2760 * or walking to the end of an already terminated %rep block,
2761 * we should ignore all directives except for condition
2762 * directives.
2763 */
2764 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002765 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2766 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002767 return NO_DIRECTIVE_FOUND;
2768 }
2769
2770 /*
2771 * If we're defining a macro or reading a %rep block, we should
2772 * ignore all directives except for %macro/%imacro (which nest),
2773 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2774 * If we're in a %rep block, another %rep nests, so should be let through.
2775 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002776 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002777 i != PP_ENDMACRO && i != PP_ENDM &&
2778 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2779 return NO_DIRECTIVE_FOUND;
2780 }
2781
2782 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002783 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002784 nested_mac_count++;
2785 return NO_DIRECTIVE_FOUND;
2786 } else if (nested_mac_count > 0) {
2787 if (i == PP_ENDMACRO) {
2788 nested_mac_count--;
2789 return NO_DIRECTIVE_FOUND;
2790 }
2791 }
2792 if (!defining->name) {
2793 if (i == PP_REP) {
2794 nested_rep_count++;
2795 return NO_DIRECTIVE_FOUND;
2796 } else if (nested_rep_count > 0) {
2797 if (i == PP_ENDREP) {
2798 nested_rep_count--;
2799 return NO_DIRECTIVE_FOUND;
2800 }
2801 }
2802 }
2803 }
2804
H. Peter Anvin4169a472007-09-12 01:29:43 +00002805 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002806 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002807 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002809
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002810 case PP_PRAGMA:
2811 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002812 * %pragma namespace options...
2813 *
2814 * The namespace "preproc" is reserved for the preprocessor;
2815 * all other namespaces generate a [pragma] assembly directive.
2816 *
2817 * Invalid %pragmas are ignored and may have different
2818 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002819 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002820 t = tline;
2821 tline = tline->next;
2822 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002823 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002824 while (tok_type_(tline, TOK_WHITESPACE)) {
2825 t = tline;
2826 tline = tline->next;
2827 delete_Token(t);
2828 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002829 if (tok_type_(tline, TOK_ID)) {
2830 if (!nasm_stricmp(tline->text, "preproc")) {
2831 /* Preprocessor pragma */
2832 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002833 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002834 } else {
2835 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002836
2837 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002838 for (t = tline; t->next; t = t->next)
2839 ;
2840 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002841
2842 /* Prepend "[pragma " */
2843 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2844 t = new_Token(t, TOK_ID, "pragma", 6);
2845 t = new_Token(t, TOK_OTHER, "[", 1);
2846 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002847 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002848 }
2849 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002850 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002851
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 case PP_STACKSIZE:
2853 /* Directive to tell NASM what the default stack size is. The
2854 * default is for a 16-bit stack, and this can be overriden with
2855 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 */
2857 tline = tline->next;
2858 if (tline && tline->type == TOK_WHITESPACE)
2859 tline = tline->next;
2860 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002861 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 }
2863 if (nasm_stricmp(tline->text, "flat") == 0) {
2864 /* All subsequent ARG directives are for a 32-bit stack */
2865 StackSize = 4;
2866 StackPointer = "ebp";
2867 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002868 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002869 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2870 /* All subsequent ARG directives are for a 64-bit stack */
2871 StackSize = 8;
2872 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002873 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002874 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 } else if (nasm_stricmp(tline->text, "large") == 0) {
2876 /* All subsequent ARG directives are for a 16-bit stack,
2877 * far function call.
2878 */
2879 StackSize = 2;
2880 StackPointer = "bp";
2881 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002882 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 } else if (nasm_stricmp(tline->text, "small") == 0) {
2884 /* All subsequent ARG directives are for a 16-bit stack,
2885 * far function call. We don't support near functions.
2886 */
2887 StackSize = 2;
2888 StackPointer = "bp";
2889 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002890 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002891 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002892 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002894 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002895
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 case PP_ARG:
2897 /* TASM like ARG directive to define arguments to functions, in
2898 * the following form:
2899 *
2900 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2901 */
2902 offset = ArgOffset;
2903 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002904 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002906
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 /* Find the argument name */
2908 tline = tline->next;
2909 if (tline && tline->type == TOK_WHITESPACE)
2910 tline = tline->next;
2911 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002912 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002913 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 }
2915 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002916
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 /* Find the argument size type */
2918 tline = tline->next;
2919 if (!tline || tline->type != TOK_OTHER
2920 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002921 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002922 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002923 }
2924 tline = tline->next;
2925 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002926 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002927 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002929
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002931 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002933 size = parse_size(tt->text);
2934 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002935 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002937 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 }
2939 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002940
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002941 /* Round up to even stack slots */
2942 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002943
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 /* Now define the macro for the argument */
2945 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2946 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002947 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002948 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002949
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 /* Move to the next argument in the list */
2951 tline = tline->next;
2952 if (tline && tline->type == TOK_WHITESPACE)
2953 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002954 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002955 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002956 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002957
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 case PP_LOCAL:
2959 /* TASM like LOCAL directive to define local variables for a
2960 * function, in the following form:
2961 *
2962 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2963 *
2964 * The '= LocalSize' at the end is ignored by NASM, but is
2965 * required by TASM to define the local parameter size (and used
2966 * by the TASM macro package).
2967 */
2968 offset = LocalOffset;
2969 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002970 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002972
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 /* Find the argument name */
2974 tline = tline->next;
2975 if (tline && tline->type == TOK_WHITESPACE)
2976 tline = tline->next;
2977 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002978 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002979 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 }
2981 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002982
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 /* Find the argument size type */
2984 tline = tline->next;
2985 if (!tline || tline->type != TOK_OTHER
2986 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002987 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002988 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 }
2990 tline = tline->next;
2991 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002992 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002993 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002995
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002997 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002999 size = parse_size(tt->text);
3000 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003001 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003003 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 }
3005 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003006
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003007 /* Round up to even stack slots */
3008 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003009
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003010 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003011
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003012 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3014 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003015 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003016
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 /* Now define the assign to setup the enter_c macro correctly */
3018 snprintf(directive, sizeof(directive),
3019 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003020 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003021
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 /* Move to the next argument in the list */
3023 tline = tline->next;
3024 if (tline && tline->type == TOK_WHITESPACE)
3025 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003026 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003027 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003028 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003029
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 case PP_CLEAR:
3031 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003032 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003033 free_macros();
3034 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003035 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003036
H. Peter Anvin418ca702008-05-30 10:42:30 -07003037 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003038 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003039 skip_white_(t);
3040 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003041 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003042 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003043 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003044 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003045 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003046 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003047 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003048 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003049 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003050 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003051 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003052
3053 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003054 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003055 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003056
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003057 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003058 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003059 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003060 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003061 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003062 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003063 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003064 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003065 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003066 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003067 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003069 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003070 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003071 (pp_mode == PP_DEPS)
3072 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003073 if (!inc->fp) {
3074 /* -MG given but file not found */
3075 nasm_free(inc);
3076 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003077 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003078 inc->lineno = src_set_linnum(0);
3079 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003080 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003081 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003082 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003083 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003084 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003085
H. Peter Anvind2456592008-06-19 15:04:18 -07003086 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003087 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003088 const struct use_package *pkg;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003089
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003090 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003091 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07003092 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003093 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003094 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003095 nasm_unquote_cstr(tline->text, NULL);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003096 pkg = nasm_find_use_package(tline->text);
3097 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003098 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003099 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003100 /*
3101 * Not already included, go ahead and include it.
3102 * Treat it as an include file for the purpose of
3103 * producing a listing.
3104 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003105 use_loaded[pkg->index] = true;
3106 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003107 nasm_new(inc);
3108 inc->next = istk;
3109 inc->fname = src_set_fname(NULL);
3110 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003111 inc->nolist = !list_option('b') || istk->nolist;
3112 istk = inc;
3113 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003114 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003115 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003116 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003119 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 tline = tline->next;
3121 skip_white_(tline);
3122 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003123 if (tline) {
3124 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003125 nasm_nonfatal("`%s' expects a context identifier",
3126 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003127 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003128 }
3129 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003130 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003131 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 p = nasm_strdup(tline->text);
3133 } else {
3134 p = NULL; /* Anonymous */
3135 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003136
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003137 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003138 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003139 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 ctx->name = p;
3142 ctx->number = unique++;
3143 cstk = ctx;
3144 } else {
3145 /* %pop or %repl */
3146 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003147 nasm_nonfatal("`%s': context stack is empty",
3148 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 } else if (i == PP_POP) {
3150 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003151 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003152 "expected %s",
3153 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 else
3155 ctx_pop();
3156 } else {
3157 /* i == PP_REPL */
3158 nasm_free(cstk->name);
3159 cstk->name = p;
3160 p = NULL;
3161 }
3162 nasm_free(p);
3163 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003164 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003165 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 severity = ERR_FATAL;
3167 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003169 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003171 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003172 /*!
3173 *!user [on] %warning directives
3174 *! controls output of \c{%warning} directives (see \k{pperror}).
3175 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003176 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003177 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003178
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003180 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182 tline->next = expand_smacro(tline->next);
3183 tline = tline->next;
3184 skip_white_(tline);
3185 t = tline ? tline->next : NULL;
3186 skip_white_(t);
3187 if (tok_type_(tline, TOK_STRING) && !t) {
3188 /* The line contains only a quoted string */
3189 p = tline->text;
3190 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003191 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 } else {
3193 /* Not a quoted string, or more than a quoted string */
3194 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003195 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003196 nasm_free(p);
3197 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003198 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003199 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003200
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003201 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003202 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003203 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003204 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003205 j = if_condition(tline->next, i);
3206 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003207 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003208 cond = nasm_malloc(sizeof(Cond));
3209 cond->next = istk->conds;
3210 cond->state = j;
3211 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003212 if(istk->mstk.mstk)
3213 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003214 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003215
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003216 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003217 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003218 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003219 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003220 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003221 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003222 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003223
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003224 case COND_DONE:
3225 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003226 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003227
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 case COND_ELSE_TRUE:
3229 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003230 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003231 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003232 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003234
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 case COND_IF_FALSE:
3236 /*
3237 * IMPORTANT: In the case of %if, we will already have
3238 * called expand_mmac_params(); however, if we're
3239 * processing an %elif we must have been in a
3240 * non-emitting mode, which would have inhibited
3241 * the normal invocation of expand_mmac_params().
3242 * Therefore, we have to do it explicitly here.
3243 */
3244 j = if_condition(expand_mmac_params(tline->next), i);
3245 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003246 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003247 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003249 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003250
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 case PP_ELSE:
3252 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003253 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003254 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003255 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003256 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003257 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003258 case COND_IF_TRUE:
3259 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003260 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003261 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003262
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003263 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003264 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003265
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003266 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003267 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003268 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003269
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003270 case COND_ELSE_TRUE:
3271 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003272 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003273 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003274 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003276 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003277 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003278
H. Peter Anvine2c80182005-01-15 22:15:51 +00003279 case PP_ENDIF:
3280 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003281 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003282 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003283 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003284 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003285 cond = istk->conds;
3286 istk->conds = cond->next;
3287 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003288 if(istk->mstk.mstk)
3289 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003290 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003291
H. Peter Anvin8b262472019-02-26 14:00:54 -08003292 case PP_RMACRO:
3293 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003294 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003295 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003296 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003297 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003298 if (i == PP_RMACRO)
3299 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003300 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003301 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003302 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003303 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003304
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003305 src_get(&defining->xline, &defining->fname);
3306
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003307 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3308 while (mmac) {
3309 if (!strcmp(mmac->name, defining->name) &&
3310 (mmac->nparam_min <= defining->nparam_max
3311 || defining->plus)
3312 && (defining->nparam_min <= mmac->nparam_max
3313 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003314 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003315 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003316 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003317 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003318 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003319 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003320 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003321
H. Peter Anvine2c80182005-01-15 22:15:51 +00003322 case PP_ENDM:
3323 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003324 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003325 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003326 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003327 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003328 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3329 defining->next = *mmhead;
3330 *mmhead = defining;
3331 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003332 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003333
H. Peter Anvin89cee572009-07-15 09:16:54 -04003334 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003335 /*
3336 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003337 * macro-end marker for a macro with a name. Then we
3338 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003339 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003340 list_for_each(l, istk->expansion)
3341 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003342 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003343
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003344 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003345 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003346 * Remove all conditional entries relative to this
3347 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003348 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003349 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3350 cond = istk->conds;
3351 istk->conds = cond->next;
3352 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003353 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003354 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003355 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003356 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003357 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003358 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003359
H. Peter Anvina26433d2008-07-16 14:40:01 -07003360 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003361 casesense = false;
3362 /* fall through */
3363 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003364 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003365 MMacro **mmac_p;
3366 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003367
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003368 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003369 spec.casesense = casesense;
3370 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003371 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003373 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3374 while (mmac_p && *mmac_p) {
3375 mmac = *mmac_p;
3376 if (mmac->casesense == spec.casesense &&
3377 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3378 mmac->nparam_min == spec.nparam_min &&
3379 mmac->nparam_max == spec.nparam_max &&
3380 mmac->plus == spec.plus) {
3381 *mmac_p = mmac->next;
3382 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003384 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003385 }
3386 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003387 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003388 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003389 }
3390
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 case PP_ROTATE:
3392 if (tline->next && tline->next->type == TOK_WHITESPACE)
3393 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003394 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003396 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 return DIRECTIVE_FOUND;
3398 }
3399 t = expand_smacro(tline->next);
3400 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003401 pps.tptr = tline = t;
3402 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 tokval.t_type = TOKEN_INVALID;
3404 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003405 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 free_tlist(tline);
3407 if (!evalresult)
3408 return DIRECTIVE_FOUND;
3409 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003410 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003412 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003413 return DIRECTIVE_FOUND;
3414 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003415 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003416 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003417 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003418 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003419 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003420 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003421 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003422
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003423 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003424 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003425 rotate += mmac->nparam;
3426
3427 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003428 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003429 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003430
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003432 {
3433 MMacro *tmp_defining;
3434
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003435 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436 do {
3437 tline = tline->next;
3438 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003439
H. Peter Anvine2c80182005-01-15 22:15:51 +00003440 if (tok_type_(tline, TOK_ID) &&
3441 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003442 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003443 do {
3444 tline = tline->next;
3445 } while (tok_type_(tline, TOK_WHITESPACE));
3446 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003447
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003449 pps.tptr = expand_smacro(tline);
3450 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003452 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003454 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003455 if (!evalresult)
3456 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003458 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003460 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003461 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003463 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003464 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003465 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3466 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003467 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003468 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003469 /*!
3470 *!negative-rep [on] regative %rep count
3471 *! warns about negative counts given to the \c{%rep}
3472 *! preprocessor directive.
3473 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003474 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003475 "negative `%%rep' count: %"PRId64, count);
3476 count = 0;
3477 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003478 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003479 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003480 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003481 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003482 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003484 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003485 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003486 defining->nolist = nolist;
3487 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003488 defining->mstk = istk->mstk;
3489 defining->dstk.mstk = tmp_defining;
3490 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003491 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003492 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003493 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003496 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003497 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003498 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 /*
3502 * Now we have a "macro" defined - although it has no name
3503 * and we won't be entering it in the hash tables - we must
3504 * push a macro-end marker for it on to istk->expansion.
3505 * After that, it will take care of propagating itself (a
3506 * macro-end marker line for a macro which is really a %rep
3507 * block will cause the macro to be re-expanded, complete
3508 * with another macro-end marker to ensure the process
3509 * continues) until the whole expansion is forcibly removed
3510 * from istk->expansion by a %exitrep.
3511 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003512 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003513 l->next = istk->expansion;
3514 l->finishes = defining;
3515 l->first = NULL;
3516 istk->expansion = l;
3517
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003518 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003519
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003520 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003521 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003522 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003523
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003525 /*
3526 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003527 * macro-end marker for a macro with no name. Then we set
3528 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003529 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003530 list_for_each(l, istk->expansion)
3531 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003532 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003533
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003534 if (l)
3535 l->finishes->in_progress = 1;
3536 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003537 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003538 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003539
H. Peter Anvin8b262472019-02-26 14:00:54 -08003540 case PP_DEFINE:
3541 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003542 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003543 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003544 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003545 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003546
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003547 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003548 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003550 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003551 lastp = &tline->next;
3552 nparam = parse_smacro_template(&lastp, &tmpl);
3553 tline = *lastp;
3554 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003555
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003556 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003557 macro_start = tline;
3558 if (!is_macro_id(macro_start)) {
3559 nasm_nonfatal("`%s' expects a macro identifier to alias",
3560 dname);
3561 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003563 tt = macro_start->next;
3564 macro_start->next = NULL;
3565 tline = tline->next;
3566 skip_white_(tline);
3567 if (tline && tline->type) {
3568 nasm_warn(WARN_OTHER,
3569 "trailing garbage after aliasing identifier ignored");
3570 }
3571 free_tlist(tt);
3572 tmpl.alias = true;
3573 } else {
3574 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003575 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003576 tline = expand_smacro(tline);
3577
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003578 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003579 macro_start = NULL;
3580 t = tline;
3581 while (t) {
3582 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003583 for (i = 0; i < nparam; i++) {
3584 if ((size_t)tmpl.params[i].namelen == t->len &&
3585 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3586 t->type = tok_smac_param(i);
3587 break;
3588 }
3589 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003590 }
3591 tt = t->next;
3592 t->next = macro_start;
3593 macro_start = t;
3594 t = tt;
3595 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 /*
3599 * Good. We now have a macro name, a parameter count, and a
3600 * token list (in reverse order) for an expansion. We ought
3601 * to be OK just to create an SMacro, store it, and let
3602 * free_tlist have the rest of the line (which we have
3603 * carefully re-terminated after chopping off the expansion
3604 * from the end).
3605 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003606 define_smacro(mname, casesense, macro_start, &tmpl);
3607 break;
3608 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003609
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003611 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003612 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003613 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003614 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003615 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003616
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003617 undef_smacro(mname, i == PP_UNDEFALIAS);
3618 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003619
H. Peter Anvin8b262472019-02-26 14:00:54 -08003620 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003621 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003622 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003623
H. Peter Anvin9e200162008-06-04 17:23:14 -07003624 last = tline;
3625 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003626 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003627
3628 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003629 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003630
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003631 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003632 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003633 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003634
3635 /*
3636 * We now have a macro name, an implicit parameter count of
3637 * zero, and a string token to use as an expansion. Create
3638 * and store an SMacro.
3639 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003640 define_smacro(mname, casesense, macro_start, NULL);
3641 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003642
H. Peter Anvin8b262472019-02-26 14:00:54 -08003643 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003644 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003645 goto done;
3646
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003647 last = tline;
3648 tline = expand_smacro(tline->next);
3649 last->next = NULL;
3650
3651 t = tline;
3652 while (tok_type_(t, TOK_WHITESPACE))
3653 t = t->next;
3654 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003655 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003656 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003657 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003658 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003659 }
3660
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003661 /*
3662 * Convert the string to a token stream. Note that smacros
3663 * are stored with the token stream reversed, so we have to
3664 * reverse the output of tokenize().
3665 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003666 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003667 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003668
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003669 /*
3670 * We now have a macro name, an implicit parameter count of
3671 * zero, and a numeric token to use as an expansion. Create
3672 * and store an SMacro.
3673 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003674 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003675 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003676 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003677
H. Peter Anvin418ca702008-05-30 10:42:30 -07003678 case PP_PATHSEARCH:
3679 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003680 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003681
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003682 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003683 goto done;
3684
H. Peter Anvin418ca702008-05-30 10:42:30 -07003685 last = tline;
3686 tline = expand_smacro(tline->next);
3687 last->next = NULL;
3688
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003689 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003690 while (tok_type_(t, TOK_WHITESPACE))
3691 t = t->next;
3692
3693 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003694 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003695 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003696 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003697 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003698 }
3699 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003700 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003701 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003702 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003703 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003704
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003705 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003706 if (!found_path)
3707 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003708 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003709
3710 /*
3711 * We now have a macro name, an implicit parameter count of
3712 * zero, and a string token to use as an expansion. Create
3713 * and store an SMacro.
3714 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003715 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003716 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003717 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003718 }
3719
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003721 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003722 goto done;
3723
H. Peter Anvine2c80182005-01-15 22:15:51 +00003724 last = tline;
3725 tline = expand_smacro(tline->next);
3726 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003727
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 t = tline;
3729 while (tok_type_(t, TOK_WHITESPACE))
3730 t = t->next;
3731 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003732 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003733 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 free_tlist(tline);
3735 free_tlist(origline);
3736 return DIRECTIVE_FOUND;
3737 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003738
H. Peter Anvin8b262472019-02-26 14:00:54 -08003739 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003740
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 /*
3742 * We now have a macro name, an implicit parameter count of
3743 * zero, and a numeric token to use as an expansion. Create
3744 * and store an SMacro.
3745 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003746 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747 free_tlist(tline);
3748 free_tlist(origline);
3749 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003750
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003751 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003752 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003753 goto done;
3754
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003755 last = tline;
3756 tline = expand_smacro(tline->next);
3757 last->next = NULL;
3758
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003759 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003760 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003761 switch (t->type) {
3762 case TOK_WHITESPACE:
3763 break;
3764 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003765 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003766 break;
3767 case TOK_OTHER:
3768 if (!strcmp(t->text, ",")) /* permit comma separators */
3769 break;
3770 /* else fall through */
3771 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003772 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003773 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003774 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003775 }
3776 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003777
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003778 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003779 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003780 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003781 memcpy(p, t->text, t->len);
3782 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003783 }
3784 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003785
3786 /*
3787 * We now have a macro name, an implicit parameter count of
3788 * zero, and a numeric token to use as an expansion. Create
3789 * and store an SMacro.
3790 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003791 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003792 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003793 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003794 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003795 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003796
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003798 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003799 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003800 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003801
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003802 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003803 goto done;
3804
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 last = tline;
3806 tline = expand_smacro(tline->next);
3807 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003808
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003809 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003810 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003811 while (tok_type_(t, TOK_WHITESPACE))
3812 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003813
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003815 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003816 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003818 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003819 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003820
H. Peter Anvin8b262472019-02-26 14:00:54 -08003821 pps.tptr = t->next;
3822 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003823 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003824 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003825 if (!evalresult) {
3826 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003827 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003828 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003829 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003830 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003831 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003832 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003833 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003834
H. Peter Anvin8b262472019-02-26 14:00:54 -08003835 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3836 pps.tptr = pps.tptr->next;
3837 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003838 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003839 } else {
3840 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003841 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003842 if (!evalresult) {
3843 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003844 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003846 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003847 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003848 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003849 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003850 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003851 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003852
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003853 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003854
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003855 /* make start and count being in range */
3856 if (start < 0)
3857 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003858 if (count < 0)
3859 count = len + count + 1 - start;
3860 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003861 count = len - start;
3862 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003863 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003864
H. Peter Anvin8b262472019-02-26 14:00:54 -08003865 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003866 macro_start->len = count;
3867 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3868 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003869
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870 /*
3871 * We now have a macro name, an implicit parameter count of
3872 * zero, and a numeric token to use as an expansion. Create
3873 * and store an SMacro.
3874 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003875 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003876 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003877 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003878 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003879
H. Peter Anvin8b262472019-02-26 14:00:54 -08003880 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003881 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003882 goto done;
3883
H. Peter Anvine2c80182005-01-15 22:15:51 +00003884 last = tline;
3885 tline = expand_smacro(tline->next);
3886 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003887
H. Peter Anvin8b262472019-02-26 14:00:54 -08003888 pps.tptr = tline;
3889 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003890 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003891 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003893 if (!evalresult)
3894 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003895
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003897 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003898
H. Peter Anvine2c80182005-01-15 22:15:51 +00003899 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003900 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003901 free_tlist(origline);
3902 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003903 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003904
H. Peter Anvin8b262472019-02-26 14:00:54 -08003905 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003906
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 /*
3908 * We now have a macro name, an implicit parameter count of
3909 * zero, and a numeric token to use as an expansion. Create
3910 * and store an SMacro.
3911 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003912 define_smacro(mname, casesense, macro_start, NULL);
3913 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003914
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 case PP_LINE:
3916 /*
3917 * Syntax is `%line nnn[+mmm] [filename]'
3918 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003919 if (unlikely(pp_noline))
3920 goto done;
3921
H. Peter Anvine2c80182005-01-15 22:15:51 +00003922 tline = tline->next;
3923 skip_white_(tline);
3924 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003925 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003926 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003928 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003929 m = 1;
3930 tline = tline->next;
3931 if (tok_is_(tline, "+")) {
3932 tline = tline->next;
3933 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003934 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003935 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003937 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003938 tline = tline->next;
3939 }
3940 skip_white_(tline);
3941 src_set_linnum(k);
3942 istk->lineinc = m;
3943 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003944 char *fname = detoken(tline, false);
3945 src_set_fname(fname);
3946 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003948 break;
3949 }
3950
3951done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 free_tlist(origline);
3953 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003954}
3955
3956/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003957 * Ensure that a macro parameter contains a condition code and
3958 * nothing else. Return the condition code index if so, or -1
3959 * otherwise.
3960 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003961static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003962{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003963 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003964
H. Peter Anvin25a99342007-09-22 17:45:45 -07003965 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003966 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003967
H. Peter Anvineba20a72002-04-30 20:53:55 +00003968 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003969 if (!t)
3970 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003972 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003973 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003974 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003975 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003977
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003978 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003979}
3980
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003981/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003982 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003983 * pasting, if @handle_explicit passed then explicit pasting
3984 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003985 * The @m array can contain a series of token types which are
3986 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003987 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003988static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003989 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003990{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003991 Token *tok, *next, **prev_next, **prev_nonspace;
3992 bool pasted = false;
3993 char *buf, *p;
3994 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003995
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003996 /*
3997 * The last token before pasting. We need it
3998 * to be able to connect new handled tokens.
3999 * In other words if there were a tokens stream
4000 *
4001 * A -> B -> C -> D
4002 *
4003 * and we've joined tokens B and C, the resulting
4004 * stream should be
4005 *
4006 * A -> BC -> D
4007 */
4008 tok = *head;
4009 prev_next = NULL;
4010
4011 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
4012 prev_nonspace = head;
4013 else
4014 prev_nonspace = NULL;
4015
4016 while (tok && (next = tok->next)) {
4017
4018 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004019 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004020 /* Zap redundant whitespaces */
4021 while (tok_type_(next, TOK_WHITESPACE))
4022 next = delete_Token(next);
4023 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004024 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004025
4026 case TOK_PASTE:
4027 /* Explicit pasting */
4028 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004029 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004030 next = delete_Token(tok);
4031
4032 while (tok_type_(next, TOK_WHITESPACE))
4033 next = delete_Token(next);
4034
4035 if (!pasted)
4036 pasted = true;
4037
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004038 /* Left pasting token is start of line */
4039 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004040 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004041
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004042 /*
4043 * No ending token, this might happen in two
4044 * cases
4045 *
4046 * 1) There indeed no right token at all
4047 * 2) There is a bare "%define ID" statement,
4048 * and @ID does expand to whitespace.
4049 *
4050 * So technically we need to do a grammar analysis
4051 * in another stage of parsing, but for now lets don't
4052 * change the behaviour people used to. Simply allow
4053 * whitespace after paste token.
4054 */
4055 if (!next) {
4056 /*
4057 * Zap ending space tokens and that's all.
4058 */
4059 tok = (*prev_nonspace)->next;
4060 while (tok_type_(tok, TOK_WHITESPACE))
4061 tok = delete_Token(tok);
4062 tok = *prev_nonspace;
4063 tok->next = NULL;
4064 break;
4065 }
4066
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004067 tok = *prev_nonspace;
4068 while (tok_type_(tok, TOK_WHITESPACE))
4069 tok = delete_Token(tok);
4070 len = strlen(tok->text);
4071 len += strlen(next->text);
4072
4073 p = buf = nasm_malloc(len + 1);
4074 strcpy(p, tok->text);
4075 p = strchr(p, '\0');
4076 strcpy(p, next->text);
4077
4078 delete_Token(tok);
4079
4080 tok = tokenize(buf);
4081 nasm_free(buf);
4082
4083 *prev_nonspace = tok;
4084 while (tok && tok->next)
4085 tok = tok->next;
4086
4087 tok->next = delete_Token(next);
4088
4089 /* Restart from pasted tokens head */
4090 tok = *prev_nonspace;
4091 break;
4092
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004093 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004094 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004095 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004096 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4097 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004098
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004099 len = 0;
4100 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4101 len += strlen(next->text);
4102 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004103 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004104
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004105 /* No match or no text to process */
4106 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004107 break;
4108
4109 len += strlen(tok->text);
4110 p = buf = nasm_malloc(len + 1);
4111
Adam Majer1a069432017-07-25 11:12:35 +02004112 strcpy(p, tok->text);
4113 p = strchr(p, '\0');
4114 tok = delete_Token(tok);
4115
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004116 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004117 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4118 strcpy(p, tok->text);
4119 p = strchr(p, '\0');
4120 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004121 tok = delete_Token(tok);
4122 }
4123
4124 tok = tokenize(buf);
4125 nasm_free(buf);
4126
4127 if (prev_next)
4128 *prev_next = tok;
4129 else
4130 *head = tok;
4131
4132 /*
4133 * Connect pasted into original stream,
4134 * ie A -> new-tokens -> B
4135 */
4136 while (tok && tok->next)
4137 tok = tok->next;
4138 tok->next = next;
4139
4140 if (!pasted)
4141 pasted = true;
4142
4143 /* Restart from pasted tokens head */
4144 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004145 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004146
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004147 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004148 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004149
4150 prev_next = &tok->next;
4151
4152 if (tok->next &&
4153 !tok_type_(tok->next, TOK_WHITESPACE) &&
4154 !tok_type_(tok->next, TOK_PASTE))
4155 prev_nonspace = prev_next;
4156
4157 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004158 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004159
4160 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004161}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004162
4163/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004164 * Computes the proper rotation of mmacro parameters
4165 */
4166static int mmac_rotate(const MMacro *mac, unsigned int n)
4167{
4168 if (--n < mac->nparam)
4169 n = (n + mac->rotate) % mac->nparam;
4170
4171 return n+1;
4172}
4173
4174/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004175 * expands to a list of tokens from %{x:y}
4176 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004177static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004178{
4179 Token *t = tline, **tt, *tm, *head;
4180 char *pos;
4181 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004182
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004183 pos = strchr(tline->text, ':');
4184 nasm_assert(pos);
4185
4186 lst = atoi(pos + 1);
4187 fst = atoi(tline->text + 1);
4188
4189 /*
4190 * only macros params are accounted so
4191 * if someone passes %0 -- we reject such
4192 * value(s)
4193 */
4194 if (lst == 0 || fst == 0)
4195 goto err;
4196
4197 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004198 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4199 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004200 goto err;
4201
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004202 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4203 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004205 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004206 fst--, lst--;
4207
4208 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004209 * It will be at least one token. Note we
4210 * need to scan params until separator, otherwise
4211 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004212 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004213 j = (fst + mac->rotate) % mac->nparam;
4214 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004215 if (!tm)
4216 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004217 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004218 tt = &head->next, tm = tm->next;
4219 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004220 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004221 *tt = t, tt = &t->next, tm = tm->next;
4222 }
4223
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004224 if (fst < lst) {
4225 for (i = fst + 1; i <= lst; i++) {
4226 t = new_Token(NULL, TOK_OTHER, ",", 0);
4227 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004228 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004229 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004230 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004231 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004232 *tt = t, tt = &t->next, tm = tm->next;
4233 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004234 }
4235 } else {
4236 for (i = fst - 1; i >= lst; i--) {
4237 t = new_Token(NULL, TOK_OTHER, ",", 0);
4238 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004239 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004240 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004241 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004242 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004243 *tt = t, tt = &t->next, tm = tm->next;
4244 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 }
4246 }
4247
4248 *last = tt;
4249 return head;
4250
4251err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004252 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004253 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004254 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004255}
4256
H. Peter Anvin76690a12002-04-30 20:52:49 +00004257/*
4258 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004259 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004260 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004261 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004262static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004263{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004264 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004265 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004266 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004267
4268 tail = &thead;
4269 thead = NULL;
4270
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004272 bool change;
4273 Token *t = tline;
4274 char *text = t->text;
4275 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004276
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004277 tline = tline->next;
4278 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004279
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004280 switch (type) {
4281 case TOK_PREPROC_ID:
4282 {
4283 Token *tt = NULL;
4284
4285 change = false;
4286
4287 if (!text || !text[0])
4288 break;
4289 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4290 ((text[1] == '+' || text[1] == '-') && text[2])))
4291 break;
4292
4293 change = true;
4294
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004295 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004296 nasm_nonfatal("`%s': not in a macro call", text);
4297 text = NULL;
4298 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004299 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004300
4301 if (strchr(text, ':')) {
4302 /*
4303 * seems we have a parameters range here
4304 */
4305 Token *head, **last;
4306 head = expand_mmac_params_range(mac, t, &last);
4307 if (head) {
4308 *tail = head;
4309 *last = tline;
4310 text = NULL;
4311 }
4312 break;
4313 }
4314
4315 switch (text[1]) {
4316 /*
4317 * We have to make a substitution of one of the
4318 * forms %1, %-1, %+1, %%foo, %0, %00.
4319 */
4320 case '0':
4321 if (!text[2]) {
4322 type = TOK_NUMBER;
4323 text = nasm_asprintf("%d", mac->nparam);
4324 break;
4325 }
4326 if (text[2] != '0' || text[3])
4327 goto invalid;
4328 /* a possible captured label == mac->params[0] */
4329 /* fall through */
4330 default:
4331 {
4332 unsigned long n;
4333 char *ep;
4334
4335 n = strtoul(text + 1, &ep, 10);
4336 if (unlikely(*ep))
4337 goto invalid;
4338
4339 if (n <= mac->nparam) {
4340 n = mmac_rotate(mac, n);
4341 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4342 }
4343 text = NULL;
4344 break;
4345 }
4346 case '%':
4347 type = TOK_ID;
4348 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4349 break;
4350 case '-':
4351 case '+':
4352 {
4353 int cc;
4354 unsigned long n;
4355 char *ep;
4356
4357 text = NULL;
4358
4359 n = strtoul(t->text + 2, &ep, 10);
4360 if (unlikely(*ep))
4361 goto invalid;
4362
4363 if (n && n < mac->nparam) {
4364 n = mmac_rotate(mac, n);
4365 tt = mac->params[n];
4366 }
4367 cc = find_cc(tt);
4368 if (cc == -1) {
4369 nasm_nonfatal("macro parameter `%s' is not a condition code",
4370 text);
4371 text = NULL;
4372 break;
4373 }
4374
4375 type = TOK_ID;
4376 if (text[1] == '-') {
4377 int ncc = inverse_ccs[cc];
4378 if (unlikely(ncc == -1)) {
4379 nasm_nonfatal("condition code `%s' is not invertible",
4380 conditions[cc]);
4381 break;
4382 }
4383 cc = ncc;
4384 }
4385 text = nasm_strdup(conditions[cc]);
4386 break;
4387 }
4388
4389 invalid:
4390 nasm_nonfatal("invalid macro parameter: `%s'", text);
4391 text = NULL;
4392 break;
4393 }
4394 break;
4395 }
4396
4397 case TOK_PREPROC_Q:
4398 if (mac) {
4399 type = TOK_ID;
4400 text = nasm_strdup(mac->iname);
4401 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004402 } else {
4403 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004404 }
4405 break;
4406
4407 case TOK_PREPROC_QQ:
4408 if (mac) {
4409 type = TOK_ID;
4410 text = nasm_strdup(mac->name);
4411 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004412 } else {
4413 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004414 }
4415 break;
4416
4417 case TOK_INDIRECT:
4418 {
4419 Token *tt;
4420
4421 tt = tokenize(t->text);
4422 tt = expand_mmac_params(tt);
4423 tt = expand_smacro(tt);
4424 /* Why dup_tlist() here? We should own tt... */
4425 dup_tlist(tt, &tail);
4426 text = NULL;
4427 change = true;
4428 break;
4429 }
4430
4431 default:
4432 change = false;
4433 break;
4434 }
4435
4436 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004437 if (!text) {
4438 delete_Token(t);
4439 } else {
4440 *tail = t;
4441 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004443 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004444 t->type = type;
4445 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004447 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004448 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004449 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 tail = &t->next;
4451 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004452 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004453
H. Peter Anvineba20a72002-04-30 20:53:55 +00004454 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004455
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004456 if (changed) {
4457 const struct tokseq_match t[] = {
4458 {
4459 PP_CONCAT_MASK(TOK_ID) |
4460 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4461 PP_CONCAT_MASK(TOK_ID) |
4462 PP_CONCAT_MASK(TOK_NUMBER) |
4463 PP_CONCAT_MASK(TOK_FLOAT) |
4464 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4465 },
4466 {
4467 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4468 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4469 }
4470 };
4471 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4472 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004473
H. Peter Anvin76690a12002-04-30 20:52:49 +00004474 return thead;
4475}
4476
H. Peter Anvin322bee02019-08-10 01:38:06 -07004477static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004478
H. Peter Anvin76690a12002-04-30 20:52:49 +00004479/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004480 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004481 * a macro at all, it is simply copied to the output and the pointer
4482 * advanced. tpp should be a pointer to a pointer (usually the next
4483 * pointer of the previous token) to the first token. **tpp is updated
4484 * to point to the last token of the expansion, and *tpp updated to
4485 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004486 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004487 * If the expansion is empty, *tpp will be unchanged but **tpp will
4488 * be advanced past the macro call.
4489 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004490 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004491 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004492static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004493{
4494 Token **params = NULL;
4495 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004496 Token *tline = **tpp;
4497 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004498 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004499 int i;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004500 Token *t, *tup, *ttail;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004501 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004502 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004503
4504 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004505 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004506
4507 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004508
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004509 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004510 smacro_deadman.levels--;
4511
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004512 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004513 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004514 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004515 smacro_deadman.triggered = true;
4516 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004517 goto not_a_macro;
4518 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004519 head = (SMacro *)hash_findix(&smacros, mname);
4520 } else if (tline->type == TOK_PREPROC_ID) {
4521 Context *ctx = get_ctx(mname, &mname);
4522 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4523 } else {
4524 goto not_a_macro;
4525 }
4526
4527 /*
4528 * We've hit an identifier of some sort. First check whether the
4529 * identifier is a single-line macro at all, then think about
4530 * checking for parameters if necessary.
4531 */
4532 list_for_each(m, head) {
4533 if (!mstrcmp(m->name, mname, m->casesense))
4534 break;
4535 }
4536
4537 if (!m) {
4538 goto not_a_macro;
4539 }
4540
4541 /* Parse parameters, if applicable */
4542
4543 params = NULL;
4544 nparam = 0;
4545
4546 if (m->nparam == 0) {
4547 /*
4548 * Simple case: the macro is parameterless.
4549 * Nothing to parse; the expansion code will
4550 * drop the macro name token.
4551 */
4552 } else {
4553 /*
4554 * Complicated case: at least one macro with this name
4555 * exists and takes parameters. We must find the
4556 * parameters in the call, count them, find the SMacro
4557 * that corresponds to that form of the macro call, and
4558 * substitute for the parameters when we expand. What a
4559 * pain.
4560 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004561 Token *t;
4562 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004563
4564 tline = tline->next;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004565 skip_white_(tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004566 if (!tok_is_(tline, "(")) {
4567 /*
4568 * This macro wasn't called with parameters: ignore
4569 * the call. (Behaviour borrowed from gnu cpp.)
4570 */
4571 goto not_a_macro;
4572 }
4573
4574 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004575 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004576 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004577 t = tline; /* tline points to leading ( */
4578
4579 while (paren) {
4580 t = t->next;
4581
4582 if (!t) {
4583 nasm_nonfatal("macro call expects terminating `)'");
4584 goto not_a_macro;
4585 }
4586
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004587 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004588 continue;
4589
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004590 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004591 case ',':
4592 if (!brackets)
4593 nparam++;
4594 break;
4595
4596 case '{':
4597 brackets++;
4598 break;
4599
4600 case '}':
4601 if (brackets > 0)
4602 brackets--;
4603 break;
4604
4605 case '(':
4606 if (!brackets)
4607 paren++;
4608 break;
4609
4610 case ')':
4611 if (!brackets)
4612 paren--;
4613 break;
4614
4615 default:
4616 break; /* Normal token */
4617 }
4618 }
4619
4620 /*
4621 * Look for a macro matching in both name and parameter count.
4622 * We already know any matches cannot be anywhere before the
4623 * current position of "m", so there is no reason to
4624 * backtrack.
4625 */
4626 while (1) {
4627 if (!m) {
4628 /*!
4629 *!macro-params-single [on] single-line macro calls with wrong parameter count
4630 *! warns about \i{single-line macros} being invoked
4631 *! with the wrong number of parameters.
4632 */
4633 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4634 "single-line macro `%s' exists, "
4635 "but not taking %d parameter%s",
4636 mname, nparam, (nparam == 1) ? "" : "s");
4637 goto not_a_macro;
4638 }
4639
4640 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004641 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004642 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004643 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004644 break; /* Also good */
4645 }
4646 m = m->next;
4647 }
4648 }
4649
4650 if (m->in_progress)
4651 goto not_a_macro;
4652
4653 /* Expand the macro */
4654 m->in_progress = true;
4655
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004656 if (nparam) {
4657 /* Extract parameters */
4658 Token **phead, **pep;
4659 int white = 0;
4660 int brackets = 0;
4661 int paren;
4662 bool bracketed = false;
4663 bool bad_bracket = false;
4664 enum sparmflags flags;
4665
4666 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004667 paren = 1;
4668 nasm_newn(params, nparam);
4669 i = 0;
4670 flags = m->params[i].flags;
4671 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004672 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004673
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004674 while (paren) {
4675 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004676 char ch;
4677
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004678 tline = tline->next;
4679
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004680 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004681 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004682
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004683 ch = 0;
4684 skip = false;
4685
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004686
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004687 switch (tline->type) {
4688 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004689 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004690 ch = tline->text[0];
4691 break;
4692
4693 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004694 if (!(flags & SPARM_NOSTRIP)) {
4695 if (brackets || *phead)
4696 white++; /* Keep interior whitespace */
4697 skip = true;
4698 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004699 break;
4700
4701 default:
4702 break;
4703 }
4704
4705 switch (ch) {
4706 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004707 if (!brackets && !(flags & SPARM_GREEDY)) {
4708 i++;
4709 nasm_assert(i < nparam);
4710 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004711 *pep = NULL;
4712 bracketed = false;
4713 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004714 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004715 }
4716 break;
4717
4718 case '{':
4719 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004720 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4721 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004722 }
4723 brackets++;
4724 break;
4725
4726 case '}':
4727 if (brackets > 0) {
4728 if (!--brackets)
4729 skip = bracketed;
4730 }
4731 break;
4732
4733 case '(':
4734 if (!brackets)
4735 paren++;
4736 break;
4737
4738 case ')':
4739 if (!brackets) {
4740 paren--;
4741 if (!paren) {
4742 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004743 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004744 }
4745 }
4746 break;
4747
4748 default:
4749 break; /* Normal token */
4750 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004751
4752 if (!skip) {
4753 Token *t;
4754
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004755 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004756
4757 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004758 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004759 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004760 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004761 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004762 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004763 pep = &t->next;
4764 white = 0;
4765 }
4766 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004767
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004768 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004769 * Possible further processing of parameters. Note that the
4770 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004771 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004772 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004773 enum sparmflags flags = m->params[i].flags;
4774
4775 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004776 /* Evaluate this parameter as a number */
4777 struct ppscan pps;
4778 struct tokenval tokval;
4779 expr *evalresult;
4780 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004781
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004782 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4783 pps.ntokens = -1;
4784 tokval.t_type = TOKEN_INVALID;
4785 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004786
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004787 free_tlist(eval_param);
4788 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004789
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004790 if (!evalresult) {
4791 /* Nothing meaningful to do */
4792 } else if (tokval.t_type) {
4793 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4794 } else if (!is_simple(evalresult)) {
4795 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4796 } else {
4797 params[i] = make_tok_num(reloc_value(evalresult));
4798 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004799 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004800
4801 if (flags & SPARM_STR) {
4802 /* Convert expansion to a quoted string */
4803 char *arg;
4804 Token *qs;
4805
4806 qs = expand_smacro_noreset(params[i]);
4807 arg = detoken(qs, false);
4808 free_tlist(qs);
4809 params[i] = make_tok_qstr(arg);
4810 nasm_free(arg);
4811 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004812 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004813 }
4814
4815 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004816 tline = tline->next; /* Remove the macro call from the input */
4817 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004818
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004819 /* Note: we own the expansion this returns. */
4820 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004821
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004822 tup = NULL;
4823 ttail = NULL; /* Pointer to the last token of the expansion */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004824 cond_comma = false;
4825
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004826 while (t) {
4827 enum pp_token_type type = t->type;
4828 Token *tnext = t->next;
4829 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004830
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004831 switch (type) {
4832 case TOK_PREPROC_Q:
4833 case TOK_PREPROC_QQ:
4834 t->type = TOK_ID;
4835 nasm_free(t->text);
4836 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4837 t->len = nasm_last_string_len();
4838 t->next = tline;
4839 break;
4840
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004841 case TOK_COND_COMMA:
4842 delete_Token(t);
4843 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4844 break;
4845
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004846 case TOK_ID:
4847 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004848 /*
4849 * Chain this into the target line *before* expanding,
4850 * that way we pick up any arguments to the new macro call,
4851 * if applicable.
4852 */
4853 t->next = tline;
4854 tp = &t;
4855 expand_one_smacro(&tp);
4856 if (t == tline)
4857 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004858 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004859
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004860 default:
4861 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004862 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004863 nasm_assert(!tup && param < nparam);
4864 delete_Token(t);
4865 t = NULL;
4866 tup = tnext;
4867 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004868 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004869 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004870 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004871 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004872 }
4873
4874 if (t) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004875 if (t->type != TOK_WHITESPACE)
4876 cond_comma = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004877 tline = t;
4878 if (!ttail)
4879 ttail = t;
4880 }
4881
4882 if (tnext) {
4883 t = tnext;
4884 } else {
4885 t = tup;
4886 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004887 }
4888 }
4889
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004890 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004891 if (ttail)
4892 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004893
4894 m->in_progress = false;
4895
4896 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004897 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004898 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004899
4900 /*
4901 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004902 * and then advance to the next input token. Note that this is
4903 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004904 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004905not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004906 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004907 m = NULL;
4908done:
4909 smacro_deadman.levels++;
4910 if (unlikely(params))
4911 free_tlist_array(params, nparam);
4912 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004913}
4914
4915/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004916 * Expand all single-line macro calls made in the given line.
4917 * Return the expanded version of the line. The original is deemed
4918 * to be destroyed in the process. (In reality we'll just move
4919 * Tokens from input to output a lot of the time, rather than
4920 * actually bothering to destroy and replicate.)
4921 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004922static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004923{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004924 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07004925 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4926 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004927 return expand_smacro_noreset(tline);
4928}
4929
4930static Token *expand_smacro_noreset(Token * tline)
4931{
4932 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004933 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004934 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004935
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004936 /*
4937 * Trick: we should avoid changing the start token pointer since it can
4938 * be contained in "next" field of other token. Because of this
4939 * we allocate a copy of first token and work with it; at the end of
4940 * routine we copy it back
4941 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004942 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004943 tline = new_Token(org_tline->next, org_tline->type,
4944 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004945 nasm_free(org_tline->text);
4946 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004947 }
4948
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004950 /*
4951 * Pretend that we always end up doing expansion on the first pass;
4952 * that way %+ get processed. However, if we process %+ before the
4953 * first pass, we end up with things like MACRO %+ TAIL trying to
4954 * look up the macro "MACROTAIL", which we don't want.
4955 */
4956 expanded = true;
4957 thead = tline;
4958 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004959 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004960 {
4961 PP_CONCAT_MASK(TOK_ID) |
4962 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4963 PP_CONCAT_MASK(TOK_ID) |
4964 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4965 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4966 }
4967 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004968
4969 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004970 while ((t = *tail)) /* main token loop */
4971 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004972
4973 if (!expanded) {
4974 tline = thead;
4975 break; /* Done! */
4976 }
4977
4978 /*
4979 * Now scan the entire line and look for successive TOK_IDs
4980 * that resulted after expansion (they can't be produced by
4981 * tokenize()). The successive TOK_IDs should be concatenated.
4982 * Also we look for %+ tokens and concatenate the tokens
4983 * before and after them (without white spaces in between).
4984 */
4985 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004986
4987 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004988 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004989
H. Peter Anvine2c80182005-01-15 22:15:51 +00004990 if (org_tline) {
4991 if (thead) {
4992 *org_tline = *thead;
4993 /* since we just gave text to org_line, don't free it */
4994 thead->text = NULL;
4995 delete_Token(thead);
4996 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004997 /*
4998 * The expression expanded to empty line;
4999 * we can't return NULL because of the "trick" above.
5000 * Just set the line to a single WHITESPACE token.
5001 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005002 memset(org_tline, 0, sizeof(*org_tline));
5003 org_tline->text = NULL;
5004 org_tline->type = TOK_WHITESPACE;
5005 }
5006 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005007 }
5008
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005009 return thead;
5010}
5011
5012/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005013 * Similar to expand_smacro but used exclusively with macro identifiers
5014 * right before they are fetched in. The reason is that there can be
5015 * identifiers consisting of several subparts. We consider that if there
5016 * are more than one element forming the name, user wants a expansion,
5017 * otherwise it will be left as-is. Example:
5018 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005019 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005020 *
5021 * the identifier %$abc will be left as-is so that the handler for %define
5022 * will suck it and define the corresponding value. Other case:
5023 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005024 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005025 *
5026 * In this case user wants name to be expanded *before* %define starts
5027 * working, so we'll expand %$abc into something (if it has a value;
5028 * otherwise it will be left as-is) then concatenate all successive
5029 * PP_IDs into one.
5030 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005031static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005032{
5033 Token *cur, *oldnext = NULL;
5034
H. Peter Anvin734b1882002-04-30 21:01:08 +00005035 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005036 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005037
5038 cur = tline;
5039 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005040 (cur->next->type == TOK_ID ||
5041 cur->next->type == TOK_PREPROC_ID
5042 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005043 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005044
5045 /* If identifier consists of just one token, don't expand */
5046 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005048
H. Peter Anvine2c80182005-01-15 22:15:51 +00005049 if (cur) {
5050 oldnext = cur->next; /* Detach the tail past identifier */
5051 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005052 }
5053
H. Peter Anvin734b1882002-04-30 21:01:08 +00005054 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005055
H. Peter Anvine2c80182005-01-15 22:15:51 +00005056 if (cur) {
5057 /* expand_smacro possibly changhed tline; re-scan for EOL */
5058 cur = tline;
5059 while (cur && cur->next)
5060 cur = cur->next;
5061 if (cur)
5062 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005063 }
5064
5065 return tline;
5066}
5067
5068/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005069 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005071 * to check for an initial label - that's taken care of in
5072 * expand_mmacro - but must check numbers of parameters. Guaranteed
5073 * to be called with tline->type == TOK_ID, so the putative macro
5074 * name is easy to find.
5075 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005076static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005077{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005078 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005079 Token **params;
5080 int nparam;
5081
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005082 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005083
5084 /*
5085 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005086 * name which isn't already excluded by macro cycle removal.
5087 * (The cycle removal test here helps optimize the case of wrapping
5088 * instructions, and is cheap to do here.)
5089 *
5090 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005091 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005092 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005093 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005094 list_for_each(m, head) {
5095 if (!mstrcmp(m->name, tline->text, m->casesense) &&
5096 (m->in_progress != 1 || m->max_depth > 0))
5097 break; /* Found something that needs consideration */
5098 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005099 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005100 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005101
5102 /*
5103 * OK, we have a potential macro. Count and demarcate the
5104 * parameters.
5105 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005106 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005107
5108 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005109 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005110 * structure that handles this number.
5111 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005112 while (m) {
5113 if (m->nparam_min <= nparam
5114 && (m->plus || nparam <= m->nparam_max)) {
5115 /*
5116 * This one is right. Just check if cycle removal
5117 * prohibits us using it before we actually celebrate...
5118 */
5119 if (m->in_progress > m->max_depth) {
5120 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005121 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005122 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005123 }
5124 nasm_free(params);
5125 return NULL;
5126 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005127 /*
5128 * It's right, and we can use it. Add its default
5129 * parameters to the end of our list if necessary.
5130 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005131 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005132 int newnparam = m->nparam_min + m->ndefs;
5133 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5134 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5135 (newnparam - nparam) * sizeof(*params));
5136 nparam = newnparam;
5137 params[nparam+1] = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005138 }
5139 /*
5140 * If we've gone over the maximum parameter count (and
5141 * we're in Plus mode), ignore parameters beyond
5142 * nparam_max.
5143 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005144 if (m->plus && nparam > m->nparam_max)
5145 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005146
5147 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005148 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005149 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005150 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005151 }
5152 /*
5153 * This one wasn't right: look for the next one with the
5154 * same name.
5155 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005156 list_for_each(m, m->next)
5157 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005158 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005159 }
5160
5161 /*
5162 * After all that, we didn't find one with the right number of
5163 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005164 *!
5165 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5166 *! warns about \i{multi-line macros} being invoked
5167 *! with the wrong number of parameters. See \k{mlmacover} for an
5168 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005169 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005170 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5171 "multi-line macro `%s' exists, but not taking %d parameter%s",
5172 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005173 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005174 return NULL;
5175}
5176
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005177
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005178#if 0
5179
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005180/*
5181 * Save MMacro invocation specific fields in
5182 * preparation for a recursive macro expansion
5183 */
5184static void push_mmacro(MMacro *m)
5185{
5186 MMacroInvocation *i;
5187
5188 i = nasm_malloc(sizeof(MMacroInvocation));
5189 i->prev = m->prev;
5190 i->params = m->params;
5191 i->iline = m->iline;
5192 i->nparam = m->nparam;
5193 i->rotate = m->rotate;
5194 i->paramlen = m->paramlen;
5195 i->unique = m->unique;
5196 i->condcnt = m->condcnt;
5197 m->prev = i;
5198}
5199
5200
5201/*
5202 * Restore MMacro invocation specific fields that were
5203 * saved during a previous recursive macro expansion
5204 */
5205static void pop_mmacro(MMacro *m)
5206{
5207 MMacroInvocation *i;
5208
5209 if (m->prev) {
5210 i = m->prev;
5211 m->prev = i->prev;
5212 m->params = i->params;
5213 m->iline = i->iline;
5214 m->nparam = i->nparam;
5215 m->rotate = i->rotate;
5216 m->paramlen = i->paramlen;
5217 m->unique = i->unique;
5218 m->condcnt = i->condcnt;
5219 nasm_free(i);
5220 }
5221}
5222
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005223#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005224
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005225/*
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005226 * List an mmacro call with arguments (-La option)
5227 */
5228static void list_mmacro_call(const MMacro *m)
5229{
5230 const char prefix[] = " ;;; [macro] ";
5231 size_t namelen, size;
5232 char *buf, *p;
5233 unsigned int i;
5234 const Token *t;
5235
5236 namelen = strlen(m->iname);
5237 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5238
5239 for (i = 1; i <= m->nparam; i++) {
5240 int j = 0;
5241 size += 3; /* Braces and space/comma */
5242 list_for_each(t, m->params[i]) {
5243 if (j++ >= m->paramlen[i])
5244 break;
5245 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5246 }
5247 }
5248
5249 buf = p = nasm_malloc(size);
5250 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5251 p = mempcpy(p, m->iname, namelen);
5252 *p++ = ' ';
5253
5254 for (i = 1; i <= m->nparam; i++) {
5255 int j = 0;
5256 *p++ = '{';
5257 list_for_each(t, m->params[i]) {
5258 if (j++ >= m->paramlen[i])
5259 break;
5260 if (!t->text) {
5261 if (t->type == TOK_WHITESPACE)
5262 *p++ = ' ';
5263 } else {
5264 p = mempcpy(p, t->text, t->len);
5265 }
5266 }
5267 *p++ = '}';
5268 *p++ = ',';
5269 }
5270
5271 *--p = '\0'; /* Replace last delimeter with null */
5272 lfmt->line(LIST_MACRO, -1, buf);
5273 nasm_free(buf);
5274}
5275
5276/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005277 * Expand the multi-line macro call made by the given line, if
5278 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005279 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005280 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005281static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005282{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005283 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005284 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005285 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005286 Token **params, *t, *tt;
5287 MMacro *m;
5288 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005289 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005290 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005291 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005292
5293 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005294 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005295 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005296 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005297 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005298 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005299 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005300 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005301 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005302 Token *last;
5303 /*
5304 * We have an id which isn't a macro call. We'll assume
5305 * it might be a label; we'll also check to see if a
5306 * colon follows it. Then, if there's another id after
5307 * that lot, we'll check it again for macro-hood.
5308 */
5309 label = last = t;
5310 t = t->next;
5311 if (tok_type_(t, TOK_WHITESPACE))
5312 last = t, t = t->next;
5313 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005314 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005315 last = t, t = t->next;
5316 if (tok_type_(t, TOK_WHITESPACE))
5317 last = t, t = t->next;
5318 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005319 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005320 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005321 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005322 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005323 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005324 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005325
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005326 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5327 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5328 if (!mmacro_deadman.triggered) {
5329 nasm_nonfatal("interminable multiline macro recursion");
5330 mmacro_deadman.triggered = true;
5331 }
5332 return 0;
5333 }
5334
5335 mmacro_deadman.total++;
5336 mmacro_deadman.levels++;
5337
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005338 /*
5339 * Fix up the parameters: this involves stripping leading and
5340 * trailing whitespace, then stripping braces if they are
5341 * present.
5342 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005343 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005344
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005345 nasm_assert(params[nparam+1] == NULL);
5346
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005347 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005348 int brace = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005349 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005350
H. Peter Anvine2c80182005-01-15 22:15:51 +00005351 skip_white_(t);
5352 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005353 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005354 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005355 while (t) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005356 if (comma) {
5357 /* Check if we hit a comma that ends this argument */
5358 if (tok_is_(t, ","))
5359 break;
5360 else if (t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
5361 break;
5362 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005363 if (brace && t->type == TOK_OTHER) {
5364 if (t->text[0] == '{')
5365 brace++; /* ... or a nested opening brace */
5366 else if (t->text[0] == '}')
5367 if (!--brace)
5368 break; /* ... or a brace */
5369 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005370 t = t->next;
5371 paramlen[i]++;
5372 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005373 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005374 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005375 }
5376
5377 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005378 * OK, we have a MMacro structure together with a set of
5379 * parameters. We must now go through the expansion and push
5380 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005381 * parameter tokens and macro-local tokens doesn't get done
5382 * until the single-line macro substitution process; this is
5383 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005384 * later through %rotate and give the right semantics for
5385 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005386 *
5387 * First, push an end marker on to istk->expansion, mark this
5388 * macro as in progress, and set up its invocation-specific
5389 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005390 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005391 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005392 ll->next = istk->expansion;
5393 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005394 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005395
5396 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005397 * Save the previous MMacro expansion in the case of
5398 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005399 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005400#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005401 if (m->max_depth && m->in_progress)
5402 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005403#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005404
5405 m->in_progress ++;
5406 m->params = params;
5407 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005408 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005409 m->nparam = nparam;
5410 m->rotate = 0;
5411 m->paramlen = paramlen;
5412 m->unique = unique++;
5413 m->lineno = 0;
5414 m->condcnt = 0;
5415
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005416 m->mstk = istk->mstk;
5417 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005418
5419 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005420 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005421 ll->next = istk->expansion;
5422 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005423 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005424 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005425
5426 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005427 * If we had a label, and this macro definition does not include
5428 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005429 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005430 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005431 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005432 /*
5433 * We had a label. If this macro contains an %00 parameter,
5434 * save the value as a special parameter (which is what it
5435 * is), otherwise push it as the first line of the macro
5436 * expansion.
5437 */
5438 if (m->capture_label) {
5439 params[0] = dup_Token(NULL, label);
5440 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005441 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005442 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005443 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005444 ll->finishes = NULL;
5445 ll->next = istk->expansion;
5446 istk->expansion = ll;
5447 ll->first = startline;
5448 if (!dont_prepend) {
5449 while (label->next)
5450 label = label->next;
5451 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005452 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005453 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005454 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005455
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005456 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005457
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005458 if (list_option('m') && !m->nolist)
5459 list_mmacro_call(m);
5460
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005461 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005462}
5463
H. Peter Anvin130736c2016-02-17 20:27:41 -08005464/*
5465 * This function adds macro names to error messages, and suppresses
5466 * them if necessary.
5467 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005468static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005469{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005470 /*
5471 * If we're in a dead branch of IF or something like it, ignore the error.
5472 * However, because %else etc are evaluated in the state context
5473 * of the previous branch, errors might get lost:
5474 * %if 0 ... %else trailing garbage ... %endif
5475 * So %else etc should set the ERR_PP_PRECOND flag.
5476 */
5477 if ((severity & ERR_MASK) < ERR_FATAL &&
5478 istk && istk->conds &&
5479 ((severity & ERR_PP_PRECOND) ?
5480 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005481 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005482 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005483
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005484 /* This doesn't make sense with the macro stack unwinding */
5485 if (0) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005486 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005487
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005488 /* get %macro name */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005489 if (!(severity & ERR_NOFILE) && istk && istk->mstk.mmac) {
5490 MMacro *mmac = istk->mstk.mmac;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005491 char *buf;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005492
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005493 nasm_set_verror(real_verror);
5494 buf = nasm_vasprintf(fmt, arg);
5495 nasm_error(severity, "(%s:%"PRId32") %s",
5496 mmac->name, mmac->lineno - delta, buf);
5497 nasm_set_verror(pp_verror);
5498 nasm_free(buf);
5499 return;
5500 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005501 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005502 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005503}
5504
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005505static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005506stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005507{
5508 (void)s;
5509 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005510 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005511
5512 return make_tok_qstr(src_get_fname());
5513}
5514
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005515static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005516stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005517{
5518 (void)s;
5519 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005520 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005521
5522 return make_tok_num(src_get_linnum());
5523}
5524
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005525static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005526stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005527{
5528 (void)s;
5529 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005530 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005531
5532 return make_tok_num(globalbits);
5533}
5534
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005535static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005536stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005537{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005538 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005539 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5540 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5541 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005542
5543 (void)s;
5544 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005545 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005546
5547 switch (globalbits) {
5548 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005549 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005550 break;
5551 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005552 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005553 break;
5554 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005555 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005556 break;
5557 default:
5558 panic();
5559 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005560
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005561 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005562}
5563
H. Peter Anvin8b262472019-02-26 14:00:54 -08005564/* Add magic standard macros */
5565struct magic_macros {
5566 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005567 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005568 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005569};
5570static const struct magic_macros magic_macros[] =
5571{
5572 { "__FILE__", 0, stdmac_file },
5573 { "__LINE__", 0, stdmac_line },
5574 { "__BITS__", 0, stdmac_bits },
5575 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005576 { NULL, 0, NULL }
5577};
5578
5579static void pp_add_magic_stdmac(void)
5580{
5581 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005582 SMacro tmpl;
5583
5584 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005585
5586 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005587 tmpl.nparam = m->nparam;
5588 tmpl.expand = m->func;
5589 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005590 }
5591}
5592
H. Peter Anvin734b1882002-04-30 21:01:08 +00005593static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005594pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005595{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005596 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005597 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005598
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005599 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005600 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005601 nested_mac_count = 0;
5602 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005603 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005604 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005605 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005606 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005607
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005608 if (!use_loaded)
5609 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5610 memset(use_loaded, 0, use_package_count * sizeof(bool));
5611
H. Peter Anvin6686de22019-08-10 05:33:14 -07005612 /* First set up the top level input file */
5613 nasm_new(istk);
5614 istk->fp = nasm_open_read(file, NF_TEXT);
5615 src_set(0, file);
5616 istk->lineinc = 1;
5617 if (!istk->fp)
5618 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5619
5620 strlist_add(deplist, file);
5621
5622 /*
5623 * Set up the stdmac packages as a virtual include file,
5624 * indicated by a null file pointer.
5625 */
5626 nasm_new(inc);
5627 inc->next = istk;
5628 inc->fname = src_set_fname(NULL);
5629 inc->nolist = !list_option('b');
5630 istk = inc;
5631 lfmt->uplevel(LIST_INCLUDE, 0);
5632
H. Peter Anvin8b262472019-02-26 14:00:54 -08005633 pp_add_magic_stdmac();
5634
H. Peter Anvinf7606612016-07-13 14:23:48 -07005635 if (tasm_compatible_mode)
5636 pp_add_stdmac(nasm_stdmac_tasm);
5637
5638 pp_add_stdmac(nasm_stdmac_nasm);
5639 pp_add_stdmac(nasm_stdmac_version);
5640
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005641 if (extrastdmac)
5642 pp_add_stdmac(extrastdmac);
5643
H. Peter Anvinf7606612016-07-13 14:23:48 -07005644 stdmacpos = stdmacros[0];
5645 stdmacnext = &stdmacros[1];
5646
H. Peter Anvind2456592008-06-19 15:04:18 -07005647 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005648
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005649 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005650 * Define the __PASS__ macro. This is defined here unlike all the
5651 * other builtins, because it is special -- it varies between
5652 * passes -- but there is really no particular reason to make it
5653 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005654 *
5655 * 0 = dependencies only
5656 * 1 = preparatory passes
5657 * 2 = final pass
5658 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005659 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005660 switch (mode) {
5661 case PP_NORMAL:
5662 apass = pass_final() ? 2 : 1;
5663 break;
5664 case PP_DEPS:
5665 apass = 0;
5666 break;
5667 case PP_PREPROC:
5668 apass = 3;
5669 break;
5670 default:
5671 panic();
5672 }
5673
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005674 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005675}
5676
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005677static void pp_init(void)
5678{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005679}
5680
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005681/*
5682 * Get a line of tokens. If we popped the macro expansion/include stack,
5683 * we return a pointer to the dummy token tok_pop; at that point if
5684 * istk is NULL then we have reached end of input;
5685 */
5686static Token tok_pop; /* Dummy token placeholder */
5687
5688static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005689{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005690 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005691 Line *l = istk->expansion;
5692 Token *tline = NULL;
5693 Token *dtline;
5694
H. Peter Anvine2c80182005-01-15 22:15:51 +00005695 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005696 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005697 * buffer or from the input file.
5698 */
5699 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005700 while (l && l->finishes) {
5701 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005702
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005703 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005704 /*
5705 * This is a macro-end marker for a macro with no
5706 * name, which means it's not really a macro at all
5707 * but a %rep block, and the `in_progress' field is
5708 * more than 1, meaning that we still need to
5709 * repeat. (1 means the natural last repetition; 0
5710 * means termination by %exitrep.) We have
5711 * therefore expanded up to the %endrep, and must
5712 * push the whole block on to the expansion buffer
5713 * again. We don't bother to remove the macro-end
5714 * marker: we'd only have to generate another one
5715 * if we did.
5716 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005717 fm->in_progress--;
5718 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005719 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005720 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005721
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005722 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005723 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005724 tail = &ll->first;
5725
5726 list_for_each(t, l->first) {
5727 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005728 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005729 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005730 }
5731 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005732 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005733 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005734 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005735 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005736 MMacro *m = istk->mstk.mstk;
5737
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005738 /*
5739 * Check whether a `%rep' was started and not ended
5740 * within this macro expansion. This can happen and
5741 * should be detected. It's a fatal error because
5742 * I'm too confused to work out how to recover
5743 * sensibly from it.
5744 */
5745 if (defining) {
5746 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005747 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005748 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005749 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005750 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005751 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005752
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005753 /*
5754 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005755 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005756 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005757 istk->mstk = m->mstk;
5758 if (m->name) {
5759 /*
5760 * This was a real macro call, not a %rep, and
5761 * therefore the parameter information needs to
5762 * be freed and the iteration count/nesting
5763 * depth adjusted.
5764 */
5765
5766 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005767 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005768 * If all mmacro processing done,
5769 * clear all counters and the deadman
5770 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005771 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005772 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005773 }
5774
Adam Majer91e72402017-07-25 10:42:01 +02005775#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005776 if (m->prev) {
5777 pop_mmacro(m);
5778 fm->in_progress --;
5779 } else
Adam Majer91e72402017-07-25 10:42:01 +02005780#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005781 {
5782 nasm_free(m->params);
5783 free_tlist(m->iline);
5784 nasm_free(m->paramlen);
5785 fm->in_progress = 0;
5786 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005787 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005788
5789 /*
5790 * FIXME It is incorrect to always free_mmacro here.
5791 * It leads to usage-after-free.
5792 *
5793 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5794 */
5795#if 0
5796 else
5797 free_mmacro(m);
5798#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005799 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005800 istk->expansion = l->next;
5801 nasm_free(l);
5802 lfmt->downlevel(LIST_MACRO);
5803 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005804 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005805
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005806 do { /* until we get a line we can use */
5807 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005808
5809 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005810 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005811 int32_t lineno;
5812
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005813 if (istk->mstk.mstk) {
5814 istk->mstk.mstk->lineno++;
5815 if (istk->mstk.mstk->fname)
5816 lineno = istk->mstk.mstk->lineno +
5817 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005818 else
5819 lineno = 0; /* Defined at init time or builtin */
5820 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005821 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005822 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005823
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005824 tline = l->first;
5825 istk->expansion = l->next;
5826 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005827
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005828 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005829 if (!istk->nolist)
5830 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005831 nasm_free(line);
5832 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005833 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005834 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005835 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005836 } else {
5837 /*
5838 * The current file has ended; work down the istk
5839 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005840 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005841 if (i->fp)
5842 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005843 if (i->conds) {
5844 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005845 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005846 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005847 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005848 if (i->next)
5849 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005850 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005851 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005852 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005853 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005854 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005855 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005856
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005857 /*
5858 * We must expand MMacro parameters and MMacro-local labels
5859 * _before_ we plunge into directive processing, to cope
5860 * with things like `%define something %1' such as STRUC
5861 * uses. Unless we're _defining_ a MMacro, in which case
5862 * those tokens should be left alone to go into the
5863 * definition; and unless we're in a non-emitting
5864 * condition, in which case we don't want to meddle with
5865 * anything.
5866 */
5867 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005868 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005869 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005870 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005871
H. Peter Anvine2c80182005-01-15 22:15:51 +00005872 /*
5873 * Check the line to see if it's a preprocessor directive.
5874 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005875 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5876 if (dtline)
5877 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005878 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005879 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005880 * We're defining a multi-line macro. We emit nothing
5881 * at all, and just
5882 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005883 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005884 MMacro *mmac = defining->dstk.mmac;
5885
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005886 Line *l = nasm_malloc(sizeof(Line));
5887 l->next = defining->expansion;
5888 l->first = tline;
5889 l->finishes = NULL;
5890 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005891
5892 /*
5893 * Remember if this mmacro expansion contains %00:
5894 * if it does, we will have to handle leading labels
5895 * specially.
5896 */
5897 if (mmac) {
5898 const Token *t;
5899 list_for_each(t, tline) {
5900 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
5901 mmac->capture_label = true;
5902 }
5903 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005904 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005905 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005906 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005907 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005908 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005909 * directive so we keep our place correctly.
5910 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005911 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005912 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005913 /*
5914 * We're in a %rep block which has been terminated, so
5915 * we're walking through to the %endrep without
5916 * emitting anything. Emit nothing at all, not even a
5917 * blank line: when we emerge from the %rep block we'll
5918 * give a line-number directive so we keep our place
5919 * correctly.
5920 */
5921 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005922 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005923 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005924 if (!expand_mmacro(tline))
5925 return tline;
5926 }
5927 }
5928}
5929
5930static char *pp_getline(void)
5931{
5932 char *line = NULL;
5933 Token *tline;
5934
5935 real_verror = nasm_set_verror(pp_verror);
5936
5937 while (true) {
5938 tline = pp_tokline();
5939 if (tline == &tok_pop) {
5940 /*
5941 * We popped the macro/include stack. If istk is empty,
5942 * we are at end of input, otherwise just loop back.
5943 */
5944 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005945 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005946 } else {
5947 /*
5948 * De-tokenize the line and emit it.
5949 */
5950 line = detoken(tline, true);
5951 free_tlist(tline);
5952 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005953 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005954 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005955
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005956 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005957 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005958 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005959 nasm_free(buf);
5960 }
5961
H. Peter Anvin130736c2016-02-17 20:27:41 -08005962 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005963 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005964}
5965
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005966static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005967{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005968 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005969
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005970 if (defining) {
5971 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005972 nasm_nonfatal("end of file while still defining macro `%s'",
5973 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005974 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005975 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005976 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005977
5978 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005979 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005980 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005981
5982 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005983
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005984 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005985 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005986 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005987 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005988 Include *i = istk;
5989 istk = istk->next;
5990 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005991 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005992 }
5993 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005994 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005995 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005996}
5997
5998static void pp_cleanup_session(void)
5999{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006000 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006001 free_llist(predef);
6002 predef = NULL;
6003 delete_Blocks();
6004 freeTokens = NULL;
6005 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006006}
6007
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006008static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006009{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006010 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006011}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006012
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006013static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006014{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006015 Token *inc, *space, *name;
6016 Line *l;
6017
H. Peter Anvin734b1882002-04-30 21:01:08 +00006018 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
6019 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
6020 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006021
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006022 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006023 l->next = predef;
6024 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006025 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006026 predef = l;
6027}
6028
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006029static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006030{
6031 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006032 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006033 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006034
H. Peter Anvin130736c2016-02-17 20:27:41 -08006035 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08006036
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006037 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00006038 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6039 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006040 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006041 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006042 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006043 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006044 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006045
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006046 if (space->next->type != TOK_PREPROC_ID &&
6047 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006048 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006049
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006050 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006051 l->next = predef;
6052 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006053 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006054 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08006055
6056 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006057}
6058
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006059static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006060{
6061 Token *def, *space;
6062 Line *l;
6063
H. Peter Anvin734b1882002-04-30 21:01:08 +00006064 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6065 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006066 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006067
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006068 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006069 l->next = predef;
6070 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006071 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006072 predef = l;
6073}
6074
H. Peter Anvin05990342018-06-11 13:32:42 -07006075/* Insert an early preprocessor command that doesn't need special handling */
6076static void pp_pre_command(const char *what, char *string)
6077{
6078 char *cmd;
6079 Token *def, *space;
6080 Line *l;
6081
6082 def = tokenize(string);
6083 if (what) {
6084 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6085 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
6086 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6087 }
6088
6089 l = nasm_malloc(sizeof(Line));
6090 l->next = predef;
6091 l->first = def;
6092 l->finishes = NULL;
6093 predef = l;
6094}
6095
H. Peter Anvinf7606612016-07-13 14:23:48 -07006096static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006097{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006098 macros_t **mp;
6099
6100 /* Find the end of the list and avoid duplicates */
6101 for (mp = stdmacros; *mp; mp++) {
6102 if (*mp == macros)
6103 return; /* Nothing to do */
6104 }
6105
6106 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6107
6108 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006109}
6110
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006111static void pp_extra_stdmac(macros_t *macros)
6112{
6113 extrastdmac = macros;
6114}
6115
H. Peter Anvin8b262472019-02-26 14:00:54 -08006116static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006117{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006118 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006119 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6120 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6121}
6122
6123static Token *make_tok_qstr(const char *str)
6124{
6125 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006126 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006127 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006128}
6129
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006130static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006131{
6132 if (!m)
6133 return;
6134
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006135 /* We need to print the mstk.mmac list in reverse order */
6136 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006137
6138 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006139 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006140 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006141 }
6142}
6143
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006144static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006145{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006146 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006147
H. Peter Anvinddb29062018-12-11 00:06:29 -08006148 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6149 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006150
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006151 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006152 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006153
H. Peter Anvinddb29062018-12-11 00:06:29 -08006154 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006155}
6156
H. Peter Anvine7469712016-02-18 02:20:59 -08006157const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006158 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006159 pp_reset,
6160 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006161 pp_cleanup_pass,
6162 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006163 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006164 pp_pre_define,
6165 pp_pre_undefine,
6166 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006167 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006168 pp_include_path,
6169 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006170};