blob: 7fc09d8e9eeaf59fb8f75b5dc4e981697cf69ff0 [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;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002318 bool err, done;
2319 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002320 Token **tn = *tpp;
2321 Token *t = *tn;
2322 Token *name;
2323
2324 while (t && t->type == TOK_WHITESPACE) {
2325 tn = &t->next;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002326 t = *tn;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002327 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002328
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002329 if (!tok_is_(t, "("))
2330 goto finish;
2331
2332 if (tmpl) {
2333 Token *tx = t;
2334 Token **txpp = &tx;
2335 int sparam;
2336
2337 /* Count parameters first */
2338 sparam = parse_smacro_template(&txpp, NULL);
2339 if (!sparam)
2340 goto finish; /* No parameters, we're done */
2341 nasm_newn(params, sparam);
2342 }
2343
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002344 /* Skip leading paren */
2345 tn = &t->next;
2346 t = *tn;
2347
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002348 name = NULL;
2349 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002350 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002351
2352 while (!done) {
2353 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002354 if (name || flags)
2355 nasm_nonfatal("`)' expected to terminate macro template");
2356 else
2357 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002358 break;
2359 }
2360
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002361 switch (t->type) {
2362 case TOK_ID:
2363 if (name)
2364 goto bad;
2365 name = t;
2366 break;
2367
2368 case TOK_OTHER:
2369 if (t->text[1])
2370 goto bad;
2371 switch (t->text[0]) {
2372 case '=':
2373 flags |= SPARM_EVAL;
2374 break;
2375 case '&':
2376 flags |= SPARM_STR;
2377 break;
2378 case '!':
2379 flags |= SPARM_NOSTRIP;
2380 break;
2381 case '+':
2382 flags |= SPARM_GREEDY;
2383 greedy = true;
2384 break;
2385 case ',':
2386 if (greedy)
2387 nasm_nonfatal("greedy parameter must be last");
2388 /* fall through */
2389 case ')':
2390 if (params) {
2391 if (name) {
2392 params[nparam].name = name->text;
2393 params[nparam].namelen = name->len;
2394 name->text = NULL;
2395 }
2396 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002397 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002398 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002399 name = NULL;
2400 flags = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002401 done = t->text[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002402 break;
2403 default:
2404 goto bad;
2405 }
2406 break;
2407
2408 case TOK_WHITESPACE:
2409 break;
2410
2411 default:
2412 bad:
2413 if (!err) {
2414 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2415 err = true;
2416 }
2417 break;
2418 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002419
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002420 tn = &t->next;
2421 t = *tn;
2422 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002423
2424finish:
2425 while (t && t->type == TOK_WHITESPACE) {
2426 tn = &t->next;
2427 t = t->next;
2428 }
2429 *tpp = tn;
2430 if (tmpl) {
2431 tmpl->nparam = nparam;
2432 tmpl->greedy = greedy;
2433 tmpl->params = params;
2434 }
2435 return nparam;
2436}
2437
2438/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002439 * Common code for defining an smacro. The tmpl argument, if not NULL,
2440 * contains any macro parameters that aren't explicit arguments;
2441 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002442 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002443static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002444 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002445{
2446 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002447 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002448 Context *ctx;
2449 bool defining_alias = false;
2450 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002451
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002452 if (tmpl) {
2453 defining_alias = tmpl->alias;
2454 nparam = tmpl->nparam;
2455 }
2456
2457 while (1) {
2458 ctx = get_ctx(mname, &mname);
2459
2460 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2461 /* Create a new macro */
2462 smtbl = ctx ? &ctx->localmac : &smacros;
2463 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2464 nasm_new(smac);
2465 smac->next = *smhead;
2466 *smhead = smac;
2467 break;
2468 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002469 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002470 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002471 /*
2472 * Some instances of the old code considered this a failure,
2473 * some others didn't. What is the right thing to do here?
2474 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002475 goto fail;
2476 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 /*
2478 * We're redefining, so we have to take over an
2479 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002480 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002481 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002482 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002483 break;
2484 } else if (smac->in_progress) {
2485 nasm_nonfatal("macro alias loop");
2486 goto fail;
2487 } else {
2488 /* It is an alias macro; follow the alias link */
2489 SMacro *s;
2490
2491 smac->in_progress = true;
2492 s = define_smacro(smac->expansion->text, casesense,
2493 expansion, tmpl);
2494 smac->in_progress = false;
2495 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002496 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002497 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002498
2499 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002500 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002501 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002502 smac->expand = smacro_expand_default;
2503 if (tmpl) {
2504 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002505 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002506 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002507 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002508 if (tmpl->expand)
2509 smac->expand = tmpl->expand;
2510 }
2511 if (list_option('m')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002512 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2513 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002514 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002515 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002516
2517fail:
2518 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002519 if (tmpl)
2520 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002521 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002522}
2523
2524/*
2525 * Undefine an smacro
2526 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002527static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002528{
2529 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002530 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002531 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002532
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002533 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002534 smtbl = ctx ? &ctx->localmac : &smacros;
2535 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002536
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002537 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002538 /*
2539 * We now have a macro name... go hunt for it.
2540 */
2541 sp = smhead;
2542 while ((s = *sp) != NULL) {
2543 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002544 if (s->alias && !undefalias) {
2545 if (s->in_progress) {
2546 nasm_nonfatal("macro alias loop");
2547 } else {
2548 s->in_progress = true;
2549 undef_smacro(s->expansion->text, false);
2550 s->in_progress = false;
2551 }
2552 } else {
2553 if (list_option('m'))
2554 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2555 ctx, s);
2556 *sp = s->next;
2557 free_smacro(s);
2558 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002559 } else {
2560 sp = &s->next;
2561 }
2562 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002563 }
2564}
2565
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002566/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002567 * Parse a mmacro specification.
2568 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002569static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002570{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002571 tline = tline->next;
2572 skip_white_(tline);
2573 tline = expand_id(tline);
2574 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002575 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002576 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002577 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002578
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002579#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002580 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002581#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002582 def->name = nasm_strdup(tline->text);
2583 def->plus = false;
2584 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002585 def->nparam_min = 0;
2586 def->nparam_max = 0;
2587
H. Peter Anvina26433d2008-07-16 14:40:01 -07002588 tline = expand_smacro(tline->next);
2589 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002590 if (!tok_type_(tline, TOK_NUMBER))
2591 nasm_nonfatal("`%s' expects a parameter count", directive);
2592 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002593 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002594 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 tline = tline->next->next;
2596 if (tok_is_(tline, "*")) {
2597 def->nparam_max = INT_MAX;
2598 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002599 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002601 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002603 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002604 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 }
2606 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002607 }
2608 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 tline = tline->next;
2610 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002611 }
2612 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 !nasm_stricmp(tline->next->text, ".nolist")) {
2614 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002615 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002616 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617
H. Peter Anvina26433d2008-07-16 14:40:01 -07002618 /*
2619 * Handle default parameters.
2620 */
2621 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 def->dlist = tline->next;
2623 tline->next = NULL;
2624 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002625 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 def->dlist = NULL;
2627 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002628 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002629 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002630
H. Peter Anvin89cee572009-07-15 09:16:54 -04002631 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002632 !def->plus) {
2633 /*
2634 *!macro-defaults [on] macros with more default than optional parameters
2635 *! warns when a macro has more default parameters than optional parameters.
2636 *! See \k{mlmacdef} for why might want to disable this warning.
2637 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002638 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002639 "too many default macro parameters in macro `%s'", def->name);
2640 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002641
H. Peter Anvina26433d2008-07-16 14:40:01 -07002642 return true;
2643}
2644
2645
2646/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002647 * Decode a size directive
2648 */
2649static int parse_size(const char *str) {
2650 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002652 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002654 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002655}
2656
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002657/*
2658 * Process a preprocessor %pragma directive. Currently there are none.
2659 * Gets passed the token list starting with the "preproc" token from
2660 * "%pragma preproc".
2661 */
2662static void do_pragma_preproc(Token *tline)
2663{
2664 /* Skip to the real stuff */
2665 tline = tline->next;
2666 skip_white_(tline);
2667 if (!tline)
2668 return;
2669
2670 (void)tline; /* Nothing else to do at present */
2671}
2672
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002673static bool is_macro_id(const Token *t)
2674{
2675 return t && (t->type == TOK_ID ||
2676 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2677}
2678
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002679static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002680{
2681 char *id;
2682 Token *t = *tp;
2683
2684 t = t->next; /* Skip directive */
2685 skip_white_(t);
2686 t = expand_id(t);
2687
2688 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002689 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002690 err ? err : "macro identifier");
2691 return NULL;
2692 }
2693
2694 id = t->text;
2695 skip_white_(t);
2696 *tp = t;
2697 return id;
2698}
2699
Ed Beroset3ab3f412002-06-11 03:31:49 +00002700/**
2701 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002702 * Find out if a line contains a preprocessor directive, and deal
2703 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002704 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002705 * If a directive _is_ found, it is the responsibility of this routine
2706 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002707 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002708 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002709 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002710 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002711 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002712 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002713static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002714{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002715 enum preproc_token i;
2716 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002717 bool err;
2718 int nparam;
2719 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002720 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002721 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002722 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002723 char *p, *pp;
2724 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002725 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002726 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002727 Include *inc;
2728 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002729 Cond *cond;
2730 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002731 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002732 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002733 struct tokenval tokval;
2734 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002735 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002736 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002737 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002738 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002739
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002740 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002741 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002742
H. Peter Anvineba20a72002-04-30 20:53:55 +00002743 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002744 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002745 (tline->text[0] && (tline->text[1] == '%' ||
2746 tline->text[1] == '$' ||
2747 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002749
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002750 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002751 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002752
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002753 casesense = true;
2754 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2755 casesense = false;
2756 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002757 }
2758
2759 /*
2760 * If we're in a non-emitting branch of a condition construct,
2761 * or walking to the end of an already terminated %rep block,
2762 * we should ignore all directives except for condition
2763 * directives.
2764 */
2765 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002766 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2767 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002768 return NO_DIRECTIVE_FOUND;
2769 }
2770
2771 /*
2772 * If we're defining a macro or reading a %rep block, we should
2773 * ignore all directives except for %macro/%imacro (which nest),
2774 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2775 * If we're in a %rep block, another %rep nests, so should be let through.
2776 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002777 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002778 i != PP_ENDMACRO && i != PP_ENDM &&
2779 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2780 return NO_DIRECTIVE_FOUND;
2781 }
2782
2783 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002784 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002785 nested_mac_count++;
2786 return NO_DIRECTIVE_FOUND;
2787 } else if (nested_mac_count > 0) {
2788 if (i == PP_ENDMACRO) {
2789 nested_mac_count--;
2790 return NO_DIRECTIVE_FOUND;
2791 }
2792 }
2793 if (!defining->name) {
2794 if (i == PP_REP) {
2795 nested_rep_count++;
2796 return NO_DIRECTIVE_FOUND;
2797 } else if (nested_rep_count > 0) {
2798 if (i == PP_ENDREP) {
2799 nested_rep_count--;
2800 return NO_DIRECTIVE_FOUND;
2801 }
2802 }
2803 }
2804 }
2805
H. Peter Anvin4169a472007-09-12 01:29:43 +00002806 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002807 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002808 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002810
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002811 case PP_PRAGMA:
2812 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002813 * %pragma namespace options...
2814 *
2815 * The namespace "preproc" is reserved for the preprocessor;
2816 * all other namespaces generate a [pragma] assembly directive.
2817 *
2818 * Invalid %pragmas are ignored and may have different
2819 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002820 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002821 t = tline;
2822 tline = tline->next;
2823 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002824 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002825 while (tok_type_(tline, TOK_WHITESPACE)) {
2826 t = tline;
2827 tline = tline->next;
2828 delete_Token(t);
2829 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002830 if (tok_type_(tline, TOK_ID)) {
2831 if (!nasm_stricmp(tline->text, "preproc")) {
2832 /* Preprocessor pragma */
2833 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002834 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002835 } else {
2836 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002837
2838 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002839 for (t = tline; t->next; t = t->next)
2840 ;
2841 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002842
2843 /* Prepend "[pragma " */
2844 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2845 t = new_Token(t, TOK_ID, "pragma", 6);
2846 t = new_Token(t, TOK_OTHER, "[", 1);
2847 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002848 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002849 }
2850 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002851 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002852
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 case PP_STACKSIZE:
2854 /* Directive to tell NASM what the default stack size is. The
2855 * default is for a 16-bit stack, and this can be overriden with
2856 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 */
2858 tline = tline->next;
2859 if (tline && tline->type == TOK_WHITESPACE)
2860 tline = tline->next;
2861 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002862 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 }
2864 if (nasm_stricmp(tline->text, "flat") == 0) {
2865 /* All subsequent ARG directives are for a 32-bit stack */
2866 StackSize = 4;
2867 StackPointer = "ebp";
2868 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002869 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002870 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2871 /* All subsequent ARG directives are for a 64-bit stack */
2872 StackSize = 8;
2873 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002874 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002875 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 } else if (nasm_stricmp(tline->text, "large") == 0) {
2877 /* All subsequent ARG directives are for a 16-bit stack,
2878 * far function call.
2879 */
2880 StackSize = 2;
2881 StackPointer = "bp";
2882 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002883 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002884 } else if (nasm_stricmp(tline->text, "small") == 0) {
2885 /* All subsequent ARG directives are for a 16-bit stack,
2886 * far function call. We don't support near functions.
2887 */
2888 StackSize = 2;
2889 StackPointer = "bp";
2890 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002891 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002892 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002893 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002894 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002895 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002896
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 case PP_ARG:
2898 /* TASM like ARG directive to define arguments to functions, in
2899 * the following form:
2900 *
2901 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2902 */
2903 offset = ArgOffset;
2904 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002905 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002906 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002907
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 /* Find the argument name */
2909 tline = tline->next;
2910 if (tline && tline->type == TOK_WHITESPACE)
2911 tline = tline->next;
2912 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002913 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002914 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 }
2916 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002917
H. Peter Anvine2c80182005-01-15 22:15:51 +00002918 /* Find the argument size type */
2919 tline = tline->next;
2920 if (!tline || tline->type != TOK_OTHER
2921 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002922 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002923 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 }
2925 tline = tline->next;
2926 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002927 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002928 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002929 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002930
H. Peter Anvine2c80182005-01-15 22:15:51 +00002931 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002932 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002934 size = parse_size(tt->text);
2935 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002936 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002937 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002938 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002939 }
2940 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002941
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002942 /* Round up to even stack slots */
2943 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002944
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 /* Now define the macro for the argument */
2946 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2947 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002948 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002950
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 /* Move to the next argument in the list */
2952 tline = tline->next;
2953 if (tline && tline->type == TOK_WHITESPACE)
2954 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002955 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002956 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002957 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002958
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 case PP_LOCAL:
2960 /* TASM like LOCAL directive to define local variables for a
2961 * function, in the following form:
2962 *
2963 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2964 *
2965 * The '= LocalSize' at the end is ignored by NASM, but is
2966 * required by TASM to define the local parameter size (and used
2967 * by the TASM macro package).
2968 */
2969 offset = LocalOffset;
2970 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002971 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002973
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 /* Find the argument name */
2975 tline = tline->next;
2976 if (tline && tline->type == TOK_WHITESPACE)
2977 tline = tline->next;
2978 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002979 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002980 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 }
2982 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002983
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 /* Find the argument size type */
2985 tline = tline->next;
2986 if (!tline || tline->type != TOK_OTHER
2987 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002988 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002989 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 }
2991 tline = tline->next;
2992 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002993 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002994 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002996
H. Peter Anvine2c80182005-01-15 22:15:51 +00002997 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002998 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003000 size = parse_size(tt->text);
3001 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003002 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003003 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003004 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 }
3006 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003007
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003008 /* Round up to even stack slots */
3009 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003010
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003011 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003012
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003013 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3015 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003016 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003017
H. Peter Anvine2c80182005-01-15 22:15:51 +00003018 /* Now define the assign to setup the enter_c macro correctly */
3019 snprintf(directive, sizeof(directive),
3020 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003021 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003022
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 /* Move to the next argument in the list */
3024 tline = tline->next;
3025 if (tline && tline->type == TOK_WHITESPACE)
3026 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003027 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003028 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003029 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003030
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 case PP_CLEAR:
3032 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003033 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003034 free_macros();
3035 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003036 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003037
H. Peter Anvin418ca702008-05-30 10:42:30 -07003038 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003040 skip_white_(t);
3041 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003042 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003043 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003044 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003045 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003046 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003047 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003048 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003049 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003050 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003051 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003052 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003053
3054 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003055 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003056 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003057
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003058 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003059 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003060 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003061 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003063 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003064 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003065 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003066 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003067 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003068 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003070 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003071 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003072 (pp_mode == PP_DEPS)
3073 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003074 if (!inc->fp) {
3075 /* -MG given but file not found */
3076 nasm_free(inc);
3077 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003078 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003079 inc->lineno = src_set_linnum(0);
3080 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003081 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003082 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003083 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003084 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003085 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003086
H. Peter Anvind2456592008-06-19 15:04:18 -07003087 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003088 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003089 const struct use_package *pkg;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003090
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003091 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003092 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07003093 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003094 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003095 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003096 nasm_unquote_cstr(tline->text, NULL);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003097 pkg = nasm_find_use_package(tline->text);
3098 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003099 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003100 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003101 /*
3102 * Not already included, go ahead and include it.
3103 * Treat it as an include file for the purpose of
3104 * producing a listing.
3105 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003106 use_loaded[pkg->index] = true;
3107 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003108 nasm_new(inc);
3109 inc->next = istk;
3110 inc->fname = src_set_fname(NULL);
3111 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003112 inc->nolist = !list_option('b') || istk->nolist;
3113 istk = inc;
3114 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003115 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003116 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003117 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003119 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003120 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 tline = tline->next;
3122 skip_white_(tline);
3123 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003124 if (tline) {
3125 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003126 nasm_nonfatal("`%s' expects a context identifier",
3127 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003128 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 }
3130 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003131 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003132 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 p = nasm_strdup(tline->text);
3134 } else {
3135 p = NULL; /* Anonymous */
3136 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003137
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003138 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003139 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003140 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003142 ctx->name = p;
3143 ctx->number = unique++;
3144 cstk = ctx;
3145 } else {
3146 /* %pop or %repl */
3147 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003148 nasm_nonfatal("`%s': context stack is empty",
3149 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003150 } else if (i == PP_POP) {
3151 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003152 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003153 "expected %s",
3154 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003155 else
3156 ctx_pop();
3157 } else {
3158 /* i == PP_REPL */
3159 nasm_free(cstk->name);
3160 cstk->name = p;
3161 p = NULL;
3162 }
3163 nasm_free(p);
3164 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003165 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003166 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003167 severity = ERR_FATAL;
3168 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003170 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003172 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003173 /*!
3174 *!user [on] %warning directives
3175 *! controls output of \c{%warning} directives (see \k{pperror}).
3176 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003177 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003179
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003181 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003183 tline->next = expand_smacro(tline->next);
3184 tline = tline->next;
3185 skip_white_(tline);
3186 t = tline ? tline->next : NULL;
3187 skip_white_(t);
3188 if (tok_type_(tline, TOK_STRING) && !t) {
3189 /* The line contains only a quoted string */
3190 p = tline->text;
3191 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003192 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003193 } else {
3194 /* Not a quoted string, or more than a quoted string */
3195 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003196 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003197 nasm_free(p);
3198 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003199 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003200 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003201
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003202 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003203 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003204 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003205 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003206 j = if_condition(tline->next, i);
3207 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003208 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003209 cond = nasm_malloc(sizeof(Cond));
3210 cond->next = istk->conds;
3211 cond->state = j;
3212 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003213 if(istk->mstk.mstk)
3214 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003215 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003216
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003217 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003218 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003219 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003220 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003221 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003222 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003223 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003224
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003225 case COND_DONE:
3226 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003227 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003228
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003229 case COND_ELSE_TRUE:
3230 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003231 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003232 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003233 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003234 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003235
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003236 case COND_IF_FALSE:
3237 /*
3238 * IMPORTANT: In the case of %if, we will already have
3239 * called expand_mmac_params(); however, if we're
3240 * processing an %elif we must have been in a
3241 * non-emitting mode, which would have inhibited
3242 * the normal invocation of expand_mmac_params().
3243 * Therefore, we have to do it explicitly here.
3244 */
3245 j = if_condition(expand_mmac_params(tline->next), i);
3246 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003247 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003248 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003250 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003251
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 case PP_ELSE:
3253 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003254 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003255 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003256 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003257 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003258 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003259 case COND_IF_TRUE:
3260 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003261 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003262 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003263
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003264 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003265 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003266
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003267 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003268 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003269 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003270
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003271 case COND_ELSE_TRUE:
3272 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003273 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003274 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003275 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003276 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003277 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003278 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003279
H. Peter Anvine2c80182005-01-15 22:15:51 +00003280 case PP_ENDIF:
3281 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003282 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003283 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003284 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003285 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003286 cond = istk->conds;
3287 istk->conds = cond->next;
3288 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003289 if(istk->mstk.mstk)
3290 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003291 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003292
H. Peter Anvin8b262472019-02-26 14:00:54 -08003293 case PP_RMACRO:
3294 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003295 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003296 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003297 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003298 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003299 if (i == PP_RMACRO)
3300 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003301 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003302 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003303 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003304 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003305
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003306 src_get(&defining->xline, &defining->fname);
3307
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003308 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3309 while (mmac) {
3310 if (!strcmp(mmac->name, defining->name) &&
3311 (mmac->nparam_min <= defining->nparam_max
3312 || defining->plus)
3313 && (defining->nparam_min <= mmac->nparam_max
3314 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003315 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003316 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003317 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003318 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003319 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003320 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003321 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003322
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 case PP_ENDM:
3324 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003325 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003326 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003327 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003328 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003329 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3330 defining->next = *mmhead;
3331 *mmhead = defining;
3332 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003333 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003334
H. Peter Anvin89cee572009-07-15 09:16:54 -04003335 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003336 /*
3337 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003338 * macro-end marker for a macro with a name. Then we
3339 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003340 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003341 list_for_each(l, istk->expansion)
3342 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003343 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003344
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003345 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003346 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003347 * Remove all conditional entries relative to this
3348 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003349 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003350 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3351 cond = istk->conds;
3352 istk->conds = cond->next;
3353 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003354 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003355 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003356 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003357 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003358 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003359 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003360
H. Peter Anvina26433d2008-07-16 14:40:01 -07003361 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003362 casesense = false;
3363 /* fall through */
3364 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003365 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003366 MMacro **mmac_p;
3367 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003368
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003369 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003370 spec.casesense = casesense;
3371 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003372 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003373 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003374 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3375 while (mmac_p && *mmac_p) {
3376 mmac = *mmac_p;
3377 if (mmac->casesense == spec.casesense &&
3378 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3379 mmac->nparam_min == spec.nparam_min &&
3380 mmac->nparam_max == spec.nparam_max &&
3381 mmac->plus == spec.plus) {
3382 *mmac_p = mmac->next;
3383 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003384 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003385 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 }
3387 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003388 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003389 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003390 }
3391
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 case PP_ROTATE:
3393 if (tline->next && tline->next->type == TOK_WHITESPACE)
3394 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003395 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003397 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398 return DIRECTIVE_FOUND;
3399 }
3400 t = expand_smacro(tline->next);
3401 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003402 pps.tptr = tline = t;
3403 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 tokval.t_type = TOKEN_INVALID;
3405 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003406 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 free_tlist(tline);
3408 if (!evalresult)
3409 return DIRECTIVE_FOUND;
3410 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003411 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003412 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003413 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 return DIRECTIVE_FOUND;
3415 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003416 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003417 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003418 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003419 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003420 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003421 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003422 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003423
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003424 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003425 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003426 rotate += mmac->nparam;
3427
3428 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003429 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003430 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003431
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003433 {
3434 MMacro *tmp_defining;
3435
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003436 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003437 do {
3438 tline = tline->next;
3439 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003440
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 if (tok_type_(tline, TOK_ID) &&
3442 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003443 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003444 do {
3445 tline = tline->next;
3446 } while (tok_type_(tline, TOK_WHITESPACE));
3447 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003448
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003450 pps.tptr = expand_smacro(tline);
3451 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003453 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003455 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003456 if (!evalresult)
3457 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003458 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003459 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003460 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003461 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003462 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003464 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003465 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003466 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3467 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003468 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003469 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003470 /*!
3471 *!negative-rep [on] regative %rep count
3472 *! warns about negative counts given to the \c{%rep}
3473 *! preprocessor directive.
3474 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003475 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003476 "negative `%%rep' count: %"PRId64, count);
3477 count = 0;
3478 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003479 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003480 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003481 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003482 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003483 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003484 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003485 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003486 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003487 defining->nolist = nolist;
3488 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003489 defining->mstk = istk->mstk;
3490 defining->dstk.mstk = tmp_defining;
3491 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003492 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003493 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003494 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003495
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003497 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003498 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003499 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003501
H. Peter Anvine2c80182005-01-15 22:15:51 +00003502 /*
3503 * Now we have a "macro" defined - although it has no name
3504 * and we won't be entering it in the hash tables - we must
3505 * push a macro-end marker for it on to istk->expansion.
3506 * After that, it will take care of propagating itself (a
3507 * macro-end marker line for a macro which is really a %rep
3508 * block will cause the macro to be re-expanded, complete
3509 * with another macro-end marker to ensure the process
3510 * continues) until the whole expansion is forcibly removed
3511 * from istk->expansion by a %exitrep.
3512 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003513 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003514 l->next = istk->expansion;
3515 l->finishes = defining;
3516 l->first = NULL;
3517 istk->expansion = l;
3518
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003519 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003520
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003521 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003522 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003523 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003524
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003526 /*
3527 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003528 * macro-end marker for a macro with no name. Then we set
3529 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003530 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003531 list_for_each(l, istk->expansion)
3532 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003533 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003534
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003535 if (l)
3536 l->finishes->in_progress = 1;
3537 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003538 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003539 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003540
H. Peter Anvin8b262472019-02-26 14:00:54 -08003541 case PP_DEFINE:
3542 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003543 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003544 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003545 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003546 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003547
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003548 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003549 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003550
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003551 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003552 lastp = &tline->next;
3553 nparam = parse_smacro_template(&lastp, &tmpl);
3554 tline = *lastp;
3555 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003556
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003557 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003558 macro_start = tline;
3559 if (!is_macro_id(macro_start)) {
3560 nasm_nonfatal("`%s' expects a macro identifier to alias",
3561 dname);
3562 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003564 tt = macro_start->next;
3565 macro_start->next = NULL;
3566 tline = tline->next;
3567 skip_white_(tline);
3568 if (tline && tline->type) {
3569 nasm_warn(WARN_OTHER,
3570 "trailing garbage after aliasing identifier ignored");
3571 }
3572 free_tlist(tt);
3573 tmpl.alias = true;
3574 } else {
3575 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003576 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003577 tline = expand_smacro(tline);
3578
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003579 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003580 macro_start = NULL;
3581 t = tline;
3582 while (t) {
3583 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003584 for (i = 0; i < nparam; i++) {
3585 if ((size_t)tmpl.params[i].namelen == t->len &&
3586 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3587 t->type = tok_smac_param(i);
3588 break;
3589 }
3590 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003591 }
3592 tt = t->next;
3593 t->next = macro_start;
3594 macro_start = t;
3595 t = tt;
3596 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003598
H. Peter Anvine2c80182005-01-15 22:15:51 +00003599 /*
3600 * Good. We now have a macro name, a parameter count, and a
3601 * token list (in reverse order) for an expansion. We ought
3602 * to be OK just to create an SMacro, store it, and let
3603 * free_tlist have the rest of the line (which we have
3604 * carefully re-terminated after chopping off the expansion
3605 * from the end).
3606 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003607 define_smacro(mname, casesense, macro_start, &tmpl);
3608 break;
3609 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003610
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003612 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003613 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003614 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003615 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003616 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003617
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003618 undef_smacro(mname, i == PP_UNDEFALIAS);
3619 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003620
H. Peter Anvin8b262472019-02-26 14:00:54 -08003621 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003622 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003623 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003624
H. Peter Anvin9e200162008-06-04 17:23:14 -07003625 last = tline;
3626 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003627 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003628
3629 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003630 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003631
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003632 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003633 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003634 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003635
3636 /*
3637 * We now have a macro name, an implicit parameter count of
3638 * zero, and a string token to use as an expansion. Create
3639 * and store an SMacro.
3640 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003641 define_smacro(mname, casesense, macro_start, NULL);
3642 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643
H. Peter Anvin8b262472019-02-26 14:00:54 -08003644 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003645 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003646 goto done;
3647
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003648 last = tline;
3649 tline = expand_smacro(tline->next);
3650 last->next = NULL;
3651
3652 t = tline;
3653 while (tok_type_(t, TOK_WHITESPACE))
3654 t = t->next;
3655 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003656 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003657 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003658 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003659 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003660 }
3661
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003662 /*
3663 * Convert the string to a token stream. Note that smacros
3664 * are stored with the token stream reversed, so we have to
3665 * reverse the output of tokenize().
3666 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003667 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003668 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003669
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003670 /*
3671 * We now have a macro name, an implicit parameter count of
3672 * zero, and a numeric token to use as an expansion. Create
3673 * and store an SMacro.
3674 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003675 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003676 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003677 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003678
H. Peter Anvin418ca702008-05-30 10:42:30 -07003679 case PP_PATHSEARCH:
3680 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003681 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003682
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003683 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003684 goto done;
3685
H. Peter Anvin418ca702008-05-30 10:42:30 -07003686 last = tline;
3687 tline = expand_smacro(tline->next);
3688 last->next = NULL;
3689
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003690 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003691 while (tok_type_(t, TOK_WHITESPACE))
3692 t = t->next;
3693
3694 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003695 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003696 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003698 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003699 }
3700 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003701 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003702 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003703 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003704 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003705
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003706 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003707 if (!found_path)
3708 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003709 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003710
3711 /*
3712 * We now have a macro name, an implicit parameter count of
3713 * zero, and a string token to use as an expansion. Create
3714 * and store an SMacro.
3715 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003716 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003717 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003718 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003719 }
3720
H. Peter Anvine2c80182005-01-15 22:15:51 +00003721 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003722 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003723 goto done;
3724
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 last = tline;
3726 tline = expand_smacro(tline->next);
3727 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003728
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 t = tline;
3730 while (tok_type_(t, TOK_WHITESPACE))
3731 t = t->next;
3732 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003733 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003734 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003735 free_tlist(tline);
3736 free_tlist(origline);
3737 return DIRECTIVE_FOUND;
3738 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003739
H. Peter Anvin8b262472019-02-26 14:00:54 -08003740 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003741
H. Peter Anvine2c80182005-01-15 22:15:51 +00003742 /*
3743 * We now have a macro name, an implicit parameter count of
3744 * zero, and a numeric token to use as an expansion. Create
3745 * and store an SMacro.
3746 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003747 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748 free_tlist(tline);
3749 free_tlist(origline);
3750 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003751
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003752 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003753 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003754 goto done;
3755
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003756 last = tline;
3757 tline = expand_smacro(tline->next);
3758 last->next = NULL;
3759
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003760 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003761 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003762 switch (t->type) {
3763 case TOK_WHITESPACE:
3764 break;
3765 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003766 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003767 break;
3768 case TOK_OTHER:
3769 if (!strcmp(t->text, ",")) /* permit comma separators */
3770 break;
3771 /* else fall through */
3772 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003773 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003774 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003775 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003776 }
3777 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003778
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003779 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003780 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003781 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003782 memcpy(p, t->text, t->len);
3783 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003784 }
3785 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003786
3787 /*
3788 * We now have a macro name, an implicit parameter count of
3789 * zero, and a numeric token to use as an expansion. Create
3790 * and store an SMacro.
3791 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003792 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003793 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003794 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003795 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003796 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003797
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003799 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003800 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003801 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003802
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003803 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003804 goto done;
3805
H. Peter Anvine2c80182005-01-15 22:15:51 +00003806 last = tline;
3807 tline = expand_smacro(tline->next);
3808 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003809
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003810 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003811 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003812 while (tok_type_(t, TOK_WHITESPACE))
3813 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003814
H. Peter Anvine2c80182005-01-15 22:15:51 +00003815 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003816 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003817 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003818 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003819 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003820 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003821
H. Peter Anvin8b262472019-02-26 14:00:54 -08003822 pps.tptr = t->next;
3823 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003824 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003825 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003826 if (!evalresult) {
3827 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003828 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003829 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003830 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003831 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003832 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003833 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003834 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003835
H. Peter Anvin8b262472019-02-26 14:00:54 -08003836 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3837 pps.tptr = pps.tptr->next;
3838 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003839 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003840 } else {
3841 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003842 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003843 if (!evalresult) {
3844 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003845 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003846 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003847 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003848 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003849 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003850 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003851 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003852 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003853
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003854 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003855
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003856 /* make start and count being in range */
3857 if (start < 0)
3858 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003859 if (count < 0)
3860 count = len + count + 1 - start;
3861 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003862 count = len - start;
3863 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003864 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003865
H. Peter Anvin8b262472019-02-26 14:00:54 -08003866 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003867 macro_start->len = count;
3868 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3869 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003870
H. Peter Anvine2c80182005-01-15 22:15:51 +00003871 /*
3872 * We now have a macro name, an implicit parameter count of
3873 * zero, and a numeric token to use as an expansion. Create
3874 * and store an SMacro.
3875 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003876 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003877 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003878 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003879 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003880
H. Peter Anvin8b262472019-02-26 14:00:54 -08003881 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003882 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003883 goto done;
3884
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885 last = tline;
3886 tline = expand_smacro(tline->next);
3887 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003888
H. Peter Anvin8b262472019-02-26 14:00:54 -08003889 pps.tptr = tline;
3890 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003891 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003892 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003893 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003894 if (!evalresult)
3895 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003896
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003898 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003899
H. Peter Anvine2c80182005-01-15 22:15:51 +00003900 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003901 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003902 free_tlist(origline);
3903 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003904 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003905
H. Peter Anvin8b262472019-02-26 14:00:54 -08003906 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003907
H. Peter Anvine2c80182005-01-15 22:15:51 +00003908 /*
3909 * We now have a macro name, an implicit parameter count of
3910 * zero, and a numeric token to use as an expansion. Create
3911 * and store an SMacro.
3912 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003913 define_smacro(mname, casesense, macro_start, NULL);
3914 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003915
H. Peter Anvine2c80182005-01-15 22:15:51 +00003916 case PP_LINE:
3917 /*
3918 * Syntax is `%line nnn[+mmm] [filename]'
3919 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003920 if (unlikely(pp_noline))
3921 goto done;
3922
H. Peter Anvine2c80182005-01-15 22:15:51 +00003923 tline = tline->next;
3924 skip_white_(tline);
3925 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003926 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003927 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003929 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003930 m = 1;
3931 tline = tline->next;
3932 if (tok_is_(tline, "+")) {
3933 tline = tline->next;
3934 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003935 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003936 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003937 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003938 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 tline = tline->next;
3940 }
3941 skip_white_(tline);
3942 src_set_linnum(k);
3943 istk->lineinc = m;
3944 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003945 char *fname = detoken(tline, false);
3946 src_set_fname(fname);
3947 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003949 break;
3950 }
3951
3952done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953 free_tlist(origline);
3954 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003955}
3956
3957/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003958 * Ensure that a macro parameter contains a condition code and
3959 * nothing else. Return the condition code index if so, or -1
3960 * otherwise.
3961 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003962static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003963{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003964 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003965
H. Peter Anvin25a99342007-09-22 17:45:45 -07003966 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003967 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003968
H. Peter Anvineba20a72002-04-30 20:53:55 +00003969 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003970 if (!t)
3971 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003972 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003973 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003974 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003975 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003976 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003977 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003978
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003979 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003980}
3981
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003982/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003983 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003984 * pasting, if @handle_explicit passed then explicit pasting
3985 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003986 * The @m array can contain a series of token types which are
3987 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003988 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003989static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003990 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003991{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003992 Token *tok, *next, **prev_next, **prev_nonspace;
3993 bool pasted = false;
3994 char *buf, *p;
3995 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003996
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003997 /*
3998 * The last token before pasting. We need it
3999 * to be able to connect new handled tokens.
4000 * In other words if there were a tokens stream
4001 *
4002 * A -> B -> C -> D
4003 *
4004 * and we've joined tokens B and C, the resulting
4005 * stream should be
4006 *
4007 * A -> BC -> D
4008 */
4009 tok = *head;
4010 prev_next = NULL;
4011
4012 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
4013 prev_nonspace = head;
4014 else
4015 prev_nonspace = NULL;
4016
4017 while (tok && (next = tok->next)) {
4018
4019 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004020 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004021 /* Zap redundant whitespaces */
4022 while (tok_type_(next, TOK_WHITESPACE))
4023 next = delete_Token(next);
4024 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004025 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004026
4027 case TOK_PASTE:
4028 /* Explicit pasting */
4029 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004030 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004031 next = delete_Token(tok);
4032
4033 while (tok_type_(next, TOK_WHITESPACE))
4034 next = delete_Token(next);
4035
4036 if (!pasted)
4037 pasted = true;
4038
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004039 /* Left pasting token is start of line */
4040 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004041 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004042
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004043 /*
4044 * No ending token, this might happen in two
4045 * cases
4046 *
4047 * 1) There indeed no right token at all
4048 * 2) There is a bare "%define ID" statement,
4049 * and @ID does expand to whitespace.
4050 *
4051 * So technically we need to do a grammar analysis
4052 * in another stage of parsing, but for now lets don't
4053 * change the behaviour people used to. Simply allow
4054 * whitespace after paste token.
4055 */
4056 if (!next) {
4057 /*
4058 * Zap ending space tokens and that's all.
4059 */
4060 tok = (*prev_nonspace)->next;
4061 while (tok_type_(tok, TOK_WHITESPACE))
4062 tok = delete_Token(tok);
4063 tok = *prev_nonspace;
4064 tok->next = NULL;
4065 break;
4066 }
4067
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004068 tok = *prev_nonspace;
4069 while (tok_type_(tok, TOK_WHITESPACE))
4070 tok = delete_Token(tok);
4071 len = strlen(tok->text);
4072 len += strlen(next->text);
4073
4074 p = buf = nasm_malloc(len + 1);
4075 strcpy(p, tok->text);
4076 p = strchr(p, '\0');
4077 strcpy(p, next->text);
4078
4079 delete_Token(tok);
4080
4081 tok = tokenize(buf);
4082 nasm_free(buf);
4083
4084 *prev_nonspace = tok;
4085 while (tok && tok->next)
4086 tok = tok->next;
4087
4088 tok->next = delete_Token(next);
4089
4090 /* Restart from pasted tokens head */
4091 tok = *prev_nonspace;
4092 break;
4093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004094 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004095 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004096 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004097 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4098 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004099
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004100 len = 0;
4101 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4102 len += strlen(next->text);
4103 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004104 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004105
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004106 /* No match or no text to process */
4107 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004108 break;
4109
4110 len += strlen(tok->text);
4111 p = buf = nasm_malloc(len + 1);
4112
Adam Majer1a069432017-07-25 11:12:35 +02004113 strcpy(p, tok->text);
4114 p = strchr(p, '\0');
4115 tok = delete_Token(tok);
4116
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004117 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004118 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4119 strcpy(p, tok->text);
4120 p = strchr(p, '\0');
4121 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004122 tok = delete_Token(tok);
4123 }
4124
4125 tok = tokenize(buf);
4126 nasm_free(buf);
4127
4128 if (prev_next)
4129 *prev_next = tok;
4130 else
4131 *head = tok;
4132
4133 /*
4134 * Connect pasted into original stream,
4135 * ie A -> new-tokens -> B
4136 */
4137 while (tok && tok->next)
4138 tok = tok->next;
4139 tok->next = next;
4140
4141 if (!pasted)
4142 pasted = true;
4143
4144 /* Restart from pasted tokens head */
4145 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004146 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004147
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004148 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004149 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004150
4151 prev_next = &tok->next;
4152
4153 if (tok->next &&
4154 !tok_type_(tok->next, TOK_WHITESPACE) &&
4155 !tok_type_(tok->next, TOK_PASTE))
4156 prev_nonspace = prev_next;
4157
4158 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004159 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004160
4161 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004162}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004163
4164/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004165 * Computes the proper rotation of mmacro parameters
4166 */
4167static int mmac_rotate(const MMacro *mac, unsigned int n)
4168{
4169 if (--n < mac->nparam)
4170 n = (n + mac->rotate) % mac->nparam;
4171
4172 return n+1;
4173}
4174
4175/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004176 * expands to a list of tokens from %{x:y}
4177 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004178static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004179{
4180 Token *t = tline, **tt, *tm, *head;
4181 char *pos;
4182 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004183
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004184 pos = strchr(tline->text, ':');
4185 nasm_assert(pos);
4186
4187 lst = atoi(pos + 1);
4188 fst = atoi(tline->text + 1);
4189
4190 /*
4191 * only macros params are accounted so
4192 * if someone passes %0 -- we reject such
4193 * value(s)
4194 */
4195 if (lst == 0 || fst == 0)
4196 goto err;
4197
4198 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004199 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4200 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004201 goto err;
4202
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004203 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4204 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004205
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004206 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004207 fst--, lst--;
4208
4209 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004210 * It will be at least one token. Note we
4211 * need to scan params until separator, otherwise
4212 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004213 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004214 j = (fst + mac->rotate) % mac->nparam;
4215 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004216 if (!tm)
4217 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004218 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004219 tt = &head->next, tm = tm->next;
4220 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004221 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004222 *tt = t, tt = &t->next, tm = tm->next;
4223 }
4224
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004225 if (fst < lst) {
4226 for (i = fst + 1; i <= lst; i++) {
4227 t = new_Token(NULL, TOK_OTHER, ",", 0);
4228 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004229 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004230 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004231 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004232 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004233 *tt = t, tt = &t->next, tm = tm->next;
4234 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004235 }
4236 } else {
4237 for (i = fst - 1; i >= lst; i--) {
4238 t = new_Token(NULL, TOK_OTHER, ",", 0);
4239 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004240 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004241 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004242 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004243 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004244 *tt = t, tt = &t->next, tm = tm->next;
4245 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004246 }
4247 }
4248
4249 *last = tt;
4250 return head;
4251
4252err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004253 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004254 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004255 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004256}
4257
H. Peter Anvin76690a12002-04-30 20:52:49 +00004258/*
4259 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004260 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004261 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004262 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004264{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004265 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004266 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004267 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004268
4269 tail = &thead;
4270 thead = NULL;
4271
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004273 bool change;
4274 Token *t = tline;
4275 char *text = t->text;
4276 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004277
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004278 tline = tline->next;
4279 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004280
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004281 switch (type) {
4282 case TOK_PREPROC_ID:
4283 {
4284 Token *tt = NULL;
4285
4286 change = false;
4287
4288 if (!text || !text[0])
4289 break;
4290 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4291 ((text[1] == '+' || text[1] == '-') && text[2])))
4292 break;
4293
4294 change = true;
4295
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004296 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004297 nasm_nonfatal("`%s': not in a macro call", text);
4298 text = NULL;
4299 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004300 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004301
4302 if (strchr(text, ':')) {
4303 /*
4304 * seems we have a parameters range here
4305 */
4306 Token *head, **last;
4307 head = expand_mmac_params_range(mac, t, &last);
4308 if (head) {
4309 *tail = head;
4310 *last = tline;
4311 text = NULL;
4312 }
4313 break;
4314 }
4315
4316 switch (text[1]) {
4317 /*
4318 * We have to make a substitution of one of the
4319 * forms %1, %-1, %+1, %%foo, %0, %00.
4320 */
4321 case '0':
4322 if (!text[2]) {
4323 type = TOK_NUMBER;
4324 text = nasm_asprintf("%d", mac->nparam);
4325 break;
4326 }
4327 if (text[2] != '0' || text[3])
4328 goto invalid;
4329 /* a possible captured label == mac->params[0] */
4330 /* fall through */
4331 default:
4332 {
4333 unsigned long n;
4334 char *ep;
4335
4336 n = strtoul(text + 1, &ep, 10);
4337 if (unlikely(*ep))
4338 goto invalid;
4339
4340 if (n <= mac->nparam) {
4341 n = mmac_rotate(mac, n);
4342 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4343 }
4344 text = NULL;
4345 break;
4346 }
4347 case '%':
4348 type = TOK_ID;
4349 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4350 break;
4351 case '-':
4352 case '+':
4353 {
4354 int cc;
4355 unsigned long n;
4356 char *ep;
4357
4358 text = NULL;
4359
4360 n = strtoul(t->text + 2, &ep, 10);
4361 if (unlikely(*ep))
4362 goto invalid;
4363
4364 if (n && n < mac->nparam) {
4365 n = mmac_rotate(mac, n);
4366 tt = mac->params[n];
4367 }
4368 cc = find_cc(tt);
4369 if (cc == -1) {
4370 nasm_nonfatal("macro parameter `%s' is not a condition code",
4371 text);
4372 text = NULL;
4373 break;
4374 }
4375
4376 type = TOK_ID;
4377 if (text[1] == '-') {
4378 int ncc = inverse_ccs[cc];
4379 if (unlikely(ncc == -1)) {
4380 nasm_nonfatal("condition code `%s' is not invertible",
4381 conditions[cc]);
4382 break;
4383 }
4384 cc = ncc;
4385 }
4386 text = nasm_strdup(conditions[cc]);
4387 break;
4388 }
4389
4390 invalid:
4391 nasm_nonfatal("invalid macro parameter: `%s'", text);
4392 text = NULL;
4393 break;
4394 }
4395 break;
4396 }
4397
4398 case TOK_PREPROC_Q:
4399 if (mac) {
4400 type = TOK_ID;
4401 text = nasm_strdup(mac->iname);
4402 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004403 } else {
4404 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004405 }
4406 break;
4407
4408 case TOK_PREPROC_QQ:
4409 if (mac) {
4410 type = TOK_ID;
4411 text = nasm_strdup(mac->name);
4412 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004413 } else {
4414 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004415 }
4416 break;
4417
4418 case TOK_INDIRECT:
4419 {
4420 Token *tt;
4421
4422 tt = tokenize(t->text);
4423 tt = expand_mmac_params(tt);
4424 tt = expand_smacro(tt);
4425 /* Why dup_tlist() here? We should own tt... */
4426 dup_tlist(tt, &tail);
4427 text = NULL;
4428 change = true;
4429 break;
4430 }
4431
4432 default:
4433 change = false;
4434 break;
4435 }
4436
4437 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 if (!text) {
4439 delete_Token(t);
4440 } else {
4441 *tail = t;
4442 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004443 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004444 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004445 t->type = type;
4446 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004447 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004448 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004449 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004450 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004451 tail = &t->next;
4452 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004453 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004454
H. Peter Anvineba20a72002-04-30 20:53:55 +00004455 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004456
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004457 if (changed) {
4458 const struct tokseq_match t[] = {
4459 {
4460 PP_CONCAT_MASK(TOK_ID) |
4461 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4462 PP_CONCAT_MASK(TOK_ID) |
4463 PP_CONCAT_MASK(TOK_NUMBER) |
4464 PP_CONCAT_MASK(TOK_FLOAT) |
4465 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4466 },
4467 {
4468 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4469 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4470 }
4471 };
4472 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4473 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004474
H. Peter Anvin76690a12002-04-30 20:52:49 +00004475 return thead;
4476}
4477
H. Peter Anvin322bee02019-08-10 01:38:06 -07004478static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004479
H. Peter Anvin76690a12002-04-30 20:52:49 +00004480/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004481 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004482 * a macro at all, it is simply copied to the output and the pointer
4483 * advanced. tpp should be a pointer to a pointer (usually the next
4484 * pointer of the previous token) to the first token. **tpp is updated
4485 * to point to the last token of the expansion, and *tpp updated to
4486 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004487 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004488 * If the expansion is empty, *tpp will be unchanged but **tpp will
4489 * be advanced past the macro call.
4490 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004491 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004492 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004493static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004494{
4495 Token **params = NULL;
4496 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004497 Token *tline = **tpp;
4498 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004499 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004500 int i;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004501 Token *t, *tup, *ttail;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004502 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004503 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004504
4505 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004506 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004507
4508 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004509
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004510 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004511 smacro_deadman.levels--;
4512
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004513 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004514 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004515 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004516 smacro_deadman.triggered = true;
4517 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004518 goto not_a_macro;
4519 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004520 head = (SMacro *)hash_findix(&smacros, mname);
4521 } else if (tline->type == TOK_PREPROC_ID) {
4522 Context *ctx = get_ctx(mname, &mname);
4523 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4524 } else {
4525 goto not_a_macro;
4526 }
4527
4528 /*
4529 * We've hit an identifier of some sort. First check whether the
4530 * identifier is a single-line macro at all, then think about
4531 * checking for parameters if necessary.
4532 */
4533 list_for_each(m, head) {
4534 if (!mstrcmp(m->name, mname, m->casesense))
4535 break;
4536 }
4537
4538 if (!m) {
4539 goto not_a_macro;
4540 }
4541
4542 /* Parse parameters, if applicable */
4543
4544 params = NULL;
4545 nparam = 0;
4546
4547 if (m->nparam == 0) {
4548 /*
4549 * Simple case: the macro is parameterless.
4550 * Nothing to parse; the expansion code will
4551 * drop the macro name token.
4552 */
4553 } else {
4554 /*
4555 * Complicated case: at least one macro with this name
4556 * exists and takes parameters. We must find the
4557 * parameters in the call, count them, find the SMacro
4558 * that corresponds to that form of the macro call, and
4559 * substitute for the parameters when we expand. What a
4560 * pain.
4561 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004562 Token *t;
4563 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004564
4565 tline = tline->next;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004566 skip_white_(tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004567 if (!tok_is_(tline, "(")) {
4568 /*
4569 * This macro wasn't called with parameters: ignore
4570 * the call. (Behaviour borrowed from gnu cpp.)
4571 */
4572 goto not_a_macro;
4573 }
4574
4575 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004576 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004577 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004578 t = tline; /* tline points to leading ( */
4579
4580 while (paren) {
4581 t = t->next;
4582
4583 if (!t) {
4584 nasm_nonfatal("macro call expects terminating `)'");
4585 goto not_a_macro;
4586 }
4587
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004588 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004589 continue;
4590
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004591 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004592 case ',':
4593 if (!brackets)
4594 nparam++;
4595 break;
4596
4597 case '{':
4598 brackets++;
4599 break;
4600
4601 case '}':
4602 if (brackets > 0)
4603 brackets--;
4604 break;
4605
4606 case '(':
4607 if (!brackets)
4608 paren++;
4609 break;
4610
4611 case ')':
4612 if (!brackets)
4613 paren--;
4614 break;
4615
4616 default:
4617 break; /* Normal token */
4618 }
4619 }
4620
4621 /*
4622 * Look for a macro matching in both name and parameter count.
4623 * We already know any matches cannot be anywhere before the
4624 * current position of "m", so there is no reason to
4625 * backtrack.
4626 */
4627 while (1) {
4628 if (!m) {
4629 /*!
4630 *!macro-params-single [on] single-line macro calls with wrong parameter count
4631 *! warns about \i{single-line macros} being invoked
4632 *! with the wrong number of parameters.
4633 */
4634 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4635 "single-line macro `%s' exists, "
4636 "but not taking %d parameter%s",
4637 mname, nparam, (nparam == 1) ? "" : "s");
4638 goto not_a_macro;
4639 }
4640
4641 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004642 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004643 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004644 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004645 break; /* Also good */
4646 }
4647 m = m->next;
4648 }
4649 }
4650
4651 if (m->in_progress)
4652 goto not_a_macro;
4653
4654 /* Expand the macro */
4655 m->in_progress = true;
4656
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004657 if (nparam) {
4658 /* Extract parameters */
4659 Token **phead, **pep;
4660 int white = 0;
4661 int brackets = 0;
4662 int paren;
4663 bool bracketed = false;
4664 bool bad_bracket = false;
4665 enum sparmflags flags;
4666
4667 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004668 paren = 1;
4669 nasm_newn(params, nparam);
4670 i = 0;
4671 flags = m->params[i].flags;
4672 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004673 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004674
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004675 while (paren) {
4676 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004677 char ch;
4678
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004679 tline = tline->next;
4680
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004681 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004682 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004683
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004684 ch = 0;
4685 skip = false;
4686
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004687
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004688 switch (tline->type) {
4689 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004690 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004691 ch = tline->text[0];
4692 break;
4693
4694 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004695 if (!(flags & SPARM_NOSTRIP)) {
4696 if (brackets || *phead)
4697 white++; /* Keep interior whitespace */
4698 skip = true;
4699 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004700 break;
4701
4702 default:
4703 break;
4704 }
4705
4706 switch (ch) {
4707 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004708 if (!brackets && !(flags & SPARM_GREEDY)) {
4709 i++;
4710 nasm_assert(i < nparam);
4711 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004712 *pep = NULL;
4713 bracketed = false;
4714 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004715 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004716 }
4717 break;
4718
4719 case '{':
4720 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004721 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4722 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004723 }
4724 brackets++;
4725 break;
4726
4727 case '}':
4728 if (brackets > 0) {
4729 if (!--brackets)
4730 skip = bracketed;
4731 }
4732 break;
4733
4734 case '(':
4735 if (!brackets)
4736 paren++;
4737 break;
4738
4739 case ')':
4740 if (!brackets) {
4741 paren--;
4742 if (!paren) {
4743 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004744 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004745 }
4746 }
4747 break;
4748
4749 default:
4750 break; /* Normal token */
4751 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004752
4753 if (!skip) {
4754 Token *t;
4755
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004756 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004757
4758 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004759 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004760 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004761 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004762 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004763 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004764 pep = &t->next;
4765 white = 0;
4766 }
4767 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004768
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004769 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004770 * Possible further processing of parameters. Note that the
4771 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004772 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004773 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004774 enum sparmflags flags = m->params[i].flags;
4775
4776 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004777 /* Evaluate this parameter as a number */
4778 struct ppscan pps;
4779 struct tokenval tokval;
4780 expr *evalresult;
4781 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004782
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004783 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4784 pps.ntokens = -1;
4785 tokval.t_type = TOKEN_INVALID;
4786 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004787
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004788 free_tlist(eval_param);
4789 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004790
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004791 if (!evalresult) {
4792 /* Nothing meaningful to do */
4793 } else if (tokval.t_type) {
4794 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4795 } else if (!is_simple(evalresult)) {
4796 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4797 } else {
4798 params[i] = make_tok_num(reloc_value(evalresult));
4799 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004800 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004801
4802 if (flags & SPARM_STR) {
4803 /* Convert expansion to a quoted string */
4804 char *arg;
4805 Token *qs;
4806
4807 qs = expand_smacro_noreset(params[i]);
4808 arg = detoken(qs, false);
4809 free_tlist(qs);
4810 params[i] = make_tok_qstr(arg);
4811 nasm_free(arg);
4812 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004813 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004814 }
4815
4816 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004817 tline = tline->next; /* Remove the macro call from the input */
4818 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004819
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004820 /* Note: we own the expansion this returns. */
4821 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004822
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004823 tup = NULL;
4824 ttail = NULL; /* Pointer to the last token of the expansion */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004825 cond_comma = false;
4826
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004827 while (t) {
4828 enum pp_token_type type = t->type;
4829 Token *tnext = t->next;
4830 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004831
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004832 switch (type) {
4833 case TOK_PREPROC_Q:
4834 case TOK_PREPROC_QQ:
4835 t->type = TOK_ID;
4836 nasm_free(t->text);
4837 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4838 t->len = nasm_last_string_len();
4839 t->next = tline;
4840 break;
4841
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004842 case TOK_COND_COMMA:
4843 delete_Token(t);
4844 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4845 break;
4846
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004847 case TOK_ID:
4848 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004849 /*
4850 * Chain this into the target line *before* expanding,
4851 * that way we pick up any arguments to the new macro call,
4852 * if applicable.
4853 */
4854 t->next = tline;
4855 tp = &t;
4856 expand_one_smacro(&tp);
4857 if (t == tline)
4858 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004859 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004860
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004861 default:
4862 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004863 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004864 nasm_assert(!tup && param < nparam);
4865 delete_Token(t);
4866 t = NULL;
4867 tup = tnext;
4868 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004869 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004870 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004871 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004872 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004873 }
4874
4875 if (t) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004876 if (t->type != TOK_WHITESPACE)
4877 cond_comma = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004878 tline = t;
4879 if (!ttail)
4880 ttail = t;
4881 }
4882
4883 if (tnext) {
4884 t = tnext;
4885 } else {
4886 t = tup;
4887 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004888 }
4889 }
4890
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004891 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004892 if (ttail)
4893 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004894
4895 m->in_progress = false;
4896
4897 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004898 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004899 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004900
4901 /*
4902 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004903 * and then advance to the next input token. Note that this is
4904 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004905 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004906not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004907 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004908 m = NULL;
4909done:
4910 smacro_deadman.levels++;
4911 if (unlikely(params))
4912 free_tlist_array(params, nparam);
4913 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004914}
4915
4916/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004917 * Expand all single-line macro calls made in the given line.
4918 * Return the expanded version of the line. The original is deemed
4919 * to be destroyed in the process. (In reality we'll just move
4920 * Tokens from input to output a lot of the time, rather than
4921 * actually bothering to destroy and replicate.)
4922 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004923static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004924{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004925 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07004926 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4927 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004928 return expand_smacro_noreset(tline);
4929}
4930
4931static Token *expand_smacro_noreset(Token * tline)
4932{
4933 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004934 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004935 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004936
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004937 /*
4938 * Trick: we should avoid changing the start token pointer since it can
4939 * be contained in "next" field of other token. Because of this
4940 * we allocate a copy of first token and work with it; at the end of
4941 * routine we copy it back
4942 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004943 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004944 tline = new_Token(org_tline->next, org_tline->type,
4945 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004946 nasm_free(org_tline->text);
4947 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004948 }
4949
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004950
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004951 /*
4952 * Pretend that we always end up doing expansion on the first pass;
4953 * that way %+ get processed. However, if we process %+ before the
4954 * first pass, we end up with things like MACRO %+ TAIL trying to
4955 * look up the macro "MACROTAIL", which we don't want.
4956 */
4957 expanded = true;
4958 thead = tline;
4959 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004960 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004961 {
4962 PP_CONCAT_MASK(TOK_ID) |
4963 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4964 PP_CONCAT_MASK(TOK_ID) |
4965 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4966 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4967 }
4968 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004969
4970 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004971 while ((t = *tail)) /* main token loop */
4972 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004973
4974 if (!expanded) {
4975 tline = thead;
4976 break; /* Done! */
4977 }
4978
4979 /*
4980 * Now scan the entire line and look for successive TOK_IDs
4981 * that resulted after expansion (they can't be produced by
4982 * tokenize()). The successive TOK_IDs should be concatenated.
4983 * Also we look for %+ tokens and concatenate the tokens
4984 * before and after them (without white spaces in between).
4985 */
4986 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004987
4988 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004989 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004990
H. Peter Anvine2c80182005-01-15 22:15:51 +00004991 if (org_tline) {
4992 if (thead) {
4993 *org_tline = *thead;
4994 /* since we just gave text to org_line, don't free it */
4995 thead->text = NULL;
4996 delete_Token(thead);
4997 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004998 /*
4999 * The expression expanded to empty line;
5000 * we can't return NULL because of the "trick" above.
5001 * Just set the line to a single WHITESPACE token.
5002 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005003 memset(org_tline, 0, sizeof(*org_tline));
5004 org_tline->text = NULL;
5005 org_tline->type = TOK_WHITESPACE;
5006 }
5007 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005008 }
5009
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005010 return thead;
5011}
5012
5013/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005014 * Similar to expand_smacro but used exclusively with macro identifiers
5015 * right before they are fetched in. The reason is that there can be
5016 * identifiers consisting of several subparts. We consider that if there
5017 * are more than one element forming the name, user wants a expansion,
5018 * otherwise it will be left as-is. Example:
5019 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005020 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005021 *
5022 * the identifier %$abc will be left as-is so that the handler for %define
5023 * will suck it and define the corresponding value. Other case:
5024 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005025 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005026 *
5027 * In this case user wants name to be expanded *before* %define starts
5028 * working, so we'll expand %$abc into something (if it has a value;
5029 * otherwise it will be left as-is) then concatenate all successive
5030 * PP_IDs into one.
5031 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005032static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005033{
5034 Token *cur, *oldnext = NULL;
5035
H. Peter Anvin734b1882002-04-30 21:01:08 +00005036 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005037 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005038
5039 cur = tline;
5040 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005041 (cur->next->type == TOK_ID ||
5042 cur->next->type == TOK_PREPROC_ID
5043 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005044 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005045
5046 /* If identifier consists of just one token, don't expand */
5047 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005048 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005049
H. Peter Anvine2c80182005-01-15 22:15:51 +00005050 if (cur) {
5051 oldnext = cur->next; /* Detach the tail past identifier */
5052 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005053 }
5054
H. Peter Anvin734b1882002-04-30 21:01:08 +00005055 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005056
H. Peter Anvine2c80182005-01-15 22:15:51 +00005057 if (cur) {
5058 /* expand_smacro possibly changhed tline; re-scan for EOL */
5059 cur = tline;
5060 while (cur && cur->next)
5061 cur = cur->next;
5062 if (cur)
5063 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005064 }
5065
5066 return tline;
5067}
5068
5069/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005070 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005071 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005072 * to check for an initial label - that's taken care of in
5073 * expand_mmacro - but must check numbers of parameters. Guaranteed
5074 * to be called with tline->type == TOK_ID, so the putative macro
5075 * name is easy to find.
5076 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005077static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005078{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005079 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005080 Token **params;
5081 int nparam;
5082
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005084
5085 /*
5086 * Efficiency: first we see if any macro exists with the given
5087 * name. If not, we can return NULL immediately. _Then_ we
5088 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005089 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005090 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005091 list_for_each(m, head)
5092 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005093 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005094 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005095 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005096
5097 /*
5098 * OK, we have a potential macro. Count and demarcate the
5099 * parameters.
5100 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005101 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005102
5103 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005104 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005105 * structure that handles this number.
5106 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005107 while (m) {
5108 if (m->nparam_min <= nparam
5109 && (m->plus || nparam <= m->nparam_max)) {
5110 /*
5111 * This one is right. Just check if cycle removal
5112 * prohibits us using it before we actually celebrate...
5113 */
5114 if (m->in_progress > m->max_depth) {
5115 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005116 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005117 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005118 }
5119 nasm_free(params);
5120 return NULL;
5121 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005122 /*
5123 * It's right, and we can use it. Add its default
5124 * parameters to the end of our list if necessary.
5125 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005126 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005127 params =
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005128 nasm_realloc(params, sizeof(*params) *
5129 (m->nparam_min + m->ndefs + 2));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005130 while (nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005131 nparam++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005132 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00005133 }
5134 }
5135 /*
5136 * If we've gone over the maximum parameter count (and
5137 * we're in Plus mode), ignore parameters beyond
5138 * nparam_max.
5139 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005140 if (m->plus && nparam > m->nparam_max)
5141 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005142
5143 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005144 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005145 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005146 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005147 }
5148 /*
5149 * This one wasn't right: look for the next one with the
5150 * same name.
5151 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005152 list_for_each(m, m->next)
5153 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005154 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005155 }
5156
5157 /*
5158 * After all that, we didn't find one with the right number of
5159 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005160 *!
5161 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5162 *! warns about \i{multi-line macros} being invoked
5163 *! with the wrong number of parameters. See \k{mlmacover} for an
5164 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005165 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005166 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5167 "multi-line macro `%s' exists, but not taking %d parameter%s",
5168 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005169 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005170 return NULL;
5171}
5172
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005173
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005174#if 0
5175
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005176/*
5177 * Save MMacro invocation specific fields in
5178 * preparation for a recursive macro expansion
5179 */
5180static void push_mmacro(MMacro *m)
5181{
5182 MMacroInvocation *i;
5183
5184 i = nasm_malloc(sizeof(MMacroInvocation));
5185 i->prev = m->prev;
5186 i->params = m->params;
5187 i->iline = m->iline;
5188 i->nparam = m->nparam;
5189 i->rotate = m->rotate;
5190 i->paramlen = m->paramlen;
5191 i->unique = m->unique;
5192 i->condcnt = m->condcnt;
5193 m->prev = i;
5194}
5195
5196
5197/*
5198 * Restore MMacro invocation specific fields that were
5199 * saved during a previous recursive macro expansion
5200 */
5201static void pop_mmacro(MMacro *m)
5202{
5203 MMacroInvocation *i;
5204
5205 if (m->prev) {
5206 i = m->prev;
5207 m->prev = i->prev;
5208 m->params = i->params;
5209 m->iline = i->iline;
5210 m->nparam = i->nparam;
5211 m->rotate = i->rotate;
5212 m->paramlen = i->paramlen;
5213 m->unique = i->unique;
5214 m->condcnt = i->condcnt;
5215 nasm_free(i);
5216 }
5217}
5218
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005219#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005220
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005221/*
5222 * Expand the multi-line macro call made by the given line, if
5223 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005224 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005225 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005226static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005227{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005228 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005229 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005230 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005231 Token **params, *t, *tt;
5232 MMacro *m;
5233 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005234 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005235 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005236 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005237
5238 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005239 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005240 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005241 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005242 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005243 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005244 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005245 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005246 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005247 Token *last;
5248 /*
5249 * We have an id which isn't a macro call. We'll assume
5250 * it might be a label; we'll also check to see if a
5251 * colon follows it. Then, if there's another id after
5252 * that lot, we'll check it again for macro-hood.
5253 */
5254 label = last = t;
5255 t = t->next;
5256 if (tok_type_(t, TOK_WHITESPACE))
5257 last = t, t = t->next;
5258 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005259 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005260 last = t, t = t->next;
5261 if (tok_type_(t, TOK_WHITESPACE))
5262 last = t, t = t->next;
5263 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005264 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005265 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005266 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005267 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005268 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005269 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005270
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005271 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5272 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5273 if (!mmacro_deadman.triggered) {
5274 nasm_nonfatal("interminable multiline macro recursion");
5275 mmacro_deadman.triggered = true;
5276 }
5277 return 0;
5278 }
5279
5280 mmacro_deadman.total++;
5281 mmacro_deadman.levels++;
5282
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005283 /*
5284 * Fix up the parameters: this involves stripping leading and
5285 * trailing whitespace, then stripping braces if they are
5286 * present.
5287 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005288 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005289
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005290 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005291 int brace = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005292 int comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005293
H. Peter Anvine2c80182005-01-15 22:15:51 +00005294 skip_white_(t);
5295 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005296 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005297 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005298 while (t) {
5299 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5300 break; /* ... because we have hit a comma */
5301 if (comma && t->type == TOK_WHITESPACE
5302 && tok_is_(t->next, ","))
5303 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005304 if (brace && t->type == TOK_OTHER) {
5305 if (t->text[0] == '{')
5306 brace++; /* ... or a nested opening brace */
5307 else if (t->text[0] == '}')
5308 if (!--brace)
5309 break; /* ... or a brace */
5310 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005311 t = t->next;
5312 paramlen[i]++;
5313 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005314 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005315 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005316 }
5317
5318 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005319 * OK, we have a MMacro structure together with a set of
5320 * parameters. We must now go through the expansion and push
5321 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005322 * parameter tokens and macro-local tokens doesn't get done
5323 * until the single-line macro substitution process; this is
5324 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005325 * later through %rotate and give the right semantics for
5326 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005327 *
5328 * First, push an end marker on to istk->expansion, mark this
5329 * macro as in progress, and set up its invocation-specific
5330 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005331 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005332 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005333 ll->next = istk->expansion;
5334 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005335 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005336
5337 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005338 * Save the previous MMacro expansion in the case of
5339 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005340 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005341#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005342 if (m->max_depth && m->in_progress)
5343 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005344#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005345
5346 m->in_progress ++;
5347 m->params = params;
5348 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005349 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005350 m->nparam = nparam;
5351 m->rotate = 0;
5352 m->paramlen = paramlen;
5353 m->unique = unique++;
5354 m->lineno = 0;
5355 m->condcnt = 0;
5356
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005357 m->mstk = istk->mstk;
5358 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005359
5360 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005361 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005362 ll->next = istk->expansion;
5363 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005364 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005365 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005366
5367 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005368 * If we had a label, and this macro definition does not include
5369 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005370 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005371 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005372 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005373 /*
5374 * We had a label. If this macro contains an %00 parameter,
5375 * save the value as a special parameter (which is what it
5376 * is), otherwise push it as the first line of the macro
5377 * expansion.
5378 */
5379 if (m->capture_label) {
5380 params[0] = dup_Token(NULL, label);
5381 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005382 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005383 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005384 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005385 ll->finishes = NULL;
5386 ll->next = istk->expansion;
5387 istk->expansion = ll;
5388 ll->first = startline;
5389 if (!dont_prepend) {
5390 while (label->next)
5391 label = label->next;
5392 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005393 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005394 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005395 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005396
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005397 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005398
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005399 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005400}
5401
H. Peter Anvin130736c2016-02-17 20:27:41 -08005402/*
5403 * This function adds macro names to error messages, and suppresses
5404 * them if necessary.
5405 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005406static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005407{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005408 /*
5409 * If we're in a dead branch of IF or something like it, ignore the error.
5410 * However, because %else etc are evaluated in the state context
5411 * of the previous branch, errors might get lost:
5412 * %if 0 ... %else trailing garbage ... %endif
5413 * So %else etc should set the ERR_PP_PRECOND flag.
5414 */
5415 if ((severity & ERR_MASK) < ERR_FATAL &&
5416 istk && istk->conds &&
5417 ((severity & ERR_PP_PRECOND) ?
5418 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005419 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005420 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005421
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005422 /* This doesn't make sense with the macro stack unwinding */
5423 if (0) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005424 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005425
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005426 /* get %macro name */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005427 if (!(severity & ERR_NOFILE) && istk && istk->mstk.mmac) {
5428 MMacro *mmac = istk->mstk.mmac;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005429 char *buf;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005430
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005431 nasm_set_verror(real_verror);
5432 buf = nasm_vasprintf(fmt, arg);
5433 nasm_error(severity, "(%s:%"PRId32") %s",
5434 mmac->name, mmac->lineno - delta, buf);
5435 nasm_set_verror(pp_verror);
5436 nasm_free(buf);
5437 return;
5438 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005439 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005440 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005441}
5442
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005443static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005444stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005445{
5446 (void)s;
5447 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005448 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005449
5450 return make_tok_qstr(src_get_fname());
5451}
5452
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005453static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005454stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005455{
5456 (void)s;
5457 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005458 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005459
5460 return make_tok_num(src_get_linnum());
5461}
5462
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005463static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005464stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005465{
5466 (void)s;
5467 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005468 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005469
5470 return make_tok_num(globalbits);
5471}
5472
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005473static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005474stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005475{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005476 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005477 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5478 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5479 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005480
5481 (void)s;
5482 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005483 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005484
5485 switch (globalbits) {
5486 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005487 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005488 break;
5489 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005490 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005491 break;
5492 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005493 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005494 break;
5495 default:
5496 panic();
5497 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005498
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005499 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005500}
5501
H. Peter Anvin8b262472019-02-26 14:00:54 -08005502/* Add magic standard macros */
5503struct magic_macros {
5504 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005505 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005506 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005507};
5508static const struct magic_macros magic_macros[] =
5509{
5510 { "__FILE__", 0, stdmac_file },
5511 { "__LINE__", 0, stdmac_line },
5512 { "__BITS__", 0, stdmac_bits },
5513 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005514 { NULL, 0, NULL }
5515};
5516
5517static void pp_add_magic_stdmac(void)
5518{
5519 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005520 SMacro tmpl;
5521
5522 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005523
5524 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005525 tmpl.nparam = m->nparam;
5526 tmpl.expand = m->func;
5527 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005528 }
5529}
5530
H. Peter Anvin734b1882002-04-30 21:01:08 +00005531static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005532pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005533{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005534 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005535 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005536
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005537 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005538 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005539 nested_mac_count = 0;
5540 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005541 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005542 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005543 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005544 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005545
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005546 if (!use_loaded)
5547 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5548 memset(use_loaded, 0, use_package_count * sizeof(bool));
5549
H. Peter Anvin6686de22019-08-10 05:33:14 -07005550 /* First set up the top level input file */
5551 nasm_new(istk);
5552 istk->fp = nasm_open_read(file, NF_TEXT);
5553 src_set(0, file);
5554 istk->lineinc = 1;
5555 if (!istk->fp)
5556 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5557
5558 strlist_add(deplist, file);
5559
5560 /*
5561 * Set up the stdmac packages as a virtual include file,
5562 * indicated by a null file pointer.
5563 */
5564 nasm_new(inc);
5565 inc->next = istk;
5566 inc->fname = src_set_fname(NULL);
5567 inc->nolist = !list_option('b');
5568 istk = inc;
5569 lfmt->uplevel(LIST_INCLUDE, 0);
5570
H. Peter Anvin8b262472019-02-26 14:00:54 -08005571 pp_add_magic_stdmac();
5572
H. Peter Anvinf7606612016-07-13 14:23:48 -07005573 if (tasm_compatible_mode)
5574 pp_add_stdmac(nasm_stdmac_tasm);
5575
5576 pp_add_stdmac(nasm_stdmac_nasm);
5577 pp_add_stdmac(nasm_stdmac_version);
5578
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005579 if (extrastdmac)
5580 pp_add_stdmac(extrastdmac);
5581
H. Peter Anvinf7606612016-07-13 14:23:48 -07005582 stdmacpos = stdmacros[0];
5583 stdmacnext = &stdmacros[1];
5584
H. Peter Anvind2456592008-06-19 15:04:18 -07005585 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005586
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005587 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005588 * Define the __PASS__ macro. This is defined here unlike all the
5589 * other builtins, because it is special -- it varies between
5590 * passes -- but there is really no particular reason to make it
5591 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005592 *
5593 * 0 = dependencies only
5594 * 1 = preparatory passes
5595 * 2 = final pass
5596 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005597 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005598 switch (mode) {
5599 case PP_NORMAL:
5600 apass = pass_final() ? 2 : 1;
5601 break;
5602 case PP_DEPS:
5603 apass = 0;
5604 break;
5605 case PP_PREPROC:
5606 apass = 3;
5607 break;
5608 default:
5609 panic();
5610 }
5611
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005612 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005613}
5614
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005615static void pp_init(void)
5616{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005617}
5618
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005619/*
5620 * Get a line of tokens. If we popped the macro expansion/include stack,
5621 * we return a pointer to the dummy token tok_pop; at that point if
5622 * istk is NULL then we have reached end of input;
5623 */
5624static Token tok_pop; /* Dummy token placeholder */
5625
5626static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005627{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005628 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005629 Line *l = istk->expansion;
5630 Token *tline = NULL;
5631 Token *dtline;
5632
H. Peter Anvine2c80182005-01-15 22:15:51 +00005633 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005634 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005635 * buffer or from the input file.
5636 */
5637 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005638 while (l && l->finishes) {
5639 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005640
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005641 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005642 /*
5643 * This is a macro-end marker for a macro with no
5644 * name, which means it's not really a macro at all
5645 * but a %rep block, and the `in_progress' field is
5646 * more than 1, meaning that we still need to
5647 * repeat. (1 means the natural last repetition; 0
5648 * means termination by %exitrep.) We have
5649 * therefore expanded up to the %endrep, and must
5650 * push the whole block on to the expansion buffer
5651 * again. We don't bother to remove the macro-end
5652 * marker: we'd only have to generate another one
5653 * if we did.
5654 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005655 fm->in_progress--;
5656 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005657 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005658 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005659
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005660 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005661 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005662 tail = &ll->first;
5663
5664 list_for_each(t, l->first) {
5665 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005666 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005667 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005668 }
5669 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005670 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005671 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005672 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005673 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005674 MMacro *m = istk->mstk.mstk;
5675
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005676 /*
5677 * Check whether a `%rep' was started and not ended
5678 * within this macro expansion. This can happen and
5679 * should be detected. It's a fatal error because
5680 * I'm too confused to work out how to recover
5681 * sensibly from it.
5682 */
5683 if (defining) {
5684 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005685 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005686 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005687 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005688 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005689 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005690
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005691 /*
5692 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005693 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005694 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005695 istk->mstk = m->mstk;
5696 if (m->name) {
5697 /*
5698 * This was a real macro call, not a %rep, and
5699 * therefore the parameter information needs to
5700 * be freed and the iteration count/nesting
5701 * depth adjusted.
5702 */
5703
5704 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005705 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005706 * If all mmacro processing done,
5707 * clear all counters and the deadman
5708 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005709 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005710 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005711 }
5712
Adam Majer91e72402017-07-25 10:42:01 +02005713#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005714 if (m->prev) {
5715 pop_mmacro(m);
5716 fm->in_progress --;
5717 } else
Adam Majer91e72402017-07-25 10:42:01 +02005718#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005719 {
5720 nasm_free(m->params);
5721 free_tlist(m->iline);
5722 nasm_free(m->paramlen);
5723 fm->in_progress = 0;
5724 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005725 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005726
5727 /*
5728 * FIXME It is incorrect to always free_mmacro here.
5729 * It leads to usage-after-free.
5730 *
5731 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5732 */
5733#if 0
5734 else
5735 free_mmacro(m);
5736#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005737 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005738 istk->expansion = l->next;
5739 nasm_free(l);
5740 lfmt->downlevel(LIST_MACRO);
5741 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005742 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005743
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005744 do { /* until we get a line we can use */
5745 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005746
5747 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005748 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005749 int32_t lineno;
5750
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005751 if (istk->mstk.mstk) {
5752 istk->mstk.mstk->lineno++;
5753 if (istk->mstk.mstk->fname)
5754 lineno = istk->mstk.mstk->lineno +
5755 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005756 else
5757 lineno = 0; /* Defined at init time or builtin */
5758 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005759 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005760 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005761
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005762 tline = l->first;
5763 istk->expansion = l->next;
5764 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005765
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005766 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005767 if (!istk->nolist)
5768 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005769 nasm_free(line);
5770 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005771 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005772 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005773 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005774 } else {
5775 /*
5776 * The current file has ended; work down the istk
5777 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005778 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005779 if (i->fp)
5780 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005781 if (i->conds) {
5782 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005783 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005784 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005785 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005786 if (i->next)
5787 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005788 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005789 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005790 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005791 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005792 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005793 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005794
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005795 /*
5796 * We must expand MMacro parameters and MMacro-local labels
5797 * _before_ we plunge into directive processing, to cope
5798 * with things like `%define something %1' such as STRUC
5799 * uses. Unless we're _defining_ a MMacro, in which case
5800 * those tokens should be left alone to go into the
5801 * definition; and unless we're in a non-emitting
5802 * condition, in which case we don't want to meddle with
5803 * anything.
5804 */
5805 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005806 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005807 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005808 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005809
H. Peter Anvine2c80182005-01-15 22:15:51 +00005810 /*
5811 * Check the line to see if it's a preprocessor directive.
5812 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005813 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5814 if (dtline)
5815 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005816 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005817 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005818 * We're defining a multi-line macro. We emit nothing
5819 * at all, and just
5820 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005821 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005822 MMacro *mmac = defining->dstk.mmac;
5823
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005824 Line *l = nasm_malloc(sizeof(Line));
5825 l->next = defining->expansion;
5826 l->first = tline;
5827 l->finishes = NULL;
5828 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005829
5830 /*
5831 * Remember if this mmacro expansion contains %00:
5832 * if it does, we will have to handle leading labels
5833 * specially.
5834 */
5835 if (mmac) {
5836 const Token *t;
5837 list_for_each(t, tline) {
5838 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
5839 mmac->capture_label = true;
5840 }
5841 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005842 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005843 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005844 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005845 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005846 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005847 * directive so we keep our place correctly.
5848 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005849 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005850 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005851 /*
5852 * We're in a %rep block which has been terminated, so
5853 * we're walking through to the %endrep without
5854 * emitting anything. Emit nothing at all, not even a
5855 * blank line: when we emerge from the %rep block we'll
5856 * give a line-number directive so we keep our place
5857 * correctly.
5858 */
5859 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005860 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005861 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005862 if (!expand_mmacro(tline))
5863 return tline;
5864 }
5865 }
5866}
5867
5868static char *pp_getline(void)
5869{
5870 char *line = NULL;
5871 Token *tline;
5872
5873 real_verror = nasm_set_verror(pp_verror);
5874
5875 while (true) {
5876 tline = pp_tokline();
5877 if (tline == &tok_pop) {
5878 /*
5879 * We popped the macro/include stack. If istk is empty,
5880 * we are at end of input, otherwise just loop back.
5881 */
5882 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005883 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005884 } else {
5885 /*
5886 * De-tokenize the line and emit it.
5887 */
5888 line = detoken(tline, true);
5889 free_tlist(tline);
5890 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005891 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005892 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005893
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005894 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005895 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005896 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005897 nasm_free(buf);
5898 }
5899
H. Peter Anvin130736c2016-02-17 20:27:41 -08005900 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005901 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005902}
5903
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005904static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005905{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005906 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005907
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005908 if (defining) {
5909 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005910 nasm_nonfatal("end of file while still defining macro `%s'",
5911 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005912 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005913 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005914 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005915
5916 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005917 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005918 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005919
5920 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005921
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005922 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005923 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005924 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005925 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005926 Include *i = istk;
5927 istk = istk->next;
5928 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005929 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005930 }
5931 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005932 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005933 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005934}
5935
5936static void pp_cleanup_session(void)
5937{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005938 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005939 free_llist(predef);
5940 predef = NULL;
5941 delete_Blocks();
5942 freeTokens = NULL;
5943 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005944}
5945
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005946static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005947{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005948 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005949}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005950
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005951static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005952{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005953 Token *inc, *space, *name;
5954 Line *l;
5955
H. Peter Anvin734b1882002-04-30 21:01:08 +00005956 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5957 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5958 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005959
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005960 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005961 l->next = predef;
5962 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005963 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005964 predef = l;
5965}
5966
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005967static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005968{
5969 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005970 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005971 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005972
H. Peter Anvin130736c2016-02-17 20:27:41 -08005973 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005974
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005975 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005976 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5977 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005978 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005979 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005980 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005981 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005982 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005983
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005984 if (space->next->type != TOK_PREPROC_ID &&
5985 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005986 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005987
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005988 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005989 l->next = predef;
5990 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005991 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005992 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005993
5994 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005995}
5996
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005997static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005998{
5999 Token *def, *space;
6000 Line *l;
6001
H. Peter Anvin734b1882002-04-30 21:01:08 +00006002 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
6003 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006004 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006005
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006006 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006007 l->next = predef;
6008 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006009 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006010 predef = l;
6011}
6012
H. Peter Anvin05990342018-06-11 13:32:42 -07006013/* Insert an early preprocessor command that doesn't need special handling */
6014static void pp_pre_command(const char *what, char *string)
6015{
6016 char *cmd;
6017 Token *def, *space;
6018 Line *l;
6019
6020 def = tokenize(string);
6021 if (what) {
6022 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6023 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
6024 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6025 }
6026
6027 l = nasm_malloc(sizeof(Line));
6028 l->next = predef;
6029 l->first = def;
6030 l->finishes = NULL;
6031 predef = l;
6032}
6033
H. Peter Anvinf7606612016-07-13 14:23:48 -07006034static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006035{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006036 macros_t **mp;
6037
6038 /* Find the end of the list and avoid duplicates */
6039 for (mp = stdmacros; *mp; mp++) {
6040 if (*mp == macros)
6041 return; /* Nothing to do */
6042 }
6043
6044 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6045
6046 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006047}
6048
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006049static void pp_extra_stdmac(macros_t *macros)
6050{
6051 extrastdmac = macros;
6052}
6053
H. Peter Anvin8b262472019-02-26 14:00:54 -08006054static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006055{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006056 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006057 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6058 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6059}
6060
6061static Token *make_tok_qstr(const char *str)
6062{
6063 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006064 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006065 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006066}
6067
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006068static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006069{
6070 if (!m)
6071 return;
6072
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006073 /* We need to print the mstk.mmac list in reverse order */
6074 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006075
6076 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006077 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006078 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006079 }
6080}
6081
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006082static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006083{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006084 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006085
H. Peter Anvinddb29062018-12-11 00:06:29 -08006086 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6087 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006088
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006089 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006090 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006091
H. Peter Anvinddb29062018-12-11 00:06:29 -08006092 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006093}
6094
H. Peter Anvine7469712016-02-18 02:20:59 -08006095const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006096 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006097 pp_reset,
6098 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006099 pp_cleanup_pass,
6100 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006101 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006102 pp_pre_define,
6103 pp_pre_undefine,
6104 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006105 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006106 pp_include_path,
6107 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006108};