blob: c94f9b68a948d2888e5803c071bed6792375ffbe [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;
84typedef struct Line Line;
85typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080086typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000087
88/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070089 * Note on the storage of both SMacro and MMacros: the hash table
90 * indexes them case-insensitively, and we then have to go through a
91 * linked list of potential case aliases (and, for MMacros, parameter
92 * ranges); this is to preserve the matching semantics of the earlier
93 * code. If the number of case aliases for a specific macro is a
94 * performance issue, you may want to reconsider your coding style.
95 */
96
97/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070098 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -070099 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700100typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700101
102/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000103 * Store the definition of a single-line macro.
104 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700105enum sparmflags {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700106 SPARM_PLAIN = 0,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700107 SPARM_EVAL = 1, /* Evaluate as a numeric expression (=) */
108 SPARM_STR = 2, /* Convert to quoted string ($) */
109 SPARM_NOSTRIP = 4, /* Don't strip braces (!) */
110 SPARM_GREEDY = 8 /* Greedy final parameter (+) */
111};
112
113struct smac_param {
114 char *name;
115 int namelen;
116 enum sparmflags flags;
117};
118
H. Peter Anvine2c80182005-01-15 22:15:51 +0000119struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800120 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800121 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700122 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700123 ExpandSMacro expand;
124 intorptr expandpvt;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700125 struct smac_param *params;
126 int nparam;
127 bool greedy;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800128 bool casesense;
129 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700130 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000131};
132
133/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800134 * Store the definition of a multi-line macro. This is also used to
135 * store the interiors of `%rep...%endrep' blocks, which are
136 * effectively self-re-invoking multi-line macros which simply
137 * don't have a name or bother to appear in the hash tables. %rep
138 * blocks are signified by having a NULL `name' field.
139 *
140 * In a MMacro describing a `%rep' block, the `in_progress' field
141 * isn't merely boolean, but gives the number of repeats left to
142 * run.
143 *
144 * The `next' field is used for storing MMacros in hash tables; the
145 * `next_active' field is for stacking them on istk entries.
146 *
147 * When a MMacro is being expanded, `params', `iline', `nparam',
148 * `paramlen', `rotate' and `unique' are local to the invocation.
149 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700150
151/*
152 * Expansion stack. Note that .mmac can point back to the macro itself,
153 * whereas .mstk cannot.
154 */
155struct mstk {
156 MMacro *mstk; /* Any expansion, real macro or not */
157 MMacro *mmac; /* Highest level actual mmacro */
158};
159
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800160struct MMacro {
161 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700162#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800163 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700164#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800165 char *name;
166 int nparam_min, nparam_max;
167 bool casesense;
168 bool plus; /* is the last parameter greedy? */
169 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700170 bool capture_label; /* macro definition has %00; capture label */
171 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800172 int32_t max_depth; /* maximum number of recursive expansions allowed */
173 Token *dlist; /* All defaults as one list */
174 Token **defaults; /* Parameter default pointers */
175 int ndefs; /* number of default parameters */
176 Line *expansion;
177
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700178 struct mstk mstk; /* Macro expansion stack */
179 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800180 Token **params; /* actual parameters */
181 Token *iline; /* invocation line */
182 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700183 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800184 int *paramlen;
185 uint64_t unique;
186 int lineno; /* Current line number on expansion */
187 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700188
H. Peter Anvin274cda82016-05-10 02:56:29 -0700189 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700190 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800191};
192
193
194/* Store the definition of a multi-line macro, as defined in a
195 * previous recursive macro expansion.
196 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700197#if 0
198
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800199struct MMacroInvocation {
200 MMacroInvocation *prev; /* previous invocation */
201 Token **params; /* actual parameters */
202 Token *iline; /* invocation line */
203 unsigned int nparam, rotate;
204 int *paramlen;
205 uint64_t unique;
206 uint64_t condcnt;
207};
208
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700209#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800210
211/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000212 * The context stack is composed of a linked list of these.
213 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000214struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800215 Context *next;
216 char *name;
217 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700218 uint64_t number;
219 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000220};
221
222/*
223 * This is the internal form which we break input lines up into.
224 * Typically stored in linked lists.
225 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800226 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
227 * not necessarily used as-is, but is also used to encode the number
228 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000229 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800230 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700231 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000232 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800233 * tok_smac_param(0) but the one representing `y' will be
234 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000235 *
236 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
237 * which doesn't need quotes around it. Used in the pre-include
238 * mechanism as an alternative to trying to find a sensible type of
239 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000240 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000241enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800242 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
243 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800244 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700245 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800246 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300247 TOK_PASTE, /* %+ */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700248 TOK_COND_COMMA, /* %, */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300249 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800250 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300251 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000252};
253
H. Peter Anvin8b262472019-02-26 14:00:54 -0800254static inline enum pp_token_type tok_smac_param(int param)
255{
256 return TOK_SMAC_START_PARAMS + param;
257}
258static int smac_nparam(enum pp_token_type toktype)
259{
260 return toktype - TOK_SMAC_START_PARAMS;
261}
262static bool is_smac_param(enum pp_token_type toktype)
263{
264 return toktype >= TOK_SMAC_START_PARAMS;
265}
266
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400267#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400268#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400269
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400270struct tokseq_match {
271 int mask_head;
272 int mask_tail;
273};
274
H. Peter Anvine2c80182005-01-15 22:15:51 +0000275struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800276 Token *next;
277 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700278 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800279 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000280};
281
282/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800283 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000284 * these, which is essentially a container to allow several linked
285 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700286 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000287 * Note that in this module, linked lists are treated as stacks
288 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800289 * `expansion' field in MMacro structures, so that the linked list,
290 * if walked, would give the macro lines in reverse order; this
291 * means that we can walk the list when expanding a macro, and thus
292 * push the lines on to the `expansion' field in _istk_ in reverse
293 * order (so that when popped back off they are in the right
294 * order). It may seem cockeyed, and it relies on my design having
295 * an even number of steps in, but it works...
296 *
297 * Some of these structures, rather than being actual lines, are
298 * markers delimiting the end of the expansion of a given macro.
299 * This is for use in the cycle-tracking and %rep-handling code.
300 * Such structures have `finishes' non-NULL, and `first' NULL. All
301 * others have `finishes' NULL, but `first' may still be NULL if
302 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000303 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000304struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305 Line *next;
306 MMacro *finishes;
307 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500308};
309
310/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000311 * To handle an arbitrary level of file inclusion, we maintain a
312 * stack (ie linked list) of these things.
313 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000314struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800315 Include *next;
316 FILE *fp;
317 Cond *conds;
318 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700319 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700320 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700321 int lineno, lineinc;
322 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000323};
324
325/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700326 * File real name hash, so we don't have to re-search the include
327 * path for every pass (and potentially more than that if a file
328 * is used more than once.)
329 */
330struct hash_table FileHash;
331
332/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700333 * Counters to trap on insane macro recursion or processing.
334 * Note: for smacros these count *down*, for mmacros they count *up*.
335 */
336struct deadman {
337 int64_t total; /* Total number of macros/tokens */
338 int64_t levels; /* Descent depth across all macros */
339 bool triggered; /* Already triggered, no need for error msg */
340};
341
342static struct deadman smacro_deadman, mmacro_deadman;
343
344/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000345 * Conditional assembly: we maintain a separate stack of these for
346 * each level of file inclusion. (The only reason we keep the
347 * stacks separate is to ensure that a stray `%endif' in a file
348 * included from within the true branch of a `%if' won't terminate
349 * it and cause confusion: instead, rightly, it'll cause an error.)
350 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700351enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 /*
353 * These states are for use just after %if or %elif: IF_TRUE
354 * means the condition has evaluated to truth so we are
355 * currently emitting, whereas IF_FALSE means we are not
356 * currently emitting but will start doing so if a %else comes
357 * up. In these states, all directives are admissible: %elif,
358 * %else and %endif. (And of course %if.)
359 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800360 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361 /*
362 * These states come up after a %else: ELSE_TRUE means we're
363 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
364 * any %elif or %else will cause an error.
365 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800366 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000367 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200368 * These states mean that we're not emitting now, and also that
369 * nothing until %endif will be emitted at all. COND_DONE is
370 * used when we've had our moment of emission
371 * and have now started seeing %elifs. COND_NEVER is used when
372 * the condition construct in question is contained within a
373 * non-emitting branch of a larger condition construct,
374 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000375 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800376 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000377};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700378struct Cond {
379 Cond *next;
380 enum cond_state state;
381};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800382#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000383
H. Peter Anvin70653092007-10-19 14:42:29 -0700384/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000385 * These defines are used as the possible return values for do_directive
386 */
387#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300388#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000389
Keith Kanios852f1ee2009-07-12 00:19:55 -0500390/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000391 * Condition codes. Note that we use c_ prefix not C_ because C_ is
392 * used in nasm.h for the "real" condition codes. At _this_ level,
393 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
394 * ones, so we need a different enum...
395 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700396static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000397 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
398 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000399 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700401enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
403 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 -0700404 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
405 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700407static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
409 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 +0000410 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411};
412
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413/*
414 * Directive names.
415 */
416/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
417static int is_condition(enum preproc_token arg)
418{
419 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
420}
421
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000422/* For TASM compatibility we need to be able to recognise TASM compatible
423 * conditional compilation directives. Using the NASM pre-processor does
424 * not work, so we look for them specifically from the following list and
425 * then jam in the equivalent NASM directive into the input stream.
426 */
427
H. Peter Anvine2c80182005-01-15 22:15:51 +0000428enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000429 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
430 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
431};
432
H. Peter Anvin476d2862007-10-02 22:04:15 -0700433static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000434 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
435 "ifndef", "include", "local"
436};
437
438static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700439static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000440static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800441static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000442
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000443static Context *cstk;
444static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300445static const struct strlist *ipath_list;
H. Peter Anvind2354082019-08-27 16:38:48 -0700446static bool do_aliases;
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 Anvin (Intel)f24d9752019-09-18 18:17:26 -0700496 * Tokens are allocated in blocks to improve speed. Set the blocksize
497 * to 0 to use regular nasm_malloc(); this is useful for debugging.
H. Peter Anvin734b1882002-04-30 21:01:08 +0000498 */
499#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700500
501#if TOKEN_BLOCKSIZE
502typedef struct Blocks Blocks;
H. Peter Anvince616072002-04-30 21:02:23 +0000503struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504 Blocks *next;
505 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000506};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800507static Blocks blocks = { NULL, NULL };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700508static Token *freeTokens = NULL;
509#endif
H. Peter Anvin734b1882002-04-30 21:01:08 +0000510
511/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000512 * Forward declarations.
513 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700514static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000515static Token *expand_mmac_params(Token * tline);
516static Token *expand_smacro(Token * tline);
517static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400518static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800519static Token *make_tok_num(int64_t val);
520static Token *make_tok_qstr(const char *str);
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 Anvin (Intel)f24d9752019-09-18 18:17:26 -0700524static Token *new_White(Token *next);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000525static Token *delete_Token(Token * t);
H. Peter Anvindd88aa92019-09-12 19:39:48 -0700526static const struct use_package *
527get_use_pkg(Token *t, const char *dname, bool *err);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000528
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -0700529/* Safe test for token type, false on x == NULL */
530static inline bool tok_type(const Token *x, enum pp_token_type t)
531{
532 return x && x->type == t;
533}
534
535/* Whitespace token? */
536static inline bool tok_white(const Token *x)
537{
538 return tok_type(x, TOK_WHITESPACE);
539}
540
541/* Skip past any whitespace */
542static inline Token *skip_white(Token *x)
543{
544 while (tok_white(x))
545 x = x->next;
546
547 return x;
548}
549
550/* Delete any whitespace */
551static Token *zap_white(Token *x)
552{
553 while (tok_white(x))
554 x = delete_Token(x);
555
556 return x;
557}
558
559/* Single special character tests */
560static inline bool tok_is(const Token *x, char c)
561{
562 return x && x->type == TOK_OTHER && x->len == 1 && x->text[0] == c;
563}
564
565/* True if any other kind of token that "c", but not NULL */
566static inline bool tok_isnt(const Token *x, char c)
567{
568 return x && (x->type != TOK_OTHER || x->len != 1 || x->text[0] != c);
569}
H. Peter Anvin76690a12002-04-30 20:52:49 +0000570
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400571/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700572 * In-place reverse a list of tokens.
573 */
574static Token *reverse_tokens(Token *t)
575{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800576 Token *prev = NULL;
577 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700578
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800579 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400580 next = t->next;
581 t->next = prev;
582 prev = t;
583 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800584 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700585
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800586 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700587}
588
589/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300590 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000591 * front of them. We do it here because I could not find any other
592 * place to do it for the moment, and it is a hack (ideally it would
593 * be nice to be able to use the NASM pre-processor to do it).
594 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000595static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000596{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000597 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400598 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000599
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400600 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000601
602 /* Binary search for the directive name */
603 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400604 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400605 q = nasm_skip_word(p);
606 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607 if (len) {
608 oldchar = p[len];
609 p[len] = 0;
610 while (j - i > 1) {
611 k = (j + i) / 2;
612 m = nasm_stricmp(p, tasm_directives[k]);
613 if (m == 0) {
614 /* We have found a directive, so jam a % in front of it
615 * so that NASM will then recognise it as one if it's own.
616 */
617 p[len] = oldchar;
618 len = strlen(p);
619 oldline = line;
620 line = nasm_malloc(len + 2);
621 line[0] = '%';
622 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700623 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300624 * NASM does not recognise IFDIFI, so we convert
625 * it to %if 0. This is not used in NASM
626 * compatible code, but does need to parse for the
627 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000628 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700629 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 } else {
631 memcpy(line + 1, p, len + 1);
632 }
633 nasm_free(oldline);
634 return line;
635 } else if (m < 0) {
636 j = k;
637 } else
638 i = k;
639 }
640 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000641 }
642 return line;
643}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000644
H. Peter Anvin76690a12002-04-30 20:52:49 +0000645/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000646 * The pre-preprocessing stage... This function translates line
647 * number indications as they emerge from GNU cpp (`# lineno "file"
648 * flags') into NASM preprocessor line number indications (`%line
649 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000650 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000651static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000652{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000653 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000654 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000655
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 if (line[0] == '#' && line[1] == ' ') {
657 oldline = line;
658 fname = oldline + 2;
659 lineno = atoi(fname);
660 fname += strspn(fname, "0123456789 ");
661 if (*fname == '"')
662 fname++;
663 fnlen = strcspn(fname, "\"");
664 line = nasm_malloc(20 + fnlen);
665 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
666 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000667 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000668 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000669 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000670 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000671}
672
673/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000674 * Free a linked list of tokens.
675 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000676static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000677{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400678 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000679 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000680}
681
682/*
683 * Free a linked list of lines.
684 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000685static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000686{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400687 Line *l, *tmp;
688 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000689 free_tlist(l->first);
690 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000691 }
692}
693
694/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700695 * Free an array of linked lists of tokens
696 */
697static void free_tlist_array(Token **array, size_t nlists)
698{
699 Token **listp = array;
700
701 while (nlists--)
702 free_tlist(*listp++);
703
704 nasm_free(array);
705}
706
707/*
708 * Duplicate a linked list of tokens.
709 */
710static Token *dup_tlist(const Token *list, Token ***tailp)
711{
712 Token *newlist = NULL;
713 Token **tailpp = &newlist;
714 const Token *t;
715
716 list_for_each(t, list) {
717 Token *nt;
718 *tailpp = nt = dup_Token(NULL, t);
719 tailpp = &nt->next;
720 }
721
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700722 if (tailp) {
723 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700724 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700725 }
726
727 return newlist;
728}
729
730/*
731 * Duplicate a linked list of tokens with a maximum count
732 */
733static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
734{
735 Token *newlist = NULL;
736 Token **tailpp = &newlist;
737 const Token *t;
738
739 list_for_each(t, list) {
740 Token *nt;
741 if (!cnt--)
742 break;
743 *tailpp = nt = dup_Token(NULL, t);
744 tailpp = &nt->next;
745 }
746
747 if (tailp) {
748 **tailp = newlist;
749 *tailp = tailpp;
750 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700751
752 return newlist;
753}
754
755/*
756 * Duplicate a linked list of tokens in reverse order
757 */
758static Token *dup_tlist_reverse(const Token *list, Token *tail)
759{
760 const Token *t;
761
762 list_for_each(t, list)
763 tail = dup_Token(tail, t);
764
765 return tail;
766}
767
768/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800769 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000770 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800771static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000772{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800773 nasm_free(m->name);
774 free_tlist(m->dlist);
775 nasm_free(m->defaults);
776 free_llist(m->expansion);
777 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000778}
779
780/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700781 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800782 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700783static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800784{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700785 if (s->params) {
786 int i;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -0700787 for (i = 0; i < s->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700788 nasm_free(s->params[i].name);
789 nasm_free(s->params);
790 }
H. Peter Anvin8b262472019-02-26 14:00:54 -0800791 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700792 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700793}
794
795static void clear_smacro(SMacro *s)
796{
797 free_smacro_members(s);
798 /* Wipe everything except the next pointer */
799 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
800}
801
802/*
803 * Free an SMacro
804 */
805static void free_smacro(SMacro *s)
806{
807 free_smacro_members(s);
808 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800809}
810
811/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700812 * Free all currently defined macros, and free the hash tables
813 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700814static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700815{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800816 struct hash_iterator it;
817 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700818
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800819 hash_for_each(smt, it, np) {
820 SMacro *tmp;
821 SMacro *s = np->data;
822 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800823 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700824 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700825 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700826 hash_free(smt);
827}
828
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800829static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700830{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800831 struct hash_iterator it;
832 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700833
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800834 hash_for_each(mmt, it, np) {
835 MMacro *tmp;
836 MMacro *m = np->data;
837 nasm_free((void *)np->key);
838 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800839 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700840 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800841 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700842}
843
844static void free_macros(void)
845{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700846 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800847 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700848}
849
850/*
851 * Initialize the hash tables
852 */
853static void init_macros(void)
854{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700855}
856
857/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000858 * Pop the context stack.
859 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000861{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000862 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000863
864 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700865 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000866 nasm_free(c->name);
867 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000868}
869
H. Peter Anvin072771e2008-05-22 13:17:51 -0700870/*
871 * Search for a key in the hash index; adding it if necessary
872 * (in which case we initialize the data pointer to NULL.)
873 */
874static void **
875hash_findi_add(struct hash_table *hash, const char *str)
876{
877 struct hash_insert hi;
878 void **r;
879 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800880 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700881
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800882 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700883 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300884 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700885
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800886 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
887 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700888 return hash_add(&hi, strx, NULL);
889}
890
891/*
892 * Like hash_findi, but returns the data element rather than a pointer
893 * to it. Used only when not adding a new element, hence no third
894 * argument.
895 */
896static void *
897hash_findix(struct hash_table *hash, const char *str)
898{
899 void **p;
900
901 p = hash_findi(hash, str, NULL);
902 return p ? *p : NULL;
903}
904
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400905/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800906 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400907 * if there no more left -- return NULL
908 */
909static char *line_from_stdmac(void)
910{
911 unsigned char c;
912 const unsigned char *p = stdmacpos;
913 char *line, *q;
914 size_t len = 0;
915
916 if (!stdmacpos)
917 return NULL;
918
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700919 /*
920 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
921 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
922 */
923 while ((c = *p++) != 127) {
924 uint8_t ndir = c - 128;
925 if (ndir < 256-96)
926 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400927 else
928 len++;
929 }
930
931 line = nasm_malloc(len + 1);
932 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700933
934 while ((c = *stdmacpos++) != 127) {
935 uint8_t ndir = c - 128;
936 if (ndir < 256-96) {
937 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
938 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400939 *q++ = ' ';
940 } else {
941 *q++ = c;
942 }
943 }
944 stdmacpos = p;
945 *q = '\0';
946
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700947 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700948 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400949 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700950 if (*stdmacnext) {
951 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400952 } else if (do_predef) {
953 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400954
955 /*
956 * Nasty hack: here we push the contents of
957 * `predef' on to the top-level expansion stack,
958 * since this is the most convenient way to
959 * implement the pre-include and pre-define
960 * features.
961 */
962 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700963 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800964 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700965 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800966 l->finishes = NULL;
967
968 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400969 }
970 do_predef = false;
971 }
972 }
973
974 return line;
975}
976
H. Peter Anvin6686de22019-08-10 05:33:14 -0700977/*
978 * Read a line from a file. Return NULL on end of file.
979 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700980static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000981{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700982 int c;
983 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400984 const unsigned int delta = 512;
985 const unsigned int pad = 8;
986 unsigned int nr_cont = 0;
987 bool cont = false;
988 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700989 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000990
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400991 size = delta;
992 p = buffer = nasm_malloc(size);
993
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700994 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700995 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400996
997 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700998 case EOF:
999 if (p == buffer) {
1000 nasm_free(buffer);
1001 return NULL;
1002 }
1003 c = 0;
1004 break;
1005
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001006 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001007 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001008 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001009 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001010 if (cont) {
1011 cont = false;
1012 continue;
1013 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001014 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001015 break;
1016
1017 case '\n':
1018 if (cont) {
1019 cont = false;
1020 continue;
1021 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001022 c = 0;
1023 break;
1024
1025 case 032: /* ^Z = legacy MS-DOS end of file mark */
1026 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001027 break;
1028
1029 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001030 next = fgetc(f);
1031 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +04001032 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001033 cont = true;
1034 nr_cont++;
1035 continue;
1036 }
1037 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001039
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001040 if (p >= (buffer + size - pad)) {
1041 buffer = nasm_realloc(buffer, size + delta);
1042 p = buffer + size - pad;
1043 size += delta;
1044 }
1045
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001046 *p++ = c;
1047 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001048
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001049 lineno = src_get_linnum() + istk->lineinc +
1050 (nr_cont * istk->lineinc);
1051 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001052
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001053 return buffer;
1054}
1055
1056/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001057 * Common read routine regardless of source
1058 */
1059static char *read_line(void)
1060{
1061 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001062 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001063
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001064 if (f)
1065 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001066 else
1067 line = line_from_stdmac();
1068
1069 if (!line)
1070 return NULL;
1071
1072 if (!istk->nolist)
1073 lfmt->line(LIST_READ, src_get_linnum(), line);
1074
1075 return line;
1076}
1077
1078/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001079 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001080 * don't need to parse the value out of e.g. numeric tokens: we
1081 * simply split one string into many.
1082 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001083static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001084{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001085 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001086 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001087 Token *list = NULL;
1088 Token *t, **tail = &list;
1089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001091 char *p = line;
1092 char *ep = NULL; /* End of token, for trimming the end */
1093
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 if (*p == '%') {
1095 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001096 if (*p == '+' && !nasm_isdigit(p[1])) {
1097 p++;
1098 type = TOK_PASTE;
1099 } else if (nasm_isdigit(*p) ||
1100 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 do {
1102 p++;
1103 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001104 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 type = TOK_PREPROC_ID;
1106 } else if (*p == '{') {
1107 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001108 while (*p) {
1109 if (*p == '}')
1110 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 p[-1] = *p;
1112 p++;
1113 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001114 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001115 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001116 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 if (*p)
1118 p++;
1119 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001120 } else if (*p == '[') {
1121 int lvl = 1;
1122 line += 2; /* Skip the leading %[ */
1123 p++;
1124 while (lvl && (c = *p++)) {
1125 switch (c) {
1126 case ']':
1127 lvl--;
1128 break;
1129 case '%':
1130 if (*p == '[')
1131 lvl++;
1132 break;
1133 case '\'':
1134 case '\"':
1135 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001136 p = nasm_skip_string(p - 1);
1137 if (*p)
1138 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 break;
1140 default:
1141 break;
1142 }
1143 }
1144 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001145 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001146 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001147 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001148 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001149 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001150 type = TOK_INDIRECT;
1151 } else if (*p == '?') {
1152 type = TOK_PREPROC_Q; /* %? */
1153 p++;
1154 if (*p == '?') {
1155 type = TOK_PREPROC_QQ; /* %?? */
1156 p++;
1157 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001158 } else if (*p == '!') {
1159 type = TOK_PREPROC_ID;
1160 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001161 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001162 do {
1163 p++;
1164 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001165 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001166 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001167 p = nasm_skip_string(p);
1168 if (*p)
1169 p++;
1170 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001171 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001172 } else {
1173 /* %! without string or identifier */
1174 type = TOK_OTHER; /* Legacy behavior... */
1175 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001176 } else if (*p == ',') {
1177 p++;
1178 type = TOK_COND_COMMA;
H. Peter Anvin13506202018-11-28 14:55:58 -08001179 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001181 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 do {
1183 p++;
1184 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001185 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 type = TOK_PREPROC_ID;
1187 } else {
1188 type = TOK_OTHER;
1189 if (*p == '%')
1190 p++;
1191 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001192 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 type = TOK_ID;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001194 while (nasm_isidchar(*++p))
1195 ;
1196 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 /*
1198 * A string token.
1199 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001201 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001202
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203 if (*p) {
1204 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001205 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001206 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 /* Handling unterminated strings by UNV */
1208 /* type = -1; */
1209 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001210 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001211 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001212 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001213 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001214 bool is_hex = false;
1215 bool is_float = false;
1216 bool has_e = false;
1217 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001218
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001220 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001222
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001223 if (*p == '$') {
1224 p++;
1225 is_hex = true;
1226 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001227
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001228 for (;;) {
1229 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001230
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001231 if (!is_hex && (c == 'e' || c == 'E')) {
1232 has_e = true;
1233 if (*p == '+' || *p == '-') {
1234 /*
1235 * e can only be followed by +/- if it is either a
1236 * prefixed hex number or a floating-point number
1237 */
1238 p++;
1239 is_float = true;
1240 }
1241 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1242 is_hex = true;
1243 } else if (c == 'P' || c == 'p') {
1244 is_float = true;
1245 if (*p == '+' || *p == '-')
1246 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001247 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001248 ; /* just advance */
1249 else if (c == '.') {
1250 /*
1251 * we need to deal with consequences of the legacy
1252 * parser, like "1.nolist" being two tokens
1253 * (TOK_NUMBER, TOK_ID) here; at least give it
1254 * a shot for now. In the future, we probably need
1255 * a flex-based scanner with proper pattern matching
1256 * to do it as well as it can be done. Nothing in
1257 * the world is going to help the person who wants
1258 * 0x123.p16 interpreted as two tokens, though.
1259 */
1260 r = p;
1261 while (*r == '_')
1262 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001263
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001264 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1265 (!is_hex && (*r == 'e' || *r == 'E')) ||
1266 (*r == 'p' || *r == 'P')) {
1267 p = r;
1268 is_float = true;
1269 } else
1270 break; /* Terminate the token */
1271 } else
1272 break;
1273 }
1274 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001275
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001276 if (p == line+1 && *line == '$') {
1277 type = TOK_OTHER; /* TOKEN_HERE */
1278 } else {
1279 if (has_e && !is_hex) {
1280 /* 1e13 is floating-point, but 1e13h is not */
1281 is_float = true;
1282 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001283
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001284 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1285 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001286 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001288 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 /*
1290 * Whitespace just before end-of-line is discarded by
1291 * pretending it's a comment; whitespace just before a
1292 * comment gets lumped into the comment.
1293 */
1294 if (!*p || *p == ';') {
1295 type = TOK_COMMENT;
1296 while (*p)
1297 p++;
1298 }
1299 } else if (*p == ';') {
1300 type = TOK_COMMENT;
1301 while (*p)
1302 p++;
1303 } else {
1304 /*
1305 * Anything else is an operator of some kind. We check
1306 * for all the double-character operators (>>, <<, //,
1307 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001308 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 */
1310 type = TOK_OTHER;
1311 if ((p[0] == '>' && p[1] == '>') ||
1312 (p[0] == '<' && p[1] == '<') ||
1313 (p[0] == '/' && p[1] == '/') ||
1314 (p[0] == '<' && p[1] == '=') ||
1315 (p[0] == '>' && p[1] == '=') ||
1316 (p[0] == '=' && p[1] == '=') ||
1317 (p[0] == '!' && p[1] == '=') ||
1318 (p[0] == '<' && p[1] == '>') ||
1319 (p[0] == '&' && p[1] == '&') ||
1320 (p[0] == '|' && p[1] == '|') ||
1321 (p[0] == '^' && p[1] == '^')) {
1322 p++;
1323 }
1324 p++;
1325 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001326
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 /* Handling unterminated string by UNV */
1328 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001329 {
1330 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1331 t->text[p-line] = *line;
1332 tail = &t->next;
1333 }
1334 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001336 if (!ep)
1337 ep = p;
1338 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 tail = &t->next;
1340 }
1341 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001342 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001343 return list;
1344}
1345
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001346#if TOKEN_BLOCKSIZE
H. Peter Anvince616072002-04-30 21:02:23 +00001347/*
1348 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001349 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001350 * deleted only all at once by the delete_Blocks function.
1351 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001352static void new_Block(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001353{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001354 Blocks *b = &blocks;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001355 int i;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001357 /* first, get to the end of the linked list */
1358 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001360 /* now allocate the requested chunk */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001361 b->chunk = nasm_malloc(TOKEN_BLOCKSIZE * sizeof(Token));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001363 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001364 b->next = nasm_zalloc(sizeof(Blocks));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001365
1366 freeTokens = b->chunk;
1367 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1368 freeTokens[i].next = &freeTokens[i + 1];
1369
1370 freeTokens[i].next = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +00001371}
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001372#endif
H. Peter Anvince616072002-04-30 21:02:23 +00001373
1374/*
1375 * this function deletes all managed blocks of memory
1376 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001378{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001379#if TOKEN_BLOCKSIZE
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001381
H. Peter Anvin70653092007-10-19 14:42:29 -07001382 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001383 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001384 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001385 * free it.
1386 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001388 if (b->chunk)
1389 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 a = b;
1391 b = b->next;
1392 if (a != &blocks)
1393 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001394 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001395 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001396
1397 freeTokens = NULL;
1398#endif
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001400
1401/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001402 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001403 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001404 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001405static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001406 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001407{
1408 Token *t;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001409
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001410#if TOKEN_BLOCKSIZE
1411 if (!freeTokens)
1412 new_Block();
H. Peter Anvin734b1882002-04-30 21:01:08 +00001413 t = freeTokens;
1414 freeTokens = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001415#else
1416 t = nasm_malloc(sizeof *t);
1417#endif
H. Peter Anvin734b1882002-04-30 21:01:08 +00001418 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001419 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001420 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001421 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 t->text = NULL;
1423 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001424 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001425 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001426 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001427 t->text = nasm_malloc(txtlen+1);
1428 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001430 }
1431 return t;
1432}
1433
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001434static Token *dup_Token(Token *next, const Token *src)
1435{
1436 return new_Token(next, src->type, src->text, src->len);
1437}
1438
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001439static Token *new_White(Token *next)
1440{
1441 return new_Token(next, TOK_WHITESPACE, NULL, 0);
1442}
1443
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001445{
1446 Token *next = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001447
H. Peter Anvin734b1882002-04-30 21:01:08 +00001448 nasm_free(t->text);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001449#if TOKEN_BLOCKSIZE
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001450 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001451 freeTokens = t;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001452#else
1453 nasm_free(t);
1454#endif
H. Peter Anvin734b1882002-04-30 21:01:08 +00001455 return next;
1456}
1457
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001458/*
1459 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001460 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1461 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001462 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001463static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001464{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001465 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001466 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001467 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001468
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001469 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001470 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001471 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001472 char *v;
1473 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001474
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001475 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001476 if (nasm_isquote(*v))
1477 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001478
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001479 if (v) {
1480 char *p = getenv(v);
1481 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001482 /*!
1483 *!environment [on] nonexistent environment variable
1484 *! warns if a nonexistent environment variable
1485 *! is accessed using the \c{%!} preprocessor
1486 *! construct (see \k{getenv}.) Such environment
1487 *! variables are treated as empty (with this
1488 *! warning issued) starting in NASM 2.15;
1489 *! earlier versions of NASM would treat this as
1490 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001491 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001492 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1493 p = "";
1494 }
1495 t->text = nasm_strdup(p);
1496 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001497 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001498 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001500
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 /* Expand local macros here and not during preprocessing */
1502 if (expand_locals &&
1503 t->type == TOK_PREPROC_ID && t->text &&
1504 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001505 const char *q;
1506 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001507 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001509 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1510 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001511 nasm_free(t->text);
1512 t->text = p;
1513 }
1514 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001515 if (t->text) {
1516 if (debug_level(2)) {
1517 unsigned long t_len = t->len;
1518 unsigned long s_len = strlen(t->text);
1519 if (t_len != s_len) {
1520 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1521 t->text, t->type, s_len, t_len);
1522 t->len = s_len;
1523 }
1524 }
1525 len += t->len;
1526 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001527 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001528 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001529 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001530
H. Peter Anvin734b1882002-04-30 21:01:08 +00001531 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001532
1533 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001534 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001535 memcpy(p, t->text, t->len);
1536 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001537 } else if (t->type == TOK_WHITESPACE) {
1538 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001540 }
1541 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001542
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001543 return line;
1544}
1545
1546/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001547 * A scanner, suitable for use by the expression evaluator, which
1548 * operates on a line of Tokens. Expects a pointer to a pointer to
1549 * the first token in the line to be passed in as its private_data
1550 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001551 *
1552 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001553 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001554struct ppscan {
1555 Token *tptr;
1556 int ntokens;
1557};
1558
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001560{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001561 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001562 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001563 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001564
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001566 if (pps->ntokens && (tline = pps->tptr)) {
1567 pps->ntokens--;
1568 pps->tptr = tline->next;
1569 } else {
1570 pps->tptr = NULL;
1571 pps->ntokens = 0;
1572 return tokval->t_type = TOKEN_EOS;
1573 }
1574 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001575
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001576 tokval->t_charptr = tline->text;
1577
H. Peter Anvin76690a12002-04-30 20:52:49 +00001578 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001580 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001582
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001584 p = tokval->t_charptr = tline->text;
1585 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 tokval->t_charptr++;
1587 return tokval->t_type = TOKEN_ID;
1588 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001589
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001590 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001591 if (r >= p+MAX_KEYWORD)
1592 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001593 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001594 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001595 *s = '\0';
1596 /* right, so we have an identifier sitting in temp storage. now,
1597 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001598 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001599 }
1600
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001602 bool rn_error;
1603 tokval->t_integer = readnum(tline->text, &rn_error);
1604 tokval->t_charptr = tline->text;
1605 if (rn_error)
1606 return tokval->t_type = TOKEN_ERRNUM;
1607 else
1608 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001609 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001610
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001611 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001612 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001613 }
1614
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001616 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001617
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001618 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001619 tokval->t_charptr = tline->text;
1620 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001621
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001622 if (ep[0] != bq || ep[1] != '\0')
1623 return tokval->t_type = TOKEN_ERRSTR;
1624 else
1625 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001626 }
1627
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 if (tline->type == TOK_OTHER) {
1629 if (!strcmp(tline->text, "<<"))
1630 return tokval->t_type = TOKEN_SHL;
1631 if (!strcmp(tline->text, ">>"))
1632 return tokval->t_type = TOKEN_SHR;
1633 if (!strcmp(tline->text, "//"))
1634 return tokval->t_type = TOKEN_SDIV;
1635 if (!strcmp(tline->text, "%%"))
1636 return tokval->t_type = TOKEN_SMOD;
1637 if (!strcmp(tline->text, "=="))
1638 return tokval->t_type = TOKEN_EQ;
1639 if (!strcmp(tline->text, "<>"))
1640 return tokval->t_type = TOKEN_NE;
1641 if (!strcmp(tline->text, "!="))
1642 return tokval->t_type = TOKEN_NE;
1643 if (!strcmp(tline->text, "<="))
1644 return tokval->t_type = TOKEN_LE;
1645 if (!strcmp(tline->text, ">="))
1646 return tokval->t_type = TOKEN_GE;
1647 if (!strcmp(tline->text, "&&"))
1648 return tokval->t_type = TOKEN_DBL_AND;
1649 if (!strcmp(tline->text, "^^"))
1650 return tokval->t_type = TOKEN_DBL_XOR;
1651 if (!strcmp(tline->text, "||"))
1652 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001653 }
1654
1655 /*
1656 * We have no other options: just return the first character of
1657 * the token text.
1658 */
1659 return tokval->t_type = tline->text[0];
1660}
1661
1662/*
H. Peter Anvind2354082019-08-27 16:38:48 -07001663 * 1. An expression (true if nonzero 0)
1664 * 2. The keywords true, on, yes for true
1665 * 3. The keywords false, off, no for false
1666 * 4. An empty line, for true
1667 *
1668 * On error, return defval (usually the previous value)
1669 */
1670static bool pp_get_boolean_option(Token *tline, bool defval)
1671{
1672 static const char * const noyes[] = {
1673 "no", "yes",
1674 "false", "true",
1675 "off", "on"
1676 };
1677 struct ppscan pps;
1678 struct tokenval tokval;
1679 expr *evalresult;
1680
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001681 tline = skip_white(tline);
1682 if (!tline)
H. Peter Anvind2354082019-08-27 16:38:48 -07001683 return true;
1684
1685 if (tline->type == TOK_ID) {
1686 size_t i;
1687 for (i = 0; i < ARRAY_SIZE(noyes); i++)
1688 if (!nasm_stricmp(tline->text, noyes[i]))
1689 return i & 1;
1690 }
1691
1692 pps.tptr = NULL;
1693 pps.tptr = tline;
1694 pps.ntokens = -1;
1695 tokval.t_type = TOKEN_INVALID;
1696 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
1697
1698 if (!evalresult)
1699 return true;
1700
1701 if (tokval.t_type)
1702 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
1703 if (!is_really_simple(evalresult)) {
1704 nasm_nonfatal("boolean flag expression must be a constant");
1705 return defval;
1706 }
1707
1708 return reloc_value(evalresult) != 0;
1709}
1710
1711/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001712 * Compare a string to the name of an existing macro; this is a
1713 * simple wrapper which calls either strcmp or nasm_stricmp
1714 * depending on the value of the `casesense' parameter.
1715 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001716static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001717{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001718 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001719}
1720
1721/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001722 * Compare a string to the name of an existing macro; this is a
1723 * simple wrapper which calls either strcmp or nasm_stricmp
1724 * depending on the value of the `casesense' parameter.
1725 */
1726static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1727{
1728 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1729}
1730
1731/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001732 * Return the Context structure associated with a %$ token. Return
1733 * NULL, having _already_ reported an error condition, if the
1734 * context stack isn't deep enough for the supplied number of $
1735 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001736 *
1737 * If "namep" is non-NULL, set it to the pointer to the macro name
1738 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001739 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001740static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001741{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001742 Context *ctx;
1743 int i;
1744
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001745 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001746 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001747
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001748 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001750
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001752 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001754 }
1755
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001756 name += 2;
1757 ctx = cstk;
1758 i = 0;
1759 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001760 name++;
1761 i++;
1762 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001763 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001765 nasm_nonfatal("`%s': context stack is only"
1766 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001768 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001769
1770 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001771 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001772
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001773 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001774}
1775
1776/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001777 * Open an include file. This routine must always return a valid
1778 * file pointer if it returns - it's responsible for throwing an
1779 * ERR_FATAL and bombing out completely if not. It should also try
1780 * the include path one by one until it finds the file or reaches
1781 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001782 *
1783 * Note: for INC_PROBE the function returns NULL at all times;
1784 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001785 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001786enum incopen_mode {
1787 INC_NEEDED, /* File must exist */
1788 INC_OPTIONAL, /* Missing is OK */
1789 INC_PROBE /* Only an existence probe */
1790};
1791
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001792/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001793static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001794 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001795{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001796 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001797 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001798 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001799 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001800 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001801
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001803 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001804 if (omode == INC_PROBE) {
1805 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001806 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001807 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001808 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001809 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001810 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001811 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001812 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001814 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001815
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001816 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001817
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001818 if (!ip) {
1819 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001820 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001821 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001822
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001823 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001824 ip = ip->next;
1825 }
1826}
1827
1828/*
1829 * Open a file, or test for the presence of one (depending on omode),
1830 * considering the include path.
1831 */
1832static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001833 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001834 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001835 enum incopen_mode omode,
1836 enum file_flags fmode)
1837{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001838 struct hash_insert hi;
1839 void **hp;
1840 char *path;
1841 FILE *fp = NULL;
1842
1843 hp = hash_find(&FileHash, file, &hi);
1844 if (hp) {
1845 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001846 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001847 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001848 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001849 } else {
1850 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001851 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001852
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001853 /* Positive or negative result */
1854 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001855
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001856 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001857 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001858 */
1859 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001860 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001861 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001862
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001863 if (!path) {
1864 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001865 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001866 } else {
1867 if (!fp && omode != INC_PROBE)
1868 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001869 }
1870
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001871 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001872 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001873
1874 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001875}
1876
1877/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001878 * Opens an include or input file. Public version, for use by modules
1879 * that get a file:lineno pair and need to look at the file again
1880 * (e.g. the CodeView debug backend). Returns NULL on failure.
1881 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001882FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001883{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001884 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001885}
1886
1887/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001888 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001889 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001890 * return true if _any_ single-line macro of that name is defined.
1891 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001892 * `nparam' or no parameters is defined.
1893 *
1894 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001895 * defined, or nparam is -1, the address of the definition structure
1896 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001897 * is NULL, no action will be taken regarding its contents, and no
1898 * error will occur.
1899 *
1900 * Note that this is also called with nparam zero to resolve
1901 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001902 *
1903 * If you already know which context macro belongs to, you can pass
1904 * the context pointer as first parameter; if you won't but name begins
1905 * with %$ the context will be automatically computed. If all_contexts
1906 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001907 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001908static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001909smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvind2354082019-08-27 16:38:48 -07001910 bool nocase, bool find_alias)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001911{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001912 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001913 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001914
H. Peter Anvin97a23472007-09-16 17:57:25 -07001915 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001916 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001917 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001919 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001921 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001923 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001925 }
H. Peter Anvind2354082019-08-27 16:38:48 -07001926
1927restart:
H. Peter Anvin166c2472008-05-28 12:28:58 -07001928 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001929
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 while (m) {
1931 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001932 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07001933 (m->greedy && nparam >= m->nparam-1))) {
H. Peter Anvind2354082019-08-27 16:38:48 -07001934 if (m->alias && !find_alias) {
1935 if (do_aliases) {
H. Peter Anvind626f352019-09-12 18:31:29 -07001936 name = m->expansion->text;
H. Peter Anvind2354082019-08-27 16:38:48 -07001937 goto restart;
1938 } else {
1939 continue;
1940 }
1941 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001943 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001945 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 }
1947 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001948 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001949
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001950 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001951}
1952
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001953/* param should be a natural number [0; INT_MAX] */
1954static int read_param_count(const char *str)
1955{
1956 int result;
1957 bool err;
1958
1959 result = readnum(str, &err);
1960 if (result < 0 || result > INT_MAX) {
1961 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001962 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1963 str, 0, INT_MAX);
1964 } else if (err)
1965 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001966 return result;
1967}
1968
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001969/*
1970 * Count and mark off the parameters in a multi-line macro call.
1971 * This is called both from within the multi-line macro expansion
1972 * code, and also to mark off the default parameters when provided
1973 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001974 *
1975 * Note that we need space in the params array for parameter 0 being
1976 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001977 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001978static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001979{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001980 int paramsize, brace;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001981 int nparam = 0;
1982 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001983
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001984 paramsize = PARAM_DELTA;
1985 params = nasm_malloc(paramsize * sizeof(*params));
1986 params[0] = NULL;
1987
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001988 while ((t = skip_white(t))) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001989 /* 2 slots for captured label and NULL */
1990 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001992 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001994 brace = 0;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001995 if (tok_is(t, '{'))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001996 brace++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001997 params[++nparam] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001998 if (brace) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07001999 while (brace && (t = t->next)) {
2000 if (tok_is(t, '{'))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002001 brace++;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002002 else if (tok_is(t, '}'))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002003 brace--;
2004 }
2005
2006 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 /*
2008 * Now we've found the closing brace, look further
2009 * for the comma.
2010 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002011 t = skip_white(t->next);
2012 if (!tok_is(t, ','))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002013 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002014 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08002015 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002016
2017 while (tok_isnt(t, ','))
2018 t = t->next;
2019
2020 if (t) /* got a comma */
2021 t = t->next; /* eat the comma */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002022 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002023
2024 params[nparam+1] = NULL;
2025 *paramsp = params;
2026 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002027}
2028
2029/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00002030 * Determine whether one of the various `if' conditions is true or
2031 * not.
2032 *
2033 * We must free the tline we get passed.
2034 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002035static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002036{
H. Peter Anvin70055962007-10-11 00:05:31 -07002037 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002038 Token *t, *tt, *origline;
2039 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002040 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002041 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002042 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07002043 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002044 const char *dname = pp_directives[ct];
2045 bool casesense = true;
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002046 enum preproc_token cond = PP_COND(ct);
H. Peter Anvin76690a12002-04-30 20:52:49 +00002047
2048 origline = tline;
2049
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002050 switch (cond) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002051 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002052 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002053 while (true) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002054 tline = skip_white(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002055 if (!tline)
2056 break;
2057 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002058 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002059 dname);
2060 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02002062 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002063 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002064 tline = tline->next;
2065 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002066 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002067
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002068 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002069 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002070 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002071 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 if (!tline || (tline->type != TOK_ID &&
2073 (tline->type != TOK_PREPROC_ID ||
2074 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002075 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002076 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002078 }
H. Peter Anvind2354082019-08-27 16:38:48 -07002079 if (smacro_defined(NULL, tline->text, 0, NULL, true, false))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002080 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002082 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002084
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002085 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002086 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002087 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002088 while (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002089 tline = skip_white(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002090 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002091 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002092 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002093 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002094 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002095 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002096 goto fail;
2097 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002098 p = tline->text;
2099 if (tline->type == TOK_PREPROC_ID)
2100 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08002101 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002102 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04002103 if (getenv(p))
2104 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002105 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002106 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07002107 break;
2108
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002109 case PP_IFIDNI:
2110 casesense = false;
2111 /* fall through */
2112 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 tline = expand_smacro(tline);
2114 t = tt = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002115 while (tok_isnt(tt, ','))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002116 tt = tt->next;
2117 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002118 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002119 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002120 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002121 }
2122 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002123 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002124 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
2125 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002126 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002127 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002128 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002129 }
2130 if (t->type == TOK_WHITESPACE) {
2131 t = t->next;
2132 continue;
2133 }
2134 if (tt->type == TOK_WHITESPACE) {
2135 tt = tt->next;
2136 continue;
2137 }
2138 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002139 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002140 break;
2141 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002142 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 size_t l1 = nasm_unquote(t->text, NULL);
2145 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002146
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002147 if (l1 != l2) {
2148 j = false;
2149 break;
2150 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002151 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002152 j = false;
2153 break;
2154 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002155 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002156 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002157 break;
2158 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002159
H. Peter Anvine2c80182005-01-15 22:15:51 +00002160 t = t->next;
2161 tt = tt->next;
2162 }
2163 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002164 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002165 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002166
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002167 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002168 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002169 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002170 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002171
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002172 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002173 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002174 if (!tok_type(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002175 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002176 goto fail;
2177 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002178 nasm_zero(searching);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002179 searching.name = nasm_strdup(tline->text);
2180 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002181 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 searching.nparam_max = INT_MAX;
2183 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002184 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002185 if (!tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002186 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002187 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002188 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002189 } else {
2190 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002191 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002192 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002193 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002195 if (tok_is(tline, '*'))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002196 searching.nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002197 else if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002198 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002199 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002200 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002201 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002202 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002203 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002204 searching.nparam_max = searching.nparam_min;
2205 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002206 }
2207 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002208 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002209 tline = tline->next;
2210 searching.plus = true;
2211 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002212 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2213 while (mmac) {
2214 if (!strcmp(mmac->name, searching.name) &&
2215 (mmac->nparam_min <= searching.nparam_max
2216 || searching.plus)
2217 && (searching.nparam_min <= mmac->nparam_max
2218 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002219 found = true;
2220 break;
2221 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002222 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002223 }
2224 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002225 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002226 nasm_free(searching.name);
2227 j = found;
2228 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002229 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002230
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002231 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002232 needtype = TOK_ID;
2233 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002234 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002235 needtype = TOK_NUMBER;
2236 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002237 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002238 needtype = TOK_STRING;
2239 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002240
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002241iftype:
2242 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002243
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002244 while (tok_white(t) ||
2245 (needtype == TOK_NUMBER && (tok_is(t, '-') | tok_is(t, '+'))))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002246 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002247
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002248 j = tok_type(t, needtype);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002249 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002250
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002251 case PP_IFTOKEN:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002252 tline = expand_smacro(tline);
2253 t = skip_white(tline);
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002254
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002255 j = false;
2256 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002257 t = skip_white(t->next); /* Skip the actual token + whitespace */
2258 j = !t;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002259 }
2260 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002261
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002262 case PP_IFEMPTY:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002263 tline = expand_smacro(tline);
2264 t = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002265 j = !t; /* Should be empty */
2266 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002267
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002268 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002269 pps.tptr = tline = expand_smacro(tline);
2270 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002272 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002273 if (!evalresult)
2274 return -1;
2275 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002276 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002278 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002279 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002280 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002281 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002282 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002283 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002284
H. Peter Anvindd88aa92019-09-12 19:39:48 -07002285 case PP_IFUSING:
2286 case PP_IFUSABLE:
2287 {
2288 const struct use_package *pkg;
2289 bool err;
2290
2291 pkg = get_use_pkg(tline, dname, &err);
2292 if (err)
2293 goto fail;
2294
2295 j = pkg && ((cond == PP_IFUSABLE) | use_loaded[pkg->index]);
2296 break;
2297 }
2298
H. Peter Anvine2c80182005-01-15 22:15:51 +00002299 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002300 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002301 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002302 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002303
2304 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002305 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002306
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002307fail:
2308 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002309 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002310}
2311
2312/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002313 * Default smacro expansion routine: just returns a copy of the
2314 * expansion list.
2315 */
2316static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002317smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002318{
2319 (void)params;
2320 (void)nparams;
2321
2322 return dup_tlist(s->expansion, NULL);
2323}
2324
2325/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002326 * Emit a macro defintion or undef to the listing file, if
2327 * desired. This is similar to detoken(), but it handles the reverse
2328 * expansion list, does not expand %! or local variable tokens, and
2329 * does some special handling for macro parameters.
2330 */
2331static void
2332list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2333{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002334 Token *t;
2335 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002336 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002337 char *context_prefix = NULL;
2338 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002339
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002340 namelen = strlen(m->name);
2341 size = namelen + 2; /* Include room for space after name + NUL */
2342
2343 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002344 int context_depth = cstk->depth - ctx->depth + 1;
2345 context_prefix =
2346 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2347 ctx->name ? ctx->name : "",
2348 ctx->number, context_depth, "");
2349
2350 context_len = nasm_last_string_len();
2351 memset(context_prefix + context_len - context_depth,
2352 '$', context_depth);
2353 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002354 }
2355
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002356 list_for_each(t, m->expansion)
2357 size += t->text ? t->len : 1;
2358
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002359 if (m->nparam) {
2360 /*
2361 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002362 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002363 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002364 int i;
2365
2366 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002367 for (i = 0; i < m->nparam; i++)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002368 size += m->params[i].namelen;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002369 }
2370
2371 def = nasm_malloc(size);
2372 p = def+size;
2373 *--p = '\0';
2374
2375 list_for_each(t, m->expansion) {
2376 if (!t->text) {
2377 *--p = ' ';
2378 } else {
2379 p -= t->len;
2380 memcpy(p, t->text, t->len);
2381 }
2382 }
2383
2384 *--p = ' ';
2385
2386 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002387 int i;
2388
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002389 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002390 for (i = m->nparam-1; i >= 0; i--) {
2391 enum sparmflags flags = m->params[i].flags;
2392 if (flags & SPARM_GREEDY)
2393 *--p = '+';
2394 if (m->params[i].name) {
2395 p -= m->params[i].namelen;
2396 memcpy(p, m->params[i].name, m->params[i].namelen);
2397 }
2398 if (flags & SPARM_NOSTRIP)
2399 *--p = '!';
2400 if (flags & SPARM_STR)
2401 *--p = '&';
2402 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002403 *--p = '=';
2404 *--p = ',';
2405 }
2406 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002407 }
2408
2409 p -= namelen;
2410 memcpy(p, m->name, namelen);
2411
H. Peter Anvin6686de22019-08-10 05:33:14 -07002412 if (context_prefix) {
2413 p -= context_len;
2414 memcpy(p, context_prefix, context_len);
2415 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002416 }
2417
2418 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002419 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002420}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002421
2422/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002423 * Parse smacro arguments, return argument count. If the tmpl argument
2424 * is set, set the nparam, greedy and params field in the template.
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002425 * *tpp is updated to point to the pointer to the first token after the
2426 * prototype.
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002427 *
2428 * The text values from any argument tokens are "stolen" and the
2429 * corresponding text fields set to NULL.
2430 */
2431static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2432{
2433 int nparam = 0;
2434 enum sparmflags flags;
2435 struct smac_param *params = NULL;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002436 bool err, done;
2437 bool greedy = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002438 Token **tn = *tpp;
2439 Token *t = *tn;
2440 Token *name;
2441
H. Peter Anvin (Intel)d4607842019-08-20 16:19:37 -07002442 /*
2443 * DO NOT skip whitespace here, or we won't be able to distinguish:
2444 *
2445 * %define foo (a,b) ; no arguments, (a,b) is the expansion
2446 * %define bar(a,b) ; two arguments, empty expansion
2447 *
2448 * This ambiguity was inherited from C.
2449 */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002450
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002451 if (!tok_is(t, '('))
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002452 goto finish;
2453
2454 if (tmpl) {
2455 Token *tx = t;
2456 Token **txpp = &tx;
2457 int sparam;
2458
2459 /* Count parameters first */
2460 sparam = parse_smacro_template(&txpp, NULL);
2461 if (!sparam)
2462 goto finish; /* No parameters, we're done */
2463 nasm_newn(params, sparam);
2464 }
2465
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002466 /* Skip leading paren */
2467 tn = &t->next;
2468 t = *tn;
2469
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002470 name = NULL;
2471 flags = 0;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07002472 err = done = false;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002473
2474 while (!done) {
2475 if (!t || !t->type) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002476 if (name || flags)
2477 nasm_nonfatal("`)' expected to terminate macro template");
2478 else
2479 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002480 break;
2481 }
2482
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002483 switch (t->type) {
2484 case TOK_ID:
2485 if (name)
2486 goto bad;
2487 name = t;
2488 break;
2489
2490 case TOK_OTHER:
2491 if (t->text[1])
2492 goto bad;
2493 switch (t->text[0]) {
2494 case '=':
2495 flags |= SPARM_EVAL;
2496 break;
2497 case '&':
2498 flags |= SPARM_STR;
2499 break;
2500 case '!':
2501 flags |= SPARM_NOSTRIP;
2502 break;
2503 case '+':
2504 flags |= SPARM_GREEDY;
2505 greedy = true;
2506 break;
2507 case ',':
2508 if (greedy)
2509 nasm_nonfatal("greedy parameter must be last");
2510 /* fall through */
2511 case ')':
2512 if (params) {
2513 if (name) {
2514 params[nparam].name = name->text;
2515 params[nparam].namelen = name->len;
2516 name->text = NULL;
2517 }
2518 params[nparam].flags = flags;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002519 }
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002520 nparam++;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002521 name = NULL;
2522 flags = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002523 done = t->text[0] == ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002524 break;
2525 default:
2526 goto bad;
2527 }
2528 break;
2529
2530 case TOK_WHITESPACE:
2531 break;
2532
2533 default:
2534 bad:
2535 if (!err) {
2536 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2537 err = true;
2538 }
2539 break;
2540 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002541
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002542 tn = &t->next;
2543 t = *tn;
2544 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002545
2546finish:
2547 while (t && t->type == TOK_WHITESPACE) {
2548 tn = &t->next;
2549 t = t->next;
2550 }
2551 *tpp = tn;
2552 if (tmpl) {
2553 tmpl->nparam = nparam;
2554 tmpl->greedy = greedy;
2555 tmpl->params = params;
2556 }
2557 return nparam;
2558}
2559
2560/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002561 * Common code for defining an smacro. The tmpl argument, if not NULL,
2562 * contains any macro parameters that aren't explicit arguments;
2563 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002564 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002565static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002566 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002567{
2568 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002569 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002570 Context *ctx;
2571 bool defining_alias = false;
2572 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002573
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002574 if (tmpl) {
2575 defining_alias = tmpl->alias;
2576 nparam = tmpl->nparam;
2577 }
2578
2579 while (1) {
2580 ctx = get_ctx(mname, &mname);
2581
H. Peter Anvind2354082019-08-27 16:38:48 -07002582 if (!smacro_defined(ctx, mname, nparam, &smac, casesense, true)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002583 /* Create a new macro */
2584 smtbl = ctx ? &ctx->localmac : &smacros;
2585 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2586 nasm_new(smac);
2587 smac->next = *smhead;
2588 *smhead = smac;
2589 break;
2590 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002591 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002592 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 /*
2594 * Some instances of the old code considered this a failure,
2595 * some others didn't. What is the right thing to do here?
2596 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002597 goto fail;
H. Peter Anvind2354082019-08-27 16:38:48 -07002598 } else if (!smac->alias || !do_aliases || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 /*
2600 * We're redefining, so we have to take over an
2601 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002602 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002604 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002605 break;
2606 } else if (smac->in_progress) {
2607 nasm_nonfatal("macro alias loop");
2608 goto fail;
2609 } else {
2610 /* It is an alias macro; follow the alias link */
2611 SMacro *s;
2612
2613 smac->in_progress = true;
2614 s = define_smacro(smac->expansion->text, casesense,
2615 expansion, tmpl);
2616 smac->in_progress = false;
2617 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002618 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002619 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002620
2621 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002622 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002623 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002624 smac->expand = smacro_expand_default;
2625 if (tmpl) {
2626 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002627 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002628 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002629 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002630 if (tmpl->expand)
2631 smac->expand = tmpl->expand;
2632 }
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002633 if (list_option('s')) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07002634 list_smacro_def((smac->alias ? PP_DEFALIAS : PP_DEFINE)
2635 + !casesense, ctx, smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002636 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002637 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002638
2639fail:
2640 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002641 if (tmpl)
2642 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002643 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002644}
2645
2646/*
2647 * Undefine an smacro
2648 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002649static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002650{
2651 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002652 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002653 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002654
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002655 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002656 smtbl = ctx ? &ctx->localmac : &smacros;
2657 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002658
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002659 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002660 /*
2661 * We now have a macro name... go hunt for it.
2662 */
2663 sp = smhead;
2664 while ((s = *sp) != NULL) {
2665 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002666 if (s->alias && !undefalias) {
H. Peter Anvind2354082019-08-27 16:38:48 -07002667 if (!do_aliases)
2668 continue;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002669 if (s->in_progress) {
2670 nasm_nonfatal("macro alias loop");
2671 } else {
2672 s->in_progress = true;
2673 undef_smacro(s->expansion->text, false);
2674 s->in_progress = false;
2675 }
2676 } else {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07002677 if (list_option('d'))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002678 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2679 ctx, s);
2680 *sp = s->next;
2681 free_smacro(s);
2682 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 } else {
2684 sp = &s->next;
2685 }
2686 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002687 }
2688}
2689
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002690/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002691 * Parse a mmacro specification.
2692 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002693static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002694{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002695 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002696 tline = skip_white(tline);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002697 tline = expand_id(tline);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002698 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002699 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002700 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002701 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002702
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002703#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002704 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002705#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002706 def->name = nasm_strdup(tline->text);
2707 def->plus = false;
2708 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002709 def->nparam_min = 0;
2710 def->nparam_max = 0;
2711
H. Peter Anvina26433d2008-07-16 14:40:01 -07002712 tline = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002713 tline = skip_white(tline);
2714 if (!tok_type(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002715 nasm_nonfatal("`%s' expects a parameter count", directive);
2716 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002717 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002718 if (tline && tok_is(tline->next, '-')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002719 tline = tline->next->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002720 if (tok_is(tline, '*')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 def->nparam_max = INT_MAX;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002722 } else if (!tok_type(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002723 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002725 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002726 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002727 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002728 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 }
2730 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002731 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002732 if (tline && tok_is(tline->next, '+')) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 tline = tline->next;
2734 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002735 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002736 if (tline && tok_type(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002737 !nasm_stricmp(tline->next->text, ".nolist")) {
2738 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002739 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002740 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741
H. Peter Anvina26433d2008-07-16 14:40:01 -07002742 /*
2743 * Handle default parameters.
2744 */
2745 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 def->dlist = tline->next;
2747 tline->next = NULL;
2748 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002749 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002750 def->dlist = NULL;
2751 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002752 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002753 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002754
H. Peter Anvin89cee572009-07-15 09:16:54 -04002755 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002756 !def->plus) {
2757 /*
2758 *!macro-defaults [on] macros with more default than optional parameters
2759 *! warns when a macro has more default parameters than optional parameters.
2760 *! See \k{mlmacdef} for why might want to disable this warning.
2761 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002762 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002763 "too many default macro parameters in macro `%s'", def->name);
2764 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002765
H. Peter Anvina26433d2008-07-16 14:40:01 -07002766 return true;
2767}
2768
2769
2770/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002771 * Decode a size directive
2772 */
2773static int parse_size(const char *str) {
2774 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002775 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002776 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002777 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002778 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002779}
2780
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002781/*
2782 * Process a preprocessor %pragma directive. Currently there are none.
2783 * Gets passed the token list starting with the "preproc" token from
2784 * "%pragma preproc".
2785 */
2786static void do_pragma_preproc(Token *tline)
2787{
2788 /* Skip to the real stuff */
2789 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002790 tline = skip_white(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002791 if (!tline)
2792 return;
2793
2794 (void)tline; /* Nothing else to do at present */
2795}
2796
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002797static bool is_macro_id(const Token *t)
2798{
2799 return t && (t->type == TOK_ID ||
2800 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2801}
2802
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002803static char *get_id(Token **tp, const char *dname)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002804{
2805 char *id;
2806 Token *t = *tp;
2807
2808 t = t->next; /* Skip directive */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002809 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002810 t = expand_id(t);
2811
2812 if (!is_macro_id(t)) {
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002813 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002814 return NULL;
2815 }
2816
2817 id = t->text;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002818 t = skip_white(t);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002819 *tp = t;
2820 return id;
2821}
2822
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002823/* Parse a %use package name and find the package. Set *err on syntax error. */
2824static const struct use_package *
2825get_use_pkg(Token *t, const char *dname, bool *err)
2826{
2827 char *id;
2828
2829 *err = false;
2830
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002831 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002832
2833 t = expand_smacro(t);
2834
2835 id = NULL;
2836 if (t) {
2837 if (t->type == TOK_ID) {
2838 id = t->text;
2839 } else if (t->type == TOK_STRING) {
2840 nasm_unquote_cstr(t->text, NULL);
2841 id = t->text;
2842 }
2843 }
2844
2845 if (!id) {
2846 nasm_nonfatal("`%s' expects a package name", dname);
2847 *err = true;
2848 return NULL;
2849 }
2850
2851 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002852 t = skip_white(t);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07002853 if (t)
2854 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
2855
2856 return nasm_find_use_package(id);
2857}
2858
Ed Beroset3ab3f412002-06-11 03:31:49 +00002859/**
2860 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002861 * Find out if a line contains a preprocessor directive, and deal
2862 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002863 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002864 * If a directive _is_ found, it is the responsibility of this routine
2865 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002866 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002867 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002868 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002869 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002870 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002871 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002872static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002873{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002874 enum preproc_token i;
2875 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002876 bool err;
2877 int nparam;
2878 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002879 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002880 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002881 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002882 char *p, *pp;
2883 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002884 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002885 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002886 Include *inc;
2887 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002888 Cond *cond;
2889 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002890 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002891 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002892 struct tokenval tokval;
2893 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002894 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002895 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002896 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002897 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002898
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002899 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002900 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002901
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002902 tline = skip_white(tline);
2903 if (!tline || !tok_type(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002904 (tline->text[0] && (tline->text[1] == '%' ||
2905 tline->text[1] == '$' ||
2906 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002908
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002909 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002910 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002911
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002912 casesense = true;
2913 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2914 casesense = false;
2915 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002916 }
2917
2918 /*
2919 * If we're in a non-emitting branch of a condition construct,
2920 * or walking to the end of an already terminated %rep block,
2921 * we should ignore all directives except for condition
2922 * directives.
2923 */
2924 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002925 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2926 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002927 return NO_DIRECTIVE_FOUND;
2928 }
2929
2930 /*
2931 * If we're defining a macro or reading a %rep block, we should
2932 * ignore all directives except for %macro/%imacro (which nest),
2933 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2934 * If we're in a %rep block, another %rep nests, so should be let through.
2935 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002936 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002937 i != PP_ENDMACRO && i != PP_ENDM &&
2938 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2939 return NO_DIRECTIVE_FOUND;
2940 }
2941
2942 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002943 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002944 nested_mac_count++;
2945 return NO_DIRECTIVE_FOUND;
2946 } else if (nested_mac_count > 0) {
2947 if (i == PP_ENDMACRO) {
2948 nested_mac_count--;
2949 return NO_DIRECTIVE_FOUND;
2950 }
2951 }
2952 if (!defining->name) {
2953 if (i == PP_REP) {
2954 nested_rep_count++;
2955 return NO_DIRECTIVE_FOUND;
2956 } else if (nested_rep_count > 0) {
2957 if (i == PP_ENDREP) {
2958 nested_rep_count--;
2959 return NO_DIRECTIVE_FOUND;
2960 }
2961 }
2962 }
2963 }
2964
H. Peter Anvin4169a472007-09-12 01:29:43 +00002965 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002966 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002967 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002969
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002970 case PP_PRAGMA:
2971 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002972 * %pragma namespace options...
2973 *
2974 * The namespace "preproc" is reserved for the preprocessor;
2975 * all other namespaces generate a [pragma] assembly directive.
2976 *
2977 * Invalid %pragmas are ignored and may have different
2978 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002979 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002980 t = tline;
2981 tline = tline->next;
2982 t->next = NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002983 tline = zap_white(expand_smacro(tline));
2984 if (tok_type(tline, TOK_ID)) {
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002985 if (!nasm_stricmp(tline->text, "preproc")) {
2986 /* Preprocessor pragma */
2987 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002988 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002989 } else {
2990 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002991
2992 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002993 for (t = tline; t->next; t = t->next)
2994 ;
2995 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002996
2997 /* Prepend "[pragma " */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07002998 t = new_White(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002999 t = new_Token(t, TOK_ID, "pragma", 6);
3000 t = new_Token(t, TOK_OTHER, "[", 1);
3001 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003002 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003003 }
3004 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003005 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07003006
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 case PP_STACKSIZE:
3008 /* Directive to tell NASM what the default stack size is. The
3009 * default is for a 16-bit stack, and this can be overriden with
3010 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003012 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003014 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003015 }
3016 if (nasm_stricmp(tline->text, "flat") == 0) {
3017 /* All subsequent ARG directives are for a 32-bit stack */
3018 StackSize = 4;
3019 StackPointer = "ebp";
3020 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003021 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003022 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
3023 /* All subsequent ARG directives are for a 64-bit stack */
3024 StackSize = 8;
3025 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03003026 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08003027 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 } else if (nasm_stricmp(tline->text, "large") == 0) {
3029 /* All subsequent ARG directives are for a 16-bit stack,
3030 * far function call.
3031 */
3032 StackSize = 2;
3033 StackPointer = "bp";
3034 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003035 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 } else if (nasm_stricmp(tline->text, "small") == 0) {
3037 /* All subsequent ARG directives are for a 16-bit stack,
3038 * far function call. We don't support near functions.
3039 */
3040 StackSize = 2;
3041 StackPointer = "bp";
3042 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003043 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003045 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003047 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 case PP_ARG:
3050 /* TASM like ARG directive to define arguments to functions, in
3051 * the following form:
3052 *
3053 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
3054 */
3055 offset = ArgOffset;
3056 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003057 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003059
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003061 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003062 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003063 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003064 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 }
3066 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003067
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 /* Find the argument size type */
3069 tline = tline->next;
3070 if (!tline || tline->type != TOK_OTHER
3071 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003072 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003073 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 }
3075 tline = tline->next;
3076 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003077 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003078 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003080
H. Peter Anvine2c80182005-01-15 22:15:51 +00003081 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003082 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003083 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003084 size = parse_size(tt->text);
3085 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003086 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003088 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 }
3090 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003091
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003092 /* Round up to even stack slots */
3093 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003094
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 /* Now define the macro for the argument */
3096 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
3097 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003098 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003100
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003102 tline = skip_white(tline->next);
3103 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003104 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003105 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003106
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 case PP_LOCAL:
3108 /* TASM like LOCAL directive to define local variables for a
3109 * function, in the following form:
3110 *
3111 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
3112 *
3113 * The '= LocalSize' at the end is ignored by NASM, but is
3114 * required by TASM to define the local parameter size (and used
3115 * by the TASM macro package).
3116 */
3117 offset = LocalOffset;
3118 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003119 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003121
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 /* Find the argument name */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003123 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003125 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003126 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 }
3128 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003129
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 /* Find the argument size type */
3131 tline = tline->next;
3132 if (!tline || tline->type != TOK_OTHER
3133 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003134 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003135 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 }
3137 tline = tline->next;
3138 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003139 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003140 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003141 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003142
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003144 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003146 size = parse_size(tt->text);
3147 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003148 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003150 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003151 }
3152 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003153
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003154 /* Round up to even stack slots */
3155 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003156
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003157 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003158
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003159 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3161 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003162 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003163
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 /* Now define the assign to setup the enter_c macro correctly */
3165 snprintf(directive, sizeof(directive),
3166 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003167 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003168
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 /* Move to the next argument in the list */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003170 tline = skip_white(tline->next);
3171 } while (tok_is(tline, ','));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003172 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003173 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003174
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 case PP_CLEAR:
3176 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003177 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 free_macros();
3179 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003180 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003181
H. Peter Anvin418ca702008-05-30 10:42:30 -07003182 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003183 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003184 t = skip_white(t);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003185 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003186 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003187 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003188 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003189 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003190 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003191 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003193 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003194 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003195 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003196 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003197
3198 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003199 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003200 t = skip_white(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003201
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003202 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003203 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003204 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003205 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003206 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003207 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003208 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003209 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003210 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003211 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003212 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003214 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003215 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003216 (pp_mode == PP_DEPS)
3217 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003218 if (!inc->fp) {
3219 /* -MG given but file not found */
3220 nasm_free(inc);
3221 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003222 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003223 inc->lineno = src_set_linnum(0);
3224 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003225 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003226 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003227 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003228 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003229 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003230
H. Peter Anvind2456592008-06-19 15:04:18 -07003231 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003232 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003233 const struct use_package *pkg;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003234 bool err;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003235
H. Peter Anvindd88aa92019-09-12 19:39:48 -07003236 pkg = get_use_pkg(tline->next, dname, &err);
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003237 if (err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003238 goto done;
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003239
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003240 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003241 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003242 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003243 /*
3244 * Not already included, go ahead and include it.
3245 * Treat it as an include file for the purpose of
3246 * producing a listing.
3247 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003248 use_loaded[pkg->index] = true;
3249 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003250 nasm_new(inc);
3251 inc->next = istk;
3252 inc->fname = src_set_fname(NULL);
3253 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003254 inc->nolist = !list_option('b') || istk->nolist;
3255 istk = inc;
3256 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003257 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003258 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003259 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003260 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003262 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003264 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003266 if (tline) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003267 if (!tok_type(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003268 nasm_nonfatal("`%s' expects a context identifier",
3269 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003270 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003271 }
3272 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003273 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003274 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 p = nasm_strdup(tline->text);
3276 } else {
3277 p = NULL; /* Anonymous */
3278 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003279
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003281 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003282 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003283 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003284 ctx->name = p;
3285 ctx->number = unique++;
3286 cstk = ctx;
3287 } else {
3288 /* %pop or %repl */
3289 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003290 nasm_nonfatal("`%s': context stack is empty",
3291 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003292 } else if (i == PP_POP) {
3293 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003294 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003295 "expected %s",
3296 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003297 else
3298 ctx_pop();
3299 } else {
3300 /* i == PP_REPL */
3301 nasm_free(cstk->name);
3302 cstk->name = p;
3303 p = NULL;
3304 }
3305 nasm_free(p);
3306 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003307 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003308 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003309 severity = ERR_FATAL;
3310 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003311 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003312 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003313 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003314 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003315 /*!
3316 *!user [on] %warning directives
3317 *! controls output of \c{%warning} directives (see \k{pperror}).
3318 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003319 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003320 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003321
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003322issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003323 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003324 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003325 tline->next = expand_smacro(tline->next);
3326 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003327 tline = skip_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 t = tline ? tline->next : NULL;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003329 t = skip_white(t);
3330 if (tok_type(tline, TOK_STRING) && !t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003331 /* The line contains only a quoted string */
3332 p = tline->text;
3333 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003334 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003335 } else {
3336 /* Not a quoted string, or more than a quoted string */
3337 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003338 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003339 nasm_free(p);
3340 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003341 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003342 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003343
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003344 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003345 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003346 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003347 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003348 j = if_condition(tline->next, i);
3349 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003350 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003351 cond = nasm_malloc(sizeof(Cond));
3352 cond->next = istk->conds;
3353 cond->state = j;
3354 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003355 if(istk->mstk.mstk)
3356 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003357 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003358
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003359 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003360 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003361 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003362 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003363 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003364 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003365 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003366
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003367 case COND_DONE:
3368 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003369 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003370
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 case COND_ELSE_TRUE:
3372 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003373 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003374 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003375 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003377
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 case COND_IF_FALSE:
3379 /*
3380 * IMPORTANT: In the case of %if, we will already have
3381 * called expand_mmac_params(); however, if we're
3382 * processing an %elif we must have been in a
3383 * non-emitting mode, which would have inhibited
3384 * the normal invocation of expand_mmac_params().
3385 * Therefore, we have to do it explicitly here.
3386 */
3387 j = if_condition(expand_mmac_params(tline->next), i);
3388 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003389 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003392 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003393
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 case PP_ELSE:
3395 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003396 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003397 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003398 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003399 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003400 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003401 case COND_IF_TRUE:
3402 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003403 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003404 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003405
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003406 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003407 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003408
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003409 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003410 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003411 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003412
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003413 case COND_ELSE_TRUE:
3414 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003415 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003416 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003417 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003418 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003419 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003420 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003421
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 case PP_ENDIF:
3423 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003424 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003425 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003426 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003427 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003428 cond = istk->conds;
3429 istk->conds = cond->next;
3430 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003431 if(istk->mstk.mstk)
3432 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003433 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003434
H. Peter Anvin8b262472019-02-26 14:00:54 -08003435 case PP_RMACRO:
3436 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003437 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003438 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003439 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003440 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003441 if (i == PP_RMACRO)
3442 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003443 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003444 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003445 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003446 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003447
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003448 src_get(&defining->xline, &defining->fname);
3449
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003450 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3451 while (mmac) {
3452 if (!strcmp(mmac->name, defining->name) &&
3453 (mmac->nparam_min <= defining->nparam_max
3454 || defining->plus)
3455 && (defining->nparam_min <= mmac->nparam_max
3456 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003457 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003458 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003459 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003460 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003461 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003462 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003463 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003464
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 case PP_ENDM:
3466 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003467 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003468 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003469 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003470 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003471 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3472 defining->next = *mmhead;
3473 *mmhead = defining;
3474 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003475 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003476
H. Peter Anvin89cee572009-07-15 09:16:54 -04003477 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003478 /*
3479 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003480 * macro-end marker for a macro with a name. Then we
3481 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003482 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003483 list_for_each(l, istk->expansion)
3484 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003485 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003486
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003487 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003488 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003489 * Remove all conditional entries relative to this
3490 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003491 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003492 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3493 cond = istk->conds;
3494 istk->conds = cond->next;
3495 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003496 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003497 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003498 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003499 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003500 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003501 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003502
H. Peter Anvina26433d2008-07-16 14:40:01 -07003503 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003504 casesense = false;
3505 /* fall through */
3506 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003507 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003508 MMacro **mmac_p;
3509 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003510
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003511 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003512 spec.casesense = casesense;
3513 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003514 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003515 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003516 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3517 while (mmac_p && *mmac_p) {
3518 mmac = *mmac_p;
3519 if (mmac->casesense == spec.casesense &&
3520 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3521 mmac->nparam_min == spec.nparam_min &&
3522 mmac->nparam_max == spec.nparam_max &&
3523 mmac->plus == spec.plus) {
3524 *mmac_p = mmac->next;
3525 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003526 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003527 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003528 }
3529 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003530 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003531 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003532 }
3533
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 case PP_ROTATE:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003535 while (tok_white(tline->next))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003537 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003538 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003539 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 return DIRECTIVE_FOUND;
3541 }
3542 t = expand_smacro(tline->next);
3543 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003544 pps.tptr = tline = t;
3545 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 tokval.t_type = TOKEN_INVALID;
3547 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003548 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 free_tlist(tline);
3550 if (!evalresult)
3551 return DIRECTIVE_FOUND;
3552 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003553 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003555 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003556 return DIRECTIVE_FOUND;
3557 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003558 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003559 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003560 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003561 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003562 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003563 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003564 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003565
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003566 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003567 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003568 rotate += mmac->nparam;
3569
3570 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003571 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003572 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003573
H. Peter Anvine2c80182005-01-15 22:15:51 +00003574 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003575 {
3576 MMacro *tmp_defining;
3577
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003578 nolist = false;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003579 tline = skip_white(tline->next);
3580 if (tok_type(tline, TOK_ID) &&
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003582 nolist = !list_option('f') || istk->nolist;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003583 tline = skip_white(tline->next);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003585
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003587 pps.tptr = expand_smacro(tline);
3588 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003590 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003592 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003593 if (!evalresult)
3594 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003595 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003596 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003598 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003599 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003601 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003602 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003603 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3604 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003605 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003606 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003607 /*!
3608 *!negative-rep [on] regative %rep count
3609 *! warns about negative counts given to the \c{%rep}
3610 *! preprocessor directive.
3611 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003612 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003613 "negative `%%rep' count: %"PRId64, count);
3614 count = 0;
3615 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003616 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003617 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003619 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003620 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003622 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003623 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003624 defining->nolist = nolist;
3625 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003626 defining->mstk = istk->mstk;
3627 defining->dstk.mstk = tmp_defining;
3628 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003629 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003630 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003631 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003632
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003634 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003635 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003636 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003638
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 /*
3640 * Now we have a "macro" defined - although it has no name
3641 * and we won't be entering it in the hash tables - we must
3642 * push a macro-end marker for it on to istk->expansion.
3643 * After that, it will take care of propagating itself (a
3644 * macro-end marker line for a macro which is really a %rep
3645 * block will cause the macro to be re-expanded, complete
3646 * with another macro-end marker to ensure the process
3647 * continues) until the whole expansion is forcibly removed
3648 * from istk->expansion by a %exitrep.
3649 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003650 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003651 l->next = istk->expansion;
3652 l->finishes = defining;
3653 l->first = NULL;
3654 istk->expansion = l;
3655
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003656 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003657
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003658 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003659 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003660 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003661
H. Peter Anvine2c80182005-01-15 22:15:51 +00003662 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003663 /*
3664 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003665 * macro-end marker for a macro with no name. Then we set
3666 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003667 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003668 list_for_each(l, istk->expansion)
3669 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003670 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003671
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003672 if (l)
3673 l->finishes->in_progress = 1;
3674 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003675 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003676 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003677
H. Peter Anvin8b262472019-02-26 14:00:54 -08003678 case PP_DEFINE:
3679 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003680 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003681 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003682 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003683 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003684
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003685 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003686 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003687
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003688 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003689 lastp = &tline->next;
3690 nparam = parse_smacro_template(&lastp, &tmpl);
3691 tline = *lastp;
3692 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003693
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003694 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003695 macro_start = tline;
3696 if (!is_macro_id(macro_start)) {
3697 nasm_nonfatal("`%s' expects a macro identifier to alias",
3698 dname);
3699 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003701 tt = macro_start->next;
3702 macro_start->next = NULL;
3703 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003704 tline = skip_white(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003705 if (tline && tline->type) {
3706 nasm_warn(WARN_OTHER,
3707 "trailing garbage after aliasing identifier ignored");
3708 }
3709 free_tlist(tt);
3710 tmpl.alias = true;
3711 } else {
3712 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003713 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003714 tline = expand_smacro(tline);
3715
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003716 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003717 macro_start = NULL;
3718 t = tline;
3719 while (t) {
3720 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003721 for (i = 0; i < nparam; i++) {
3722 if ((size_t)tmpl.params[i].namelen == t->len &&
3723 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3724 t->type = tok_smac_param(i);
3725 break;
3726 }
3727 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003728 }
3729 tt = t->next;
3730 t->next = macro_start;
3731 macro_start = t;
3732 t = tt;
3733 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003735
H. Peter Anvine2c80182005-01-15 22:15:51 +00003736 /*
3737 * Good. We now have a macro name, a parameter count, and a
3738 * token list (in reverse order) for an expansion. We ought
3739 * to be OK just to create an SMacro, store it, and let
3740 * free_tlist have the rest of the line (which we have
3741 * carefully re-terminated after chopping off the expansion
3742 * from the end).
3743 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003744 define_smacro(mname, casesense, macro_start, &tmpl);
3745 break;
3746 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003747
H. Peter Anvine2c80182005-01-15 22:15:51 +00003748 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003749 case PP_UNDEFALIAS:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003750 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003751 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003752 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003753 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003754
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003755 undef_smacro(mname, i == PP_UNDEFALIAS);
3756 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003757
H. Peter Anvin8b262472019-02-26 14:00:54 -08003758 case PP_DEFSTR:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003759 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003760 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003761
H. Peter Anvin9e200162008-06-04 17:23:14 -07003762 last = tline;
3763 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003764 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003765
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003766 tline = zap_white(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003767 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003768 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003769 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003770
3771 /*
3772 * We now have a macro name, an implicit parameter count of
3773 * zero, and a string token to use as an expansion. Create
3774 * and store an SMacro.
3775 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003776 define_smacro(mname, casesense, macro_start, NULL);
3777 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003778
H. Peter Anvin8b262472019-02-26 14:00:54 -08003779 case PP_DEFTOK:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003780 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003781 goto done;
3782
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003783 last = tline;
3784 tline = expand_smacro(tline->next);
3785 last->next = NULL;
3786
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003787 t = skip_white(tline);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003788 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003789 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003790 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003791 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003792 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003793 }
3794
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003795 /*
3796 * Convert the string to a token stream. Note that smacros
3797 * are stored with the token stream reversed, so we have to
3798 * reverse the output of tokenize().
3799 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003800 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003801 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003802
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003803 /*
3804 * We now have a macro name, an implicit parameter count of
3805 * zero, and a numeric token to use as an expansion. Create
3806 * and store an SMacro.
3807 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003808 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003809 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003810 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003811
H. Peter Anvin418ca702008-05-30 10:42:30 -07003812 case PP_PATHSEARCH:
3813 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003814 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003815
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003816 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003817 goto done;
3818
H. Peter Anvin418ca702008-05-30 10:42:30 -07003819 last = tline;
3820 tline = expand_smacro(tline->next);
3821 last->next = NULL;
3822
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003823 t = skip_white(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003824 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003825 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003826 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003827 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003828 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003829 }
3830 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003831 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003832 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003833 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003834 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003835
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003836 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003837 if (!found_path)
3838 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003839 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003840
3841 /*
3842 * We now have a macro name, an implicit parameter count of
3843 * zero, and a string token to use as an expansion. Create
3844 * and store an SMacro.
3845 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003846 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003847 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003848 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003849 }
3850
H. Peter Anvine2c80182005-01-15 22:15:51 +00003851 case PP_STRLEN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003852 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003853 goto done;
3854
H. Peter Anvine2c80182005-01-15 22:15:51 +00003855 last = tline;
3856 tline = expand_smacro(tline->next);
3857 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003858
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003859 t = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003860 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003861 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003862 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003863 free_tlist(tline);
3864 free_tlist(origline);
3865 return DIRECTIVE_FOUND;
3866 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003867
H. Peter Anvin8b262472019-02-26 14:00:54 -08003868 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003869
H. Peter Anvine2c80182005-01-15 22:15:51 +00003870 /*
3871 * We now have a macro name, an implicit parameter count of
3872 * zero, and a numeric token to use as an expansion. Create
3873 * and store an SMacro.
3874 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003875 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003876 free_tlist(tline);
3877 free_tlist(origline);
3878 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003879
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003880 case PP_STRCAT:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003881 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003882 goto done;
3883
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003884 last = tline;
3885 tline = expand_smacro(tline->next);
3886 last->next = NULL;
3887
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003888 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003889 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003890 switch (t->type) {
3891 case TOK_WHITESPACE:
3892 break;
3893 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003894 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003895 break;
3896 case TOK_OTHER:
3897 if (!strcmp(t->text, ",")) /* permit comma separators */
3898 break;
3899 /* else fall through */
3900 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003901 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003902 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003903 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003904 }
3905 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003906
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003907 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003908 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003909 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003910 memcpy(p, t->text, t->len);
3911 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003912 }
3913 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003914
3915 /*
3916 * We now have a macro name, an implicit parameter count of
3917 * zero, and a numeric token to use as an expansion. Create
3918 * and store an SMacro.
3919 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003920 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003921 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003922 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003923 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003924 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003925
H. Peter Anvine2c80182005-01-15 22:15:51 +00003926 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003927 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003928 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003929 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003930
H. Peter Anvina039fcd2019-09-12 19:27:42 -07003931 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003932 goto done;
3933
H. Peter Anvine2c80182005-01-15 22:15:51 +00003934 last = tline;
3935 tline = expand_smacro(tline->next);
3936 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003937
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003938 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003939 t = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003940
3941 t = skip_white(t);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003942
H. Peter Anvine2c80182005-01-15 22:15:51 +00003943 /* t should now point to the string */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003944 if (!tok_type(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003945 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003946 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003947 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003948 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003949
H. Peter Anvin8b262472019-02-26 14:00:54 -08003950 pps.tptr = t->next;
3951 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003952 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003953 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003954 if (!evalresult) {
3955 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003956 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003957 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003958 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003959 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003960 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003961 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003962 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003963
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07003964 pps.tptr = skip_white(pps.tptr);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003965 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003966 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003967 } else {
3968 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003969 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003970 if (!evalresult) {
3971 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003972 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003973 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003974 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003975 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003976 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003977 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003978 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003979 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003980
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003981 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003982
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003983 /* make start and count being in range */
3984 if (start < 0)
3985 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003986 if (count < 0)
3987 count = len + count + 1 - start;
3988 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003989 count = len - start;
3990 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003991 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003992
H. Peter Anvin8b262472019-02-26 14:00:54 -08003993 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003994 macro_start->len = count;
3995 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3996 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003997
H. Peter Anvine2c80182005-01-15 22:15:51 +00003998 /*
3999 * We now have a macro name, an implicit parameter count of
4000 * zero, and a numeric token to use as an expansion. Create
4001 * and store an SMacro.
4002 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004003 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004004 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004005 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07004006 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004007
H. Peter Anvin8b262472019-02-26 14:00:54 -08004008 case PP_ASSIGN:
H. Peter Anvina039fcd2019-09-12 19:27:42 -07004009 if (!(mname = get_id(&tline, dname)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004010 goto done;
4011
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 last = tline;
4013 tline = expand_smacro(tline->next);
4014 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004015
H. Peter Anvin8b262472019-02-26 14:00:54 -08004016 pps.tptr = tline;
4017 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004019 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004020 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004021 if (!evalresult)
4022 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004023
H. Peter Anvine2c80182005-01-15 22:15:51 +00004024 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004025 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004026
H. Peter Anvine2c80182005-01-15 22:15:51 +00004027 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004028 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004029 free_tlist(origline);
4030 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004031 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004032
H. Peter Anvin8b262472019-02-26 14:00:54 -08004033 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00004034
H. Peter Anvine2c80182005-01-15 22:15:51 +00004035 /*
4036 * We now have a macro name, an implicit parameter count of
4037 * zero, and a numeric token to use as an expansion. Create
4038 * and store an SMacro.
4039 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004040 define_smacro(mname, casesense, macro_start, NULL);
4041 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004042
H. Peter Anvind2354082019-08-27 16:38:48 -07004043 case PP_ALIASES:
4044 tline = tline->next;
4045 tline = expand_smacro(tline);
4046 do_aliases = pp_get_boolean_option(tline, do_aliases);
4047 break;
4048
H. Peter Anvine2c80182005-01-15 22:15:51 +00004049 case PP_LINE:
4050 /*
4051 * Syntax is `%line nnn[+mmm] [filename]'
4052 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004053 if (unlikely(pp_noline))
4054 goto done;
4055
H. Peter Anvine2c80182005-01-15 22:15:51 +00004056 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004057 tline = skip_white(tline);
4058 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004059 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004060 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004061 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004062 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004063 m = 1;
4064 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004065 if (tok_is(tline, '+')) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004066 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004067 if (!tok_type(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07004068 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004069 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004070 }
H. Peter Anvin70055962007-10-11 00:05:31 -07004071 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004072 tline = tline->next;
4073 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004074 tline = skip_white(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004075 src_set_linnum(k);
4076 istk->lineinc = m;
4077 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07004078 char *fname = detoken(tline, false);
4079 src_set_fname(fname);
4080 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004081 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07004082 break;
4083 }
4084
4085done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004086 free_tlist(origline);
4087 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004088}
4089
4090/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00004091 * Ensure that a macro parameter contains a condition code and
4092 * nothing else. Return the condition code index if so, or -1
4093 * otherwise.
4094 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004095static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004096{
H. Peter Anvin76690a12002-04-30 20:52:49 +00004097 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004098
H. Peter Anvin25a99342007-09-22 17:45:45 -07004099 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004100 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07004101
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004102 t = skip_white(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03004103 if (!t)
4104 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004105 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004106 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004107 tt = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004108 tt = skip_white(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00004109 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004110 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004111
Cyrill Gorcunov19456392012-05-02 00:18:56 +04004112 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00004113}
4114
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004115/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004116 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004117 * pasting, if @handle_explicit passed then explicit pasting
4118 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004119 * The @m array can contain a series of token types which are
4120 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004121 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004122static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004123 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07004124{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004125 Token *tok, *t, *next, **prev_next, **prev_nonspace;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004126 bool pasted = false;
4127 char *buf, *p;
4128 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07004129
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004130 /*
4131 * The last token before pasting. We need it
4132 * to be able to connect new handled tokens.
4133 * In other words if there were a tokens stream
4134 *
4135 * A -> B -> C -> D
4136 *
4137 * and we've joined tokens B and C, the resulting
4138 * stream should be
4139 *
4140 * A -> BC -> D
4141 */
4142 tok = *head;
4143 prev_next = NULL;
4144
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004145 if (!tok_white(tok) && !tok_type(tok, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004146 prev_nonspace = head;
4147 else
4148 prev_nonspace = NULL;
4149
4150 while (tok && (next = tok->next)) {
4151
4152 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004153 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004154 /* Zap redundant whitespaces */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004155 next = zap_white(next);
H. Peter Anvind784a082009-04-20 14:01:18 -07004156 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004157
4158 case TOK_PASTE:
4159 /* Explicit pasting */
4160 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004161 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004162
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004163 /* Left pasting token is start of line */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004164 if (!prev_nonspace) {
4165 nasm_nonfatal("No lvalue found on pasting");
4166 tok = delete_Token(tok);
4167 break;
4168 }
4169
4170 pasted = true;
4171 t = *prev_nonspace;
4172
4173 /* Delete leading whitespace */
4174 next = zap_white(t->next);
4175
4176 /* Delete the %+ token itself */
4177 nasm_assert(next == tok);
4178 next = delete_Token(next);
4179
4180 /* Delete trailing whitespace */
4181 next = zap_white(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004182
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004183 /*
4184 * No ending token, this might happen in two
4185 * cases
4186 *
4187 * 1) There indeed no right token at all
4188 * 2) There is a bare "%define ID" statement,
4189 * and @ID does expand to whitespace.
4190 *
4191 * So technically we need to do a grammar analysis
4192 * in another stage of parsing, but for now lets don't
4193 * change the behaviour people used to. Simply allow
4194 * whitespace after paste token.
4195 */
4196 if (!next) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004197 tok = NULL; /* End of line */
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004198 break;
4199 }
4200
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004201 p = buf = nasm_malloc(t->len + next->len + 1);
4202 p = mempcpy(p, t->text, t->len);
4203 p = mempcpy(p, next->text, next->len);
4204 *p = '\0';
4205 delete_Token(t);
4206 t = tokenize(buf);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004207 nasm_free(buf);
4208
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004209 if (unlikely(!t)) {
4210 /*
4211 * No output at all? Replace with a single whitespace.
4212 * This should never happen.
4213 */
4214 t = new_White(NULL);
4215 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004216
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004217 /* We want to restart from the head of the pasted token */
4218 *prev_nonspace = tok = t;
4219 while (t->next)
4220 t = t->next; /* Find the last token produced */
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004221
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004222 /* Delete the second token and attach to the end of the list */
4223 t->next = delete_Token(next);
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004224 break;
4225
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004226 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004227 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004228 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004229 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4230 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004231
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004232 len = 0;
4233 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004234 len += next->len;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004235 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004236 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004237
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004238 /* No match or no text to process */
4239 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004240 break;
4241
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004242 len += tok->len;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004243 p = buf = nasm_malloc(len + 1);
4244
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004245 p = mempcpy(p, tok->text, tok->len);
Adam Majer1a069432017-07-25 11:12:35 +02004246 tok = delete_Token(tok);
4247
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004248 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004249 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004250 p = mempcpy(p, tok->text, tok->len);
4251 tok = delete_Token(tok);
Adam Majer1a069432017-07-25 11:12:35 +02004252 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004253 tok = delete_Token(tok);
4254 }
4255
4256 tok = tokenize(buf);
4257 nasm_free(buf);
4258
4259 if (prev_next)
4260 *prev_next = tok;
4261 else
4262 *head = tok;
4263
4264 /*
4265 * Connect pasted into original stream,
4266 * ie A -> new-tokens -> B
4267 */
4268 while (tok && tok->next)
4269 tok = tok->next;
4270 tok->next = next;
4271
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004272 pasted = true;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004273
4274 /* Restart from pasted tokens head */
4275 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004276 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004277
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004278 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004279 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004280
4281 prev_next = &tok->next;
4282
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004283 if (tok->next && !tok_white(tok->next) &&
4284 !tok_type(tok->next, TOK_PASTE))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004285 prev_nonspace = prev_next;
4286
4287 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004288 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004289
4290 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004291}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004292
4293/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004294 * Computes the proper rotation of mmacro parameters
4295 */
4296static int mmac_rotate(const MMacro *mac, unsigned int n)
4297{
4298 if (--n < mac->nparam)
4299 n = (n + mac->rotate) % mac->nparam;
4300
4301 return n+1;
4302}
4303
4304/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004305 * expands to a list of tokens from %{x:y}
4306 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004307static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004308{
4309 Token *t = tline, **tt, *tm, *head;
4310 char *pos;
4311 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004312
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004313 pos = strchr(tline->text, ':');
4314 nasm_assert(pos);
4315
4316 lst = atoi(pos + 1);
4317 fst = atoi(tline->text + 1);
4318
4319 /*
4320 * only macros params are accounted so
4321 * if someone passes %0 -- we reject such
4322 * value(s)
4323 */
4324 if (lst == 0 || fst == 0)
4325 goto err;
4326
4327 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004328 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4329 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004330 goto err;
4331
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004332 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4333 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004334
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004335 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004336 fst--, lst--;
4337
4338 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004339 * It will be at least one token. Note we
4340 * need to scan params until separator, otherwise
4341 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004342 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004343 j = (fst + mac->rotate) % mac->nparam;
4344 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004345 if (!tm)
4346 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004347 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004348 tt = &head->next, tm = tm->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004349 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004350 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004351 *tt = t, tt = &t->next, tm = tm->next;
4352 }
4353
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004354 if (fst < lst) {
4355 for (i = fst + 1; i <= lst; i++) {
4356 t = new_Token(NULL, TOK_OTHER, ",", 0);
4357 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004358 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004359 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004360 while (tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004361 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004362 *tt = t, tt = &t->next, tm = tm->next;
4363 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004364 }
4365 } else {
4366 for (i = fst - 1; i >= lst; i--) {
4367 t = new_Token(NULL, TOK_OTHER, ",", 0);
4368 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004369 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004370 tm = mac->params[j+1];
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004371 while (!tok_isnt(tm, ',')) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004372 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004373 *tt = t, tt = &t->next, tm = tm->next;
4374 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004375 }
4376 }
4377
4378 *last = tt;
4379 return head;
4380
4381err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004382 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004383 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004384 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004385}
4386
H. Peter Anvin76690a12002-04-30 20:52:49 +00004387/*
4388 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004389 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004390 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004391 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004392static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004393{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004394 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004395 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004396 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004397
4398 tail = &thead;
4399 thead = NULL;
4400
H. Peter Anvine2c80182005-01-15 22:15:51 +00004401 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004402 bool change;
4403 Token *t = tline;
4404 char *text = t->text;
4405 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004406
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004407 tline = tline->next;
4408 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004409
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004410 switch (type) {
4411 case TOK_PREPROC_ID:
4412 {
4413 Token *tt = NULL;
4414
4415 change = false;
4416
4417 if (!text || !text[0])
4418 break;
4419 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4420 ((text[1] == '+' || text[1] == '-') && text[2])))
4421 break;
4422
4423 change = true;
4424
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004425 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004426 nasm_nonfatal("`%s': not in a macro call", text);
4427 text = NULL;
4428 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004429 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004430
4431 if (strchr(text, ':')) {
4432 /*
4433 * seems we have a parameters range here
4434 */
4435 Token *head, **last;
4436 head = expand_mmac_params_range(mac, t, &last);
4437 if (head) {
4438 *tail = head;
4439 *last = tline;
4440 text = NULL;
4441 }
4442 break;
4443 }
4444
4445 switch (text[1]) {
4446 /*
4447 * We have to make a substitution of one of the
4448 * forms %1, %-1, %+1, %%foo, %0, %00.
4449 */
4450 case '0':
4451 if (!text[2]) {
4452 type = TOK_NUMBER;
4453 text = nasm_asprintf("%d", mac->nparam);
4454 break;
4455 }
4456 if (text[2] != '0' || text[3])
4457 goto invalid;
4458 /* a possible captured label == mac->params[0] */
4459 /* fall through */
4460 default:
4461 {
4462 unsigned long n;
4463 char *ep;
4464
4465 n = strtoul(text + 1, &ep, 10);
4466 if (unlikely(*ep))
4467 goto invalid;
4468
4469 if (n <= mac->nparam) {
4470 n = mmac_rotate(mac, n);
4471 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4472 }
4473 text = NULL;
4474 break;
4475 }
4476 case '%':
4477 type = TOK_ID;
4478 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4479 break;
4480 case '-':
4481 case '+':
4482 {
4483 int cc;
4484 unsigned long n;
4485 char *ep;
4486
4487 text = NULL;
4488
4489 n = strtoul(t->text + 2, &ep, 10);
4490 if (unlikely(*ep))
4491 goto invalid;
4492
4493 if (n && n < mac->nparam) {
4494 n = mmac_rotate(mac, n);
4495 tt = mac->params[n];
4496 }
4497 cc = find_cc(tt);
4498 if (cc == -1) {
4499 nasm_nonfatal("macro parameter `%s' is not a condition code",
H. Peter Anvind2354082019-08-27 16:38:48 -07004500 t->text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004501 text = NULL;
4502 break;
4503 }
4504
4505 type = TOK_ID;
4506 if (text[1] == '-') {
4507 int ncc = inverse_ccs[cc];
4508 if (unlikely(ncc == -1)) {
4509 nasm_nonfatal("condition code `%s' is not invertible",
4510 conditions[cc]);
4511 break;
4512 }
4513 cc = ncc;
4514 }
4515 text = nasm_strdup(conditions[cc]);
4516 break;
4517 }
4518
4519 invalid:
4520 nasm_nonfatal("invalid macro parameter: `%s'", text);
4521 text = NULL;
4522 break;
4523 }
4524 break;
4525 }
4526
4527 case TOK_PREPROC_Q:
4528 if (mac) {
4529 type = TOK_ID;
4530 text = nasm_strdup(mac->iname);
4531 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004532 } else {
4533 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004534 }
4535 break;
4536
4537 case TOK_PREPROC_QQ:
4538 if (mac) {
4539 type = TOK_ID;
4540 text = nasm_strdup(mac->name);
4541 change = true;
H. Peter Anvin (Intel)68075f82019-08-20 12:28:05 -07004542 } else {
4543 change = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004544 }
4545 break;
4546
4547 case TOK_INDIRECT:
4548 {
4549 Token *tt;
4550
4551 tt = tokenize(t->text);
4552 tt = expand_mmac_params(tt);
4553 tt = expand_smacro(tt);
4554 /* Why dup_tlist() here? We should own tt... */
4555 dup_tlist(tt, &tail);
4556 text = NULL;
4557 change = true;
4558 break;
4559 }
4560
4561 default:
4562 change = false;
4563 break;
4564 }
4565
4566 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004567 if (!text) {
4568 delete_Token(t);
4569 } else {
4570 *tail = t;
4571 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004572 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004573 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004574 t->type = type;
4575 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004576 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004577 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004578 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004579 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004580 tail = &t->next;
4581 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004582 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004583
H. Peter Anvineba20a72002-04-30 20:53:55 +00004584 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004585
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004586 if (changed) {
4587 const struct tokseq_match t[] = {
4588 {
4589 PP_CONCAT_MASK(TOK_ID) |
4590 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4591 PP_CONCAT_MASK(TOK_ID) |
4592 PP_CONCAT_MASK(TOK_NUMBER) |
4593 PP_CONCAT_MASK(TOK_FLOAT) |
4594 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4595 },
4596 {
4597 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4598 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4599 }
4600 };
4601 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4602 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004603
H. Peter Anvin76690a12002-04-30 20:52:49 +00004604 return thead;
4605}
4606
H. Peter Anvin322bee02019-08-10 01:38:06 -07004607static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004608
H. Peter Anvin76690a12002-04-30 20:52:49 +00004609/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004610 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004611 * a macro at all, it is simply copied to the output and the pointer
4612 * advanced. tpp should be a pointer to a pointer (usually the next
4613 * pointer of the previous token) to the first token. **tpp is updated
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004614 * to point to the first token of the expansion, and *tpp updated to
4615 * point to the next pointer of the last token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004616 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004617 * If the expansion is empty, *tpp will be unchanged but **tpp will
4618 * be advanced past the macro call.
4619 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004620 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004621 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004622static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004623{
4624 Token **params = NULL;
4625 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004626 Token *mstart = **tpp;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004627 Token *tline = mstart;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004628 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004629 int i;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004630 Token *t, *tup, *tafter;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004631 int nparam = 0;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004632 bool cond_comma;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004633
4634 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004635 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004636
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004637 mname = mstart->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004638
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004639 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004640 smacro_deadman.levels--;
4641
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004642 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004643 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004644 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004645 smacro_deadman.triggered = true;
4646 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004647 goto not_a_macro;
4648 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004649 head = (SMacro *)hash_findix(&smacros, mname);
4650 } else if (tline->type == TOK_PREPROC_ID) {
4651 Context *ctx = get_ctx(mname, &mname);
4652 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4653 } else {
4654 goto not_a_macro;
4655 }
4656
4657 /*
4658 * We've hit an identifier of some sort. First check whether the
4659 * identifier is a single-line macro at all, then think about
4660 * checking for parameters if necessary.
4661 */
4662 list_for_each(m, head) {
H. Peter Anvind2354082019-08-27 16:38:48 -07004663 if (unlikely(m->alias && !do_aliases))
4664 continue;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004665 if (!mstrcmp(m->name, mname, m->casesense))
4666 break;
4667 }
4668
4669 if (!m) {
4670 goto not_a_macro;
4671 }
4672
4673 /* Parse parameters, if applicable */
4674
4675 params = NULL;
4676 nparam = 0;
4677
4678 if (m->nparam == 0) {
4679 /*
4680 * Simple case: the macro is parameterless.
4681 * Nothing to parse; the expansion code will
4682 * drop the macro name token.
4683 */
4684 } else {
4685 /*
4686 * Complicated case: at least one macro with this name
4687 * exists and takes parameters. We must find the
4688 * parameters in the call, count them, find the SMacro
4689 * that corresponds to that form of the macro call, and
4690 * substitute for the parameters when we expand. What a
4691 * pain.
4692 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004693 Token *t;
4694 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004695
4696 tline = tline->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004697 tline = skip_white(tline);
4698 if (!tok_is(tline, '(')) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004699 /*
4700 * This macro wasn't called with parameters: ignore
4701 * the call. (Behaviour borrowed from gnu cpp.)
4702 */
4703 goto not_a_macro;
4704 }
4705
4706 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004707 nparam = 1;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004708 brackets = 0;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004709 t = tline; /* tline points to leading ( */
4710
4711 while (paren) {
4712 t = t->next;
4713
4714 if (!t) {
4715 nasm_nonfatal("macro call expects terminating `)'");
4716 goto not_a_macro;
4717 }
4718
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004719 if (t->type != TOK_OTHER || t->len != 1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004720 continue;
4721
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004722 switch (t->text[0]) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004723 case ',':
4724 if (!brackets)
4725 nparam++;
4726 break;
4727
4728 case '{':
4729 brackets++;
4730 break;
4731
4732 case '}':
4733 if (brackets > 0)
4734 brackets--;
4735 break;
4736
4737 case '(':
4738 if (!brackets)
4739 paren++;
4740 break;
4741
4742 case ')':
4743 if (!brackets)
4744 paren--;
4745 break;
4746
4747 default:
4748 break; /* Normal token */
4749 }
4750 }
4751
4752 /*
4753 * Look for a macro matching in both name and parameter count.
4754 * We already know any matches cannot be anywhere before the
4755 * current position of "m", so there is no reason to
4756 * backtrack.
4757 */
4758 while (1) {
4759 if (!m) {
4760 /*!
4761 *!macro-params-single [on] single-line macro calls with wrong parameter count
4762 *! warns about \i{single-line macros} being invoked
4763 *! with the wrong number of parameters.
4764 */
4765 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4766 "single-line macro `%s' exists, "
4767 "but not taking %d parameter%s",
4768 mname, nparam, (nparam == 1) ? "" : "s");
4769 goto not_a_macro;
4770 }
4771
4772 if (!mstrcmp(m->name, mname, m->casesense)) {
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004773 if (nparam == m->nparam)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004774 break; /* It's good */
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004775 if (m->greedy && nparam >= m->nparam-1)
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004776 break; /* Also good */
4777 }
4778 m = m->next;
4779 }
4780 }
4781
4782 if (m->in_progress)
4783 goto not_a_macro;
4784
4785 /* Expand the macro */
4786 m->in_progress = true;
4787
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004788 if (nparam) {
4789 /* Extract parameters */
4790 Token **phead, **pep;
4791 int white = 0;
4792 int brackets = 0;
4793 int paren;
4794 bool bracketed = false;
4795 bool bad_bracket = false;
4796 enum sparmflags flags;
4797
4798 nparam = m->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004799 paren = 1;
4800 nasm_newn(params, nparam);
4801 i = 0;
4802 flags = m->params[i].flags;
4803 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004804 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004805
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004806 while (paren) {
4807 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004808 char ch;
4809
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004810 tline = tline->next;
4811
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004812 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004813 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004814
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004815 ch = 0;
4816 skip = false;
4817
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004818
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004819 switch (tline->type) {
4820 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004821 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004822 ch = tline->text[0];
4823 break;
4824
4825 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004826 if (!(flags & SPARM_NOSTRIP)) {
4827 if (brackets || *phead)
4828 white++; /* Keep interior whitespace */
4829 skip = true;
4830 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004831 break;
4832
4833 default:
4834 break;
4835 }
4836
4837 switch (ch) {
4838 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004839 if (!brackets && !(flags & SPARM_GREEDY)) {
4840 i++;
4841 nasm_assert(i < nparam);
4842 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004843 *pep = NULL;
4844 bracketed = false;
4845 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004846 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004847 }
4848 break;
4849
4850 case '{':
4851 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004852 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4853 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004854 }
4855 brackets++;
4856 break;
4857
4858 case '}':
4859 if (brackets > 0) {
4860 if (!--brackets)
4861 skip = bracketed;
4862 }
4863 break;
4864
4865 case '(':
4866 if (!brackets)
4867 paren++;
4868 break;
4869
4870 case ')':
4871 if (!brackets) {
4872 paren--;
4873 if (!paren) {
4874 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004875 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004876 }
4877 }
4878 break;
4879
4880 default:
4881 break; /* Normal token */
4882 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004883
4884 if (!skip) {
4885 Token *t;
4886
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004887 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004888
4889 if (white) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004890 *pep = t = new_White(NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004891 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004892 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004893 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004894 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004895 pep = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004896 }
4897 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004898
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004899 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004900 * Possible further processing of parameters. Note that the
4901 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004902 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004903 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004904 enum sparmflags flags = m->params[i].flags;
4905
4906 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004907 /* Evaluate this parameter as a number */
4908 struct ppscan pps;
4909 struct tokenval tokval;
4910 expr *evalresult;
4911 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004912
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004913 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4914 pps.ntokens = -1;
4915 tokval.t_type = TOKEN_INVALID;
4916 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004917
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004918 free_tlist(eval_param);
4919 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004920
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004921 if (!evalresult) {
4922 /* Nothing meaningful to do */
4923 } else if (tokval.t_type) {
4924 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4925 } else if (!is_simple(evalresult)) {
4926 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4927 } else {
4928 params[i] = make_tok_num(reloc_value(evalresult));
4929 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004930 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004931
4932 if (flags & SPARM_STR) {
4933 /* Convert expansion to a quoted string */
4934 char *arg;
4935 Token *qs;
4936
4937 qs = expand_smacro_noreset(params[i]);
4938 arg = detoken(qs, false);
4939 free_tlist(qs);
4940 params[i] = make_tok_qstr(arg);
4941 nasm_free(arg);
4942 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004943 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004944 }
4945
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004946 /* Note: we own the expansion this returns. */
4947 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004948
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004949 tafter = tline->next; /* Skip past the macro call */
4950 tline->next = NULL; /* Truncate list at the macro call end */
4951 tline = tafter;
4952
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004953 tup = NULL;
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004954 cond_comma = false;
4955
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004956 while (t) {
4957 enum pp_token_type type = t->type;
4958 Token *tnext = t->next;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004959
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004960 switch (type) {
4961 case TOK_PREPROC_Q:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004962 delete_Token(t);
4963 t = dup_Token(tline, mstart);
4964 break;
4965
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004966 case TOK_PREPROC_QQ:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004967 {
4968 size_t len = strlen(m->name);
4969 char *p;
4970
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004971 nasm_free(t->text);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004972 t->type = mstart->type;
4973 if (t->type == TOK_PREPROC_ID) {
4974 const char *pep; /* prefix end pointer */
4975 size_t plen; /* prefix length */
4976
4977 get_ctx(mstart->text, &pep);
4978 plen = pep - mstart->text;
4979
4980 t->len = len + plen;
4981 t->text = p = nasm_malloc(t->len + 1);
4982 p = mempcpy(p, mstart->text, plen);
4983 } else {
4984 t->len = len;
4985 t->text = p = nasm_malloc(len + 1);
4986 }
4987 p = mempcpy(p, m->name, len);
4988 *p = '\0';
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004989 t->next = tline;
4990 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07004991 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004992
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07004993 case TOK_COND_COMMA:
4994 delete_Token(t);
4995 t = cond_comma ? new_Token(tline, TOK_OTHER, ",", 1) : NULL;
4996 break;
4997
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004998 case TOK_ID:
4999 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005000 {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005001 /*
5002 * Chain this into the target line *before* expanding,
5003 * that way we pick up any arguments to the new macro call,
5004 * if applicable.
5005 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005006 Token **tp = &t;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005007 t->next = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005008 expand_one_smacro(&tp);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005009 tline = *tp; /* First token left after any macro call */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005010 break;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005011 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005012 default:
5013 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005014 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005015 nasm_assert(!tup && param < nparam);
5016 delete_Token(t);
5017 t = NULL;
5018 tup = tnext;
5019 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)a1a84462019-08-20 01:32:28 -07005020 cond_comma = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005021 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005022 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005023 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005024 }
5025
5026 if (t) {
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005027 Token *endt = tline;
5028
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005029 tline = t;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005030 while (!cond_comma && t && t != endt)
5031 cond_comma = t->type != TOK_WHITESPACE;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005032 }
5033
5034 if (tnext) {
5035 t = tnext;
5036 } else {
5037 t = tup;
5038 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005039 }
5040 }
5041
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005042 **tpp = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005043 for (t = tline; t != tafter; t = t->next)
5044 *tpp = &t->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005045
5046 m->in_progress = false;
5047
5048 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005049 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07005050 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005051
5052 /*
5053 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07005054 * and then advance to the next input token. Note that this is
5055 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005056 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005057not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005058 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07005059 m = NULL;
5060done:
5061 smacro_deadman.levels++;
5062 if (unlikely(params))
5063 free_tlist_array(params, nparam);
5064 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005065}
5066
5067/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005068 * Expand all single-line macro calls made in the given line.
5069 * Return the expanded version of the line. The original is deemed
5070 * to be destroyed in the process. (In reality we'll just move
5071 * Tokens from input to output a lot of the time, rather than
5072 * actually bothering to destroy and replicate.)
5073 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005074static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005075{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005076 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07005077 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
5078 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005079 return expand_smacro_noreset(tline);
5080}
5081
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005082static Token *expand_smacro_noreset(Token *org_tline)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005083{
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005084 Token *tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005085 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005086
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005087 if (!org_tline)
5088 return NULL; /* Empty input */
5089
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005090 /*
5091 * Trick: we should avoid changing the start token pointer since it can
5092 * be contained in "next" field of other token. Because of this
5093 * we allocate a copy of first token and work with it; at the end of
5094 * routine we copy it back
5095 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005096 tline = dup_Token(org_tline->next, org_tline);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005097
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005098 /*
5099 * Pretend that we always end up doing expansion on the first pass;
5100 * that way %+ get processed. However, if we process %+ before the
5101 * first pass, we end up with things like MACRO %+ TAIL trying to
5102 * look up the macro "MACROTAIL", which we don't want.
5103 */
5104 expanded = true;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005105 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005106 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04005107 {
5108 PP_CONCAT_MASK(TOK_ID) |
5109 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
5110 PP_CONCAT_MASK(TOK_ID) |
5111 PP_CONCAT_MASK(TOK_PREPROC_ID) |
5112 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
5113 }
5114 };
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005115 Token **tail = &tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005116
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005117 while (*tail) /* main token loop */
H. Peter Anvin322bee02019-08-10 01:38:06 -07005118 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005119
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005120 if (!expanded)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005121 break; /* Done! */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07005122
5123 /*
5124 * Now scan the entire line and look for successive TOK_IDs
5125 * that resulted after expansion (they can't be produced by
5126 * tokenize()). The successive TOK_IDs should be concatenated.
5127 * Also we look for %+ tokens and concatenate the tokens
5128 * before and after them (without white spaces in between).
5129 */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005130 if (!paste_tokens(&tline, tmatch, ARRAY_SIZE(tmatch), true))
5131 break; /* Done again! */
5132
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005133 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005134 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005135
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005136 if (tline) {
5137 *org_tline = *tline;
5138 /* since we just gave text to org_tline, don't free it */
5139 tline->text = NULL;
5140 delete_Token(tline);
5141 } else {
5142 /*
5143 * The expression expanded to empty line;
5144 * we can't return NULL because of the "trick" above.
5145 * Just set the line to a single WHITESPACE token.
5146 */
5147 nasm_zero(*org_tline);
5148 org_tline->type = TOK_WHITESPACE;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005149 }
5150
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005151 return org_tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005152}
5153
5154/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005155 * Similar to expand_smacro but used exclusively with macro identifiers
5156 * right before they are fetched in. The reason is that there can be
5157 * identifiers consisting of several subparts. We consider that if there
5158 * are more than one element forming the name, user wants a expansion,
5159 * otherwise it will be left as-is. Example:
5160 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005161 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005162 *
5163 * the identifier %$abc will be left as-is so that the handler for %define
5164 * will suck it and define the corresponding value. Other case:
5165 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005166 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005167 *
5168 * In this case user wants name to be expanded *before* %define starts
5169 * working, so we'll expand %$abc into something (if it has a value;
5170 * otherwise it will be left as-is) then concatenate all successive
5171 * PP_IDs into one.
5172 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005173static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005174{
5175 Token *cur, *oldnext = NULL;
5176
H. Peter Anvin734b1882002-04-30 21:01:08 +00005177 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005178 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005179
5180 cur = tline;
5181 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 (cur->next->type == TOK_ID ||
5183 cur->next->type == TOK_PREPROC_ID
5184 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005185 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005186
5187 /* If identifier consists of just one token, don't expand */
5188 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005189 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005190
H. Peter Anvine2c80182005-01-15 22:15:51 +00005191 if (cur) {
5192 oldnext = cur->next; /* Detach the tail past identifier */
5193 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005194 }
5195
H. Peter Anvin734b1882002-04-30 21:01:08 +00005196 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005197
H. Peter Anvine2c80182005-01-15 22:15:51 +00005198 if (cur) {
5199 /* expand_smacro possibly changhed tline; re-scan for EOL */
5200 cur = tline;
5201 while (cur && cur->next)
5202 cur = cur->next;
5203 if (cur)
5204 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005205 }
5206
5207 return tline;
5208}
5209
5210/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005211 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005212 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005213 * to check for an initial label - that's taken care of in
5214 * expand_mmacro - but must check numbers of parameters. Guaranteed
5215 * to be called with tline->type == TOK_ID, so the putative macro
5216 * name is easy to find.
5217 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005218static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005219{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005220 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005221 Token **params;
5222 int nparam;
5223
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005224 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005225
5226 /*
5227 * Efficiency: first we see if any macro exists with the given
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005228 * name which isn't already excluded by macro cycle removal.
5229 * (The cycle removal test here helps optimize the case of wrapping
5230 * instructions, and is cheap to do here.)
5231 *
5232 * If not, we can return NULL immediately. _Then_ we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005233 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005234 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005235 */
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005236 list_for_each(m, head) {
5237 if (!mstrcmp(m->name, tline->text, m->casesense) &&
5238 (m->in_progress != 1 || m->max_depth > 0))
5239 break; /* Found something that needs consideration */
5240 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005241 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005242 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005243
5244 /*
5245 * OK, we have a potential macro. Count and demarcate the
5246 * parameters.
5247 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005248 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005249
5250 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005251 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005252 * structure that handles this number.
5253 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005254 while (m) {
5255 if (m->nparam_min <= nparam
5256 && (m->plus || nparam <= m->nparam_max)) {
5257 /*
5258 * This one is right. Just check if cycle removal
5259 * prohibits us using it before we actually celebrate...
5260 */
5261 if (m->in_progress > m->max_depth) {
5262 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005263 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005264 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005265 }
5266 nasm_free(params);
5267 return NULL;
5268 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005269 /*
5270 * It's right, and we can use it. Add its default
5271 * parameters to the end of our list if necessary.
5272 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005273 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005274 int newnparam = m->nparam_min + m->ndefs;
5275 params = nasm_realloc(params, sizeof(*params) * (newnparam+2));
5276 memcpy(&params[nparam+1], &m->defaults[nparam+1-m->nparam_min],
5277 (newnparam - nparam) * sizeof(*params));
5278 nparam = newnparam;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005279 }
5280 /*
5281 * If we've gone over the maximum parameter count (and
5282 * we're in Plus mode), ignore parameters beyond
5283 * nparam_max.
5284 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005285 if (m->plus && nparam > m->nparam_max)
5286 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005287
H. Peter Anvin (Intel)7eb18212019-08-20 16:24:46 -07005288 /*
5289 * If nparam was adjusted above, make sure the list is still
5290 * NULL-terminated.
5291 */
5292 params[nparam+1] = NULL;
5293
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005294 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005295 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005296 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005297 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005298 }
5299 /*
5300 * This one wasn't right: look for the next one with the
5301 * same name.
5302 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005303 list_for_each(m, m->next)
5304 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005305 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005306 }
5307
5308 /*
5309 * After all that, we didn't find one with the right number of
5310 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005311 *!
5312 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5313 *! warns about \i{multi-line macros} being invoked
5314 *! with the wrong number of parameters. See \k{mlmacover} for an
5315 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005316 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005317 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5318 "multi-line macro `%s' exists, but not taking %d parameter%s",
5319 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005320 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005321 return NULL;
5322}
5323
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005324
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005325#if 0
5326
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005327/*
5328 * Save MMacro invocation specific fields in
5329 * preparation for a recursive macro expansion
5330 */
5331static void push_mmacro(MMacro *m)
5332{
5333 MMacroInvocation *i;
5334
5335 i = nasm_malloc(sizeof(MMacroInvocation));
5336 i->prev = m->prev;
5337 i->params = m->params;
5338 i->iline = m->iline;
5339 i->nparam = m->nparam;
5340 i->rotate = m->rotate;
5341 i->paramlen = m->paramlen;
5342 i->unique = m->unique;
5343 i->condcnt = m->condcnt;
5344 m->prev = i;
5345}
5346
5347
5348/*
5349 * Restore MMacro invocation specific fields that were
5350 * saved during a previous recursive macro expansion
5351 */
5352static void pop_mmacro(MMacro *m)
5353{
5354 MMacroInvocation *i;
5355
5356 if (m->prev) {
5357 i = m->prev;
5358 m->prev = i->prev;
5359 m->params = i->params;
5360 m->iline = i->iline;
5361 m->nparam = i->nparam;
5362 m->rotate = i->rotate;
5363 m->paramlen = i->paramlen;
5364 m->unique = i->unique;
5365 m->condcnt = i->condcnt;
5366 nasm_free(i);
5367 }
5368}
5369
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005370#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005371
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005372/*
H. Peter Anvin (Intel)ffe89dd2019-08-20 16:06:36 -07005373 * List an mmacro call with arguments (-Lm option)
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005374 */
5375static void list_mmacro_call(const MMacro *m)
5376{
5377 const char prefix[] = " ;;; [macro] ";
5378 size_t namelen, size;
5379 char *buf, *p;
5380 unsigned int i;
5381 const Token *t;
5382
5383 namelen = strlen(m->iname);
5384 size = namelen + sizeof(prefix); /* Includes final null (from prefix) */
5385
5386 for (i = 1; i <= m->nparam; i++) {
5387 int j = 0;
5388 size += 3; /* Braces and space/comma */
5389 list_for_each(t, m->params[i]) {
5390 if (j++ >= m->paramlen[i])
5391 break;
5392 size += (t->type == TOK_WHITESPACE) ? 1 : t->len;
5393 }
5394 }
5395
5396 buf = p = nasm_malloc(size);
5397 p = mempcpy(p, prefix, sizeof(prefix) - 1);
5398 p = mempcpy(p, m->iname, namelen);
5399 *p++ = ' ';
5400
5401 for (i = 1; i <= m->nparam; i++) {
5402 int j = 0;
5403 *p++ = '{';
5404 list_for_each(t, m->params[i]) {
5405 if (j++ >= m->paramlen[i])
5406 break;
5407 if (!t->text) {
5408 if (t->type == TOK_WHITESPACE)
5409 *p++ = ' ';
5410 } else {
5411 p = mempcpy(p, t->text, t->len);
5412 }
5413 }
5414 *p++ = '}';
5415 *p++ = ',';
5416 }
5417
5418 *--p = '\0'; /* Replace last delimeter with null */
5419 lfmt->line(LIST_MACRO, -1, buf);
5420 nasm_free(buf);
5421}
5422
5423/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005424 * Expand the multi-line macro call made by the given line, if
5425 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005426 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005427 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005428static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005429{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005430 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005431 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005432 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005433 Token **params, *t, *tt;
5434 MMacro *m;
5435 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005436 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005437 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005438 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005439
5440 t = tline;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005441 t = skip_white(t);
5442 /* if (!tok_type(t, TOK_ID)) Lino 02/25/02 */
5443 if (!tok_type(t, TOK_ID) && !tok_type(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005444 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005445 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005446 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005447 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005448 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005449 Token *last;
5450 /*
5451 * We have an id which isn't a macro call. We'll assume
5452 * it might be a label; we'll also check to see if a
5453 * colon follows it. Then, if there's another id after
5454 * that lot, we'll check it again for macro-hood.
5455 */
5456 label = last = t;
5457 t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005458 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005459 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005460 if (tok_is(t, ':')) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005461 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005462 last = t, t = t->next;
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005463 if (tok_white(t))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005464 last = t, t = t->next;
5465 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005466 if (!tok_type(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005467 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005468 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005469 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005470 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005471 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005472
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005473 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5474 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5475 if (!mmacro_deadman.triggered) {
5476 nasm_nonfatal("interminable multiline macro recursion");
5477 mmacro_deadman.triggered = true;
5478 }
5479 return 0;
5480 }
5481
5482 mmacro_deadman.total++;
5483 mmacro_deadman.levels++;
5484
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005485 /*
5486 * Fix up the parameters: this involves stripping leading and
5487 * trailing whitespace, then stripping braces if they are
5488 * present.
5489 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005490 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005491
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005492 nasm_assert(params[nparam+1] == NULL);
5493
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005494 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005495 int brace = 0;
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005496 bool comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005497
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005498 t = skip_white(t);
5499 if (tok_is(t, '{'))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005500 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005501 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005502 while (t) {
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005503 if (comma) {
5504 /* Check if we hit a comma that ends this argument */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005505 Token *tns = skip_white(t);
5506 if (tok_is(tns, ','))
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005507 break;
5508 }
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005509 if (brace && t->type == TOK_OTHER && t->len == 1) {
5510 const char c = t->text[0];
5511 if (c == '{')
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005512 brace++; /* ... or a nested opening brace */
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07005513 else if (c == '}')
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005514 if (!--brace)
5515 break; /* ... or a brace */
5516 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005517 t = t->next;
5518 paramlen[i]++;
5519 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005520 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005521 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005522 }
5523
5524 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005525 * OK, we have a MMacro structure together with a set of
5526 * parameters. We must now go through the expansion and push
5527 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005528 * parameter tokens and macro-local tokens doesn't get done
5529 * until the single-line macro substitution process; this is
5530 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005531 * later through %rotate and give the right semantics for
5532 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005533 *
5534 * First, push an end marker on to istk->expansion, mark this
5535 * macro as in progress, and set up its invocation-specific
5536 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005537 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005538 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005539 ll->next = istk->expansion;
5540 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005541 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005542
5543 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005544 * Save the previous MMacro expansion in the case of
5545 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005546 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005547#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005548 if (m->max_depth && m->in_progress)
5549 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005550#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005551
5552 m->in_progress ++;
5553 m->params = params;
5554 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005555 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005556 m->nparam = nparam;
5557 m->rotate = 0;
5558 m->paramlen = paramlen;
5559 m->unique = unique++;
5560 m->lineno = 0;
5561 m->condcnt = 0;
5562
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005563 m->mstk = istk->mstk;
5564 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005565
5566 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005567 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005568 ll->next = istk->expansion;
5569 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005570 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005571 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005572
5573 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005574 * If we had a label, and this macro definition does not include
5575 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005576 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005577 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005578 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005579 /*
5580 * We had a label. If this macro contains an %00 parameter,
5581 * save the value as a special parameter (which is what it
5582 * is), otherwise push it as the first line of the macro
5583 * expansion.
5584 */
5585 if (m->capture_label) {
5586 params[0] = dup_Token(NULL, label);
5587 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005588 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005589 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005590 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005591 ll->finishes = NULL;
5592 ll->next = istk->expansion;
5593 istk->expansion = ll;
5594 ll->first = startline;
5595 if (!dont_prepend) {
5596 while (label->next)
5597 label = label->next;
5598 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005599 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005600 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005601 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005602
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005603 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005604
H. Peter Anvin (Intel)41d91a92019-08-20 16:00:57 -07005605 if (list_option('m') && !m->nolist)
5606 list_mmacro_call(m);
5607
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005608 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005609}
5610
H. Peter Anvin130736c2016-02-17 20:27:41 -08005611/*
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005612 * This function decides if an error message should be suppressed.
5613 * It will never be called with a severity level of ERR_FATAL or
5614 * higher.
H. Peter Anvin130736c2016-02-17 20:27:41 -08005615 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005616static bool pp_suppress_error(errflags severity)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005617{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005618 /*
5619 * If we're in a dead branch of IF or something like it, ignore the error.
5620 * However, because %else etc are evaluated in the state context
5621 * of the previous branch, errors might get lost:
5622 * %if 0 ... %else trailing garbage ... %endif
5623 * So %else etc should set the ERR_PP_PRECOND flag.
5624 */
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005625 if (istk && istk->conds &&
H. Peter Anvin130736c2016-02-17 20:27:41 -08005626 ((severity & ERR_PP_PRECOND) ?
5627 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005628 !emitting(istk->conds->state)))
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005629 return true;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005630
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07005631 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005632}
5633
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005634static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005635stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005636{
5637 (void)s;
5638 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005639 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005640
5641 return make_tok_qstr(src_get_fname());
5642}
5643
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005644static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005645stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005646{
5647 (void)s;
5648 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005649 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005650
5651 return make_tok_num(src_get_linnum());
5652}
5653
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005654static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005655stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005656{
5657 (void)s;
5658 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005659 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005660
5661 return make_tok_num(globalbits);
5662}
5663
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005664static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005665stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005666{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005667 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005668 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5669 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5670 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005671
5672 (void)s;
5673 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005674 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005675
5676 switch (globalbits) {
5677 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005678 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005679 break;
5680 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005681 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005682 break;
5683 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005684 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005685 break;
5686 default:
5687 panic();
5688 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005689
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005690 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005691}
5692
H. Peter Anvin8b262472019-02-26 14:00:54 -08005693/* Add magic standard macros */
5694struct magic_macros {
5695 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005696 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005697 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005698};
5699static const struct magic_macros magic_macros[] =
5700{
H. Peter Anvind2354082019-08-27 16:38:48 -07005701 { "__?FILE?__", 0, stdmac_file },
5702 { "__?LINE?__", 0, stdmac_line },
5703 { "__?BITS?__", 0, stdmac_bits },
5704 { "__?PTR?__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005705 { NULL, 0, NULL }
5706};
5707
5708static void pp_add_magic_stdmac(void)
5709{
5710 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005711 SMacro tmpl;
5712
5713 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005714
5715 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005716 tmpl.nparam = m->nparam;
5717 tmpl.expand = m->func;
5718 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005719 }
5720}
5721
H. Peter Anvin734b1882002-04-30 21:01:08 +00005722static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005723pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005724{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005725 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005726 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005727
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005728 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005729 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005730 nested_mac_count = 0;
5731 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005732 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005733 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005734 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005735 pp_mode = mode;
H. Peter Anvind2354082019-08-27 16:38:48 -07005736 do_aliases = true;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005737
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005738 if (!use_loaded)
5739 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5740 memset(use_loaded, 0, use_package_count * sizeof(bool));
5741
H. Peter Anvin6686de22019-08-10 05:33:14 -07005742 /* First set up the top level input file */
5743 nasm_new(istk);
5744 istk->fp = nasm_open_read(file, NF_TEXT);
5745 src_set(0, file);
5746 istk->lineinc = 1;
5747 if (!istk->fp)
5748 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5749
5750 strlist_add(deplist, file);
5751
5752 /*
5753 * Set up the stdmac packages as a virtual include file,
5754 * indicated by a null file pointer.
5755 */
5756 nasm_new(inc);
5757 inc->next = istk;
5758 inc->fname = src_set_fname(NULL);
5759 inc->nolist = !list_option('b');
5760 istk = inc;
5761 lfmt->uplevel(LIST_INCLUDE, 0);
5762
H. Peter Anvin8b262472019-02-26 14:00:54 -08005763 pp_add_magic_stdmac();
5764
H. Peter Anvinf7606612016-07-13 14:23:48 -07005765 if (tasm_compatible_mode)
5766 pp_add_stdmac(nasm_stdmac_tasm);
5767
5768 pp_add_stdmac(nasm_stdmac_nasm);
5769 pp_add_stdmac(nasm_stdmac_version);
5770
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005771 if (extrastdmac)
5772 pp_add_stdmac(extrastdmac);
5773
H. Peter Anvinf7606612016-07-13 14:23:48 -07005774 stdmacpos = stdmacros[0];
5775 stdmacnext = &stdmacros[1];
5776
H. Peter Anvind2456592008-06-19 15:04:18 -07005777 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005778
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005779 /*
H. Peter Anvind2354082019-08-27 16:38:48 -07005780 * Define the __?PASS?__ macro. This is defined here unlike all the
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005781 * other builtins, because it is special -- it varies between
5782 * passes -- but there is really no particular reason to make it
5783 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005784 *
5785 * 0 = dependencies only
5786 * 1 = preparatory passes
5787 * 2 = final pass
5788 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005789 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005790 switch (mode) {
5791 case PP_NORMAL:
5792 apass = pass_final() ? 2 : 1;
5793 break;
5794 case PP_DEPS:
5795 apass = 0;
5796 break;
5797 case PP_PREPROC:
5798 apass = 3;
5799 break;
5800 default:
5801 panic();
5802 }
5803
H. Peter Anvind2354082019-08-27 16:38:48 -07005804 define_smacro("__?PASS?__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005805}
5806
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005807static void pp_init(void)
5808{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005809}
5810
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005811/*
5812 * Get a line of tokens. If we popped the macro expansion/include stack,
5813 * we return a pointer to the dummy token tok_pop; at that point if
5814 * istk is NULL then we have reached end of input;
5815 */
5816static Token tok_pop; /* Dummy token placeholder */
5817
5818static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005819{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005820 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005821 Line *l = istk->expansion;
5822 Token *tline = NULL;
5823 Token *dtline;
5824
H. Peter Anvine2c80182005-01-15 22:15:51 +00005825 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005826 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005827 * buffer or from the input file.
5828 */
5829 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005830 while (l && l->finishes) {
5831 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005832
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005833 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005834 /*
5835 * This is a macro-end marker for a macro with no
5836 * name, which means it's not really a macro at all
5837 * but a %rep block, and the `in_progress' field is
5838 * more than 1, meaning that we still need to
5839 * repeat. (1 means the natural last repetition; 0
5840 * means termination by %exitrep.) We have
5841 * therefore expanded up to the %endrep, and must
5842 * push the whole block on to the expansion buffer
5843 * again. We don't bother to remove the macro-end
5844 * marker: we'd only have to generate another one
5845 * if we did.
5846 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005847 fm->in_progress--;
5848 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005849 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005850 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005851
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005852 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005853 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005854 tail = &ll->first;
5855
5856 list_for_each(t, l->first) {
5857 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005858 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005859 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005860 }
5861 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005862 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005863 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005864 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005865 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005866 MMacro *m = istk->mstk.mstk;
5867
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005868 /*
5869 * Check whether a `%rep' was started and not ended
5870 * within this macro expansion. This can happen and
5871 * should be detected. It's a fatal error because
5872 * I'm too confused to work out how to recover
5873 * sensibly from it.
5874 */
5875 if (defining) {
5876 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005877 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005878 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005879 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005880 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005881 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005882
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005883 /*
5884 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005885 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005886 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005887 istk->mstk = m->mstk;
5888 if (m->name) {
5889 /*
5890 * This was a real macro call, not a %rep, and
5891 * therefore the parameter information needs to
5892 * be freed and the iteration count/nesting
5893 * depth adjusted.
5894 */
5895
5896 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005897 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005898 * If all mmacro processing done,
5899 * clear all counters and the deadman
5900 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005901 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005902 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005903 }
5904
Adam Majer91e72402017-07-25 10:42:01 +02005905#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005906 if (m->prev) {
5907 pop_mmacro(m);
5908 fm->in_progress --;
5909 } else
Adam Majer91e72402017-07-25 10:42:01 +02005910#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005911 {
5912 nasm_free(m->params);
5913 free_tlist(m->iline);
5914 nasm_free(m->paramlen);
5915 fm->in_progress = 0;
5916 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005917 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005918
5919 /*
5920 * FIXME It is incorrect to always free_mmacro here.
5921 * It leads to usage-after-free.
5922 *
5923 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5924 */
5925#if 0
5926 else
5927 free_mmacro(m);
5928#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005929 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005930 istk->expansion = l->next;
5931 nasm_free(l);
5932 lfmt->downlevel(LIST_MACRO);
5933 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005934 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005935
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005936 do { /* until we get a line we can use */
5937 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005938
5939 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005940 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005941 int32_t lineno;
5942
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005943 if (istk->mstk.mstk) {
5944 istk->mstk.mstk->lineno++;
5945 if (istk->mstk.mstk->fname)
5946 lineno = istk->mstk.mstk->lineno +
5947 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005948 else
5949 lineno = 0; /* Defined at init time or builtin */
5950 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005951 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005952 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005953
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005954 tline = l->first;
5955 istk->expansion = l->next;
5956 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005957
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005958 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005959 if (!istk->nolist)
5960 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005961 nasm_free(line);
5962 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005963 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005964 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005965 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005966 } else {
5967 /*
5968 * The current file has ended; work down the istk
5969 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005970 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005971 if (i->fp)
5972 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005973 if (i->conds) {
5974 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005975 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005976 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005977 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005978 if (i->next)
5979 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005980 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005981 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005982 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005983 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005984 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005985 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005986
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005987 /*
5988 * We must expand MMacro parameters and MMacro-local labels
5989 * _before_ we plunge into directive processing, to cope
5990 * with things like `%define something %1' such as STRUC
5991 * uses. Unless we're _defining_ a MMacro, in which case
5992 * those tokens should be left alone to go into the
5993 * definition; and unless we're in a non-emitting
5994 * condition, in which case we don't want to meddle with
5995 * anything.
5996 */
5997 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005998 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005999 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006000 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006001
H. Peter Anvine2c80182005-01-15 22:15:51 +00006002 /*
6003 * Check the line to see if it's a preprocessor directive.
6004 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006005 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
6006 if (dtline)
6007 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006008 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006009 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006010 * We're defining a multi-line macro. We emit nothing
6011 * at all, and just
6012 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006013 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006014 MMacro *mmac = defining->dstk.mmac;
6015
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006016 Line *l = nasm_malloc(sizeof(Line));
6017 l->next = defining->expansion;
6018 l->first = tline;
6019 l->finishes = NULL;
6020 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006021
6022 /*
6023 * Remember if this mmacro expansion contains %00:
6024 * if it does, we will have to handle leading labels
6025 * specially.
6026 */
6027 if (mmac) {
6028 const Token *t;
6029 list_for_each(t, tline) {
6030 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
6031 mmac->capture_label = true;
6032 }
6033 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006034 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006035 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006036 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00006037 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006038 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00006039 * directive so we keep our place correctly.
6040 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006041 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006042 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006043 /*
6044 * We're in a %rep block which has been terminated, so
6045 * we're walking through to the %endrep without
6046 * emitting anything. Emit nothing at all, not even a
6047 * blank line: when we emerge from the %rep block we'll
6048 * give a line-number directive so we keep our place
6049 * correctly.
6050 */
6051 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00006052 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006053 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006054 if (!expand_mmacro(tline))
6055 return tline;
6056 }
6057 }
6058}
6059
6060static char *pp_getline(void)
6061{
6062 char *line = NULL;
6063 Token *tline;
6064
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006065 while (true) {
6066 tline = pp_tokline();
6067 if (tline == &tok_pop) {
6068 /*
6069 * We popped the macro/include stack. If istk is empty,
6070 * we are at end of input, otherwise just loop back.
6071 */
6072 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006073 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07006074 } else {
6075 /*
6076 * De-tokenize the line and emit it.
6077 */
6078 line = detoken(tline, true);
6079 free_tlist(tline);
6080 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00006081 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006082 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006083
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006084 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006085 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07006086 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07006087 nasm_free(buf);
6088 }
6089
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006090 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006091}
6092
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006093static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006094{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006095 if (defining) {
6096 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006097 nasm_nonfatal("end of file while still defining macro `%s'",
6098 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006099 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03006100 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006101 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006102
6103 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03006104 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006105 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08006106
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006107 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006108 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07006109 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006110 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00006111 Include *i = istk;
6112 istk = istk->next;
6113 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03006114 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006115 }
6116 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006117 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07006118 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006119}
6120
6121static void pp_cleanup_session(void)
6122{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07006123 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006124 free_llist(predef);
6125 predef = NULL;
6126 delete_Blocks();
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006127 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006128}
6129
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006130static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006131{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03006132 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006133}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00006134
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006135static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006136{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006137 Token *inc, *space, *name;
6138 Line *l;
6139
H. Peter Anvin734b1882002-04-30 21:01:08 +00006140 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006141 space = new_White(name);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006142 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006143
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006144 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006145 l->next = predef;
6146 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006147 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006148 predef = l;
6149}
6150
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006151static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006152{
6153 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006154 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00006155 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006156
6157 equals = strchr(definition, '=');
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006158 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006159 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006160 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006161 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00006162 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006163 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00006164 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006165
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006166 if (space->next->type != TOK_PREPROC_ID &&
6167 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08006168 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03006169
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006170 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006171 l->next = predef;
6172 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006173 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00006174 predef = l;
6175}
6176
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006177static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00006178{
6179 Token *def, *space;
6180 Line *l;
6181
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006182 space = new_White(NULL);
H. Peter Anvin734b1882002-04-30 21:01:08 +00006183 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00006184 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00006185
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006186 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00006187 l->next = predef;
6188 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08006189 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00006190 predef = l;
6191}
6192
H. Peter Anvin05990342018-06-11 13:32:42 -07006193/* Insert an early preprocessor command that doesn't need special handling */
6194static void pp_pre_command(const char *what, char *string)
6195{
6196 char *cmd;
6197 Token *def, *space;
6198 Line *l;
6199
6200 def = tokenize(string);
6201 if (what) {
6202 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
H. Peter Anvin (Intel)f24d9752019-09-18 18:17:26 -07006203 space = new_White(def);
H. Peter Anvin05990342018-06-11 13:32:42 -07006204 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6205 }
6206
6207 l = nasm_malloc(sizeof(Line));
6208 l->next = predef;
6209 l->first = def;
6210 l->finishes = NULL;
6211 predef = l;
6212}
6213
H. Peter Anvinf7606612016-07-13 14:23:48 -07006214static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006215{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006216 macros_t **mp;
6217
6218 /* Find the end of the list and avoid duplicates */
6219 for (mp = stdmacros; *mp; mp++) {
6220 if (*mp == macros)
6221 return; /* Nothing to do */
6222 }
6223
6224 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6225
6226 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006227}
6228
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006229static void pp_extra_stdmac(macros_t *macros)
6230{
6231 extrastdmac = macros;
6232}
6233
H. Peter Anvin8b262472019-02-26 14:00:54 -08006234static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006235{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006236 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006237 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6238 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6239}
6240
6241static Token *make_tok_qstr(const char *str)
6242{
6243 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006244 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006245 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006246}
6247
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006248static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006249{
6250 if (!m)
6251 return;
6252
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006253 /* We need to print the mstk.mmac list in reverse order */
6254 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006255
6256 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006257 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006258 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006259 }
6260}
6261
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006262static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006263{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006264 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006265
H. Peter Anvinddb29062018-12-11 00:06:29 -08006266 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6267 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006268
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006269 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006270 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006271
H. Peter Anvinddb29062018-12-11 00:06:29 -08006272 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006273}
6274
H. Peter Anvine7469712016-02-18 02:20:59 -08006275const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006276 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006277 pp_reset,
6278 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006279 pp_cleanup_pass,
6280 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006281 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006282 pp_pre_define,
6283 pp_pre_undefine,
6284 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006285 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006286 pp_include_path,
6287 pp_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -07006288 pp_suppress_error
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006289};