blob: ce5c41c887d50fcbaf91f7c027a93ee5569429a0 [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))
530#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
531#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 Anvine2c80182005-01-15 22:15:51 +00001872 while (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 }
1878 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001879 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001881 brace++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001882 params[++nparam] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001883 if (brace) {
1884 while (brace && (t = t->next) != NULL) {
1885 if (tok_is_(t, "{"))
1886 brace++;
1887 else if (tok_is_(t, "}"))
1888 brace--;
1889 }
1890
1891 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 /*
1893 * Now we've found the closing brace, look further
1894 * for the comma.
1895 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001896 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 skip_white_(t);
1898 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001899 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 while (tok_isnt_(t, ","))
1901 t = t->next;
1902 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001904 } else {
1905 while (tok_isnt_(t, ","))
1906 t = t->next;
1907 }
1908 if (t) { /* got a comma/brace */
1909 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001911 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001912
1913 params[nparam+1] = NULL;
1914 *paramsp = params;
1915 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001916}
1917
1918/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001919 * Determine whether one of the various `if' conditions is true or
1920 * not.
1921 *
1922 * We must free the tline we get passed.
1923 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001924static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001925{
H. Peter Anvin70055962007-10-11 00:05:31 -07001926 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001927 Token *t, *tt, *origline;
1928 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001929 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001930 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001931 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001932 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001933 const char *dname = pp_directives[ct];
1934 bool casesense = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001935
1936 origline = tline;
1937
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001938 switch (PP_COND(ct)) {
1939 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001940 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001941 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001943 if (!tline)
1944 break;
1945 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001946 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001947 dname);
1948 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001950 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001951 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 tline = tline->next;
1953 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001955
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001956 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001957 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001958 while (tline) {
1959 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 if (!tline || (tline->type != TOK_ID &&
1961 (tline->type != TOK_PREPROC_ID ||
1962 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001963 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001964 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001967 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001968 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001969 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001970 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001972
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001973 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001974 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001975 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001976 while (tline) {
1977 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001978 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001979 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001980 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001981 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001982 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001983 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001984 goto fail;
1985 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001986 p = tline->text;
1987 if (tline->type == TOK_PREPROC_ID)
1988 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001989 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001990 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001991 if (getenv(p))
1992 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001993 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001994 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001995 break;
1996
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001997 case PP_IFIDNI:
1998 casesense = false;
1999 /* fall through */
2000 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002001 tline = expand_smacro(tline);
2002 t = tt = tline;
2003 while (tok_isnt_(tt, ","))
2004 tt = tt->next;
2005 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002006 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002007 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002009 }
2010 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002011 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002012 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
2013 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002014 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002015 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002016 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002017 }
2018 if (t->type == TOK_WHITESPACE) {
2019 t = t->next;
2020 continue;
2021 }
2022 if (tt->type == TOK_WHITESPACE) {
2023 tt = tt->next;
2024 continue;
2025 }
2026 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002027 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002028 break;
2029 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002030 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 size_t l1 = nasm_unquote(t->text, NULL);
2033 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002034
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002035 if (l1 != l2) {
2036 j = false;
2037 break;
2038 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002039 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 j = false;
2041 break;
2042 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002043 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002044 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002045 break;
2046 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002047
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 t = t->next;
2049 tt = tt->next;
2050 }
2051 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002052 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002053 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002054
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002055 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002056 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002058 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002059
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002060 skip_white_(tline);
2061 tline = expand_id(tline);
2062 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002063 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002064 goto fail;
2065 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002066 nasm_zero(searching);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002067 searching.name = nasm_strdup(tline->text);
2068 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002069 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002070 searching.nparam_max = INT_MAX;
2071 tline = expand_smacro(tline->next);
2072 skip_white_(tline);
2073 if (!tline) {
2074 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002075 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002076 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 } else {
2078 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002079 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002080 }
2081 if (tline && tok_is_(tline->next, "-")) {
2082 tline = tline->next->next;
2083 if (tok_is_(tline, "*"))
2084 searching.nparam_max = INT_MAX;
2085 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002086 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002087 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002089 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002090 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002091 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002092 searching.nparam_max = searching.nparam_min;
2093 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002094 }
2095 }
2096 if (tline && tok_is_(tline->next, "+")) {
2097 tline = tline->next;
2098 searching.plus = true;
2099 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002100 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2101 while (mmac) {
2102 if (!strcmp(mmac->name, searching.name) &&
2103 (mmac->nparam_min <= searching.nparam_max
2104 || searching.plus)
2105 && (searching.nparam_min <= mmac->nparam_max
2106 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002107 found = true;
2108 break;
2109 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002110 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002111 }
2112 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002113 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002114 nasm_free(searching.name);
2115 j = found;
2116 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002117 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002118
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002119 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 needtype = TOK_ID;
2121 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002122 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002123 needtype = TOK_NUMBER;
2124 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002125 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 needtype = TOK_STRING;
2127 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002128
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002129iftype:
2130 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002131
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002132 while (tok_type_(t, TOK_WHITESPACE) ||
2133 (needtype == TOK_NUMBER &&
2134 tok_type_(t, TOK_OTHER) &&
2135 (t->text[0] == '-' || t->text[0] == '+') &&
2136 !t->text[1]))
2137 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002138
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 j = tok_type_(t, needtype);
2140 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002141
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002142 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002143 t = tline = expand_smacro(tline);
2144 while (tok_type_(t, TOK_WHITESPACE))
2145 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002146
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 j = false;
2148 if (t) {
2149 t = t->next; /* Skip the actual token */
2150 while (tok_type_(t, TOK_WHITESPACE))
2151 t = t->next;
2152 j = !t; /* Should be nothing left */
2153 }
2154 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002155
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002156 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002157 t = tline = expand_smacro(tline);
2158 while (tok_type_(t, TOK_WHITESPACE))
2159 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002160
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002161 j = !t; /* Should be empty */
2162 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002163
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002164 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002165 pps.tptr = tline = expand_smacro(tline);
2166 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002167 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002168 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002169 if (!evalresult)
2170 return -1;
2171 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002172 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002173 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002174 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002175 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002176 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002178 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002179 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002180
H. Peter Anvine2c80182005-01-15 22:15:51 +00002181 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002182 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002183 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002184 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002185
2186 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002187 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002188
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002189fail:
2190 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002191 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002192}
2193
2194/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002195 * Default smacro expansion routine: just returns a copy of the
2196 * expansion list.
2197 */
2198static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002199smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002200{
2201 (void)params;
2202 (void)nparams;
2203
2204 return dup_tlist(s->expansion, NULL);
2205}
2206
2207/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002208 * Emit a macro defintion or undef to the listing file, if
2209 * desired. This is similar to detoken(), but it handles the reverse
2210 * expansion list, does not expand %! or local variable tokens, and
2211 * does some special handling for macro parameters.
2212 */
2213static void
2214list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2215{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002216 Token *t;
2217 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002218 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002219 char *context_prefix = NULL;
2220 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002221
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002222 namelen = strlen(m->name);
2223 size = namelen + 2; /* Include room for space after name + NUL */
2224
2225 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002226 int context_depth = cstk->depth - ctx->depth + 1;
2227 context_prefix =
2228 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2229 ctx->name ? ctx->name : "",
2230 ctx->number, context_depth, "");
2231
2232 context_len = nasm_last_string_len();
2233 memset(context_prefix + context_len - context_depth,
2234 '$', context_depth);
2235 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002236 }
2237
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002238 list_for_each(t, m->expansion)
2239 size += t->text ? t->len : 1;
2240
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002241 if (m->nparam) {
2242 /*
2243 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002244 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002245 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002246 int i;
2247
2248 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002249 for (i = 0; i < m->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002250 size += m->params[i].namelen;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002251 }
2252
2253 def = nasm_malloc(size);
2254 p = def+size;
2255 *--p = '\0';
2256
2257 list_for_each(t, m->expansion) {
2258 if (!t->text) {
2259 *--p = ' ';
2260 } else {
2261 p -= t->len;
2262 memcpy(p, t->text, t->len);
2263 }
2264 }
2265
2266 *--p = ' ';
2267
2268 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002269 int i;
2270
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002271 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002272 for (i = m->nparam-1; i >= 0; i--) {
2273 enum sparmflags flags = m->params[i].flags;
2274 if (flags & SPARM_GREEDY)
2275 *--p = '+';
2276 if (m->params[i].name) {
2277 p -= m->params[i].namelen;
2278 memcpy(p, m->params[i].name, m->params[i].namelen);
2279 }
2280 if (flags & SPARM_NOSTRIP)
2281 *--p = '!';
2282 if (flags & SPARM_STR)
2283 *--p = '&';
2284 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002285 *--p = '=';
2286 *--p = ',';
2287 }
2288 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002289 }
2290
2291 p -= namelen;
2292 memcpy(p, m->name, namelen);
2293
H. Peter Anvin6686de22019-08-10 05:33:14 -07002294 if (context_prefix) {
2295 p -= context_len;
2296 memcpy(p, context_prefix, context_len);
2297 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002298 }
2299
2300 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002301 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002302}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002303
2304/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002305 * Parse smacro arguments, return argument count. If the tmpl argument
2306 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002307 * *tpp is updated to point to the pointer to the first token after the
2308 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002309 *
2310 * The text values from any argument tokens are "stolen" and the
2311 * corresponding text fields set to NULL.
2312 */
2313static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2314{
2315 int nparam = 0;
2316 enum sparmflags flags;
2317 struct smac_param *params = NULL;
2318 bool err, done, greedy;
2319 Token **tn = *tpp;
2320 Token *t = *tn;
2321 Token *name;
2322
2323 while (t && t->type == TOK_WHITESPACE) {
2324 tn = &t->next;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002325 t = *tn;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002326 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002327
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002328 if (!tok_is_(t, "("))
2329 goto finish;
2330
2331 if (tmpl) {
2332 Token *tx = t;
2333 Token **txpp = &tx;
2334 int sparam;
2335
2336 /* Count parameters first */
2337 sparam = parse_smacro_template(&txpp, NULL);
2338 if (!sparam)
2339 goto finish; /* No parameters, we're done */
2340 nasm_newn(params, sparam);
2341 }
2342
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002343 /* Skip leading paren */
2344 tn = &t->next;
2345 t = *tn;
2346
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002347 name = NULL;
2348 flags = 0;
2349 err = done = greedy = false;
2350
2351 while (!done) {
2352 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002353 if (name || flags)
2354 nasm_nonfatal("`)' expected to terminate macro template");
2355 else
2356 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002357 break;
2358 }
2359
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002360 switch (t->type) {
2361 case TOK_ID:
2362 if (name)
2363 goto bad;
2364 name = t;
2365 break;
2366
2367 case TOK_OTHER:
2368 if (t->text[1])
2369 goto bad;
2370 switch (t->text[0]) {
2371 case '=':
2372 flags |= SPARM_EVAL;
2373 break;
2374 case '&':
2375 flags |= SPARM_STR;
2376 break;
2377 case '!':
2378 flags |= SPARM_NOSTRIP;
2379 break;
2380 case '+':
2381 flags |= SPARM_GREEDY;
2382 greedy = true;
2383 break;
2384 case ',':
2385 if (greedy)
2386 nasm_nonfatal("greedy parameter must be last");
2387 /* fall through */
2388 case ')':
2389 if (params) {
2390 if (name) {
2391 params[nparam].name = name->text;
2392 params[nparam].namelen = name->len;
2393 name->text = NULL;
2394 }
2395 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002396 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002397 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002398 name = NULL;
2399 flags = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002400 done = t->text[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002401 break;
2402 default:
2403 goto bad;
2404 }
2405 break;
2406
2407 case TOK_WHITESPACE:
2408 break;
2409
2410 default:
2411 bad:
2412 if (!err) {
2413 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2414 err = true;
2415 }
2416 break;
2417 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002418
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002419 tn = &t->next;
2420 t = *tn;
2421 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002422
2423finish:
2424 while (t && t->type == TOK_WHITESPACE) {
2425 tn = &t->next;
2426 t = t->next;
2427 }
2428 *tpp = tn;
2429 if (tmpl) {
2430 tmpl->nparam = nparam;
2431 tmpl->greedy = greedy;
2432 tmpl->params = params;
2433 }
2434 return nparam;
2435}
2436
2437/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002438 * Common code for defining an smacro. The tmpl argument, if not NULL,
2439 * contains any macro parameters that aren't explicit arguments;
2440 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002441 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002442static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002443 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002444{
2445 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002446 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002447 Context *ctx;
2448 bool defining_alias = false;
2449 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002450
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002451 if (tmpl) {
2452 defining_alias = tmpl->alias;
2453 nparam = tmpl->nparam;
2454 }
2455
2456 while (1) {
2457 ctx = get_ctx(mname, &mname);
2458
2459 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2460 /* Create a new macro */
2461 smtbl = ctx ? &ctx->localmac : &smacros;
2462 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2463 nasm_new(smac);
2464 smac->next = *smhead;
2465 *smhead = smac;
2466 break;
2467 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002468 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002469 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 /*
2471 * Some instances of the old code considered this a failure,
2472 * some others didn't. What is the right thing to do here?
2473 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002474 goto fail;
2475 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002476 /*
2477 * We're redefining, so we have to take over an
2478 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002479 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002480 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002481 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002482 break;
2483 } else if (smac->in_progress) {
2484 nasm_nonfatal("macro alias loop");
2485 goto fail;
2486 } else {
2487 /* It is an alias macro; follow the alias link */
2488 SMacro *s;
2489
2490 smac->in_progress = true;
2491 s = define_smacro(smac->expansion->text, casesense,
2492 expansion, tmpl);
2493 smac->in_progress = false;
2494 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002495 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002496 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002497
2498 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002499 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002500 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002501 smac->expand = smacro_expand_default;
2502 if (tmpl) {
2503 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002504 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002505 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002506 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002507 if (tmpl->expand)
2508 smac->expand = tmpl->expand;
2509 }
2510 if (list_option('m')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002511 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2512 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002513 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002514 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002515
2516fail:
2517 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002518 if (tmpl)
2519 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002520 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002521}
2522
2523/*
2524 * Undefine an smacro
2525 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002526static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002527{
2528 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002529 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002530 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002531
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002532 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002533 smtbl = ctx ? &ctx->localmac : &smacros;
2534 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002535
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002536 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002537 /*
2538 * We now have a macro name... go hunt for it.
2539 */
2540 sp = smhead;
2541 while ((s = *sp) != NULL) {
2542 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002543 if (s->alias && !undefalias) {
2544 if (s->in_progress) {
2545 nasm_nonfatal("macro alias loop");
2546 } else {
2547 s->in_progress = true;
2548 undef_smacro(s->expansion->text, false);
2549 s->in_progress = false;
2550 }
2551 } else {
2552 if (list_option('m'))
2553 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2554 ctx, s);
2555 *sp = s->next;
2556 free_smacro(s);
2557 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002558 } else {
2559 sp = &s->next;
2560 }
2561 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002562 }
2563}
2564
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002565/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002566 * Parse a mmacro specification.
2567 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002568static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002569{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002570 tline = tline->next;
2571 skip_white_(tline);
2572 tline = expand_id(tline);
2573 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002574 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002576 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002577
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002578#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002579 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002580#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002581 def->name = nasm_strdup(tline->text);
2582 def->plus = false;
2583 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002584 def->nparam_min = 0;
2585 def->nparam_max = 0;
2586
H. Peter Anvina26433d2008-07-16 14:40:01 -07002587 tline = expand_smacro(tline->next);
2588 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002589 if (!tok_type_(tline, TOK_NUMBER))
2590 nasm_nonfatal("`%s' expects a parameter count", directive);
2591 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002592 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002593 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002594 tline = tline->next->next;
2595 if (tok_is_(tline, "*")) {
2596 def->nparam_max = INT_MAX;
2597 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002598 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002600 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002602 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002603 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 }
2605 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002606 }
2607 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 tline = tline->next;
2609 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002610 }
2611 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 !nasm_stricmp(tline->next->text, ".nolist")) {
2613 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002614 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002615 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616
H. Peter Anvina26433d2008-07-16 14:40:01 -07002617 /*
2618 * Handle default parameters.
2619 */
2620 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 def->dlist = tline->next;
2622 tline->next = NULL;
2623 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002624 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 def->dlist = NULL;
2626 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002627 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002628 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002629
H. Peter Anvin89cee572009-07-15 09:16:54 -04002630 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002631 !def->plus) {
2632 /*
2633 *!macro-defaults [on] macros with more default than optional parameters
2634 *! warns when a macro has more default parameters than optional parameters.
2635 *! See \k{mlmacdef} for why might want to disable this warning.
2636 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002637 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002638 "too many default macro parameters in macro `%s'", def->name);
2639 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002640
H. Peter Anvina26433d2008-07-16 14:40:01 -07002641 return true;
2642}
2643
2644
2645/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002646 * Decode a size directive
2647 */
2648static int parse_size(const char *str) {
2649 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002651 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002652 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002653 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002654}
2655
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002656/*
2657 * Process a preprocessor %pragma directive. Currently there are none.
2658 * Gets passed the token list starting with the "preproc" token from
2659 * "%pragma preproc".
2660 */
2661static void do_pragma_preproc(Token *tline)
2662{
2663 /* Skip to the real stuff */
2664 tline = tline->next;
2665 skip_white_(tline);
2666 if (!tline)
2667 return;
2668
2669 (void)tline; /* Nothing else to do at present */
2670}
2671
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002672static bool is_macro_id(const Token *t)
2673{
2674 return t && (t->type == TOK_ID ||
2675 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2676}
2677
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002678static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002679{
2680 char *id;
2681 Token *t = *tp;
2682
2683 t = t->next; /* Skip directive */
2684 skip_white_(t);
2685 t = expand_id(t);
2686
2687 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002688 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002689 err ? err : "macro identifier");
2690 return NULL;
2691 }
2692
2693 id = t->text;
2694 skip_white_(t);
2695 *tp = t;
2696 return id;
2697}
2698
Ed Beroset3ab3f412002-06-11 03:31:49 +00002699/**
2700 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002701 * Find out if a line contains a preprocessor directive, and deal
2702 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002703 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002704 * If a directive _is_ found, it is the responsibility of this routine
2705 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002706 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002707 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002708 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002709 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002710 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002711 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002712static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002713{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002714 enum preproc_token i;
2715 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002716 bool err;
2717 int nparam;
2718 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002719 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002720 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002721 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002722 char *p, *pp;
2723 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002724 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002725 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002726 Include *inc;
2727 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002728 Cond *cond;
2729 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002730 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002731 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002732 struct tokenval tokval;
2733 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002734 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002735 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002736 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002737 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002738
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002739 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002740 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002741
H. Peter Anvineba20a72002-04-30 20:53:55 +00002742 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002743 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002744 (tline->text[0] && (tline->text[1] == '%' ||
2745 tline->text[1] == '$' ||
2746 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002748
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002749 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002750 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002751
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002752 casesense = true;
2753 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2754 casesense = false;
2755 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002756 }
2757
2758 /*
2759 * If we're in a non-emitting branch of a condition construct,
2760 * or walking to the end of an already terminated %rep block,
2761 * we should ignore all directives except for condition
2762 * directives.
2763 */
2764 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002765 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2766 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002767 return NO_DIRECTIVE_FOUND;
2768 }
2769
2770 /*
2771 * If we're defining a macro or reading a %rep block, we should
2772 * ignore all directives except for %macro/%imacro (which nest),
2773 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2774 * If we're in a %rep block, another %rep nests, so should be let through.
2775 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002776 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002777 i != PP_ENDMACRO && i != PP_ENDM &&
2778 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2779 return NO_DIRECTIVE_FOUND;
2780 }
2781
2782 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002783 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002784 nested_mac_count++;
2785 return NO_DIRECTIVE_FOUND;
2786 } else if (nested_mac_count > 0) {
2787 if (i == PP_ENDMACRO) {
2788 nested_mac_count--;
2789 return NO_DIRECTIVE_FOUND;
2790 }
2791 }
2792 if (!defining->name) {
2793 if (i == PP_REP) {
2794 nested_rep_count++;
2795 return NO_DIRECTIVE_FOUND;
2796 } else if (nested_rep_count > 0) {
2797 if (i == PP_ENDREP) {
2798 nested_rep_count--;
2799 return NO_DIRECTIVE_FOUND;
2800 }
2801 }
2802 }
2803 }
2804
H. Peter Anvin4169a472007-09-12 01:29:43 +00002805 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002806 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002807 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002809
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002810 case PP_PRAGMA:
2811 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002812 * %pragma namespace options...
2813 *
2814 * The namespace "preproc" is reserved for the preprocessor;
2815 * all other namespaces generate a [pragma] assembly directive.
2816 *
2817 * Invalid %pragmas are ignored and may have different
2818 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002819 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002820 t = tline;
2821 tline = tline->next;
2822 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002823 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002824 while (tok_type_(tline, TOK_WHITESPACE)) {
2825 t = tline;
2826 tline = tline->next;
2827 delete_Token(t);
2828 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002829 if (tok_type_(tline, TOK_ID)) {
2830 if (!nasm_stricmp(tline->text, "preproc")) {
2831 /* Preprocessor pragma */
2832 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002833 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002834 } else {
2835 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002836
2837 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002838 for (t = tline; t->next; t = t->next)
2839 ;
2840 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002841
2842 /* Prepend "[pragma " */
2843 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2844 t = new_Token(t, TOK_ID, "pragma", 6);
2845 t = new_Token(t, TOK_OTHER, "[", 1);
2846 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002847 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002848 }
2849 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002850 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002851
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 case PP_STACKSIZE:
2853 /* Directive to tell NASM what the default stack size is. The
2854 * default is for a 16-bit stack, and this can be overriden with
2855 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 */
2857 tline = tline->next;
2858 if (tline && tline->type == TOK_WHITESPACE)
2859 tline = tline->next;
2860 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002861 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 }
2863 if (nasm_stricmp(tline->text, "flat") == 0) {
2864 /* All subsequent ARG directives are for a 32-bit stack */
2865 StackSize = 4;
2866 StackPointer = "ebp";
2867 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002868 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002869 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2870 /* All subsequent ARG directives are for a 64-bit stack */
2871 StackSize = 8;
2872 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002873 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002874 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 } else if (nasm_stricmp(tline->text, "large") == 0) {
2876 /* All subsequent ARG directives are for a 16-bit stack,
2877 * far function call.
2878 */
2879 StackSize = 2;
2880 StackPointer = "bp";
2881 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002882 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 } else if (nasm_stricmp(tline->text, "small") == 0) {
2884 /* All subsequent ARG directives are for a 16-bit stack,
2885 * far function call. We don't support near functions.
2886 */
2887 StackSize = 2;
2888 StackPointer = "bp";
2889 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002890 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002891 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002892 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002894 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002895
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 case PP_ARG:
2897 /* TASM like ARG directive to define arguments to functions, in
2898 * the following form:
2899 *
2900 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2901 */
2902 offset = ArgOffset;
2903 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002904 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002906
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 /* Find the argument name */
2908 tline = tline->next;
2909 if (tline && tline->type == TOK_WHITESPACE)
2910 tline = tline->next;
2911 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002912 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002913 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 }
2915 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002916
H. Peter Anvine2c80182005-01-15 22:15:51 +00002917 /* Find the argument size type */
2918 tline = tline->next;
2919 if (!tline || tline->type != TOK_OTHER
2920 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002921 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002922 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002923 }
2924 tline = tline->next;
2925 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002926 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002927 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002929
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002931 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002933 size = parse_size(tt->text);
2934 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002935 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002937 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 }
2939 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002940
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002941 /* Round up to even stack slots */
2942 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002943
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 /* Now define the macro for the argument */
2945 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2946 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002947 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002948 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002949
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 /* Move to the next argument in the list */
2951 tline = tline->next;
2952 if (tline && tline->type == TOK_WHITESPACE)
2953 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002954 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002955 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002956 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002957
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 case PP_LOCAL:
2959 /* TASM like LOCAL directive to define local variables for a
2960 * function, in the following form:
2961 *
2962 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2963 *
2964 * The '= LocalSize' at the end is ignored by NASM, but is
2965 * required by TASM to define the local parameter size (and used
2966 * by the TASM macro package).
2967 */
2968 offset = LocalOffset;
2969 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002970 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002972
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 /* Find the argument name */
2974 tline = tline->next;
2975 if (tline && tline->type == TOK_WHITESPACE)
2976 tline = tline->next;
2977 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002978 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002979 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 }
2981 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002982
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 /* Find the argument size type */
2984 tline = tline->next;
2985 if (!tline || tline->type != TOK_OTHER
2986 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002987 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002988 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 }
2990 tline = tline->next;
2991 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002992 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002993 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002995
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002997 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002999 size = parse_size(tt->text);
3000 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003001 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003003 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 }
3005 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003006
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003007 /* Round up to even stack slots */
3008 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003009
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003010 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003011
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003012 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3014 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003015 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003016
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 /* Now define the assign to setup the enter_c macro correctly */
3018 snprintf(directive, sizeof(directive),
3019 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003020 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003021
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 /* Move to the next argument in the list */
3023 tline = tline->next;
3024 if (tline && tline->type == TOK_WHITESPACE)
3025 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003026 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003027 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003028 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003029
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 case PP_CLEAR:
3031 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003032 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003033 free_macros();
3034 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003035 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003036
H. Peter Anvin418ca702008-05-30 10:42:30 -07003037 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003038 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003039 skip_white_(t);
3040 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003041 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003042 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003043 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003044 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003045 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003046 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003047 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003048 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003049 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003050 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003051 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003052
3053 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003054 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003055 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003056
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003057 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003058 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003059 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003060 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003061 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003062 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003063 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003064 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003065 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003066 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003067 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003069 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003070 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003071 (pp_mode == PP_DEPS)
3072 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003073 if (!inc->fp) {
3074 /* -MG given but file not found */
3075 nasm_free(inc);
3076 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003077 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003078 inc->lineno = src_set_linnum(0);
3079 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003080 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003081 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003082 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003083 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003084 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003085
H. Peter Anvind2456592008-06-19 15:04:18 -07003086 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003087 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003088 const struct use_package *pkg;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003089
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003090 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003091 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07003092 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003093 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003094 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003095 nasm_unquote_cstr(tline->text, NULL);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003096 pkg = nasm_find_use_package(tline->text);
3097 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003098 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003099 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003100 /*
3101 * Not already included, go ahead and include it.
3102 * Treat it as an include file for the purpose of
3103 * producing a listing.
3104 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003105 use_loaded[pkg->index] = true;
3106 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003107 nasm_new(inc);
3108 inc->next = istk;
3109 inc->fname = src_set_fname(NULL);
3110 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003111 inc->nolist = !list_option('b') || istk->nolist;
3112 istk = inc;
3113 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003114 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003115 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003116 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003119 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 tline = tline->next;
3121 skip_white_(tline);
3122 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003123 if (tline) {
3124 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003125 nasm_nonfatal("`%s' expects a context identifier",
3126 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003127 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003128 }
3129 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003130 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003131 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 p = nasm_strdup(tline->text);
3133 } else {
3134 p = NULL; /* Anonymous */
3135 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003136
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003137 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003138 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003139 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003140 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 ctx->name = p;
3142 ctx->number = unique++;
3143 cstk = ctx;
3144 } else {
3145 /* %pop or %repl */
3146 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003147 nasm_nonfatal("`%s': context stack is empty",
3148 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 } else if (i == PP_POP) {
3150 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003151 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003152 "expected %s",
3153 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 else
3155 ctx_pop();
3156 } else {
3157 /* i == PP_REPL */
3158 nasm_free(cstk->name);
3159 cstk->name = p;
3160 p = NULL;
3161 }
3162 nasm_free(p);
3163 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003164 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003165 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003166 severity = ERR_FATAL;
3167 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003168 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003169 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003170 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003171 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003172 /*!
3173 *!user [on] %warning directives
3174 *! controls output of \c{%warning} directives (see \k{pperror}).
3175 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003176 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003177 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003178
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003180 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003181 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182 tline->next = expand_smacro(tline->next);
3183 tline = tline->next;
3184 skip_white_(tline);
3185 t = tline ? tline->next : NULL;
3186 skip_white_(t);
3187 if (tok_type_(tline, TOK_STRING) && !t) {
3188 /* The line contains only a quoted string */
3189 p = tline->text;
3190 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003191 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 } else {
3193 /* Not a quoted string, or more than a quoted string */
3194 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003195 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003196 nasm_free(p);
3197 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003198 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003199 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003200
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003201 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003202 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003203 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003204 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003205 j = if_condition(tline->next, i);
3206 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003207 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003208 cond = nasm_malloc(sizeof(Cond));
3209 cond->next = istk->conds;
3210 cond->state = j;
3211 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003212 if(istk->mstk.mstk)
3213 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003214 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003215
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003216 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003217 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003218 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003219 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003220 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003221 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003222 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003223
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003224 case COND_DONE:
3225 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003226 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003227
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 case COND_ELSE_TRUE:
3229 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003230 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003231 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003232 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003233 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003234
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003235 case COND_IF_FALSE:
3236 /*
3237 * IMPORTANT: In the case of %if, we will already have
3238 * called expand_mmac_params(); however, if we're
3239 * processing an %elif we must have been in a
3240 * non-emitting mode, which would have inhibited
3241 * the normal invocation of expand_mmac_params().
3242 * Therefore, we have to do it explicitly here.
3243 */
3244 j = if_condition(expand_mmac_params(tline->next), i);
3245 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003246 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003247 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003249 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003250
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 case PP_ELSE:
3252 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003253 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003254 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003255 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003256 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003257 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003258 case COND_IF_TRUE:
3259 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003260 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003261 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003262
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003263 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003264 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003265
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003266 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003267 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003268 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003269
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003270 case COND_ELSE_TRUE:
3271 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003272 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003273 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003274 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003276 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003277 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003278
H. Peter Anvine2c80182005-01-15 22:15:51 +00003279 case PP_ENDIF:
3280 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003281 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003282 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003283 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003284 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003285 cond = istk->conds;
3286 istk->conds = cond->next;
3287 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003288 if(istk->mstk.mstk)
3289 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003290 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003291
H. Peter Anvin8b262472019-02-26 14:00:54 -08003292 case PP_RMACRO:
3293 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003294 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003295 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003296 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003297 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003298 if (i == PP_RMACRO)
3299 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003300 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003301 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003302 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003303 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003304
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003305 src_get(&defining->xline, &defining->fname);
3306
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003307 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3308 while (mmac) {
3309 if (!strcmp(mmac->name, defining->name) &&
3310 (mmac->nparam_min <= defining->nparam_max
3311 || defining->plus)
3312 && (defining->nparam_min <= mmac->nparam_max
3313 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003314 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003315 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003316 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003317 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003318 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003319 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003320 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003321
H. Peter Anvine2c80182005-01-15 22:15:51 +00003322 case PP_ENDM:
3323 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003324 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003325 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003326 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003327 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003328 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3329 defining->next = *mmhead;
3330 *mmhead = defining;
3331 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003332 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003333
H. Peter Anvin89cee572009-07-15 09:16:54 -04003334 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003335 /*
3336 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003337 * macro-end marker for a macro with a name. Then we
3338 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003339 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003340 list_for_each(l, istk->expansion)
3341 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003342 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003343
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003344 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003345 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003346 * Remove all conditional entries relative to this
3347 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003348 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003349 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3350 cond = istk->conds;
3351 istk->conds = cond->next;
3352 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003353 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003354 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003355 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003356 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003357 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003358 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003359
H. Peter Anvina26433d2008-07-16 14:40:01 -07003360 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003361 casesense = false;
3362 /* fall through */
3363 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003364 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003365 MMacro **mmac_p;
3366 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003367
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003368 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003369 spec.casesense = casesense;
3370 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003371 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003372 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003373 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3374 while (mmac_p && *mmac_p) {
3375 mmac = *mmac_p;
3376 if (mmac->casesense == spec.casesense &&
3377 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3378 mmac->nparam_min == spec.nparam_min &&
3379 mmac->nparam_max == spec.nparam_max &&
3380 mmac->plus == spec.plus) {
3381 *mmac_p = mmac->next;
3382 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003384 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003385 }
3386 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003387 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003388 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003389 }
3390
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 case PP_ROTATE:
3392 if (tline->next && tline->next->type == TOK_WHITESPACE)
3393 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003394 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003396 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 return DIRECTIVE_FOUND;
3398 }
3399 t = expand_smacro(tline->next);
3400 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003401 pps.tptr = tline = t;
3402 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 tokval.t_type = TOKEN_INVALID;
3404 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003405 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 free_tlist(tline);
3407 if (!evalresult)
3408 return DIRECTIVE_FOUND;
3409 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003410 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003412 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003413 return DIRECTIVE_FOUND;
3414 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003415 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003416 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003417 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003418 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003419 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003420 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003421 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003422
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003423 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003424 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003425 rotate += mmac->nparam;
3426
3427 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003428 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003429 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003430
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003432 {
3433 MMacro *tmp_defining;
3434
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003435 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003436 do {
3437 tline = tline->next;
3438 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003439
H. Peter Anvine2c80182005-01-15 22:15:51 +00003440 if (tok_type_(tline, TOK_ID) &&
3441 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003442 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003443 do {
3444 tline = tline->next;
3445 } while (tok_type_(tline, TOK_WHITESPACE));
3446 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003447
H. Peter Anvine2c80182005-01-15 22:15:51 +00003448 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003449 pps.tptr = expand_smacro(tline);
3450 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003452 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003453 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003454 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003455 if (!evalresult)
3456 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003457 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003458 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003459 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003460 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003461 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003462 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003463 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003464 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003465 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3466 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003467 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003468 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003469 /*!
3470 *!negative-rep [on] regative %rep count
3471 *! warns about negative counts given to the \c{%rep}
3472 *! preprocessor directive.
3473 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003474 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003475 "negative `%%rep' count: %"PRId64, count);
3476 count = 0;
3477 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003478 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003479 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003480 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003481 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003482 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003484 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003485 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003486 defining->nolist = nolist;
3487 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003488 defining->mstk = istk->mstk;
3489 defining->dstk.mstk = tmp_defining;
3490 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003491 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003492 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003493 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003494
H. Peter Anvine2c80182005-01-15 22:15:51 +00003495 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003496 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003497 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003498 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003500
H. Peter Anvine2c80182005-01-15 22:15:51 +00003501 /*
3502 * Now we have a "macro" defined - although it has no name
3503 * and we won't be entering it in the hash tables - we must
3504 * push a macro-end marker for it on to istk->expansion.
3505 * After that, it will take care of propagating itself (a
3506 * macro-end marker line for a macro which is really a %rep
3507 * block will cause the macro to be re-expanded, complete
3508 * with another macro-end marker to ensure the process
3509 * continues) until the whole expansion is forcibly removed
3510 * from istk->expansion by a %exitrep.
3511 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003512 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003513 l->next = istk->expansion;
3514 l->finishes = defining;
3515 l->first = NULL;
3516 istk->expansion = l;
3517
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003518 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003519
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003520 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003521 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003522 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003523
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003525 /*
3526 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003527 * macro-end marker for a macro with no name. Then we set
3528 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003529 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003530 list_for_each(l, istk->expansion)
3531 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003532 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003533
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003534 if (l)
3535 l->finishes->in_progress = 1;
3536 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003537 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003538 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003539
H. Peter Anvin8b262472019-02-26 14:00:54 -08003540 case PP_DEFINE:
3541 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003542 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003543 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003544 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003545 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003546
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003547 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003548 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003549
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003550 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003551 lastp = &tline->next;
3552 nparam = parse_smacro_template(&lastp, &tmpl);
3553 tline = *lastp;
3554 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003555
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003556 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003557 macro_start = tline;
3558 if (!is_macro_id(macro_start)) {
3559 nasm_nonfatal("`%s' expects a macro identifier to alias",
3560 dname);
3561 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003562 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003563 tt = macro_start->next;
3564 macro_start->next = NULL;
3565 tline = tline->next;
3566 skip_white_(tline);
3567 if (tline && tline->type) {
3568 nasm_warn(WARN_OTHER,
3569 "trailing garbage after aliasing identifier ignored");
3570 }
3571 free_tlist(tt);
3572 tmpl.alias = true;
3573 } else {
3574 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003575 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003576 tline = expand_smacro(tline);
3577
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003578 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003579 macro_start = NULL;
3580 t = tline;
3581 while (t) {
3582 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003583 for (i = 0; i < nparam; i++) {
3584 if ((size_t)tmpl.params[i].namelen == t->len &&
3585 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3586 t->type = tok_smac_param(i);
3587 break;
3588 }
3589 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003590 }
3591 tt = t->next;
3592 t->next = macro_start;
3593 macro_start = t;
3594 t = tt;
3595 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003597
H. Peter Anvine2c80182005-01-15 22:15:51 +00003598 /*
3599 * Good. We now have a macro name, a parameter count, and a
3600 * token list (in reverse order) for an expansion. We ought
3601 * to be OK just to create an SMacro, store it, and let
3602 * free_tlist have the rest of the line (which we have
3603 * carefully re-terminated after chopping off the expansion
3604 * from the end).
3605 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003606 define_smacro(mname, casesense, macro_start, &tmpl);
3607 break;
3608 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003609
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003611 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003612 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003613 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003614 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003615 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003616
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003617 undef_smacro(mname, i == PP_UNDEFALIAS);
3618 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003619
H. Peter Anvin8b262472019-02-26 14:00:54 -08003620 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003621 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003622 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003623
H. Peter Anvin9e200162008-06-04 17:23:14 -07003624 last = tline;
3625 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003626 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003627
3628 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003629 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003630
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003631 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003632 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003633 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003634
3635 /*
3636 * We now have a macro name, an implicit parameter count of
3637 * zero, and a string token to use as an expansion. Create
3638 * and store an SMacro.
3639 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003640 define_smacro(mname, casesense, macro_start, NULL);
3641 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003642
H. Peter Anvin8b262472019-02-26 14:00:54 -08003643 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003644 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003645 goto done;
3646
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003647 last = tline;
3648 tline = expand_smacro(tline->next);
3649 last->next = NULL;
3650
3651 t = tline;
3652 while (tok_type_(t, TOK_WHITESPACE))
3653 t = t->next;
3654 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003655 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003656 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003657 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003658 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003659 }
3660
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003661 /*
3662 * Convert the string to a token stream. Note that smacros
3663 * are stored with the token stream reversed, so we have to
3664 * reverse the output of tokenize().
3665 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003666 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003667 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003668
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003669 /*
3670 * We now have a macro name, an implicit parameter count of
3671 * zero, and a numeric token to use as an expansion. Create
3672 * and store an SMacro.
3673 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003674 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003675 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003676 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003677
H. Peter Anvin418ca702008-05-30 10:42:30 -07003678 case PP_PATHSEARCH:
3679 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003680 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003681
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003682 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003683 goto done;
3684
H. Peter Anvin418ca702008-05-30 10:42:30 -07003685 last = tline;
3686 tline = expand_smacro(tline->next);
3687 last->next = NULL;
3688
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003689 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003690 while (tok_type_(t, TOK_WHITESPACE))
3691 t = t->next;
3692
3693 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003694 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003695 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003696 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003697 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003698 }
3699 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003700 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003701 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003702 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003703 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003704
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003705 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003706 if (!found_path)
3707 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003708 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003709
3710 /*
3711 * We now have a macro name, an implicit parameter count of
3712 * zero, and a string token to use as an expansion. Create
3713 * and store an SMacro.
3714 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003715 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003716 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003717 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003718 }
3719
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003721 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003722 goto done;
3723
H. Peter Anvine2c80182005-01-15 22:15:51 +00003724 last = tline;
3725 tline = expand_smacro(tline->next);
3726 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003727
H. Peter Anvine2c80182005-01-15 22:15:51 +00003728 t = tline;
3729 while (tok_type_(t, TOK_WHITESPACE))
3730 t = t->next;
3731 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003732 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003733 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 free_tlist(tline);
3735 free_tlist(origline);
3736 return DIRECTIVE_FOUND;
3737 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003738
H. Peter Anvin8b262472019-02-26 14:00:54 -08003739 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003740
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 /*
3742 * We now have a macro name, an implicit parameter count of
3743 * zero, and a numeric token to use as an expansion. Create
3744 * and store an SMacro.
3745 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003746 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747 free_tlist(tline);
3748 free_tlist(origline);
3749 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003750
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003751 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003752 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003753 goto done;
3754
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003755 last = tline;
3756 tline = expand_smacro(tline->next);
3757 last->next = NULL;
3758
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003759 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003760 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003761 switch (t->type) {
3762 case TOK_WHITESPACE:
3763 break;
3764 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003765 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003766 break;
3767 case TOK_OTHER:
3768 if (!strcmp(t->text, ",")) /* permit comma separators */
3769 break;
3770 /* else fall through */
3771 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003772 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003773 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003774 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003775 }
3776 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003777
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003778 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003779 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003780 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003781 memcpy(p, t->text, t->len);
3782 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003783 }
3784 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003785
3786 /*
3787 * We now have a macro name, an implicit parameter count of
3788 * zero, and a numeric token to use as an expansion. Create
3789 * and store an SMacro.
3790 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003791 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003792 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003793 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003794 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003795 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003796
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003798 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003799 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003800 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003801
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003802 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003803 goto done;
3804
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 last = tline;
3806 tline = expand_smacro(tline->next);
3807 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003808
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003809 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003810 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003811 while (tok_type_(t, TOK_WHITESPACE))
3812 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003813
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003815 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003816 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003818 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003819 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003820
H. Peter Anvin8b262472019-02-26 14:00:54 -08003821 pps.tptr = t->next;
3822 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003823 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003824 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003825 if (!evalresult) {
3826 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003827 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003828 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003829 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003830 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003831 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003832 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003833 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003834
H. Peter Anvin8b262472019-02-26 14:00:54 -08003835 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3836 pps.tptr = pps.tptr->next;
3837 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003838 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003839 } else {
3840 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003841 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003842 if (!evalresult) {
3843 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003844 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003846 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003847 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003848 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003849 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003850 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003851 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003852
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003853 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003854
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003855 /* make start and count being in range */
3856 if (start < 0)
3857 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003858 if (count < 0)
3859 count = len + count + 1 - start;
3860 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003861 count = len - start;
3862 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003863 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003864
H. Peter Anvin8b262472019-02-26 14:00:54 -08003865 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003866 macro_start->len = count;
3867 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3868 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003869
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870 /*
3871 * We now have a macro name, an implicit parameter count of
3872 * zero, and a numeric token to use as an expansion. Create
3873 * and store an SMacro.
3874 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003875 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003876 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003877 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003878 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003879
H. Peter Anvin8b262472019-02-26 14:00:54 -08003880 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003881 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003882 goto done;
3883
H. Peter Anvine2c80182005-01-15 22:15:51 +00003884 last = tline;
3885 tline = expand_smacro(tline->next);
3886 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003887
H. Peter Anvin8b262472019-02-26 14:00:54 -08003888 pps.tptr = tline;
3889 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003890 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003891 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003892 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003893 if (!evalresult)
3894 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003895
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003897 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003898
H. Peter Anvine2c80182005-01-15 22:15:51 +00003899 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003900 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003901 free_tlist(origline);
3902 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003903 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003904
H. Peter Anvin8b262472019-02-26 14:00:54 -08003905 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003906
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 /*
3908 * We now have a macro name, an implicit parameter count of
3909 * zero, and a numeric token to use as an expansion. Create
3910 * and store an SMacro.
3911 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003912 define_smacro(mname, casesense, macro_start, NULL);
3913 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003914
H. Peter Anvine2c80182005-01-15 22:15:51 +00003915 case PP_LINE:
3916 /*
3917 * Syntax is `%line nnn[+mmm] [filename]'
3918 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003919 if (unlikely(pp_noline))
3920 goto done;
3921
H. Peter Anvine2c80182005-01-15 22:15:51 +00003922 tline = tline->next;
3923 skip_white_(tline);
3924 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003925 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003926 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003927 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003928 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003929 m = 1;
3930 tline = tline->next;
3931 if (tok_is_(tline, "+")) {
3932 tline = tline->next;
3933 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003934 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003935 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003936 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003937 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003938 tline = tline->next;
3939 }
3940 skip_white_(tline);
3941 src_set_linnum(k);
3942 istk->lineinc = m;
3943 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003944 char *fname = detoken(tline, false);
3945 src_set_fname(fname);
3946 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003947 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003948 break;
3949 }
3950
3951done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 free_tlist(origline);
3953 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003954}
3955
3956/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003957 * Ensure that a macro parameter contains a condition code and
3958 * nothing else. Return the condition code index if so, or -1
3959 * otherwise.
3960 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003961static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003962{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003963 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003964
H. Peter Anvin25a99342007-09-22 17:45:45 -07003965 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003966 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003967
H. Peter Anvineba20a72002-04-30 20:53:55 +00003968 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003969 if (!t)
3970 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003972 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003973 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003974 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003975 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003976 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003977
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003978 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003979}
3980
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003981/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003982 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003983 * pasting, if @handle_explicit passed then explicit pasting
3984 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003985 * The @m array can contain a series of token types which are
3986 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003987 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003988static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003989 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003990{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003991 Token *tok, *next, **prev_next, **prev_nonspace;
3992 bool pasted = false;
3993 char *buf, *p;
3994 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003995
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003996 /*
3997 * The last token before pasting. We need it
3998 * to be able to connect new handled tokens.
3999 * In other words if there were a tokens stream
4000 *
4001 * A -> B -> C -> D
4002 *
4003 * and we've joined tokens B and C, the resulting
4004 * stream should be
4005 *
4006 * A -> BC -> D
4007 */
4008 tok = *head;
4009 prev_next = NULL;
4010
4011 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
4012 prev_nonspace = head;
4013 else
4014 prev_nonspace = NULL;
4015
4016 while (tok && (next = tok->next)) {
4017
4018 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004019 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004020 /* Zap redundant whitespaces */
4021 while (tok_type_(next, TOK_WHITESPACE))
4022 next = delete_Token(next);
4023 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004024 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004025
4026 case TOK_PASTE:
4027 /* Explicit pasting */
4028 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004029 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004030 next = delete_Token(tok);
4031
4032 while (tok_type_(next, TOK_WHITESPACE))
4033 next = delete_Token(next);
4034
4035 if (!pasted)
4036 pasted = true;
4037
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004038 /* Left pasting token is start of line */
4039 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004040 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004041
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004042 /*
4043 * No ending token, this might happen in two
4044 * cases
4045 *
4046 * 1) There indeed no right token at all
4047 * 2) There is a bare "%define ID" statement,
4048 * and @ID does expand to whitespace.
4049 *
4050 * So technically we need to do a grammar analysis
4051 * in another stage of parsing, but for now lets don't
4052 * change the behaviour people used to. Simply allow
4053 * whitespace after paste token.
4054 */
4055 if (!next) {
4056 /*
4057 * Zap ending space tokens and that's all.
4058 */
4059 tok = (*prev_nonspace)->next;
4060 while (tok_type_(tok, TOK_WHITESPACE))
4061 tok = delete_Token(tok);
4062 tok = *prev_nonspace;
4063 tok->next = NULL;
4064 break;
4065 }
4066
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004067 tok = *prev_nonspace;
4068 while (tok_type_(tok, TOK_WHITESPACE))
4069 tok = delete_Token(tok);
4070 len = strlen(tok->text);
4071 len += strlen(next->text);
4072
4073 p = buf = nasm_malloc(len + 1);
4074 strcpy(p, tok->text);
4075 p = strchr(p, '\0');
4076 strcpy(p, next->text);
4077
4078 delete_Token(tok);
4079
4080 tok = tokenize(buf);
4081 nasm_free(buf);
4082
4083 *prev_nonspace = tok;
4084 while (tok && tok->next)
4085 tok = tok->next;
4086
4087 tok->next = delete_Token(next);
4088
4089 /* Restart from pasted tokens head */
4090 tok = *prev_nonspace;
4091 break;
4092
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004093 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004094 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004095 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004096 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4097 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004098
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004099 len = 0;
4100 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4101 len += strlen(next->text);
4102 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004103 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004104
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004105 /* No match or no text to process */
4106 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004107 break;
4108
4109 len += strlen(tok->text);
4110 p = buf = nasm_malloc(len + 1);
4111
Adam Majer1a069432017-07-25 11:12:35 +02004112 strcpy(p, tok->text);
4113 p = strchr(p, '\0');
4114 tok = delete_Token(tok);
4115
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004116 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004117 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4118 strcpy(p, tok->text);
4119 p = strchr(p, '\0');
4120 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004121 tok = delete_Token(tok);
4122 }
4123
4124 tok = tokenize(buf);
4125 nasm_free(buf);
4126
4127 if (prev_next)
4128 *prev_next = tok;
4129 else
4130 *head = tok;
4131
4132 /*
4133 * Connect pasted into original stream,
4134 * ie A -> new-tokens -> B
4135 */
4136 while (tok && tok->next)
4137 tok = tok->next;
4138 tok->next = next;
4139
4140 if (!pasted)
4141 pasted = true;
4142
4143 /* Restart from pasted tokens head */
4144 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004145 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004146
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004147 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004148 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004149
4150 prev_next = &tok->next;
4151
4152 if (tok->next &&
4153 !tok_type_(tok->next, TOK_WHITESPACE) &&
4154 !tok_type_(tok->next, TOK_PASTE))
4155 prev_nonspace = prev_next;
4156
4157 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004158 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004159
4160 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004161}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004162
4163/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004164 * Computes the proper rotation of mmacro parameters
4165 */
4166static int mmac_rotate(const MMacro *mac, unsigned int n)
4167{
4168 if (--n < mac->nparam)
4169 n = (n + mac->rotate) % mac->nparam;
4170
4171 return n+1;
4172}
4173
4174/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004175 * expands to a list of tokens from %{x:y}
4176 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004177static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004178{
4179 Token *t = tline, **tt, *tm, *head;
4180 char *pos;
4181 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004182
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004183 pos = strchr(tline->text, ':');
4184 nasm_assert(pos);
4185
4186 lst = atoi(pos + 1);
4187 fst = atoi(tline->text + 1);
4188
4189 /*
4190 * only macros params are accounted so
4191 * if someone passes %0 -- we reject such
4192 * value(s)
4193 */
4194 if (lst == 0 || fst == 0)
4195 goto err;
4196
4197 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004198 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4199 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004200 goto err;
4201
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004202 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4203 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004205 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004206 fst--, lst--;
4207
4208 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004209 * It will be at least one token. Note we
4210 * need to scan params until separator, otherwise
4211 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004212 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004213 j = (fst + mac->rotate) % mac->nparam;
4214 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004215 if (!tm)
4216 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004217 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004218 tt = &head->next, tm = tm->next;
4219 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004220 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004221 *tt = t, tt = &t->next, tm = tm->next;
4222 }
4223
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004224 if (fst < lst) {
4225 for (i = fst + 1; i <= lst; i++) {
4226 t = new_Token(NULL, TOK_OTHER, ",", 0);
4227 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004228 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004229 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004230 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004231 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004232 *tt = t, tt = &t->next, tm = tm->next;
4233 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004234 }
4235 } else {
4236 for (i = fst - 1; i >= lst; i--) {
4237 t = new_Token(NULL, TOK_OTHER, ",", 0);
4238 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004239 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004240 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004241 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004242 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004243 *tt = t, tt = &t->next, tm = tm->next;
4244 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 }
4246 }
4247
4248 *last = tt;
4249 return head;
4250
4251err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004252 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004253 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004254 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004255}
4256
H. Peter Anvin76690a12002-04-30 20:52:49 +00004257/*
4258 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004259 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004260 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004261 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004262static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004263{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004264 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004265 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004266 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004267
4268 tail = &thead;
4269 thead = NULL;
4270
H. Peter Anvine2c80182005-01-15 22:15:51 +00004271 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004272 bool change;
4273 Token *t = tline;
4274 char *text = t->text;
4275 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004276
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004277 tline = tline->next;
4278 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004279
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004280 switch (type) {
4281 case TOK_PREPROC_ID:
4282 {
4283 Token *tt = NULL;
4284
4285 change = false;
4286
4287 if (!text || !text[0])
4288 break;
4289 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4290 ((text[1] == '+' || text[1] == '-') && text[2])))
4291 break;
4292
4293 change = true;
4294
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004295 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004296 nasm_nonfatal("`%s': not in a macro call", text);
4297 text = NULL;
4298 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004299 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004300
4301 if (strchr(text, ':')) {
4302 /*
4303 * seems we have a parameters range here
4304 */
4305 Token *head, **last;
4306 head = expand_mmac_params_range(mac, t, &last);
4307 if (head) {
4308 *tail = head;
4309 *last = tline;
4310 text = NULL;
4311 }
4312 break;
4313 }
4314
4315 switch (text[1]) {
4316 /*
4317 * We have to make a substitution of one of the
4318 * forms %1, %-1, %+1, %%foo, %0, %00.
4319 */
4320 case '0':
4321 if (!text[2]) {
4322 type = TOK_NUMBER;
4323 text = nasm_asprintf("%d", mac->nparam);
4324 break;
4325 }
4326 if (text[2] != '0' || text[3])
4327 goto invalid;
4328 /* a possible captured label == mac->params[0] */
4329 /* fall through */
4330 default:
4331 {
4332 unsigned long n;
4333 char *ep;
4334
4335 n = strtoul(text + 1, &ep, 10);
4336 if (unlikely(*ep))
4337 goto invalid;
4338
4339 if (n <= mac->nparam) {
4340 n = mmac_rotate(mac, n);
4341 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4342 }
4343 text = NULL;
4344 break;
4345 }
4346 case '%':
4347 type = TOK_ID;
4348 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4349 break;
4350 case '-':
4351 case '+':
4352 {
4353 int cc;
4354 unsigned long n;
4355 char *ep;
4356
4357 text = NULL;
4358
4359 n = strtoul(t->text + 2, &ep, 10);
4360 if (unlikely(*ep))
4361 goto invalid;
4362
4363 if (n && n < mac->nparam) {
4364 n = mmac_rotate(mac, n);
4365 tt = mac->params[n];
4366 }
4367 cc = find_cc(tt);
4368 if (cc == -1) {
4369 nasm_nonfatal("macro parameter `%s' is not a condition code",
4370 text);
4371 text = NULL;
4372 break;
4373 }
4374
4375 type = TOK_ID;
4376 if (text[1] == '-') {
4377 int ncc = inverse_ccs[cc];
4378 if (unlikely(ncc == -1)) {
4379 nasm_nonfatal("condition code `%s' is not invertible",
4380 conditions[cc]);
4381 break;
4382 }
4383 cc = ncc;
4384 }
4385 text = nasm_strdup(conditions[cc]);
4386 break;
4387 }
4388
4389 invalid:
4390 nasm_nonfatal("invalid macro parameter: `%s'", text);
4391 text = NULL;
4392 break;
4393 }
4394 break;
4395 }
4396
4397 case TOK_PREPROC_Q:
4398 if (mac) {
4399 type = TOK_ID;
4400 text = nasm_strdup(mac->iname);
4401 change = true;
4402 }
4403 break;
4404
4405 case TOK_PREPROC_QQ:
4406 if (mac) {
4407 type = TOK_ID;
4408 text = nasm_strdup(mac->name);
4409 change = true;
4410 }
4411 break;
4412
4413 case TOK_INDIRECT:
4414 {
4415 Token *tt;
4416
4417 tt = tokenize(t->text);
4418 tt = expand_mmac_params(tt);
4419 tt = expand_smacro(tt);
4420 /* Why dup_tlist() here? We should own tt... */
4421 dup_tlist(tt, &tail);
4422 text = NULL;
4423 change = true;
4424 break;
4425 }
4426
4427 default:
4428 change = false;
4429 break;
4430 }
4431
4432 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004433 if (!text) {
4434 delete_Token(t);
4435 } else {
4436 *tail = t;
4437 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004439 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004440 t->type = type;
4441 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004442 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004443 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004444 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004445 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004446 tail = &t->next;
4447 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004448 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004449
H. Peter Anvineba20a72002-04-30 20:53:55 +00004450 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004451
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004452 if (changed) {
4453 const struct tokseq_match t[] = {
4454 {
4455 PP_CONCAT_MASK(TOK_ID) |
4456 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4457 PP_CONCAT_MASK(TOK_ID) |
4458 PP_CONCAT_MASK(TOK_NUMBER) |
4459 PP_CONCAT_MASK(TOK_FLOAT) |
4460 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4461 },
4462 {
4463 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4464 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4465 }
4466 };
4467 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4468 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004469
H. Peter Anvin76690a12002-04-30 20:52:49 +00004470 return thead;
4471}
4472
H. Peter Anvin322bee02019-08-10 01:38:06 -07004473static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004474
H. Peter Anvin76690a12002-04-30 20:52:49 +00004475/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004476 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004477 * a macro at all, it is simply copied to the output and the pointer
4478 * advanced. tpp should be a pointer to a pointer (usually the next
4479 * pointer of the previous token) to the first token. **tpp is updated
4480 * to point to the last token of the expansion, and *tpp updated to
4481 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004482 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004483 * If the expansion is empty, *tpp will be unchanged but **tpp will
4484 * be advanced past the macro call.
4485 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004486 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004487 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004488static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004489{
4490 Token **params = NULL;
4491 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004492 Token *tline = **tpp;
4493 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004494 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004495 int i;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004496 Token *t, *tup, *ttail;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004497 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004498 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004499
4500 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004501 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004502
4503 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004504
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004505 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004506 smacro_deadman.levels--;
4507
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004508 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004509 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004510 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004511 smacro_deadman.triggered = true;
4512 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004513 goto not_a_macro;
4514 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004515 head = (SMacro *)hash_findix(&smacros, mname);
4516 } else if (tline->type == TOK_PREPROC_ID) {
4517 Context *ctx = get_ctx(mname, &mname);
4518 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4519 } else {
4520 goto not_a_macro;
4521 }
4522
4523 /*
4524 * We've hit an identifier of some sort. First check whether the
4525 * identifier is a single-line macro at all, then think about
4526 * checking for parameters if necessary.
4527 */
4528 list_for_each(m, head) {
4529 if (!mstrcmp(m->name, mname, m->casesense))
4530 break;
4531 }
4532
4533 if (!m) {
4534 goto not_a_macro;
4535 }
4536
4537 /* Parse parameters, if applicable */
4538
4539 params = NULL;
4540 nparam = 0;
4541
4542 if (m->nparam == 0) {
4543 /*
4544 * Simple case: the macro is parameterless.
4545 * Nothing to parse; the expansion code will
4546 * drop the macro name token.
4547 */
4548 } else {
4549 /*
4550 * Complicated case: at least one macro with this name
4551 * exists and takes parameters. We must find the
4552 * parameters in the call, count them, find the SMacro
4553 * that corresponds to that form of the macro call, and
4554 * substitute for the parameters when we expand. What a
4555 * pain.
4556 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004557 Token *t;
4558 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004559
4560 tline = tline->next;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004561 skip_white_(tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004562 if (!tok_is_(tline, "(")) {
4563 /*
4564 * This macro wasn't called with parameters: ignore
4565 * the call. (Behaviour borrowed from gnu cpp.)
4566 */
4567 goto not_a_macro;
4568 }
4569
4570 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004571 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004572 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004573 t = tline; /* tline points to leading ( */
4574
4575 while (paren) {
4576 t = t->next;
4577
4578 if (!t) {
4579 nasm_nonfatal("macro call expects terminating `)'");
4580 goto not_a_macro;
4581 }
4582
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004583 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004584 continue;
4585
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004586 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004587 case ',':
4588 if (!brackets)
4589 nparam++;
4590 break;
4591
4592 case '{':
4593 brackets++;
4594 break;
4595
4596 case '}':
4597 if (brackets > 0)
4598 brackets--;
4599 break;
4600
4601 case '(':
4602 if (!brackets)
4603 paren++;
4604 break;
4605
4606 case ')':
4607 if (!brackets)
4608 paren--;
4609 break;
4610
4611 default:
4612 break; /* Normal token */
4613 }
4614 }
4615
4616 /*
4617 * Look for a macro matching in both name and parameter count.
4618 * We already know any matches cannot be anywhere before the
4619 * current position of "m", so there is no reason to
4620 * backtrack.
4621 */
4622 while (1) {
4623 if (!m) {
4624 /*!
4625 *!macro-params-single [on] single-line macro calls with wrong parameter count
4626 *! warns about \i{single-line macros} being invoked
4627 *! with the wrong number of parameters.
4628 */
4629 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4630 "single-line macro `%s' exists, "
4631 "but not taking %d parameter%s",
4632 mname, nparam, (nparam == 1) ? "" : "s");
4633 goto not_a_macro;
4634 }
4635
4636 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004637 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004638 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004639 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004640 break; /* Also good */
4641 }
4642 m = m->next;
4643 }
4644 }
4645
4646 if (m->in_progress)
4647 goto not_a_macro;
4648
4649 /* Expand the macro */
4650 m->in_progress = true;
4651
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004652 if (nparam) {
4653 /* Extract parameters */
4654 Token **phead, **pep;
4655 int white = 0;
4656 int brackets = 0;
4657 int paren;
4658 bool bracketed = false;
4659 bool bad_bracket = false;
4660 enum sparmflags flags;
4661
4662 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004663 paren = 1;
4664 nasm_newn(params, nparam);
4665 i = 0;
4666 flags = m->params[i].flags;
4667 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004668 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004669
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004670 while (paren) {
4671 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004672 char ch;
4673
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004674 tline = tline->next;
4675
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004676 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004677 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004678
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004679 ch = 0;
4680 skip = false;
4681
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004682
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004683 switch (tline->type) {
4684 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004685 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004686 ch = tline->text[0];
4687 break;
4688
4689 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004690 if (!(flags & SPARM_NOSTRIP)) {
4691 if (brackets || *phead)
4692 white++; /* Keep interior whitespace */
4693 skip = true;
4694 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004695 break;
4696
4697 default:
4698 break;
4699 }
4700
4701 switch (ch) {
4702 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004703 if (!brackets && !(flags & SPARM_GREEDY)) {
4704 i++;
4705 nasm_assert(i < nparam);
4706 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004707 *pep = NULL;
4708 bracketed = false;
4709 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004710 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004711 }
4712 break;
4713
4714 case '{':
4715 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004716 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4717 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004718 }
4719 brackets++;
4720 break;
4721
4722 case '}':
4723 if (brackets > 0) {
4724 if (!--brackets)
4725 skip = bracketed;
4726 }
4727 break;
4728
4729 case '(':
4730 if (!brackets)
4731 paren++;
4732 break;
4733
4734 case ')':
4735 if (!brackets) {
4736 paren--;
4737 if (!paren) {
4738 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004739 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004740 }
4741 }
4742 break;
4743
4744 default:
4745 break; /* Normal token */
4746 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004747
4748 if (!skip) {
4749 Token *t;
4750
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004751 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004752
4753 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004754 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004755 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004756 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004757 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004758 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004759 pep = &t->next;
4760 white = 0;
4761 }
4762 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004763
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004764 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004765 * Possible further processing of parameters. Note that the
4766 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004767 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004768 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004769 enum sparmflags flags = m->params[i].flags;
4770
4771 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004772 /* Evaluate this parameter as a number */
4773 struct ppscan pps;
4774 struct tokenval tokval;
4775 expr *evalresult;
4776 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004777
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004778 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4779 pps.ntokens = -1;
4780 tokval.t_type = TOKEN_INVALID;
4781 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004782
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004783 free_tlist(eval_param);
4784 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004785
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004786 if (!evalresult) {
4787 /* Nothing meaningful to do */
4788 } else if (tokval.t_type) {
4789 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4790 } else if (!is_simple(evalresult)) {
4791 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4792 } else {
4793 params[i] = make_tok_num(reloc_value(evalresult));
4794 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004795 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004796
4797 if (flags & SPARM_STR) {
4798 /* Convert expansion to a quoted string */
4799 char *arg;
4800 Token *qs;
4801
4802 qs = expand_smacro_noreset(params[i]);
4803 arg = detoken(qs, false);
4804 free_tlist(qs);
4805 params[i] = make_tok_qstr(arg);
4806 nasm_free(arg);
4807 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004808 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004809 }
4810
4811 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004812 tline = tline->next; /* Remove the macro call from the input */
4813 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004814
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004815 /* Note: we own the expansion this returns. */
4816 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004817
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004818 tup = NULL;
4819 ttail = NULL; /* Pointer to the last token of the expansion */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004820 cond_comma = false;
4821
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004822 while (t) {
4823 enum pp_token_type type = t->type;
4824 Token *tnext = t->next;
4825 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004826
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004827 switch (type) {
4828 case TOK_PREPROC_Q:
4829 case TOK_PREPROC_QQ:
4830 t->type = TOK_ID;
4831 nasm_free(t->text);
4832 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4833 t->len = nasm_last_string_len();
4834 t->next = tline;
4835 break;
4836
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004837 case TOK_COND_COMMA:
4838 delete_Token(t);
4839 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4840 break;
4841
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004842 case TOK_ID:
4843 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004844 /*
4845 * Chain this into the target line *before* expanding,
4846 * that way we pick up any arguments to the new macro call,
4847 * if applicable.
4848 */
4849 t->next = tline;
4850 tp = &t;
4851 expand_one_smacro(&tp);
4852 if (t == tline)
4853 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004854 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004855
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004856 default:
4857 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004858 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004859 nasm_assert(!tup && param < nparam);
4860 delete_Token(t);
4861 t = NULL;
4862 tup = tnext;
4863 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004864 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004865 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004866 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004867 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004868 }
4869
4870 if (t) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004871 if (t->type != TOK_WHITESPACE)
4872 cond_comma = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004873 tline = t;
4874 if (!ttail)
4875 ttail = t;
4876 }
4877
4878 if (tnext) {
4879 t = tnext;
4880 } else {
4881 t = tup;
4882 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004883 }
4884 }
4885
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004886 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004887 if (ttail)
4888 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004889
4890 m->in_progress = false;
4891
4892 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004893 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004894 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004895
4896 /*
4897 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004898 * and then advance to the next input token. Note that this is
4899 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004900 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004901not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004902 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004903 m = NULL;
4904done:
4905 smacro_deadman.levels++;
4906 if (unlikely(params))
4907 free_tlist_array(params, nparam);
4908 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004909}
4910
4911/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004912 * Expand all single-line macro calls made in the given line.
4913 * Return the expanded version of the line. The original is deemed
4914 * to be destroyed in the process. (In reality we'll just move
4915 * Tokens from input to output a lot of the time, rather than
4916 * actually bothering to destroy and replicate.)
4917 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004918static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004919{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004920 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07004921 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4922 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004923 return expand_smacro_noreset(tline);
4924}
4925
4926static Token *expand_smacro_noreset(Token * tline)
4927{
4928 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004929 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004930 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004931
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004932 /*
4933 * Trick: we should avoid changing the start token pointer since it can
4934 * be contained in "next" field of other token. Because of this
4935 * we allocate a copy of first token and work with it; at the end of
4936 * routine we copy it back
4937 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004938 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004939 tline = new_Token(org_tline->next, org_tline->type,
4940 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004941 nasm_free(org_tline->text);
4942 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004943 }
4944
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004945
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004946 /*
4947 * Pretend that we always end up doing expansion on the first pass;
4948 * that way %+ get processed. However, if we process %+ before the
4949 * first pass, we end up with things like MACRO %+ TAIL trying to
4950 * look up the macro "MACROTAIL", which we don't want.
4951 */
4952 expanded = true;
4953 thead = tline;
4954 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004955 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004956 {
4957 PP_CONCAT_MASK(TOK_ID) |
4958 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4959 PP_CONCAT_MASK(TOK_ID) |
4960 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4961 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4962 }
4963 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004964
4965 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004966 while ((t = *tail)) /* main token loop */
4967 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004968
4969 if (!expanded) {
4970 tline = thead;
4971 break; /* Done! */
4972 }
4973
4974 /*
4975 * Now scan the entire line and look for successive TOK_IDs
4976 * that resulted after expansion (they can't be produced by
4977 * tokenize()). The successive TOK_IDs should be concatenated.
4978 * Also we look for %+ tokens and concatenate the tokens
4979 * before and after them (without white spaces in between).
4980 */
4981 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004982
4983 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004984 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004985
H. Peter Anvine2c80182005-01-15 22:15:51 +00004986 if (org_tline) {
4987 if (thead) {
4988 *org_tline = *thead;
4989 /* since we just gave text to org_line, don't free it */
4990 thead->text = NULL;
4991 delete_Token(thead);
4992 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004993 /*
4994 * The expression expanded to empty line;
4995 * we can't return NULL because of the "trick" above.
4996 * Just set the line to a single WHITESPACE token.
4997 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004998 memset(org_tline, 0, sizeof(*org_tline));
4999 org_tline->text = NULL;
5000 org_tline->type = TOK_WHITESPACE;
5001 }
5002 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005003 }
5004
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005005 return thead;
5006}
5007
5008/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005009 * Similar to expand_smacro but used exclusively with macro identifiers
5010 * right before they are fetched in. The reason is that there can be
5011 * identifiers consisting of several subparts. We consider that if there
5012 * are more than one element forming the name, user wants a expansion,
5013 * otherwise it will be left as-is. Example:
5014 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005015 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005016 *
5017 * the identifier %$abc will be left as-is so that the handler for %define
5018 * will suck it and define the corresponding value. Other case:
5019 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005020 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005021 *
5022 * In this case user wants name to be expanded *before* %define starts
5023 * working, so we'll expand %$abc into something (if it has a value;
5024 * otherwise it will be left as-is) then concatenate all successive
5025 * PP_IDs into one.
5026 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005027static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005028{
5029 Token *cur, *oldnext = NULL;
5030
H. Peter Anvin734b1882002-04-30 21:01:08 +00005031 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005032 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005033
5034 cur = tline;
5035 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005036 (cur->next->type == TOK_ID ||
5037 cur->next->type == TOK_PREPROC_ID
5038 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005039 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005040
5041 /* If identifier consists of just one token, don't expand */
5042 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005043 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005044
H. Peter Anvine2c80182005-01-15 22:15:51 +00005045 if (cur) {
5046 oldnext = cur->next; /* Detach the tail past identifier */
5047 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005048 }
5049
H. Peter Anvin734b1882002-04-30 21:01:08 +00005050 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005051
H. Peter Anvine2c80182005-01-15 22:15:51 +00005052 if (cur) {
5053 /* expand_smacro possibly changhed tline; re-scan for EOL */
5054 cur = tline;
5055 while (cur && cur->next)
5056 cur = cur->next;
5057 if (cur)
5058 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005059 }
5060
5061 return tline;
5062}
5063
5064/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005065 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005066 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005067 * to check for an initial label - that's taken care of in
5068 * expand_mmacro - but must check numbers of parameters. Guaranteed
5069 * to be called with tline->type == TOK_ID, so the putative macro
5070 * name is easy to find.
5071 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005072static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005073{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005074 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005075 Token **params;
5076 int nparam;
5077
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005078 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005079
5080 /*
5081 * Efficiency: first we see if any macro exists with the given
5082 * name. If not, we can return NULL immediately. _Then_ we
5083 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005084 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005085 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005086 list_for_each(m, head)
5087 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005088 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005089 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005090 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005091
5092 /*
5093 * OK, we have a potential macro. Count and demarcate the
5094 * parameters.
5095 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005096 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005097
5098 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005099 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005100 * structure that handles this number.
5101 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005102 while (m) {
5103 if (m->nparam_min <= nparam
5104 && (m->plus || nparam <= m->nparam_max)) {
5105 /*
5106 * This one is right. Just check if cycle removal
5107 * prohibits us using it before we actually celebrate...
5108 */
5109 if (m->in_progress > m->max_depth) {
5110 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005111 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005112 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005113 }
5114 nasm_free(params);
5115 return NULL;
5116 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005117 /*
5118 * It's right, and we can use it. Add its default
5119 * parameters to the end of our list if necessary.
5120 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005121 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005122 params =
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005123 nasm_realloc(params, sizeof(*params) *
5124 (m->nparam_min + m->ndefs + 2));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005125 while (nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005126 nparam++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005127 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00005128 }
5129 }
5130 /*
5131 * If we've gone over the maximum parameter count (and
5132 * we're in Plus mode), ignore parameters beyond
5133 * nparam_max.
5134 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005135 if (m->plus && nparam > m->nparam_max)
5136 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005137
5138 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005139 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005140 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005141 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005142 }
5143 /*
5144 * This one wasn't right: look for the next one with the
5145 * same name.
5146 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005147 list_for_each(m, m->next)
5148 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005149 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005150 }
5151
5152 /*
5153 * After all that, we didn't find one with the right number of
5154 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005155 *!
5156 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5157 *! warns about \i{multi-line macros} being invoked
5158 *! with the wrong number of parameters. See \k{mlmacover} for an
5159 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005160 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005161 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5162 "multi-line macro `%s' exists, but not taking %d parameter%s",
5163 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005164 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005165 return NULL;
5166}
5167
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005168
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005169#if 0
5170
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005171/*
5172 * Save MMacro invocation specific fields in
5173 * preparation for a recursive macro expansion
5174 */
5175static void push_mmacro(MMacro *m)
5176{
5177 MMacroInvocation *i;
5178
5179 i = nasm_malloc(sizeof(MMacroInvocation));
5180 i->prev = m->prev;
5181 i->params = m->params;
5182 i->iline = m->iline;
5183 i->nparam = m->nparam;
5184 i->rotate = m->rotate;
5185 i->paramlen = m->paramlen;
5186 i->unique = m->unique;
5187 i->condcnt = m->condcnt;
5188 m->prev = i;
5189}
5190
5191
5192/*
5193 * Restore MMacro invocation specific fields that were
5194 * saved during a previous recursive macro expansion
5195 */
5196static void pop_mmacro(MMacro *m)
5197{
5198 MMacroInvocation *i;
5199
5200 if (m->prev) {
5201 i = m->prev;
5202 m->prev = i->prev;
5203 m->params = i->params;
5204 m->iline = i->iline;
5205 m->nparam = i->nparam;
5206 m->rotate = i->rotate;
5207 m->paramlen = i->paramlen;
5208 m->unique = i->unique;
5209 m->condcnt = i->condcnt;
5210 nasm_free(i);
5211 }
5212}
5213
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005214#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005215
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005216/*
5217 * Expand the multi-line macro call made by the given line, if
5218 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005219 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005220 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005221static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005222{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005223 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005224 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005225 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005226 Token **params, *t, *tt;
5227 MMacro *m;
5228 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005229 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005230 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005231 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005232
5233 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005234 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005235 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005236 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005237 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005238 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005239 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005240 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005241 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005242 Token *last;
5243 /*
5244 * We have an id which isn't a macro call. We'll assume
5245 * it might be a label; we'll also check to see if a
5246 * colon follows it. Then, if there's another id after
5247 * that lot, we'll check it again for macro-hood.
5248 */
5249 label = last = t;
5250 t = t->next;
5251 if (tok_type_(t, TOK_WHITESPACE))
5252 last = t, t = t->next;
5253 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005254 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005255 last = t, t = t->next;
5256 if (tok_type_(t, TOK_WHITESPACE))
5257 last = t, t = t->next;
5258 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005259 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005260 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005261 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005262 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005263 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005264 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005265
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005266 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5267 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5268 if (!mmacro_deadman.triggered) {
5269 nasm_nonfatal("interminable multiline macro recursion");
5270 mmacro_deadman.triggered = true;
5271 }
5272 return 0;
5273 }
5274
5275 mmacro_deadman.total++;
5276 mmacro_deadman.levels++;
5277
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005278 /*
5279 * Fix up the parameters: this involves stripping leading and
5280 * trailing whitespace, then stripping braces if they are
5281 * present.
5282 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005283 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005284
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005285 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005286 int brace = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005287 int comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005288
H. Peter Anvine2c80182005-01-15 22:15:51 +00005289 skip_white_(t);
5290 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005291 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005292 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005293 while (t) {
5294 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5295 break; /* ... because we have hit a comma */
5296 if (comma && t->type == TOK_WHITESPACE
5297 && tok_is_(t->next, ","))
5298 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005299 if (brace && t->type == TOK_OTHER) {
5300 if (t->text[0] == '{')
5301 brace++; /* ... or a nested opening brace */
5302 else if (t->text[0] == '}')
5303 if (!--brace)
5304 break; /* ... or a brace */
5305 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005306 t = t->next;
5307 paramlen[i]++;
5308 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005309 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005310 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005311 }
5312
5313 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005314 * OK, we have a MMacro structure together with a set of
5315 * parameters. We must now go through the expansion and push
5316 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005317 * parameter tokens and macro-local tokens doesn't get done
5318 * until the single-line macro substitution process; this is
5319 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005320 * later through %rotate and give the right semantics for
5321 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005322 *
5323 * First, push an end marker on to istk->expansion, mark this
5324 * macro as in progress, and set up its invocation-specific
5325 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005326 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005327 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005328 ll->next = istk->expansion;
5329 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005330 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005331
5332 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005333 * Save the previous MMacro expansion in the case of
5334 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005335 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005336#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005337 if (m->max_depth && m->in_progress)
5338 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005339#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005340
5341 m->in_progress ++;
5342 m->params = params;
5343 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005344 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005345 m->nparam = nparam;
5346 m->rotate = 0;
5347 m->paramlen = paramlen;
5348 m->unique = unique++;
5349 m->lineno = 0;
5350 m->condcnt = 0;
5351
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005352 m->mstk = istk->mstk;
5353 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005354
5355 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005356 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005357 ll->next = istk->expansion;
5358 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005359 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005360 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005361
5362 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005363 * If we had a label, and this macro definition does not include
5364 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005365 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005366 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005367 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005368 /*
5369 * We had a label. If this macro contains an %00 parameter,
5370 * save the value as a special parameter (which is what it
5371 * is), otherwise push it as the first line of the macro
5372 * expansion.
5373 */
5374 if (m->capture_label) {
5375 params[0] = dup_Token(NULL, label);
5376 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005377 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005378 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005379 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005380 ll->finishes = NULL;
5381 ll->next = istk->expansion;
5382 istk->expansion = ll;
5383 ll->first = startline;
5384 if (!dont_prepend) {
5385 while (label->next)
5386 label = label->next;
5387 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005388 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005389 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005390 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005391
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005392 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005393
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005394 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005395}
5396
H. Peter Anvin130736c2016-02-17 20:27:41 -08005397/*
5398 * This function adds macro names to error messages, and suppresses
5399 * them if necessary.
5400 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005401static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005402{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005403 /*
5404 * If we're in a dead branch of IF or something like it, ignore the error.
5405 * However, because %else etc are evaluated in the state context
5406 * of the previous branch, errors might get lost:
5407 * %if 0 ... %else trailing garbage ... %endif
5408 * So %else etc should set the ERR_PP_PRECOND flag.
5409 */
5410 if ((severity & ERR_MASK) < ERR_FATAL &&
5411 istk && istk->conds &&
5412 ((severity & ERR_PP_PRECOND) ?
5413 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005414 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005415 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005416
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005417 /* This doesn't make sense with the macro stack unwinding */
5418 if (0) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005419 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005420
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005421 /* get %macro name */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005422 if (!(severity & ERR_NOFILE) && istk && istk->mstk.mmac) {
5423 MMacro *mmac = istk->mstk.mmac;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005424 char *buf;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005425
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005426 nasm_set_verror(real_verror);
5427 buf = nasm_vasprintf(fmt, arg);
5428 nasm_error(severity, "(%s:%"PRId32") %s",
5429 mmac->name, mmac->lineno - delta, buf);
5430 nasm_set_verror(pp_verror);
5431 nasm_free(buf);
5432 return;
5433 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005434 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005435 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005436}
5437
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005438static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005439stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005440{
5441 (void)s;
5442 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005443 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005444
5445 return make_tok_qstr(src_get_fname());
5446}
5447
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005448static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005449stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005450{
5451 (void)s;
5452 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005453 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005454
5455 return make_tok_num(src_get_linnum());
5456}
5457
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005458static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005459stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005460{
5461 (void)s;
5462 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005463 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005464
5465 return make_tok_num(globalbits);
5466}
5467
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005468static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005469stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005470{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005471 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005472 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5473 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5474 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005475
5476 (void)s;
5477 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005478 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005479
5480 switch (globalbits) {
5481 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005482 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005483 break;
5484 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005485 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005486 break;
5487 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005488 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005489 break;
5490 default:
5491 panic();
5492 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005493
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005494 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005495}
5496
H. Peter Anvin8b262472019-02-26 14:00:54 -08005497/* Add magic standard macros */
5498struct magic_macros {
5499 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005500 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005501 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005502};
5503static const struct magic_macros magic_macros[] =
5504{
5505 { "__FILE__", 0, stdmac_file },
5506 { "__LINE__", 0, stdmac_line },
5507 { "__BITS__", 0, stdmac_bits },
5508 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005509 { NULL, 0, NULL }
5510};
5511
5512static void pp_add_magic_stdmac(void)
5513{
5514 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005515 SMacro tmpl;
5516
5517 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005518
5519 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005520 tmpl.nparam = m->nparam;
5521 tmpl.expand = m->func;
5522 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005523 }
5524}
5525
H. Peter Anvin734b1882002-04-30 21:01:08 +00005526static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005527pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005528{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005529 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005530 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005531
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005532 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005533 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005534 nested_mac_count = 0;
5535 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005536 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005537 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005538 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005539 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005540
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005541 if (!use_loaded)
5542 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5543 memset(use_loaded, 0, use_package_count * sizeof(bool));
5544
H. Peter Anvin6686de22019-08-10 05:33:14 -07005545 /* First set up the top level input file */
5546 nasm_new(istk);
5547 istk->fp = nasm_open_read(file, NF_TEXT);
5548 src_set(0, file);
5549 istk->lineinc = 1;
5550 if (!istk->fp)
5551 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5552
5553 strlist_add(deplist, file);
5554
5555 /*
5556 * Set up the stdmac packages as a virtual include file,
5557 * indicated by a null file pointer.
5558 */
5559 nasm_new(inc);
5560 inc->next = istk;
5561 inc->fname = src_set_fname(NULL);
5562 inc->nolist = !list_option('b');
5563 istk = inc;
5564 lfmt->uplevel(LIST_INCLUDE, 0);
5565
H. Peter Anvin8b262472019-02-26 14:00:54 -08005566 pp_add_magic_stdmac();
5567
H. Peter Anvinf7606612016-07-13 14:23:48 -07005568 if (tasm_compatible_mode)
5569 pp_add_stdmac(nasm_stdmac_tasm);
5570
5571 pp_add_stdmac(nasm_stdmac_nasm);
5572 pp_add_stdmac(nasm_stdmac_version);
5573
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005574 if (extrastdmac)
5575 pp_add_stdmac(extrastdmac);
5576
H. Peter Anvinf7606612016-07-13 14:23:48 -07005577 stdmacpos = stdmacros[0];
5578 stdmacnext = &stdmacros[1];
5579
H. Peter Anvind2456592008-06-19 15:04:18 -07005580 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005581
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005582 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005583 * Define the __PASS__ macro. This is defined here unlike all the
5584 * other builtins, because it is special -- it varies between
5585 * passes -- but there is really no particular reason to make it
5586 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005587 *
5588 * 0 = dependencies only
5589 * 1 = preparatory passes
5590 * 2 = final pass
5591 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005592 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005593 switch (mode) {
5594 case PP_NORMAL:
5595 apass = pass_final() ? 2 : 1;
5596 break;
5597 case PP_DEPS:
5598 apass = 0;
5599 break;
5600 case PP_PREPROC:
5601 apass = 3;
5602 break;
5603 default:
5604 panic();
5605 }
5606
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005607 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005608}
5609
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005610static void pp_init(void)
5611{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005612}
5613
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005614/*
5615 * Get a line of tokens. If we popped the macro expansion/include stack,
5616 * we return a pointer to the dummy token tok_pop; at that point if
5617 * istk is NULL then we have reached end of input;
5618 */
5619static Token tok_pop; /* Dummy token placeholder */
5620
5621static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005622{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005623 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005624 Line *l = istk->expansion;
5625 Token *tline = NULL;
5626 Token *dtline;
5627
H. Peter Anvine2c80182005-01-15 22:15:51 +00005628 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005629 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005630 * buffer or from the input file.
5631 */
5632 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005633 while (l && l->finishes) {
5634 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005635
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005636 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005637 /*
5638 * This is a macro-end marker for a macro with no
5639 * name, which means it's not really a macro at all
5640 * but a %rep block, and the `in_progress' field is
5641 * more than 1, meaning that we still need to
5642 * repeat. (1 means the natural last repetition; 0
5643 * means termination by %exitrep.) We have
5644 * therefore expanded up to the %endrep, and must
5645 * push the whole block on to the expansion buffer
5646 * again. We don't bother to remove the macro-end
5647 * marker: we'd only have to generate another one
5648 * if we did.
5649 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005650 fm->in_progress--;
5651 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005652 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005653 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005654
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005655 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005656 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005657 tail = &ll->first;
5658
5659 list_for_each(t, l->first) {
5660 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005661 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005662 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005663 }
5664 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005665 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005666 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005667 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005668 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005669 MMacro *m = istk->mstk.mstk;
5670
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005671 /*
5672 * Check whether a `%rep' was started and not ended
5673 * within this macro expansion. This can happen and
5674 * should be detected. It's a fatal error because
5675 * I'm too confused to work out how to recover
5676 * sensibly from it.
5677 */
5678 if (defining) {
5679 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005680 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005681 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005682 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005683 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005684 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005685
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005686 /*
5687 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005688 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005689 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005690 istk->mstk = m->mstk;
5691 if (m->name) {
5692 /*
5693 * This was a real macro call, not a %rep, and
5694 * therefore the parameter information needs to
5695 * be freed and the iteration count/nesting
5696 * depth adjusted.
5697 */
5698
5699 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005700 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005701 * If all mmacro processing done,
5702 * clear all counters and the deadman
5703 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005704 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005705 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005706 }
5707
Adam Majer91e72402017-07-25 10:42:01 +02005708#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005709 if (m->prev) {
5710 pop_mmacro(m);
5711 fm->in_progress --;
5712 } else
Adam Majer91e72402017-07-25 10:42:01 +02005713#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005714 {
5715 nasm_free(m->params);
5716 free_tlist(m->iline);
5717 nasm_free(m->paramlen);
5718 fm->in_progress = 0;
5719 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005720 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005721
5722 /*
5723 * FIXME It is incorrect to always free_mmacro here.
5724 * It leads to usage-after-free.
5725 *
5726 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5727 */
5728#if 0
5729 else
5730 free_mmacro(m);
5731#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005732 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005733 istk->expansion = l->next;
5734 nasm_free(l);
5735 lfmt->downlevel(LIST_MACRO);
5736 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005737 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005738
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005739 do { /* until we get a line we can use */
5740 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005741
5742 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005743 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005744 int32_t lineno;
5745
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005746 if (istk->mstk.mstk) {
5747 istk->mstk.mstk->lineno++;
5748 if (istk->mstk.mstk->fname)
5749 lineno = istk->mstk.mstk->lineno +
5750 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005751 else
5752 lineno = 0; /* Defined at init time or builtin */
5753 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005754 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005755 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005756
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005757 tline = l->first;
5758 istk->expansion = l->next;
5759 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005760
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005761 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005762 if (!istk->nolist)
5763 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005764 nasm_free(line);
5765 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005766 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005767 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005768 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005769 } else {
5770 /*
5771 * The current file has ended; work down the istk
5772 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005773 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005774 if (i->fp)
5775 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005776 if (i->conds) {
5777 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005778 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005779 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005780 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005781 if (i->next)
5782 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005783 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005784 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005785 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005786 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005787 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005788 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005789
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005790 /*
5791 * We must expand MMacro parameters and MMacro-local labels
5792 * _before_ we plunge into directive processing, to cope
5793 * with things like `%define something %1' such as STRUC
5794 * uses. Unless we're _defining_ a MMacro, in which case
5795 * those tokens should be left alone to go into the
5796 * definition; and unless we're in a non-emitting
5797 * condition, in which case we don't want to meddle with
5798 * anything.
5799 */
5800 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005801 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005802 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005803 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005804
H. Peter Anvine2c80182005-01-15 22:15:51 +00005805 /*
5806 * Check the line to see if it's a preprocessor directive.
5807 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005808 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5809 if (dtline)
5810 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005811 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005812 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005813 * We're defining a multi-line macro. We emit nothing
5814 * at all, and just
5815 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005816 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005817 MMacro *mmac = defining->dstk.mmac;
5818
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005819 Line *l = nasm_malloc(sizeof(Line));
5820 l->next = defining->expansion;
5821 l->first = tline;
5822 l->finishes = NULL;
5823 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005824
5825 /*
5826 * Remember if this mmacro expansion contains %00:
5827 * if it does, we will have to handle leading labels
5828 * specially.
5829 */
5830 if (mmac) {
5831 const Token *t;
5832 list_for_each(t, tline) {
5833 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
5834 mmac->capture_label = true;
5835 }
5836 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005837 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005838 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005839 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005840 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005841 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005842 * directive so we keep our place correctly.
5843 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005844 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005845 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005846 /*
5847 * We're in a %rep block which has been terminated, so
5848 * we're walking through to the %endrep without
5849 * emitting anything. Emit nothing at all, not even a
5850 * blank line: when we emerge from the %rep block we'll
5851 * give a line-number directive so we keep our place
5852 * correctly.
5853 */
5854 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005855 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005856 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005857 if (!expand_mmacro(tline))
5858 return tline;
5859 }
5860 }
5861}
5862
5863static char *pp_getline(void)
5864{
5865 char *line = NULL;
5866 Token *tline;
5867
5868 real_verror = nasm_set_verror(pp_verror);
5869
5870 while (true) {
5871 tline = pp_tokline();
5872 if (tline == &tok_pop) {
5873 /*
5874 * We popped the macro/include stack. If istk is empty,
5875 * we are at end of input, otherwise just loop back.
5876 */
5877 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005878 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005879 } else {
5880 /*
5881 * De-tokenize the line and emit it.
5882 */
5883 line = detoken(tline, true);
5884 free_tlist(tline);
5885 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005886 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005887 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005888
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005889 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005890 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005891 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005892 nasm_free(buf);
5893 }
5894
H. Peter Anvin130736c2016-02-17 20:27:41 -08005895 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005896 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005897}
5898
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005899static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005900{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005901 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005902
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005903 if (defining) {
5904 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005905 nasm_nonfatal("end of file while still defining macro `%s'",
5906 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005907 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005908 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005909 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005910
5911 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005912 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005913 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005914
5915 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005916
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005917 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005918 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005919 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005920 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005921 Include *i = istk;
5922 istk = istk->next;
5923 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005924 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005925 }
5926 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005927 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005928 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005929}
5930
5931static void pp_cleanup_session(void)
5932{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005933 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005934 free_llist(predef);
5935 predef = NULL;
5936 delete_Blocks();
5937 freeTokens = NULL;
5938 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005939}
5940
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005941static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005942{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005943 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005944}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005945
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005946static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005947{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005948 Token *inc, *space, *name;
5949 Line *l;
5950
H. Peter Anvin734b1882002-04-30 21:01:08 +00005951 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5952 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5953 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005954
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005955 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005956 l->next = predef;
5957 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005958 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005959 predef = l;
5960}
5961
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005962static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005963{
5964 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005965 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005966 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005967
H. Peter Anvin130736c2016-02-17 20:27:41 -08005968 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005969
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005970 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005971 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5972 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005973 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005974 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005975 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005976 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005977 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005978
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005979 if (space->next->type != TOK_PREPROC_ID &&
5980 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005981 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005982
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005983 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005984 l->next = predef;
5985 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005986 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005987 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005988
5989 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005990}
5991
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005992static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005993{
5994 Token *def, *space;
5995 Line *l;
5996
H. Peter Anvin734b1882002-04-30 21:01:08 +00005997 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5998 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005999 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006000
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006001 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006002 l->next = predef;
6003 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006004 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006005 predef = l;
6006}
6007
H. Peter Anvin05990342018-06-11 13:32:42 -07006008/* Insert an early preprocessor command that doesn't need special handling */
6009static void pp_pre_command(const char *what, char *string)
6010{
6011 char *cmd;
6012 Token *def, *space;
6013 Line *l;
6014
6015 def = tokenize(string);
6016 if (what) {
6017 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6018 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
6019 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6020 }
6021
6022 l = nasm_malloc(sizeof(Line));
6023 l->next = predef;
6024 l->first = def;
6025 l->finishes = NULL;
6026 predef = l;
6027}
6028
H. Peter Anvinf7606612016-07-13 14:23:48 -07006029static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006030{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006031 macros_t **mp;
6032
6033 /* Find the end of the list and avoid duplicates */
6034 for (mp = stdmacros; *mp; mp++) {
6035 if (*mp == macros)
6036 return; /* Nothing to do */
6037 }
6038
6039 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6040
6041 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006042}
6043
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006044static void pp_extra_stdmac(macros_t *macros)
6045{
6046 extrastdmac = macros;
6047}
6048
H. Peter Anvin8b262472019-02-26 14:00:54 -08006049static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006050{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006051 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006052 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6053 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6054}
6055
6056static Token *make_tok_qstr(const char *str)
6057{
6058 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006059 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006060 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006061}
6062
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006063static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006064{
6065 if (!m)
6066 return;
6067
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006068 /* We need to print the mstk.mmac list in reverse order */
6069 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006070
6071 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006072 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006073 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006074 }
6075}
6076
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006077static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006078{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006079 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006080
H. Peter Anvinddb29062018-12-11 00:06:29 -08006081 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6082 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006083
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006084 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006085 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006086
H. Peter Anvinddb29062018-12-11 00:06:29 -08006087 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006088}
6089
H. Peter Anvine7469712016-02-18 02:20:59 -08006090const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006091 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006092 pp_reset,
6093 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006094 pp_cleanup_pass,
6095 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006096 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006097 pp_pre_define,
6098 pp_pre_undefine,
6099 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006100 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006101 pp_include_path,
6102 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006103};