blob: c8d86656c3821534f413c19628d61e6725b549c5 [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
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002323 /*
2324 * DO NOT skip whitespace here, or we won't be able to distinguish:
2325 *
2326 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2327 * %define bar(a,b) ; two arguments, empty expansion
2328 *
2329 * This ambiguity was inherited from C.
2330 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002331
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002332 if (!tok_is_(t, "("))
2333 goto finish;
2334
2335 if (tmpl) {
2336 Token *tx = t;
2337 Token **txpp = &tx;
2338 int sparam;
2339
2340 /* Count parameters first */
2341 sparam = parse_smacro_template(&txpp, NULL);
2342 if (!sparam)
2343 goto finish; /* No parameters, we're done */
2344 nasm_newn(params, sparam);
2345 }
2346
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002347 /* Skip leading paren */
2348 tn = &t->next;
2349 t = *tn;
2350
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002351 name = NULL;
2352 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002353 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002354
2355 while (!done) {
2356 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002357 if (name || flags)
2358 nasm_nonfatal("`)' expected to terminate macro template");
2359 else
2360 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002361 break;
2362 }
2363
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002364 switch (t->type) {
2365 case TOK_ID:
2366 if (name)
2367 goto bad;
2368 name = t;
2369 break;
2370
2371 case TOK_OTHER:
2372 if (t->text[1])
2373 goto bad;
2374 switch (t->text[0]) {
2375 case '=':
2376 flags |= SPARM_EVAL;
2377 break;
2378 case '&':
2379 flags |= SPARM_STR;
2380 break;
2381 case '!':
2382 flags |= SPARM_NOSTRIP;
2383 break;
2384 case '+':
2385 flags |= SPARM_GREEDY;
2386 greedy = true;
2387 break;
2388 case ',':
2389 if (greedy)
2390 nasm_nonfatal("greedy parameter must be last");
2391 /* fall through */
2392 case ')':
2393 if (params) {
2394 if (name) {
2395 params[nparam].name = name->text;
2396 params[nparam].namelen = name->len;
2397 name->text = NULL;
2398 }
2399 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002400 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002401 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002402 name = NULL;
2403 flags = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002404 done = t->text[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002405 break;
2406 default:
2407 goto bad;
2408 }
2409 break;
2410
2411 case TOK_WHITESPACE:
2412 break;
2413
2414 default:
2415 bad:
2416 if (!err) {
2417 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2418 err = true;
2419 }
2420 break;
2421 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002422
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002423 tn = &t->next;
2424 t = *tn;
2425 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002426
2427finish:
2428 while (t && t->type == TOK_WHITESPACE) {
2429 tn = &t->next;
2430 t = t->next;
2431 }
2432 *tpp = tn;
2433 if (tmpl) {
2434 tmpl->nparam = nparam;
2435 tmpl->greedy = greedy;
2436 tmpl->params = params;
2437 }
2438 return nparam;
2439}
2440
2441/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002442 * Common code for defining an smacro. The tmpl argument, if not NULL,
2443 * contains any macro parameters that aren't explicit arguments;
2444 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002445 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002446static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002447 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002448{
2449 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002450 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002451 Context *ctx;
2452 bool defining_alias = false;
2453 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002454
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002455 if (tmpl) {
2456 defining_alias = tmpl->alias;
2457 nparam = tmpl->nparam;
2458 }
2459
2460 while (1) {
2461 ctx = get_ctx(mname, &mname);
2462
2463 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2464 /* Create a new macro */
2465 smtbl = ctx ? &ctx->localmac : &smacros;
2466 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2467 nasm_new(smac);
2468 smac->next = *smhead;
2469 *smhead = smac;
2470 break;
2471 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002472 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002473 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002474 /*
2475 * Some instances of the old code considered this a failure,
2476 * some others didn't. What is the right thing to do here?
2477 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002478 goto fail;
2479 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002480 /*
2481 * We're redefining, so we have to take over an
2482 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002483 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002484 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002485 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002486 break;
2487 } else if (smac->in_progress) {
2488 nasm_nonfatal("macro alias loop");
2489 goto fail;
2490 } else {
2491 /* It is an alias macro; follow the alias link */
2492 SMacro *s;
2493
2494 smac->in_progress = true;
2495 s = define_smacro(smac->expansion->text, casesense,
2496 expansion, tmpl);
2497 smac->in_progress = false;
2498 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002499 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002500 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002501
2502 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002503 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002504 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002505 smac->expand = smacro_expand_default;
2506 if (tmpl) {
2507 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002508 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002509 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002510 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002511 if (tmpl->expand)
2512 smac->expand = tmpl->expand;
2513 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002514 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002515 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2516 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002517 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002518 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002519
2520fail:
2521 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002522 if (tmpl)
2523 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002524 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002525}
2526
2527/*
2528 * Undefine an smacro
2529 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002530static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002531{
2532 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002533 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002534 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002535
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002536 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002537 smtbl = ctx ? &ctx->localmac : &smacros;
2538 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002539
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002540 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002541 /*
2542 * We now have a macro name... go hunt for it.
2543 */
2544 sp = smhead;
2545 while ((s = *sp) != NULL) {
2546 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002547 if (s->alias && !undefalias) {
2548 if (s->in_progress) {
2549 nasm_nonfatal("macro alias loop");
2550 } else {
2551 s->in_progress = true;
2552 undef_smacro(s->expansion->text, false);
2553 s->in_progress = false;
2554 }
2555 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002556 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002557 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2558 ctx, s);
2559 *sp = s->next;
2560 free_smacro(s);
2561 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 } else {
2563 sp = &s->next;
2564 }
2565 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002566 }
2567}
2568
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002569/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002570 * Parse a mmacro specification.
2571 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002572static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002573{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002574 tline = tline->next;
2575 skip_white_(tline);
2576 tline = expand_id(tline);
2577 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002578 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002579 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002580 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002581
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002582#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002583 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002584#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002585 def->name = nasm_strdup(tline->text);
2586 def->plus = false;
2587 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002588 def->nparam_min = 0;
2589 def->nparam_max = 0;
2590
H. Peter Anvina26433d2008-07-16 14:40:01 -07002591 tline = expand_smacro(tline->next);
2592 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002593 if (!tok_type_(tline, TOK_NUMBER))
2594 nasm_nonfatal("`%s' expects a parameter count", directive);
2595 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002596 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002597 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002598 tline = tline->next->next;
2599 if (tok_is_(tline, "*")) {
2600 def->nparam_max = INT_MAX;
2601 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002602 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002604 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002606 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002607 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 }
2609 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002610 }
2611 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 tline = tline->next;
2613 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002614 }
2615 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 !nasm_stricmp(tline->next->text, ".nolist")) {
2617 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002618 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002619 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620
H. Peter Anvina26433d2008-07-16 14:40:01 -07002621 /*
2622 * Handle default parameters.
2623 */
2624 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 def->dlist = tline->next;
2626 tline->next = NULL;
2627 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002628 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 def->dlist = NULL;
2630 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002631 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002632 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002633
H. Peter Anvin89cee572009-07-15 09:16:54 -04002634 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002635 !def->plus) {
2636 /*
2637 *!macro-defaults [on] macros with more default than optional parameters
2638 *! warns when a macro has more default parameters than optional parameters.
2639 *! See \k{mlmacdef} for why might want to disable this warning.
2640 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002641 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002642 "too many default macro parameters in macro `%s'", def->name);
2643 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002644
H. Peter Anvina26433d2008-07-16 14:40:01 -07002645 return true;
2646}
2647
2648
2649/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002650 * Decode a size directive
2651 */
2652static int parse_size(const char *str) {
2653 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002655 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002657 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002658}
2659
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002660/*
2661 * Process a preprocessor %pragma directive. Currently there are none.
2662 * Gets passed the token list starting with the "preproc" token from
2663 * "%pragma preproc".
2664 */
2665static void do_pragma_preproc(Token *tline)
2666{
2667 /* Skip to the real stuff */
2668 tline = tline->next;
2669 skip_white_(tline);
2670 if (!tline)
2671 return;
2672
2673 (void)tline; /* Nothing else to do at present */
2674}
2675
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002676static bool is_macro_id(const Token *t)
2677{
2678 return t && (t->type == TOK_ID ||
2679 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2680}
2681
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002682static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002683{
2684 char *id;
2685 Token *t = *tp;
2686
2687 t = t->next; /* Skip directive */
2688 skip_white_(t);
2689 t = expand_id(t);
2690
2691 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002692 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002693 err ? err : "macro identifier");
2694 return NULL;
2695 }
2696
2697 id = t->text;
2698 skip_white_(t);
2699 *tp = t;
2700 return id;
2701}
2702
Ed Beroset3ab3f412002-06-11 03:31:49 +00002703/**
2704 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002705 * Find out if a line contains a preprocessor directive, and deal
2706 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002707 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002708 * If a directive _is_ found, it is the responsibility of this routine
2709 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002710 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002711 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002712 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002713 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002714 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002715 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002716static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002717{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002718 enum preproc_token i;
2719 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002720 bool err;
2721 int nparam;
2722 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002723 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002724 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002725 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002726 char *p, *pp;
2727 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002728 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002729 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002730 Include *inc;
2731 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002732 Cond *cond;
2733 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002734 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002735 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002736 struct tokenval tokval;
2737 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002738 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002739 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002740 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002741 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002742
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002743 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002744 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002745
H. Peter Anvineba20a72002-04-30 20:53:55 +00002746 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002747 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002748 (tline->text[0] && (tline->text[1] == '%' ||
2749 tline->text[1] == '$' ||
2750 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002751 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002752
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002753 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002754 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002755
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002756 casesense = true;
2757 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2758 casesense = false;
2759 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002760 }
2761
2762 /*
2763 * If we're in a non-emitting branch of a condition construct,
2764 * or walking to the end of an already terminated %rep block,
2765 * we should ignore all directives except for condition
2766 * directives.
2767 */
2768 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002769 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2770 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002771 return NO_DIRECTIVE_FOUND;
2772 }
2773
2774 /*
2775 * If we're defining a macro or reading a %rep block, we should
2776 * ignore all directives except for %macro/%imacro (which nest),
2777 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2778 * If we're in a %rep block, another %rep nests, so should be let through.
2779 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002780 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002781 i != PP_ENDMACRO && i != PP_ENDM &&
2782 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2783 return NO_DIRECTIVE_FOUND;
2784 }
2785
2786 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002787 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002788 nested_mac_count++;
2789 return NO_DIRECTIVE_FOUND;
2790 } else if (nested_mac_count > 0) {
2791 if (i == PP_ENDMACRO) {
2792 nested_mac_count--;
2793 return NO_DIRECTIVE_FOUND;
2794 }
2795 }
2796 if (!defining->name) {
2797 if (i == PP_REP) {
2798 nested_rep_count++;
2799 return NO_DIRECTIVE_FOUND;
2800 } else if (nested_rep_count > 0) {
2801 if (i == PP_ENDREP) {
2802 nested_rep_count--;
2803 return NO_DIRECTIVE_FOUND;
2804 }
2805 }
2806 }
2807 }
2808
H. Peter Anvin4169a472007-09-12 01:29:43 +00002809 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002810 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002811 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002813
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002814 case PP_PRAGMA:
2815 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002816 * %pragma namespace options...
2817 *
2818 * The namespace "preproc" is reserved for the preprocessor;
2819 * all other namespaces generate a [pragma] assembly directive.
2820 *
2821 * Invalid %pragmas are ignored and may have different
2822 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002823 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002824 t = tline;
2825 tline = tline->next;
2826 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002827 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002828 while (tok_type_(tline, TOK_WHITESPACE)) {
2829 t = tline;
2830 tline = tline->next;
2831 delete_Token(t);
2832 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002833 if (tok_type_(tline, TOK_ID)) {
2834 if (!nasm_stricmp(tline->text, "preproc")) {
2835 /* Preprocessor pragma */
2836 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002837 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002838 } else {
2839 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002840
2841 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002842 for (t = tline; t->next; t = t->next)
2843 ;
2844 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002845
2846 /* Prepend "[pragma " */
2847 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2848 t = new_Token(t, TOK_ID, "pragma", 6);
2849 t = new_Token(t, TOK_OTHER, "[", 1);
2850 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002851 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002852 }
2853 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002854 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002855
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 case PP_STACKSIZE:
2857 /* Directive to tell NASM what the default stack size is. The
2858 * default is for a 16-bit stack, and this can be overriden with
2859 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 */
2861 tline = tline->next;
2862 if (tline && tline->type == TOK_WHITESPACE)
2863 tline = tline->next;
2864 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002865 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 }
2867 if (nasm_stricmp(tline->text, "flat") == 0) {
2868 /* All subsequent ARG directives are for a 32-bit stack */
2869 StackSize = 4;
2870 StackPointer = "ebp";
2871 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002872 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002873 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2874 /* All subsequent ARG directives are for a 64-bit stack */
2875 StackSize = 8;
2876 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002877 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002878 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 } else if (nasm_stricmp(tline->text, "large") == 0) {
2880 /* All subsequent ARG directives are for a 16-bit stack,
2881 * far function call.
2882 */
2883 StackSize = 2;
2884 StackPointer = "bp";
2885 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002886 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 } else if (nasm_stricmp(tline->text, "small") == 0) {
2888 /* All subsequent ARG directives are for a 16-bit stack,
2889 * far function call. We don't support near functions.
2890 */
2891 StackSize = 2;
2892 StackPointer = "bp";
2893 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002894 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002895 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002896 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002898 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002899
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 case PP_ARG:
2901 /* TASM like ARG directive to define arguments to functions, in
2902 * the following form:
2903 *
2904 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2905 */
2906 offset = ArgOffset;
2907 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002908 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002910
H. Peter Anvine2c80182005-01-15 22:15:51 +00002911 /* Find the argument name */
2912 tline = tline->next;
2913 if (tline && tline->type == TOK_WHITESPACE)
2914 tline = tline->next;
2915 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002916 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002917 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 }
2919 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002920
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 /* Find the argument size type */
2922 tline = tline->next;
2923 if (!tline || tline->type != TOK_OTHER
2924 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002925 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002926 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 }
2928 tline = tline->next;
2929 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002930 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002931 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002933
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002935 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002937 size = parse_size(tt->text);
2938 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002939 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002940 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002941 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 }
2943 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002944
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002945 /* Round up to even stack slots */
2946 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002947
H. Peter Anvine2c80182005-01-15 22:15:51 +00002948 /* Now define the macro for the argument */
2949 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2950 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002951 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002952 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002953
H. Peter Anvine2c80182005-01-15 22:15:51 +00002954 /* Move to the next argument in the list */
2955 tline = tline->next;
2956 if (tline && tline->type == TOK_WHITESPACE)
2957 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002958 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002959 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002960 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002961
H. Peter Anvine2c80182005-01-15 22:15:51 +00002962 case PP_LOCAL:
2963 /* TASM like LOCAL directive to define local variables for a
2964 * function, in the following form:
2965 *
2966 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2967 *
2968 * The '= LocalSize' at the end is ignored by NASM, but is
2969 * required by TASM to define the local parameter size (and used
2970 * by the TASM macro package).
2971 */
2972 offset = LocalOffset;
2973 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002974 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002976
H. Peter Anvine2c80182005-01-15 22:15:51 +00002977 /* Find the argument name */
2978 tline = tline->next;
2979 if (tline && tline->type == TOK_WHITESPACE)
2980 tline = tline->next;
2981 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002982 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002983 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 }
2985 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002986
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 /* Find the argument size type */
2988 tline = tline->next;
2989 if (!tline || tline->type != TOK_OTHER
2990 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002991 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002992 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 }
2994 tline = tline->next;
2995 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002996 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002997 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002999
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003001 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003003 size = parse_size(tt->text);
3004 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003005 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003007 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 }
3009 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003010
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003011 /* Round up to even stack slots */
3012 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003013
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003014 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003015
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003016 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3018 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003019 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003020
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 /* Now define the assign to setup the enter_c macro correctly */
3022 snprintf(directive, sizeof(directive),
3023 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003024 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003025
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 /* Move to the next argument in the list */
3027 tline = tline->next;
3028 if (tline && tline->type == TOK_WHITESPACE)
3029 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003030 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003031 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003032 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003033
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 case PP_CLEAR:
3035 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003036 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003037 free_macros();
3038 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003039 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003040
H. Peter Anvin418ca702008-05-30 10:42:30 -07003041 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003042 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003043 skip_white_(t);
3044 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003045 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003046 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003047 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003048 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003049 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003050 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003051 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003052 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003053 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003054 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003055 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003056
3057 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003058 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003059 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003060
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003061 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003062 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003063 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003064 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003066 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003067 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003068 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003069 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003070 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003071 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003073 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003074 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003075 (pp_mode == PP_DEPS)
3076 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003077 if (!inc->fp) {
3078 /* -MG given but file not found */
3079 nasm_free(inc);
3080 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003081 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003082 inc->lineno = src_set_linnum(0);
3083 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003084 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003085 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003086 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003087 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003088 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003089
H. Peter Anvind2456592008-06-19 15:04:18 -07003090 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003091 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003092 const struct use_package *pkg;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003093
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003094 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003095 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07003096 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003097 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003098 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003099 nasm_unquote_cstr(tline->text, NULL);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003100 pkg = nasm_find_use_package(tline->text);
3101 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003102 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003103 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003104 /*
3105 * Not already included, go ahead and include it.
3106 * Treat it as an include file for the purpose of
3107 * producing a listing.
3108 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003109 use_loaded[pkg->index] = true;
3110 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003111 nasm_new(inc);
3112 inc->next = istk;
3113 inc->fname = src_set_fname(NULL);
3114 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003115 inc->nolist = !list_option('b') || istk->nolist;
3116 istk = inc;
3117 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003118 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003119 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003120 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003123 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 tline = tline->next;
3125 skip_white_(tline);
3126 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003127 if (tline) {
3128 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003129 nasm_nonfatal("`%s' expects a context identifier",
3130 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003131 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 }
3133 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003134 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003135 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003136 p = nasm_strdup(tline->text);
3137 } else {
3138 p = NULL; /* Anonymous */
3139 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003140
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003142 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003143 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003144 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003145 ctx->name = p;
3146 ctx->number = unique++;
3147 cstk = ctx;
3148 } else {
3149 /* %pop or %repl */
3150 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003151 nasm_nonfatal("`%s': context stack is empty",
3152 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003153 } else if (i == PP_POP) {
3154 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003155 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003156 "expected %s",
3157 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003158 else
3159 ctx_pop();
3160 } else {
3161 /* i == PP_REPL */
3162 nasm_free(cstk->name);
3163 cstk->name = p;
3164 p = NULL;
3165 }
3166 nasm_free(p);
3167 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003168 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003169 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 severity = ERR_FATAL;
3171 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003173 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003175 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003176 /*!
3177 *!user [on] %warning directives
3178 *! controls output of \c{%warning} directives (see \k{pperror}).
3179 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003180 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003182
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003183issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003184 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003185 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003186 tline->next = expand_smacro(tline->next);
3187 tline = tline->next;
3188 skip_white_(tline);
3189 t = tline ? tline->next : NULL;
3190 skip_white_(t);
3191 if (tok_type_(tline, TOK_STRING) && !t) {
3192 /* The line contains only a quoted string */
3193 p = tline->text;
3194 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003195 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003196 } else {
3197 /* Not a quoted string, or more than a quoted string */
3198 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003199 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 nasm_free(p);
3201 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003202 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003203 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003204
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003205 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003206 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003207 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003208 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003209 j = if_condition(tline->next, i);
3210 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003211 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003212 cond = nasm_malloc(sizeof(Cond));
3213 cond->next = istk->conds;
3214 cond->state = j;
3215 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003216 if(istk->mstk.mstk)
3217 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003218 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003219
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003220 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003221 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003222 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003223 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003224 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003225 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003226 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003227
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003228 case COND_DONE:
3229 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003230 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003231
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003232 case COND_ELSE_TRUE:
3233 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003234 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003235 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003236 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003237 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003238
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239 case COND_IF_FALSE:
3240 /*
3241 * IMPORTANT: In the case of %if, we will already have
3242 * called expand_mmac_params(); however, if we're
3243 * processing an %elif we must have been in a
3244 * non-emitting mode, which would have inhibited
3245 * the normal invocation of expand_mmac_params().
3246 * Therefore, we have to do it explicitly here.
3247 */
3248 j = if_condition(expand_mmac_params(tline->next), i);
3249 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003250 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003251 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003253 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003254
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 case PP_ELSE:
3256 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003257 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003258 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003259 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003260 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003261 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003262 case COND_IF_TRUE:
3263 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003264 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003265 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003266
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003267 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003268 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003269
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003270 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003271 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003272 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003273
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003274 case COND_ELSE_TRUE:
3275 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003276 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003277 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003278 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003279 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003280 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003281 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003282
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 case PP_ENDIF:
3284 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003285 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003286 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003287 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003288 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003289 cond = istk->conds;
3290 istk->conds = cond->next;
3291 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003292 if(istk->mstk.mstk)
3293 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003294 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003295
H. Peter Anvin8b262472019-02-26 14:00:54 -08003296 case PP_RMACRO:
3297 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003298 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003299 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003300 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003301 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003302 if (i == PP_RMACRO)
3303 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003304 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003305 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003306 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003307 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003308
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003309 src_get(&defining->xline, &defining->fname);
3310
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003311 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3312 while (mmac) {
3313 if (!strcmp(mmac->name, defining->name) &&
3314 (mmac->nparam_min <= defining->nparam_max
3315 || defining->plus)
3316 && (defining->nparam_min <= mmac->nparam_max
3317 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003318 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003319 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003320 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003321 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003322 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003323 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003324 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003325
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 case PP_ENDM:
3327 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003328 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003329 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003330 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003331 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003332 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3333 defining->next = *mmhead;
3334 *mmhead = defining;
3335 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003336 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003337
H. Peter Anvin89cee572009-07-15 09:16:54 -04003338 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003339 /*
3340 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003341 * macro-end marker for a macro with a name. Then we
3342 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003343 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003344 list_for_each(l, istk->expansion)
3345 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003346 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003347
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003348 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003349 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003350 * Remove all conditional entries relative to this
3351 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003352 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003353 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3354 cond = istk->conds;
3355 istk->conds = cond->next;
3356 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003357 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003358 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003359 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003360 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003361 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003362 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003363
H. Peter Anvina26433d2008-07-16 14:40:01 -07003364 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003365 casesense = false;
3366 /* fall through */
3367 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003368 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003369 MMacro **mmac_p;
3370 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003371
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003372 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003373 spec.casesense = casesense;
3374 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003375 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003377 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3378 while (mmac_p && *mmac_p) {
3379 mmac = *mmac_p;
3380 if (mmac->casesense == spec.casesense &&
3381 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3382 mmac->nparam_min == spec.nparam_min &&
3383 mmac->nparam_max == spec.nparam_max &&
3384 mmac->plus == spec.plus) {
3385 *mmac_p = mmac->next;
3386 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003387 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003388 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003389 }
3390 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003391 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003392 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003393 }
3394
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 case PP_ROTATE:
3396 if (tline->next && tline->next->type == TOK_WHITESPACE)
3397 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003398 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003400 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003401 return DIRECTIVE_FOUND;
3402 }
3403 t = expand_smacro(tline->next);
3404 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003405 pps.tptr = tline = t;
3406 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 tokval.t_type = TOKEN_INVALID;
3408 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003409 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003410 free_tlist(tline);
3411 if (!evalresult)
3412 return DIRECTIVE_FOUND;
3413 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003414 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003416 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003417 return DIRECTIVE_FOUND;
3418 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003419 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003420 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003421 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003422 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003423 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003424 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003425 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003426
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003427 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003428 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003429 rotate += mmac->nparam;
3430
3431 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003432 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003433 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003434
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003436 {
3437 MMacro *tmp_defining;
3438
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003439 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003440 do {
3441 tline = tline->next;
3442 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003443
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 if (tok_type_(tline, TOK_ID) &&
3445 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003446 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 do {
3448 tline = tline->next;
3449 } while (tok_type_(tline, TOK_WHITESPACE));
3450 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003451
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003453 pps.tptr = expand_smacro(tline);
3454 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003455 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003456 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003458 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003459 if (!evalresult)
3460 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003461 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003462 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003464 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003465 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003466 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003467 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003468 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003469 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3470 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003471 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003472 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003473 /*!
3474 *!negative-rep [on] regative %rep count
3475 *! warns about negative counts given to the \c{%rep}
3476 *! preprocessor directive.
3477 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003478 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003479 "negative `%%rep' count: %"PRId64, count);
3480 count = 0;
3481 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003482 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003483 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003484 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003485 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003486 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003488 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003489 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003490 defining->nolist = nolist;
3491 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003492 defining->mstk = istk->mstk;
3493 defining->dstk.mstk = tmp_defining;
3494 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003495 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003496 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003497 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003498
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003500 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003501 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003502 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003503 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003504
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 /*
3506 * Now we have a "macro" defined - although it has no name
3507 * and we won't be entering it in the hash tables - we must
3508 * push a macro-end marker for it on to istk->expansion.
3509 * After that, it will take care of propagating itself (a
3510 * macro-end marker line for a macro which is really a %rep
3511 * block will cause the macro to be re-expanded, complete
3512 * with another macro-end marker to ensure the process
3513 * continues) until the whole expansion is forcibly removed
3514 * from istk->expansion by a %exitrep.
3515 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003516 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003517 l->next = istk->expansion;
3518 l->finishes = defining;
3519 l->first = NULL;
3520 istk->expansion = l;
3521
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003522 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003523
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003524 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003525 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003526 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003527
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003529 /*
3530 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003531 * macro-end marker for a macro with no name. Then we set
3532 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003533 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003534 list_for_each(l, istk->expansion)
3535 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003536 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003537
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003538 if (l)
3539 l->finishes->in_progress = 1;
3540 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003541 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003542 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003543
H. Peter Anvin8b262472019-02-26 14:00:54 -08003544 case PP_DEFINE:
3545 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003546 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003547 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003548 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003549 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003550
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003551 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003552 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003553
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003554 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003555 lastp = &tline->next;
3556 nparam = parse_smacro_template(&lastp, &tmpl);
3557 tline = *lastp;
3558 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003559
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003560 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003561 macro_start = tline;
3562 if (!is_macro_id(macro_start)) {
3563 nasm_nonfatal("`%s' expects a macro identifier to alias",
3564 dname);
3565 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003566 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003567 tt = macro_start->next;
3568 macro_start->next = NULL;
3569 tline = tline->next;
3570 skip_white_(tline);
3571 if (tline && tline->type) {
3572 nasm_warn(WARN_OTHER,
3573 "trailing garbage after aliasing identifier ignored");
3574 }
3575 free_tlist(tt);
3576 tmpl.alias = true;
3577 } else {
3578 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003579 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003580 tline = expand_smacro(tline);
3581
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003582 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003583 macro_start = NULL;
3584 t = tline;
3585 while (t) {
3586 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003587 for (i = 0; i < nparam; i++) {
3588 if ((size_t)tmpl.params[i].namelen == t->len &&
3589 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3590 t->type = tok_smac_param(i);
3591 break;
3592 }
3593 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003594 }
3595 tt = t->next;
3596 t->next = macro_start;
3597 macro_start = t;
3598 t = tt;
3599 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003601
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 /*
3603 * Good. We now have a macro name, a parameter count, and a
3604 * token list (in reverse order) for an expansion. We ought
3605 * to be OK just to create an SMacro, store it, and let
3606 * free_tlist have the rest of the line (which we have
3607 * carefully re-terminated after chopping off the expansion
3608 * from the end).
3609 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003610 define_smacro(mname, casesense, macro_start, &tmpl);
3611 break;
3612 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003613
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003615 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003616 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003617 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003618 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003619 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003620
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003621 undef_smacro(mname, i == PP_UNDEFALIAS);
3622 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003623
H. Peter Anvin8b262472019-02-26 14:00:54 -08003624 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003625 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003626 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003627
H. Peter Anvin9e200162008-06-04 17:23:14 -07003628 last = tline;
3629 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003630 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003631
3632 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003633 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003634
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003636 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003637 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003638
3639 /*
3640 * We now have a macro name, an implicit parameter count of
3641 * zero, and a string token to use as an expansion. Create
3642 * and store an SMacro.
3643 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003644 define_smacro(mname, casesense, macro_start, NULL);
3645 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003646
H. Peter Anvin8b262472019-02-26 14:00:54 -08003647 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003648 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003649 goto done;
3650
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003651 last = tline;
3652 tline = expand_smacro(tline->next);
3653 last->next = NULL;
3654
3655 t = tline;
3656 while (tok_type_(t, TOK_WHITESPACE))
3657 t = t->next;
3658 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003659 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003660 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003661 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003662 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003663 }
3664
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003665 /*
3666 * Convert the string to a token stream. Note that smacros
3667 * are stored with the token stream reversed, so we have to
3668 * reverse the output of tokenize().
3669 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003670 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003671 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003672
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003673 /*
3674 * We now have a macro name, an implicit parameter count of
3675 * zero, and a numeric token to use as an expansion. Create
3676 * and store an SMacro.
3677 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003678 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003679 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003680 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003681
H. Peter Anvin418ca702008-05-30 10:42:30 -07003682 case PP_PATHSEARCH:
3683 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003684 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003685
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003686 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003687 goto done;
3688
H. Peter Anvin418ca702008-05-30 10:42:30 -07003689 last = tline;
3690 tline = expand_smacro(tline->next);
3691 last->next = NULL;
3692
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003693 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003694 while (tok_type_(t, TOK_WHITESPACE))
3695 t = t->next;
3696
3697 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003698 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003699 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003700 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003701 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003702 }
3703 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003704 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003706 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003707 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003708
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003709 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003710 if (!found_path)
3711 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003712 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003713
3714 /*
3715 * We now have a macro name, an implicit parameter count of
3716 * zero, and a string token to use as an expansion. Create
3717 * and store an SMacro.
3718 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003719 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003720 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003721 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003722 }
3723
H. Peter Anvine2c80182005-01-15 22:15:51 +00003724 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003725 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003726 goto done;
3727
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 last = tline;
3729 tline = expand_smacro(tline->next);
3730 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003731
H. Peter Anvine2c80182005-01-15 22:15:51 +00003732 t = tline;
3733 while (tok_type_(t, TOK_WHITESPACE))
3734 t = t->next;
3735 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003736 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003737 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 free_tlist(tline);
3739 free_tlist(origline);
3740 return DIRECTIVE_FOUND;
3741 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003742
H. Peter Anvin8b262472019-02-26 14:00:54 -08003743 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003744
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 /*
3746 * We now have a macro name, an implicit parameter count of
3747 * zero, and a numeric token to use as an expansion. Create
3748 * and store an SMacro.
3749 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003750 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003751 free_tlist(tline);
3752 free_tlist(origline);
3753 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003754
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003755 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003756 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003757 goto done;
3758
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003759 last = tline;
3760 tline = expand_smacro(tline->next);
3761 last->next = NULL;
3762
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003763 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003764 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003765 switch (t->type) {
3766 case TOK_WHITESPACE:
3767 break;
3768 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003769 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003770 break;
3771 case TOK_OTHER:
3772 if (!strcmp(t->text, ",")) /* permit comma separators */
3773 break;
3774 /* else fall through */
3775 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003776 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003777 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003778 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003779 }
3780 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003781
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003782 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003783 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003784 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003785 memcpy(p, t->text, t->len);
3786 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003787 }
3788 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003789
3790 /*
3791 * We now have a macro name, an implicit parameter count of
3792 * zero, and a numeric token to use as an expansion. Create
3793 * and store an SMacro.
3794 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003795 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003796 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003797 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003798 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003799 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003800
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003802 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003803 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003804 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003805
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003806 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003807 goto done;
3808
H. Peter Anvine2c80182005-01-15 22:15:51 +00003809 last = tline;
3810 tline = expand_smacro(tline->next);
3811 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003812
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003813 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003814 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003815 while (tok_type_(t, TOK_WHITESPACE))
3816 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003817
H. Peter Anvine2c80182005-01-15 22:15:51 +00003818 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003819 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003820 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003821 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003822 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003823 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003824
H. Peter Anvin8b262472019-02-26 14:00:54 -08003825 pps.tptr = t->next;
3826 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003827 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003828 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003829 if (!evalresult) {
3830 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003831 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003832 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003833 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003834 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003835 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003837 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003838
H. Peter Anvin8b262472019-02-26 14:00:54 -08003839 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3840 pps.tptr = pps.tptr->next;
3841 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003842 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003843 } else {
3844 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003845 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003846 if (!evalresult) {
3847 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003848 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003849 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003850 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003851 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003852 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003853 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003854 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003855 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003856
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003857 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003858
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003859 /* make start and count being in range */
3860 if (start < 0)
3861 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003862 if (count < 0)
3863 count = len + count + 1 - start;
3864 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003865 count = len - start;
3866 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003867 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003868
H. Peter Anvin8b262472019-02-26 14:00:54 -08003869 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003870 macro_start->len = count;
3871 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3872 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003873
H. Peter Anvine2c80182005-01-15 22:15:51 +00003874 /*
3875 * We now have a macro name, an implicit parameter count of
3876 * zero, and a numeric token to use as an expansion. Create
3877 * and store an SMacro.
3878 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003879 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003880 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003881 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003882 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003883
H. Peter Anvin8b262472019-02-26 14:00:54 -08003884 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003885 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003886 goto done;
3887
H. Peter Anvine2c80182005-01-15 22:15:51 +00003888 last = tline;
3889 tline = expand_smacro(tline->next);
3890 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003891
H. Peter Anvin8b262472019-02-26 14:00:54 -08003892 pps.tptr = tline;
3893 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003894 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003895 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003897 if (!evalresult)
3898 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003899
H. Peter Anvine2c80182005-01-15 22:15:51 +00003900 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003901 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003902
H. Peter Anvine2c80182005-01-15 22:15:51 +00003903 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003904 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003905 free_tlist(origline);
3906 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003907 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003908
H. Peter Anvin8b262472019-02-26 14:00:54 -08003909 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003910
H. Peter Anvine2c80182005-01-15 22:15:51 +00003911 /*
3912 * We now have a macro name, an implicit parameter count of
3913 * zero, and a numeric token to use as an expansion. Create
3914 * and store an SMacro.
3915 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003916 define_smacro(mname, casesense, macro_start, NULL);
3917 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003918
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 case PP_LINE:
3920 /*
3921 * Syntax is `%line nnn[+mmm] [filename]'
3922 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003923 if (unlikely(pp_noline))
3924 goto done;
3925
H. Peter Anvine2c80182005-01-15 22:15:51 +00003926 tline = tline->next;
3927 skip_white_(tline);
3928 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003929 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003930 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003931 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003932 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003933 m = 1;
3934 tline = tline->next;
3935 if (tok_is_(tline, "+")) {
3936 tline = tline->next;
3937 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003938 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003939 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003940 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003941 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003942 tline = tline->next;
3943 }
3944 skip_white_(tline);
3945 src_set_linnum(k);
3946 istk->lineinc = m;
3947 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003948 char *fname = detoken(tline, false);
3949 src_set_fname(fname);
3950 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003951 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003952 break;
3953 }
3954
3955done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956 free_tlist(origline);
3957 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003958}
3959
3960/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003961 * Ensure that a macro parameter contains a condition code and
3962 * nothing else. Return the condition code index if so, or -1
3963 * otherwise.
3964 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003966{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003967 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003968
H. Peter Anvin25a99342007-09-22 17:45:45 -07003969 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003970 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003971
H. Peter Anvineba20a72002-04-30 20:53:55 +00003972 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003973 if (!t)
3974 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003975 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003977 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003978 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003979 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003980 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003981
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003982 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003983}
3984
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003985/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003986 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003987 * pasting, if @handle_explicit passed then explicit pasting
3988 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003989 * The @m array can contain a series of token types which are
3990 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003991 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003992static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003993 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003994{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003995 Token *tok, *next, **prev_next, **prev_nonspace;
3996 bool pasted = false;
3997 char *buf, *p;
3998 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003999
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004000 /*
4001 * The last token before pasting. We need it
4002 * to be able to connect new handled tokens.
4003 * In other words if there were a tokens stream
4004 *
4005 * A -> B -> C -> D
4006 *
4007 * and we've joined tokens B and C, the resulting
4008 * stream should be
4009 *
4010 * A -> BC -> D
4011 */
4012 tok = *head;
4013 prev_next = NULL;
4014
4015 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
4016 prev_nonspace = head;
4017 else
4018 prev_nonspace = NULL;
4019
4020 while (tok && (next = tok->next)) {
4021
4022 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004023 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004024 /* Zap redundant whitespaces */
4025 while (tok_type_(next, TOK_WHITESPACE))
4026 next = delete_Token(next);
4027 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004028 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004029
4030 case TOK_PASTE:
4031 /* Explicit pasting */
4032 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004033 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004034 next = delete_Token(tok);
4035
4036 while (tok_type_(next, TOK_WHITESPACE))
4037 next = delete_Token(next);
4038
4039 if (!pasted)
4040 pasted = true;
4041
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004042 /* Left pasting token is start of line */
4043 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004044 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004045
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004046 /*
4047 * No ending token, this might happen in two
4048 * cases
4049 *
4050 * 1) There indeed no right token at all
4051 * 2) There is a bare "%define ID" statement,
4052 * and @ID does expand to whitespace.
4053 *
4054 * So technically we need to do a grammar analysis
4055 * in another stage of parsing, but for now lets don't
4056 * change the behaviour people used to. Simply allow
4057 * whitespace after paste token.
4058 */
4059 if (!next) {
4060 /*
4061 * Zap ending space tokens and that's all.
4062 */
4063 tok = (*prev_nonspace)->next;
4064 while (tok_type_(tok, TOK_WHITESPACE))
4065 tok = delete_Token(tok);
4066 tok = *prev_nonspace;
4067 tok->next = NULL;
4068 break;
4069 }
4070
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004071 tok = *prev_nonspace;
4072 while (tok_type_(tok, TOK_WHITESPACE))
4073 tok = delete_Token(tok);
4074 len = strlen(tok->text);
4075 len += strlen(next->text);
4076
4077 p = buf = nasm_malloc(len + 1);
4078 strcpy(p, tok->text);
4079 p = strchr(p, '\0');
4080 strcpy(p, next->text);
4081
4082 delete_Token(tok);
4083
4084 tok = tokenize(buf);
4085 nasm_free(buf);
4086
4087 *prev_nonspace = tok;
4088 while (tok && tok->next)
4089 tok = tok->next;
4090
4091 tok->next = delete_Token(next);
4092
4093 /* Restart from pasted tokens head */
4094 tok = *prev_nonspace;
4095 break;
4096
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004097 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004098 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004099 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004100 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4101 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004102
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004103 len = 0;
4104 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4105 len += strlen(next->text);
4106 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004107 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004108
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004109 /* No match or no text to process */
4110 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004111 break;
4112
4113 len += strlen(tok->text);
4114 p = buf = nasm_malloc(len + 1);
4115
Adam Majer1a069432017-07-25 11:12:35 +02004116 strcpy(p, tok->text);
4117 p = strchr(p, '\0');
4118 tok = delete_Token(tok);
4119
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004120 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004121 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4122 strcpy(p, tok->text);
4123 p = strchr(p, '\0');
4124 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004125 tok = delete_Token(tok);
4126 }
4127
4128 tok = tokenize(buf);
4129 nasm_free(buf);
4130
4131 if (prev_next)
4132 *prev_next = tok;
4133 else
4134 *head = tok;
4135
4136 /*
4137 * Connect pasted into original stream,
4138 * ie A -> new-tokens -> B
4139 */
4140 while (tok && tok->next)
4141 tok = tok->next;
4142 tok->next = next;
4143
4144 if (!pasted)
4145 pasted = true;
4146
4147 /* Restart from pasted tokens head */
4148 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004149 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004150
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004151 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004152 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004153
4154 prev_next = &tok->next;
4155
4156 if (tok->next &&
4157 !tok_type_(tok->next, TOK_WHITESPACE) &&
4158 !tok_type_(tok->next, TOK_PASTE))
4159 prev_nonspace = prev_next;
4160
4161 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004162 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004163
4164 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004165}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004166
4167/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004168 * Computes the proper rotation of mmacro parameters
4169 */
4170static int mmac_rotate(const MMacro *mac, unsigned int n)
4171{
4172 if (--n < mac->nparam)
4173 n = (n + mac->rotate) % mac->nparam;
4174
4175 return n+1;
4176}
4177
4178/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004179 * expands to a list of tokens from %{x:y}
4180 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004181static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004182{
4183 Token *t = tline, **tt, *tm, *head;
4184 char *pos;
4185 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004186
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004187 pos = strchr(tline->text, ':');
4188 nasm_assert(pos);
4189
4190 lst = atoi(pos + 1);
4191 fst = atoi(tline->text + 1);
4192
4193 /*
4194 * only macros params are accounted so
4195 * if someone passes %0 -- we reject such
4196 * value(s)
4197 */
4198 if (lst == 0 || fst == 0)
4199 goto err;
4200
4201 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004202 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4203 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204 goto err;
4205
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004206 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4207 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004208
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004209 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004210 fst--, lst--;
4211
4212 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004213 * It will be at least one token. Note we
4214 * need to scan params until separator, otherwise
4215 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004216 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004217 j = (fst + mac->rotate) % mac->nparam;
4218 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004219 if (!tm)
4220 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004221 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004222 tt = &head->next, tm = tm->next;
4223 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004224 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004225 *tt = t, tt = &t->next, tm = tm->next;
4226 }
4227
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004228 if (fst < lst) {
4229 for (i = fst + 1; i <= lst; i++) {
4230 t = new_Token(NULL, TOK_OTHER, ",", 0);
4231 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004232 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004233 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004234 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004235 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004236 *tt = t, tt = &t->next, tm = tm->next;
4237 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004238 }
4239 } else {
4240 for (i = fst - 1; i >= lst; i--) {
4241 t = new_Token(NULL, TOK_OTHER, ",", 0);
4242 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004243 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004244 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004245 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004246 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004247 *tt = t, tt = &t->next, tm = tm->next;
4248 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004249 }
4250 }
4251
4252 *last = tt;
4253 return head;
4254
4255err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004256 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004257 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004258 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004259}
4260
H. Peter Anvin76690a12002-04-30 20:52:49 +00004261/*
4262 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004263 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004264 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004265 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004266static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004267{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004268 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004269 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004270 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004271
4272 tail = &thead;
4273 thead = NULL;
4274
H. Peter Anvine2c80182005-01-15 22:15:51 +00004275 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004276 bool change;
4277 Token *t = tline;
4278 char *text = t->text;
4279 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004280
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004281 tline = tline->next;
4282 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004283
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004284 switch (type) {
4285 case TOK_PREPROC_ID:
4286 {
4287 Token *tt = NULL;
4288
4289 change = false;
4290
4291 if (!text || !text[0])
4292 break;
4293 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4294 ((text[1] == '+' || text[1] == '-') && text[2])))
4295 break;
4296
4297 change = true;
4298
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004299 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004300 nasm_nonfatal("`%s': not in a macro call", text);
4301 text = NULL;
4302 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004303 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004304
4305 if (strchr(text, ':')) {
4306 /*
4307 * seems we have a parameters range here
4308 */
4309 Token *head, **last;
4310 head = expand_mmac_params_range(mac, t, &last);
4311 if (head) {
4312 *tail = head;
4313 *last = tline;
4314 text = NULL;
4315 }
4316 break;
4317 }
4318
4319 switch (text[1]) {
4320 /*
4321 * We have to make a substitution of one of the
4322 * forms %1, %-1, %+1, %%foo, %0, %00.
4323 */
4324 case '0':
4325 if (!text[2]) {
4326 type = TOK_NUMBER;
4327 text = nasm_asprintf("%d", mac->nparam);
4328 break;
4329 }
4330 if (text[2] != '0' || text[3])
4331 goto invalid;
4332 /* a possible captured label == mac->params[0] */
4333 /* fall through */
4334 default:
4335 {
4336 unsigned long n;
4337 char *ep;
4338
4339 n = strtoul(text + 1, &ep, 10);
4340 if (unlikely(*ep))
4341 goto invalid;
4342
4343 if (n <= mac->nparam) {
4344 n = mmac_rotate(mac, n);
4345 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4346 }
4347 text = NULL;
4348 break;
4349 }
4350 case '%':
4351 type = TOK_ID;
4352 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4353 break;
4354 case '-':
4355 case '+':
4356 {
4357 int cc;
4358 unsigned long n;
4359 char *ep;
4360
4361 text = NULL;
4362
4363 n = strtoul(t->text + 2, &ep, 10);
4364 if (unlikely(*ep))
4365 goto invalid;
4366
4367 if (n && n < mac->nparam) {
4368 n = mmac_rotate(mac, n);
4369 tt = mac->params[n];
4370 }
4371 cc = find_cc(tt);
4372 if (cc == -1) {
4373 nasm_nonfatal("macro parameter `%s' is not a condition code",
4374 text);
4375 text = NULL;
4376 break;
4377 }
4378
4379 type = TOK_ID;
4380 if (text[1] == '-') {
4381 int ncc = inverse_ccs[cc];
4382 if (unlikely(ncc == -1)) {
4383 nasm_nonfatal("condition code `%s' is not invertible",
4384 conditions[cc]);
4385 break;
4386 }
4387 cc = ncc;
4388 }
4389 text = nasm_strdup(conditions[cc]);
4390 break;
4391 }
4392
4393 invalid:
4394 nasm_nonfatal("invalid macro parameter: `%s'", text);
4395 text = NULL;
4396 break;
4397 }
4398 break;
4399 }
4400
4401 case TOK_PREPROC_Q:
4402 if (mac) {
4403 type = TOK_ID;
4404 text = nasm_strdup(mac->iname);
4405 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004406 } else {
4407 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004408 }
4409 break;
4410
4411 case TOK_PREPROC_QQ:
4412 if (mac) {
4413 type = TOK_ID;
4414 text = nasm_strdup(mac->name);
4415 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004416 } else {
4417 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004418 }
4419 break;
4420
4421 case TOK_INDIRECT:
4422 {
4423 Token *tt;
4424
4425 tt = tokenize(t->text);
4426 tt = expand_mmac_params(tt);
4427 tt = expand_smacro(tt);
4428 /* Why dup_tlist() here? We should own tt... */
4429 dup_tlist(tt, &tail);
4430 text = NULL;
4431 change = true;
4432 break;
4433 }
4434
4435 default:
4436 change = false;
4437 break;
4438 }
4439
4440 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004441 if (!text) {
4442 delete_Token(t);
4443 } else {
4444 *tail = t;
4445 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004447 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004448 t->type = type;
4449 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004450 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004451 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004452 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004453 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004454 tail = &t->next;
4455 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004456 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004457
H. Peter Anvineba20a72002-04-30 20:53:55 +00004458 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004459
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004460 if (changed) {
4461 const struct tokseq_match t[] = {
4462 {
4463 PP_CONCAT_MASK(TOK_ID) |
4464 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4465 PP_CONCAT_MASK(TOK_ID) |
4466 PP_CONCAT_MASK(TOK_NUMBER) |
4467 PP_CONCAT_MASK(TOK_FLOAT) |
4468 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4469 },
4470 {
4471 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4472 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4473 }
4474 };
4475 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4476 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004477
H. Peter Anvin76690a12002-04-30 20:52:49 +00004478 return thead;
4479}
4480
H. Peter Anvin322bee02019-08-10 01:38:06 -07004481static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004482
H. Peter Anvin76690a12002-04-30 20:52:49 +00004483/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004484 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004485 * a macro at all, it is simply copied to the output and the pointer
4486 * advanced. tpp should be a pointer to a pointer (usually the next
4487 * pointer of the previous token) to the first token. **tpp is updated
4488 * to point to the last token of the expansion, and *tpp updated to
4489 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004490 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004491 * If the expansion is empty, *tpp will be unchanged but **tpp will
4492 * be advanced past the macro call.
4493 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004494 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004495 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004496static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004497{
4498 Token **params = NULL;
4499 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004500 Token *tline = **tpp;
4501 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004502 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004503 int i;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004504 Token *t, *tup, *ttail;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004505 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004506 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004507
4508 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004509 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004510
4511 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004512
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004513 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004514 smacro_deadman.levels--;
4515
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004516 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004517 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004518 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004519 smacro_deadman.triggered = true;
4520 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004521 goto not_a_macro;
4522 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004523 head = (SMacro *)hash_findix(&smacros, mname);
4524 } else if (tline->type == TOK_PREPROC_ID) {
4525 Context *ctx = get_ctx(mname, &mname);
4526 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4527 } else {
4528 goto not_a_macro;
4529 }
4530
4531 /*
4532 * We've hit an identifier of some sort. First check whether the
4533 * identifier is a single-line macro at all, then think about
4534 * checking for parameters if necessary.
4535 */
4536 list_for_each(m, head) {
4537 if (!mstrcmp(m->name, mname, m->casesense))
4538 break;
4539 }
4540
4541 if (!m) {
4542 goto not_a_macro;
4543 }
4544
4545 /* Parse parameters, if applicable */
4546
4547 params = NULL;
4548 nparam = 0;
4549
4550 if (m->nparam == 0) {
4551 /*
4552 * Simple case: the macro is parameterless.
4553 * Nothing to parse; the expansion code will
4554 * drop the macro name token.
4555 */
4556 } else {
4557 /*
4558 * Complicated case: at least one macro with this name
4559 * exists and takes parameters. We must find the
4560 * parameters in the call, count them, find the SMacro
4561 * that corresponds to that form of the macro call, and
4562 * substitute for the parameters when we expand. What a
4563 * pain.
4564 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004565 Token *t;
4566 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004567
4568 tline = tline->next;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004569 skip_white_(tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004570 if (!tok_is_(tline, "(")) {
4571 /*
4572 * This macro wasn't called with parameters: ignore
4573 * the call. (Behaviour borrowed from gnu cpp.)
4574 */
4575 goto not_a_macro;
4576 }
4577
4578 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004579 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004580 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004581 t = tline; /* tline points to leading ( */
4582
4583 while (paren) {
4584 t = t->next;
4585
4586 if (!t) {
4587 nasm_nonfatal("macro call expects terminating `)'");
4588 goto not_a_macro;
4589 }
4590
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004591 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004592 continue;
4593
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004594 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004595 case ',':
4596 if (!brackets)
4597 nparam++;
4598 break;
4599
4600 case '{':
4601 brackets++;
4602 break;
4603
4604 case '}':
4605 if (brackets > 0)
4606 brackets--;
4607 break;
4608
4609 case '(':
4610 if (!brackets)
4611 paren++;
4612 break;
4613
4614 case ')':
4615 if (!brackets)
4616 paren--;
4617 break;
4618
4619 default:
4620 break; /* Normal token */
4621 }
4622 }
4623
4624 /*
4625 * Look for a macro matching in both name and parameter count.
4626 * We already know any matches cannot be anywhere before the
4627 * current position of "m", so there is no reason to
4628 * backtrack.
4629 */
4630 while (1) {
4631 if (!m) {
4632 /*!
4633 *!macro-params-single [on] single-line macro calls with wrong parameter count
4634 *! warns about \i{single-line macros} being invoked
4635 *! with the wrong number of parameters.
4636 */
4637 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4638 "single-line macro `%s' exists, "
4639 "but not taking %d parameter%s",
4640 mname, nparam, (nparam == 1) ? "" : "s");
4641 goto not_a_macro;
4642 }
4643
4644 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004645 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004646 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004647 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004648 break; /* Also good */
4649 }
4650 m = m->next;
4651 }
4652 }
4653
4654 if (m->in_progress)
4655 goto not_a_macro;
4656
4657 /* Expand the macro */
4658 m->in_progress = true;
4659
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004660 if (nparam) {
4661 /* Extract parameters */
4662 Token **phead, **pep;
4663 int white = 0;
4664 int brackets = 0;
4665 int paren;
4666 bool bracketed = false;
4667 bool bad_bracket = false;
4668 enum sparmflags flags;
4669
4670 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004671 paren = 1;
4672 nasm_newn(params, nparam);
4673 i = 0;
4674 flags = m->params[i].flags;
4675 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004676 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004677
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004678 while (paren) {
4679 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004680 char ch;
4681
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004682 tline = tline->next;
4683
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004684 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004685 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004686
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004687 ch = 0;
4688 skip = false;
4689
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004690
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004691 switch (tline->type) {
4692 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004693 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004694 ch = tline->text[0];
4695 break;
4696
4697 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004698 if (!(flags & SPARM_NOSTRIP)) {
4699 if (brackets || *phead)
4700 white++; /* Keep interior whitespace */
4701 skip = true;
4702 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004703 break;
4704
4705 default:
4706 break;
4707 }
4708
4709 switch (ch) {
4710 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004711 if (!brackets && !(flags & SPARM_GREEDY)) {
4712 i++;
4713 nasm_assert(i < nparam);
4714 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004715 *pep = NULL;
4716 bracketed = false;
4717 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004718 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004719 }
4720 break;
4721
4722 case '{':
4723 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004724 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4725 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004726 }
4727 brackets++;
4728 break;
4729
4730 case '}':
4731 if (brackets > 0) {
4732 if (!--brackets)
4733 skip = bracketed;
4734 }
4735 break;
4736
4737 case '(':
4738 if (!brackets)
4739 paren++;
4740 break;
4741
4742 case ')':
4743 if (!brackets) {
4744 paren--;
4745 if (!paren) {
4746 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004747 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004748 }
4749 }
4750 break;
4751
4752 default:
4753 break; /* Normal token */
4754 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004755
4756 if (!skip) {
4757 Token *t;
4758
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004759 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004760
4761 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004762 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004763 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004764 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004765 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004766 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004767 pep = &t->next;
4768 white = 0;
4769 }
4770 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004771
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004772 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004773 * Possible further processing of parameters. Note that the
4774 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004775 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004776 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004777 enum sparmflags flags = m->params[i].flags;
4778
4779 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004780 /* Evaluate this parameter as a number */
4781 struct ppscan pps;
4782 struct tokenval tokval;
4783 expr *evalresult;
4784 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004785
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004786 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4787 pps.ntokens = -1;
4788 tokval.t_type = TOKEN_INVALID;
4789 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004790
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004791 free_tlist(eval_param);
4792 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004793
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004794 if (!evalresult) {
4795 /* Nothing meaningful to do */
4796 } else if (tokval.t_type) {
4797 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4798 } else if (!is_simple(evalresult)) {
4799 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4800 } else {
4801 params[i] = make_tok_num(reloc_value(evalresult));
4802 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004803 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004804
4805 if (flags & SPARM_STR) {
4806 /* Convert expansion to a quoted string */
4807 char *arg;
4808 Token *qs;
4809
4810 qs = expand_smacro_noreset(params[i]);
4811 arg = detoken(qs, false);
4812 free_tlist(qs);
4813 params[i] = make_tok_qstr(arg);
4814 nasm_free(arg);
4815 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004816 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004817 }
4818
4819 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004820 tline = tline->next; /* Remove the macro call from the input */
4821 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004822
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004823 /* Note: we own the expansion this returns. */
4824 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004825
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004826 tup = NULL;
4827 ttail = NULL; /* Pointer to the last token of the expansion */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004828 cond_comma = false;
4829
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004830 while (t) {
4831 enum pp_token_type type = t->type;
4832 Token *tnext = t->next;
4833 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004834
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004835 switch (type) {
4836 case TOK_PREPROC_Q:
4837 case TOK_PREPROC_QQ:
4838 t->type = TOK_ID;
4839 nasm_free(t->text);
4840 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4841 t->len = nasm_last_string_len();
4842 t->next = tline;
4843 break;
4844
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004845 case TOK_COND_COMMA:
4846 delete_Token(t);
4847 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4848 break;
4849
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004850 case TOK_ID:
4851 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004852 /*
4853 * Chain this into the target line *before* expanding,
4854 * that way we pick up any arguments to the new macro call,
4855 * if applicable.
4856 */
4857 t->next = tline;
4858 tp = &t;
4859 expand_one_smacro(&tp);
4860 if (t == tline)
4861 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004862 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004863
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004864 default:
4865 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004866 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004867 nasm_assert(!tup && param < nparam);
4868 delete_Token(t);
4869 t = NULL;
4870 tup = tnext;
4871 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004872 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004873 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004874 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004875 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004876 }
4877
4878 if (t) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004879 if (t->type != TOK_WHITESPACE)
4880 cond_comma = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004881 tline = t;
4882 if (!ttail)
4883 ttail = t;
4884 }
4885
4886 if (tnext) {
4887 t = tnext;
4888 } else {
4889 t = tup;
4890 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004891 }
4892 }
4893
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004894 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004895 if (ttail)
4896 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004897
4898 m->in_progress = false;
4899
4900 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004901 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004902 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004903
4904 /*
4905 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004906 * and then advance to the next input token. Note that this is
4907 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004908 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004909not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004910 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004911 m = NULL;
4912done:
4913 smacro_deadman.levels++;
4914 if (unlikely(params))
4915 free_tlist_array(params, nparam);
4916 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004917}
4918
4919/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004920 * Expand all single-line macro calls made in the given line.
4921 * Return the expanded version of the line. The original is deemed
4922 * to be destroyed in the process. (In reality we'll just move
4923 * Tokens from input to output a lot of the time, rather than
4924 * actually bothering to destroy and replicate.)
4925 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004926static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004927{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004928 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07004929 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4930 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004931 return expand_smacro_noreset(tline);
4932}
4933
4934static Token *expand_smacro_noreset(Token * tline)
4935{
4936 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004937 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004938 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004939
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004940 /*
4941 * Trick: we should avoid changing the start token pointer since it can
4942 * be contained in "next" field of other token. Because of this
4943 * we allocate a copy of first token and work with it; at the end of
4944 * routine we copy it back
4945 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004946 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004947 tline = new_Token(org_tline->next, org_tline->type,
4948 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004949 nasm_free(org_tline->text);
4950 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004951 }
4952
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004953
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004954 /*
4955 * Pretend that we always end up doing expansion on the first pass;
4956 * that way %+ get processed. However, if we process %+ before the
4957 * first pass, we end up with things like MACRO %+ TAIL trying to
4958 * look up the macro "MACROTAIL", which we don't want.
4959 */
4960 expanded = true;
4961 thead = tline;
4962 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004963 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004964 {
4965 PP_CONCAT_MASK(TOK_ID) |
4966 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4967 PP_CONCAT_MASK(TOK_ID) |
4968 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4969 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4970 }
4971 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004972
4973 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004974 while ((t = *tail)) /* main token loop */
4975 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004976
4977 if (!expanded) {
4978 tline = thead;
4979 break; /* Done! */
4980 }
4981
4982 /*
4983 * Now scan the entire line and look for successive TOK_IDs
4984 * that resulted after expansion (they can't be produced by
4985 * tokenize()). The successive TOK_IDs should be concatenated.
4986 * Also we look for %+ tokens and concatenate the tokens
4987 * before and after them (without white spaces in between).
4988 */
4989 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004990
4991 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004992 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004993
H. Peter Anvine2c80182005-01-15 22:15:51 +00004994 if (org_tline) {
4995 if (thead) {
4996 *org_tline = *thead;
4997 /* since we just gave text to org_line, don't free it */
4998 thead->text = NULL;
4999 delete_Token(thead);
5000 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005001 /*
5002 * The expression expanded to empty line;
5003 * we can't return NULL because of the "trick" above.
5004 * Just set the line to a single WHITESPACE token.
5005 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005006 memset(org_tline, 0, sizeof(*org_tline));
5007 org_tline->text = NULL;
5008 org_tline->type = TOK_WHITESPACE;
5009 }
5010 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005011 }
5012
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005013 return thead;
5014}
5015
5016/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005017 * Similar to expand_smacro but used exclusively with macro identifiers
5018 * right before they are fetched in. The reason is that there can be
5019 * identifiers consisting of several subparts. We consider that if there
5020 * are more than one element forming the name, user wants a expansion,
5021 * otherwise it will be left as-is. Example:
5022 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005023 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005024 *
5025 * the identifier %$abc will be left as-is so that the handler for %define
5026 * will suck it and define the corresponding value. Other case:
5027 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005028 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005029 *
5030 * In this case user wants name to be expanded *before* %define starts
5031 * working, so we'll expand %$abc into something (if it has a value;
5032 * otherwise it will be left as-is) then concatenate all successive
5033 * PP_IDs into one.
5034 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005035static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005036{
5037 Token *cur, *oldnext = NULL;
5038
H. Peter Anvin734b1882002-04-30 21:01:08 +00005039 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005040 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005041
5042 cur = tline;
5043 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005044 (cur->next->type == TOK_ID ||
5045 cur->next->type == TOK_PREPROC_ID
5046 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005048
5049 /* If identifier consists of just one token, don't expand */
5050 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005051 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005052
H. Peter Anvine2c80182005-01-15 22:15:51 +00005053 if (cur) {
5054 oldnext = cur->next; /* Detach the tail past identifier */
5055 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005056 }
5057
H. Peter Anvin734b1882002-04-30 21:01:08 +00005058 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005059
H. Peter Anvine2c80182005-01-15 22:15:51 +00005060 if (cur) {
5061 /* expand_smacro possibly changhed tline; re-scan for EOL */
5062 cur = tline;
5063 while (cur && cur->next)
5064 cur = cur->next;
5065 if (cur)
5066 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005067 }
5068
5069 return tline;
5070}
5071
5072/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005073 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005074 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005075 * to check for an initial label - that's taken care of in
5076 * expand_mmacro - but must check numbers of parameters. Guaranteed
5077 * to be called with tline->type == TOK_ID, so the putative macro
5078 * name is easy to find.
5079 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005080static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005081{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005082 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005083 Token **params;
5084 int nparam;
5085
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005086 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005087
5088 /*
5089 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005090 * name which isn't already excluded by macro cycle removal.
5091 * (The cycle removal test here helps optimize the case of wrapping
5092 * instructions, and is cheap to do here.)
5093 *
5094 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005095 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005096 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005097 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005098 list_for_each(m, head) {
5099 if (!mstrcmp(m->name, tline->text, m->casesense) &&
5100 (m->in_progress != 1 || m->max_depth > 0))
5101 break; /* Found something that needs consideration */
5102 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005103 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005104 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005105
5106 /*
5107 * OK, we have a potential macro. Count and demarcate the
5108 * parameters.
5109 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005110 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005111
5112 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005113 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005114 * structure that handles this number.
5115 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005116 while (m) {
5117 if (m->nparam_min <= nparam
5118 && (m->plus || nparam <= m->nparam_max)) {
5119 /*
5120 * This one is right. Just check if cycle removal
5121 * prohibits us using it before we actually celebrate...
5122 */
5123 if (m->in_progress > m->max_depth) {
5124 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005125 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005126 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005127 }
5128 nasm_free(params);
5129 return NULL;
5130 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005131 /*
5132 * It's right, and we can use it. Add its default
5133 * parameters to the end of our list if necessary.
5134 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005135 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005136 int newnparam = m->nparam_min + m->ndefs;
5137 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5138 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5139 (newnparam - nparam) * sizeof(*params));
5140 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005141 }
5142 /*
5143 * If we've gone over the maximum parameter count (and
5144 * we're in Plus mode), ignore parameters beyond
5145 * nparam_max.
5146 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005147 if (m->plus && nparam > m->nparam_max)
5148 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005149
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005150 /*
5151 * If nparam was adjusted above, make sure the list is still
5152 * NULL-terminated.
5153 */
5154 params[nparam+1] = NULL;
5155
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005156 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005157 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005158 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005159 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005160 }
5161 /*
5162 * This one wasn't right: look for the next one with the
5163 * same name.
5164 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005165 list_for_each(m, m->next)
5166 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005167 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005168 }
5169
5170 /*
5171 * After all that, we didn't find one with the right number of
5172 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005173 *!
5174 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5175 *! warns about \i{multi-line macros} being invoked
5176 *! with the wrong number of parameters. See \k{mlmacover} for an
5177 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005178 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005179 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5180 "multi-line macro `%s' exists, but not taking %d parameter%s",
5181 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005182 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005183 return NULL;
5184}
5185
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005186
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005187#if 0
5188
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005189/*
5190 * Save MMacro invocation specific fields in
5191 * preparation for a recursive macro expansion
5192 */
5193static void push_mmacro(MMacro *m)
5194{
5195 MMacroInvocation *i;
5196
5197 i = nasm_malloc(sizeof(MMacroInvocation));
5198 i->prev = m->prev;
5199 i->params = m->params;
5200 i->iline = m->iline;
5201 i->nparam = m->nparam;
5202 i->rotate = m->rotate;
5203 i->paramlen = m->paramlen;
5204 i->unique = m->unique;
5205 i->condcnt = m->condcnt;
5206 m->prev = i;
5207}
5208
5209
5210/*
5211 * Restore MMacro invocation specific fields that were
5212 * saved during a previous recursive macro expansion
5213 */
5214static void pop_mmacro(MMacro *m)
5215{
5216 MMacroInvocation *i;
5217
5218 if (m->prev) {
5219 i = m->prev;
5220 m->prev = i->prev;
5221 m->params = i->params;
5222 m->iline = i->iline;
5223 m->nparam = i->nparam;
5224 m->rotate = i->rotate;
5225 m->paramlen = i->paramlen;
5226 m->unique = i->unique;
5227 m->condcnt = i->condcnt;
5228 nasm_free(i);
5229 }
5230}
5231
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005232#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005233
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005234/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005235 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005236 */
5237static void list_mmacro_call(const MMacro *m)
5238{
5239 const char prefix[] = " ;;; [macro] ";
5240 size_t namelen, size;
5241 char *buf, *p;
5242 unsigned int i;
5243 const Token *t;
5244
5245 namelen = strlen(m->iname);
5246 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5247
5248 for (i = 1; i <= m->nparam; i++) {
5249 int j = 0;
5250 size += 3; /* Braces and space/comma */
5251 list_for_each(t, m->params[i]) {
5252 if (j++ >= m->paramlen[i])
5253 break;
5254 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5255 }
5256 }
5257
5258 buf = p = nasm_malloc(size);
5259 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5260 p = mempcpy(p, m->iname, namelen);
5261 *p++ = ' ';
5262
5263 for (i = 1; i <= m->nparam; i++) {
5264 int j = 0;
5265 *p++ = '{';
5266 list_for_each(t, m->params[i]) {
5267 if (j++ >= m->paramlen[i])
5268 break;
5269 if (!t->text) {
5270 if (t->type == TOK_WHITESPACE)
5271 *p++ = ' ';
5272 } else {
5273 p = mempcpy(p, t->text, t->len);
5274 }
5275 }
5276 *p++ = '}';
5277 *p++ = ',';
5278 }
5279
5280 *--p = '\0'; /* Replace last delimeter with null */
5281 lfmt->line(LIST_MACRO, -1, buf);
5282 nasm_free(buf);
5283}
5284
5285/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005286 * Expand the multi-line macro call made by the given line, if
5287 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005288 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005289 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005290static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005291{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005292 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005293 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005294 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005295 Token **params, *t, *tt;
5296 MMacro *m;
5297 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005298 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005299 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005300 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005301
5302 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005303 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005304 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005305 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005306 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005307 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005308 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005309 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005310 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005311 Token *last;
5312 /*
5313 * We have an id which isn't a macro call. We'll assume
5314 * it might be a label; we'll also check to see if a
5315 * colon follows it. Then, if there's another id after
5316 * that lot, we'll check it again for macro-hood.
5317 */
5318 label = last = t;
5319 t = t->next;
5320 if (tok_type_(t, TOK_WHITESPACE))
5321 last = t, t = t->next;
5322 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005323 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005324 last = t, t = t->next;
5325 if (tok_type_(t, TOK_WHITESPACE))
5326 last = t, t = t->next;
5327 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005328 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005329 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005330 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005331 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005332 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005333 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005334
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005335 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5336 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5337 if (!mmacro_deadman.triggered) {
5338 nasm_nonfatal("interminable multiline macro recursion");
5339 mmacro_deadman.triggered = true;
5340 }
5341 return 0;
5342 }
5343
5344 mmacro_deadman.total++;
5345 mmacro_deadman.levels++;
5346
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005347 /*
5348 * Fix up the parameters: this involves stripping leading and
5349 * trailing whitespace, then stripping braces if they are
5350 * present.
5351 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005352 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005353
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005354 nasm_assert(params[nparam+1] == NULL);
5355
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005356 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005357 int brace = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005358 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005359
H. Peter Anvine2c80182005-01-15 22:15:51 +00005360 skip_white_(t);
5361 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005362 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005363 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005364 while (t) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005365 if (comma) {
5366 /* Check if we hit a comma that ends this argument */
5367 if (tok_is_(t, ","))
5368 break;
5369 else if (t->type == TOK_WHITESPACE && tok_is_(t->next, ","))
5370 break;
5371 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005372 if (brace && t->type == TOK_OTHER) {
5373 if (t->text[0] == '{')
5374 brace++; /* ... or a nested opening brace */
5375 else if (t->text[0] == '}')
5376 if (!--brace)
5377 break; /* ... or a brace */
5378 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005379 t = t->next;
5380 paramlen[i]++;
5381 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005382 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005383 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005384 }
5385
5386 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005387 * OK, we have a MMacro structure together with a set of
5388 * parameters. We must now go through the expansion and push
5389 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005390 * parameter tokens and macro-local tokens doesn't get done
5391 * until the single-line macro substitution process; this is
5392 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005393 * later through %rotate and give the right semantics for
5394 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005395 *
5396 * First, push an end marker on to istk->expansion, mark this
5397 * macro as in progress, and set up its invocation-specific
5398 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005399 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005400 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005401 ll->next = istk->expansion;
5402 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005403 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005404
5405 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005406 * Save the previous MMacro expansion in the case of
5407 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005408 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005409#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005410 if (m->max_depth && m->in_progress)
5411 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005412#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005413
5414 m->in_progress ++;
5415 m->params = params;
5416 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005417 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005418 m->nparam = nparam;
5419 m->rotate = 0;
5420 m->paramlen = paramlen;
5421 m->unique = unique++;
5422 m->lineno = 0;
5423 m->condcnt = 0;
5424
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005425 m->mstk = istk->mstk;
5426 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005427
5428 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005429 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005430 ll->next = istk->expansion;
5431 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005432 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005433 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005434
5435 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005436 * If we had a label, and this macro definition does not include
5437 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005438 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005439 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005440 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005441 /*
5442 * We had a label. If this macro contains an %00 parameter,
5443 * save the value as a special parameter (which is what it
5444 * is), otherwise push it as the first line of the macro
5445 * expansion.
5446 */
5447 if (m->capture_label) {
5448 params[0] = dup_Token(NULL, label);
5449 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005450 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005451 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005452 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005453 ll->finishes = NULL;
5454 ll->next = istk->expansion;
5455 istk->expansion = ll;
5456 ll->first = startline;
5457 if (!dont_prepend) {
5458 while (label->next)
5459 label = label->next;
5460 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005461 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005462 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005463 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005464
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005465 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005466
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005467 if (list_option('m') && !m->nolist)
5468 list_mmacro_call(m);
5469
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005470 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005471}
5472
H. Peter Anvin130736c2016-02-17 20:27:41 -08005473/*
5474 * This function adds macro names to error messages, and suppresses
5475 * them if necessary.
5476 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005477static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005478{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005479 /*
5480 * If we're in a dead branch of IF or something like it, ignore the error.
5481 * However, because %else etc are evaluated in the state context
5482 * of the previous branch, errors might get lost:
5483 * %if 0 ... %else trailing garbage ... %endif
5484 * So %else etc should set the ERR_PP_PRECOND flag.
5485 */
5486 if ((severity & ERR_MASK) < ERR_FATAL &&
5487 istk && istk->conds &&
5488 ((severity & ERR_PP_PRECOND) ?
5489 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005490 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005491 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005492
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005493 /* This doesn't make sense with the macro stack unwinding */
5494 if (0) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005495 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005496
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005497 /* get %macro name */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005498 if (!(severity & ERR_NOFILE) && istk && istk->mstk.mmac) {
5499 MMacro *mmac = istk->mstk.mmac;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005500 char *buf;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005501
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005502 nasm_set_verror(real_verror);
5503 buf = nasm_vasprintf(fmt, arg);
5504 nasm_error(severity, "(%s:%"PRId32") %s",
5505 mmac->name, mmac->lineno - delta, buf);
5506 nasm_set_verror(pp_verror);
5507 nasm_free(buf);
5508 return;
5509 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005510 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005511 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005512}
5513
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005514static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005515stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005516{
5517 (void)s;
5518 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005519 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005520
5521 return make_tok_qstr(src_get_fname());
5522}
5523
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005524static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005525stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005526{
5527 (void)s;
5528 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005529 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005530
5531 return make_tok_num(src_get_linnum());
5532}
5533
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005534static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005535stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005536{
5537 (void)s;
5538 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005539 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005540
5541 return make_tok_num(globalbits);
5542}
5543
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005544static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005545stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005546{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005547 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005548 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5549 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5550 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005551
5552 (void)s;
5553 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005554 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005555
5556 switch (globalbits) {
5557 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005558 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005559 break;
5560 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005561 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005562 break;
5563 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005564 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005565 break;
5566 default:
5567 panic();
5568 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005569
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005570 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005571}
5572
H. Peter Anvin8b262472019-02-26 14:00:54 -08005573/* Add magic standard macros */
5574struct magic_macros {
5575 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005576 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005577 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005578};
5579static const struct magic_macros magic_macros[] =
5580{
5581 { "__FILE__", 0, stdmac_file },
5582 { "__LINE__", 0, stdmac_line },
5583 { "__BITS__", 0, stdmac_bits },
5584 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005585 { NULL, 0, NULL }
5586};
5587
5588static void pp_add_magic_stdmac(void)
5589{
5590 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005591 SMacro tmpl;
5592
5593 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005594
5595 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005596 tmpl.nparam = m->nparam;
5597 tmpl.expand = m->func;
5598 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005599 }
5600}
5601
H. Peter Anvin734b1882002-04-30 21:01:08 +00005602static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005603pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005604{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005605 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005606 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005607
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005608 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005609 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005610 nested_mac_count = 0;
5611 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005612 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005613 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005614 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005615 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005616
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005617 if (!use_loaded)
5618 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5619 memset(use_loaded, 0, use_package_count * sizeof(bool));
5620
H. Peter Anvin6686de22019-08-10 05:33:14 -07005621 /* First set up the top level input file */
5622 nasm_new(istk);
5623 istk->fp = nasm_open_read(file, NF_TEXT);
5624 src_set(0, file);
5625 istk->lineinc = 1;
5626 if (!istk->fp)
5627 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5628
5629 strlist_add(deplist, file);
5630
5631 /*
5632 * Set up the stdmac packages as a virtual include file,
5633 * indicated by a null file pointer.
5634 */
5635 nasm_new(inc);
5636 inc->next = istk;
5637 inc->fname = src_set_fname(NULL);
5638 inc->nolist = !list_option('b');
5639 istk = inc;
5640 lfmt->uplevel(LIST_INCLUDE, 0);
5641
H. Peter Anvin8b262472019-02-26 14:00:54 -08005642 pp_add_magic_stdmac();
5643
H. Peter Anvinf7606612016-07-13 14:23:48 -07005644 if (tasm_compatible_mode)
5645 pp_add_stdmac(nasm_stdmac_tasm);
5646
5647 pp_add_stdmac(nasm_stdmac_nasm);
5648 pp_add_stdmac(nasm_stdmac_version);
5649
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005650 if (extrastdmac)
5651 pp_add_stdmac(extrastdmac);
5652
H. Peter Anvinf7606612016-07-13 14:23:48 -07005653 stdmacpos = stdmacros[0];
5654 stdmacnext = &stdmacros[1];
5655
H. Peter Anvind2456592008-06-19 15:04:18 -07005656 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005657
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005658 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005659 * Define the __PASS__ macro. This is defined here unlike all the
5660 * other builtins, because it is special -- it varies between
5661 * passes -- but there is really no particular reason to make it
5662 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005663 *
5664 * 0 = dependencies only
5665 * 1 = preparatory passes
5666 * 2 = final pass
5667 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005668 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005669 switch (mode) {
5670 case PP_NORMAL:
5671 apass = pass_final() ? 2 : 1;
5672 break;
5673 case PP_DEPS:
5674 apass = 0;
5675 break;
5676 case PP_PREPROC:
5677 apass = 3;
5678 break;
5679 default:
5680 panic();
5681 }
5682
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005683 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005684}
5685
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005686static void pp_init(void)
5687{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005688}
5689
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005690/*
5691 * Get a line of tokens. If we popped the macro expansion/include stack,
5692 * we return a pointer to the dummy token tok_pop; at that point if
5693 * istk is NULL then we have reached end of input;
5694 */
5695static Token tok_pop; /* Dummy token placeholder */
5696
5697static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005698{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005699 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005700 Line *l = istk->expansion;
5701 Token *tline = NULL;
5702 Token *dtline;
5703
H. Peter Anvine2c80182005-01-15 22:15:51 +00005704 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005705 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005706 * buffer or from the input file.
5707 */
5708 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005709 while (l && l->finishes) {
5710 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005711
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005712 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005713 /*
5714 * This is a macro-end marker for a macro with no
5715 * name, which means it's not really a macro at all
5716 * but a %rep block, and the `in_progress' field is
5717 * more than 1, meaning that we still need to
5718 * repeat. (1 means the natural last repetition; 0
5719 * means termination by %exitrep.) We have
5720 * therefore expanded up to the %endrep, and must
5721 * push the whole block on to the expansion buffer
5722 * again. We don't bother to remove the macro-end
5723 * marker: we'd only have to generate another one
5724 * if we did.
5725 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005726 fm->in_progress--;
5727 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005728 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005729 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005730
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005731 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005732 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005733 tail = &ll->first;
5734
5735 list_for_each(t, l->first) {
5736 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005737 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005738 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005739 }
5740 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005741 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005742 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005743 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005744 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005745 MMacro *m = istk->mstk.mstk;
5746
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005747 /*
5748 * Check whether a `%rep' was started and not ended
5749 * within this macro expansion. This can happen and
5750 * should be detected. It's a fatal error because
5751 * I'm too confused to work out how to recover
5752 * sensibly from it.
5753 */
5754 if (defining) {
5755 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005756 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005757 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005758 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005759 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005760 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005761
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005762 /*
5763 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005764 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005765 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005766 istk->mstk = m->mstk;
5767 if (m->name) {
5768 /*
5769 * This was a real macro call, not a %rep, and
5770 * therefore the parameter information needs to
5771 * be freed and the iteration count/nesting
5772 * depth adjusted.
5773 */
5774
5775 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005776 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005777 * If all mmacro processing done,
5778 * clear all counters and the deadman
5779 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005780 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005781 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005782 }
5783
Adam Majer91e72402017-07-25 10:42:01 +02005784#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005785 if (m->prev) {
5786 pop_mmacro(m);
5787 fm->in_progress --;
5788 } else
Adam Majer91e72402017-07-25 10:42:01 +02005789#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005790 {
5791 nasm_free(m->params);
5792 free_tlist(m->iline);
5793 nasm_free(m->paramlen);
5794 fm->in_progress = 0;
5795 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005796 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005797
5798 /*
5799 * FIXME It is incorrect to always free_mmacro here.
5800 * It leads to usage-after-free.
5801 *
5802 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5803 */
5804#if 0
5805 else
5806 free_mmacro(m);
5807#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005808 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005809 istk->expansion = l->next;
5810 nasm_free(l);
5811 lfmt->downlevel(LIST_MACRO);
5812 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005813 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005814
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005815 do { /* until we get a line we can use */
5816 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005817
5818 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005819 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005820 int32_t lineno;
5821
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005822 if (istk->mstk.mstk) {
5823 istk->mstk.mstk->lineno++;
5824 if (istk->mstk.mstk->fname)
5825 lineno = istk->mstk.mstk->lineno +
5826 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005827 else
5828 lineno = 0; /* Defined at init time or builtin */
5829 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005830 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005831 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005832
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005833 tline = l->first;
5834 istk->expansion = l->next;
5835 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005836
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005837 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005838 if (!istk->nolist)
5839 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005840 nasm_free(line);
5841 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005842 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005843 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005844 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005845 } else {
5846 /*
5847 * The current file has ended; work down the istk
5848 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005849 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005850 if (i->fp)
5851 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005852 if (i->conds) {
5853 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005854 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005855 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005856 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005857 if (i->next)
5858 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005859 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005860 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005861 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005862 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005863 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005864 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005865
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005866 /*
5867 * We must expand MMacro parameters and MMacro-local labels
5868 * _before_ we plunge into directive processing, to cope
5869 * with things like `%define something %1' such as STRUC
5870 * uses. Unless we're _defining_ a MMacro, in which case
5871 * those tokens should be left alone to go into the
5872 * definition; and unless we're in a non-emitting
5873 * condition, in which case we don't want to meddle with
5874 * anything.
5875 */
5876 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005877 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005878 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005879 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005880
H. Peter Anvine2c80182005-01-15 22:15:51 +00005881 /*
5882 * Check the line to see if it's a preprocessor directive.
5883 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005884 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5885 if (dtline)
5886 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005887 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005888 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005889 * We're defining a multi-line macro. We emit nothing
5890 * at all, and just
5891 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005892 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005893 MMacro *mmac = defining->dstk.mmac;
5894
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005895 Line *l = nasm_malloc(sizeof(Line));
5896 l->next = defining->expansion;
5897 l->first = tline;
5898 l->finishes = NULL;
5899 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005900
5901 /*
5902 * Remember if this mmacro expansion contains %00:
5903 * if it does, we will have to handle leading labels
5904 * specially.
5905 */
5906 if (mmac) {
5907 const Token *t;
5908 list_for_each(t, tline) {
5909 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
5910 mmac->capture_label = true;
5911 }
5912 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005913 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005914 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005915 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005916 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005917 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005918 * directive so we keep our place correctly.
5919 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005920 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005921 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005922 /*
5923 * We're in a %rep block which has been terminated, so
5924 * we're walking through to the %endrep without
5925 * emitting anything. Emit nothing at all, not even a
5926 * blank line: when we emerge from the %rep block we'll
5927 * give a line-number directive so we keep our place
5928 * correctly.
5929 */
5930 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005931 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005932 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005933 if (!expand_mmacro(tline))
5934 return tline;
5935 }
5936 }
5937}
5938
5939static char *pp_getline(void)
5940{
5941 char *line = NULL;
5942 Token *tline;
5943
5944 real_verror = nasm_set_verror(pp_verror);
5945
5946 while (true) {
5947 tline = pp_tokline();
5948 if (tline == &tok_pop) {
5949 /*
5950 * We popped the macro/include stack. If istk is empty,
5951 * we are at end of input, otherwise just loop back.
5952 */
5953 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005954 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005955 } else {
5956 /*
5957 * De-tokenize the line and emit it.
5958 */
5959 line = detoken(tline, true);
5960 free_tlist(tline);
5961 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005962 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005963 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005964
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005965 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005966 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005967 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005968 nasm_free(buf);
5969 }
5970
H. Peter Anvin130736c2016-02-17 20:27:41 -08005971 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005972 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005973}
5974
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005975static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005976{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005977 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005978
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005979 if (defining) {
5980 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005981 nasm_nonfatal("end of file while still defining macro `%s'",
5982 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005983 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005984 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005985 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005986
5987 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005988 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005989 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005990
5991 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005992
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005993 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005994 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005995 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005996 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005997 Include *i = istk;
5998 istk = istk->next;
5999 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006000 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006001 }
6002 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006003 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006004 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006005}
6006
6007static void pp_cleanup_session(void)
6008{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006009 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006010 free_llist(predef);
6011 predef = NULL;
6012 delete_Blocks();
6013 freeTokens = NULL;
6014 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006015}
6016
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006017static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006018{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006019 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006020}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006021
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006022static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006023{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006024 Token *inc, *space, *name;
6025 Line *l;
6026
H. Peter Anvin734b1882002-04-30 21:01:08 +00006027 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
6028 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
6029 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006030
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006031 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006032 l->next = predef;
6033 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006034 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006035 predef = l;
6036}
6037
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006038static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006039{
6040 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006041 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006042 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006043
H. Peter Anvin130736c2016-02-17 20:27:41 -08006044 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08006045
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006046 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00006047 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6048 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006049 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006050 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006051 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006052 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006053 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006054
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006055 if (space->next->type != TOK_PREPROC_ID &&
6056 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006057 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006058
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006059 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006060 l->next = predef;
6061 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006062 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006063 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08006064
6065 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006066}
6067
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006068static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006069{
6070 Token *def, *space;
6071 Line *l;
6072
H. Peter Anvin734b1882002-04-30 21:01:08 +00006073 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6074 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006075 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006076
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006077 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006078 l->next = predef;
6079 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006080 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006081 predef = l;
6082}
6083
H. Peter Anvin05990342018-06-11 13:32:42 -07006084/* Insert an early preprocessor command that doesn't need special handling */
6085static void pp_pre_command(const char *what, char *string)
6086{
6087 char *cmd;
6088 Token *def, *space;
6089 Line *l;
6090
6091 def = tokenize(string);
6092 if (what) {
6093 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6094 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
6095 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6096 }
6097
6098 l = nasm_malloc(sizeof(Line));
6099 l->next = predef;
6100 l->first = def;
6101 l->finishes = NULL;
6102 predef = l;
6103}
6104
H. Peter Anvinf7606612016-07-13 14:23:48 -07006105static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006106{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006107 macros_t **mp;
6108
6109 /* Find the end of the list and avoid duplicates */
6110 for (mp = stdmacros; *mp; mp++) {
6111 if (*mp == macros)
6112 return; /* Nothing to do */
6113 }
6114
6115 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6116
6117 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006118}
6119
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006120static void pp_extra_stdmac(macros_t *macros)
6121{
6122 extrastdmac = macros;
6123}
6124
H. Peter Anvin8b262472019-02-26 14:00:54 -08006125static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006126{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006127 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006128 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6129 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6130}
6131
6132static Token *make_tok_qstr(const char *str)
6133{
6134 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006135 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006136 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006137}
6138
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006139static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006140{
6141 if (!m)
6142 return;
6143
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006144 /* We need to print the mstk.mmac list in reverse order */
6145 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006146
6147 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006148 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006149 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006150 }
6151}
6152
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006153static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006154{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006155 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006156
H. Peter Anvinddb29062018-12-11 00:06:29 -08006157 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6158 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006159
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006160 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006161 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006162
H. Peter Anvinddb29062018-12-11 00:06:29 -08006163 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006164}
6165
H. Peter Anvine7469712016-02-18 02:20:59 -08006166const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006167 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006168 pp_reset,
6169 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006170 pp_cleanup_pass,
6171 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006172 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006173 pp_pre_define,
6174 pp_pre_undefine,
6175 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006176 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006177 pp_include_path,
6178 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006179};