blob: fe22b37f99abf1ae0d3724968d00806fffffacff [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000084typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000085typedef struct Line Line;
86typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080087typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088
89/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070090 * Note on the storage of both SMacro and MMacros: the hash table
91 * indexes them case-insensitively, and we then have to go through a
92 * linked list of potential case aliases (and, for MMacros, parameter
93 * ranges); this is to preserve the matching semantics of the earlier
94 * code. If the number of case aliases for a specific macro is a
95 * performance issue, you may want to reconsider your coding style.
96 */
97
98/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070099 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700100 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700101typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params, int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700102
103/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000104 * Store the definition of a single-line macro.
105 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700106enum sparmflags {
107 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, /* %+ */
248 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800249 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300250 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000251};
252
H. Peter Anvin8b262472019-02-26 14:00:54 -0800253static inline enum pp_token_type tok_smac_param(int param)
254{
255 return TOK_SMAC_START_PARAMS + param;
256}
257static int smac_nparam(enum pp_token_type toktype)
258{
259 return toktype - TOK_SMAC_START_PARAMS;
260}
261static bool is_smac_param(enum pp_token_type toktype)
262{
263 return toktype >= TOK_SMAC_START_PARAMS;
264}
265
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400266#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400267#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400268
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400269struct tokseq_match {
270 int mask_head;
271 int mask_tail;
272};
273
H. Peter Anvine2c80182005-01-15 22:15:51 +0000274struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800275 Token *next;
276 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700277 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800278 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000279};
280
281/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800282 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000283 * these, which is essentially a container to allow several linked
284 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700285 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000286 * Note that in this module, linked lists are treated as stacks
287 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800288 * `expansion' field in MMacro structures, so that the linked list,
289 * if walked, would give the macro lines in reverse order; this
290 * means that we can walk the list when expanding a macro, and thus
291 * push the lines on to the `expansion' field in _istk_ in reverse
292 * order (so that when popped back off they are in the right
293 * order). It may seem cockeyed, and it relies on my design having
294 * an even number of steps in, but it works...
295 *
296 * Some of these structures, rather than being actual lines, are
297 * markers delimiting the end of the expansion of a given macro.
298 * This is for use in the cycle-tracking and %rep-handling code.
299 * Such structures have `finishes' non-NULL, and `first' NULL. All
300 * others have `finishes' NULL, but `first' may still be NULL if
301 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000302 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000303struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800304 Line *next;
305 MMacro *finishes;
306 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500307};
308
309/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310 * To handle an arbitrary level of file inclusion, we maintain a
311 * stack (ie linked list) of these things.
312 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000313struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800314 Include *next;
315 FILE *fp;
316 Cond *conds;
317 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700318 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700319 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700320 int lineno, lineinc;
321 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322};
323
324/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700325 * File real name hash, so we don't have to re-search the include
326 * path for every pass (and potentially more than that if a file
327 * is used more than once.)
328 */
329struct hash_table FileHash;
330
331/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700332 * Counters to trap on insane macro recursion or processing.
333 * Note: for smacros these count *down*, for mmacros they count *up*.
334 */
335struct deadman {
336 int64_t total; /* Total number of macros/tokens */
337 int64_t levels; /* Descent depth across all macros */
338 bool triggered; /* Already triggered, no need for error msg */
339};
340
341static struct deadman smacro_deadman, mmacro_deadman;
342
343/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 * Conditional assembly: we maintain a separate stack of these for
345 * each level of file inclusion. (The only reason we keep the
346 * stacks separate is to ensure that a stray `%endif' in a file
347 * included from within the true branch of a `%if' won't terminate
348 * it and cause confusion: instead, rightly, it'll cause an error.)
349 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700350enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000351 /*
352 * These states are for use just after %if or %elif: IF_TRUE
353 * means the condition has evaluated to truth so we are
354 * currently emitting, whereas IF_FALSE means we are not
355 * currently emitting but will start doing so if a %else comes
356 * up. In these states, all directives are admissible: %elif,
357 * %else and %endif. (And of course %if.)
358 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800359 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360 /*
361 * These states come up after a %else: ELSE_TRUE means we're
362 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
363 * any %elif or %else will cause an error.
364 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800365 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200367 * These states mean that we're not emitting now, and also that
368 * nothing until %endif will be emitted at all. COND_DONE is
369 * used when we've had our moment of emission
370 * and have now started seeing %elifs. COND_NEVER is used when
371 * the condition construct in question is contained within a
372 * non-emitting branch of a larger condition construct,
373 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000374 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800375 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000376};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700377struct Cond {
378 Cond *next;
379 enum cond_state state;
380};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800381#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000382
H. Peter Anvin70653092007-10-19 14:42:29 -0700383/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000384 * These defines are used as the possible return values for do_directive
385 */
386#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300387#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000388
Keith Kanios852f1ee2009-07-12 00:19:55 -0500389/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000390 * Condition codes. Note that we use c_ prefix not C_ because C_ is
391 * used in nasm.h for the "real" condition codes. At _this_ level,
392 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
393 * ones, so we need a different enum...
394 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700395static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000396 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
397 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000398 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700400enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
402 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 -0700403 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
404 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700406static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
408 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 +0000409 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410};
411
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800412/*
413 * Directive names.
414 */
415/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
416static int is_condition(enum preproc_token arg)
417{
418 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
419}
420
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000421/* For TASM compatibility we need to be able to recognise TASM compatible
422 * conditional compilation directives. Using the NASM pre-processor does
423 * not work, so we look for them specifically from the following list and
424 * then jam in the equivalent NASM directive into the input stream.
425 */
426
H. Peter Anvine2c80182005-01-15 22:15:51 +0000427enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000428 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
429 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
430};
431
H. Peter Anvin476d2862007-10-02 22:04:15 -0700432static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000433 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
434 "ifndef", "include", "local"
435};
436
437static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700438static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000439static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800440static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000441
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000442static Context *cstk;
443static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300444static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000445
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300446static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000447
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300448static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000449
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800450static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700451static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800452static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000453
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000454/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800455 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000456 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800457static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000458
459/*
460 * The current set of single-line macros we have defined.
461 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700462static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000463
464/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800465 * The multi-line macro we are currently defining, or the %rep
466 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000467 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800468static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469
Charles Crayned4200be2008-07-12 16:42:33 -0700470static uint64_t nested_mac_count;
471static uint64_t nested_rep_count;
472
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000473/*
474 * The number of macro parameters to allocate space for at a time.
475 */
476#define PARAM_DELTA 16
477
478/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700479 * The standard macro set: defined in macros.c in a set of arrays.
480 * This gives our position in any macro set, while we are processing it.
481 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000482 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700483static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700484static macros_t **stdmacnext;
485static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300486static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000487
488/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700489 * Map of which %use packages have been loaded
490 */
491static bool *use_loaded;
492
493/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000494 * Tokens are allocated in blocks to improve speed
495 */
496#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800497static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000498struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499 Blocks *next;
500 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000501};
502
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800503static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000504
505/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000506 * Forward declarations.
507 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700508static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000509static Token *expand_mmac_params(Token * tline);
510static Token *expand_smacro(Token * tline);
511static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400512static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800513static Token *make_tok_num(int64_t val);
514static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800515static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800516static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000517static void *new_Block(size_t size);
518static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700519static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700520 const char *text, size_t txtlen);
521static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000522static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523
524/*
525 * Macros for safe checking of token pointers, avoid *(NULL)
526 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300527#define tok_type_(x,t) ((x) && (x)->type == (t))
528#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
529#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
530#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000531
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400532/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700533 * In-place reverse a list of tokens.
534 */
535static Token *reverse_tokens(Token *t)
536{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800537 Token *prev = NULL;
538 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700539
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800540 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400541 next = t->next;
542 t->next = prev;
543 prev = t;
544 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800545 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700546
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800547 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700548}
549
550/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300551 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000552 * front of them. We do it here because I could not find any other
553 * place to do it for the moment, and it is a hack (ideally it would
554 * be nice to be able to use the NASM pre-processor to do it).
555 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000556static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000557{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000558 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400559 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000560
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400561 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000562
563 /* Binary search for the directive name */
564 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400565 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400566 q = nasm_skip_word(p);
567 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 if (len) {
569 oldchar = p[len];
570 p[len] = 0;
571 while (j - i > 1) {
572 k = (j + i) / 2;
573 m = nasm_stricmp(p, tasm_directives[k]);
574 if (m == 0) {
575 /* We have found a directive, so jam a % in front of it
576 * so that NASM will then recognise it as one if it's own.
577 */
578 p[len] = oldchar;
579 len = strlen(p);
580 oldline = line;
581 line = nasm_malloc(len + 2);
582 line[0] = '%';
583 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700584 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300585 * NASM does not recognise IFDIFI, so we convert
586 * it to %if 0. This is not used in NASM
587 * compatible code, but does need to parse for the
588 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700590 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 } else {
592 memcpy(line + 1, p, len + 1);
593 }
594 nasm_free(oldline);
595 return line;
596 } else if (m < 0) {
597 j = k;
598 } else
599 i = k;
600 }
601 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000602 }
603 return line;
604}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000605
H. Peter Anvin76690a12002-04-30 20:52:49 +0000606/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000607 * The pre-preprocessing stage... This function translates line
608 * number indications as they emerge from GNU cpp (`# lineno "file"
609 * flags') into NASM preprocessor line number indications (`%line
610 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000612static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000614 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000615 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000616
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 if (line[0] == '#' && line[1] == ' ') {
618 oldline = line;
619 fname = oldline + 2;
620 lineno = atoi(fname);
621 fname += strspn(fname, "0123456789 ");
622 if (*fname == '"')
623 fname++;
624 fnlen = strcspn(fname, "\"");
625 line = nasm_malloc(20 + fnlen);
626 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
627 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000628 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000629 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000631 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000632}
633
634/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000635 * Free a linked list of tokens.
636 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000638{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400639 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000641}
642
643/*
644 * Free a linked list of lines.
645 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000646static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000647{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400648 Line *l, *tmp;
649 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000650 free_tlist(l->first);
651 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000652 }
653}
654
655/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700656 * Free an array of linked lists of tokens
657 */
658static void free_tlist_array(Token **array, size_t nlists)
659{
660 Token **listp = array;
661
662 while (nlists--)
663 free_tlist(*listp++);
664
665 nasm_free(array);
666}
667
668/*
669 * Duplicate a linked list of tokens.
670 */
671static Token *dup_tlist(const Token *list, Token ***tailp)
672{
673 Token *newlist = NULL;
674 Token **tailpp = &newlist;
675 const Token *t;
676
677 list_for_each(t, list) {
678 Token *nt;
679 *tailpp = nt = dup_Token(NULL, t);
680 tailpp = &nt->next;
681 }
682
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700683 if (tailp) {
684 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700685 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700686 }
687
688 return newlist;
689}
690
691/*
692 * Duplicate a linked list of tokens with a maximum count
693 */
694static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
695{
696 Token *newlist = NULL;
697 Token **tailpp = &newlist;
698 const Token *t;
699
700 list_for_each(t, list) {
701 Token *nt;
702 if (!cnt--)
703 break;
704 *tailpp = nt = dup_Token(NULL, t);
705 tailpp = &nt->next;
706 }
707
708 if (tailp) {
709 **tailp = newlist;
710 *tailp = tailpp;
711 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700712
713 return newlist;
714}
715
716/*
717 * Duplicate a linked list of tokens in reverse order
718 */
719static Token *dup_tlist_reverse(const Token *list, Token *tail)
720{
721 const Token *t;
722
723 list_for_each(t, list)
724 tail = dup_Token(tail, t);
725
726 return tail;
727}
728
729/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800730 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000731 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800732static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000733{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800734 nasm_free(m->name);
735 free_tlist(m->dlist);
736 nasm_free(m->defaults);
737 free_llist(m->expansion);
738 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000739}
740
741/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700742 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800743 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700744static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800745{
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -0700746 if (s->params) {
747 int i;
748 for (i = 0; s->nparam; i++)
749 nasm_free(s->params[i].name);
750 nasm_free(s->params);
751 }
H. Peter Anvin8b262472019-02-26 14:00:54 -0800752 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700753 free_tlist(s->expansion);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700754}
755
756static void clear_smacro(SMacro *s)
757{
758 free_smacro_members(s);
759 /* Wipe everything except the next pointer */
760 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
761}
762
763/*
764 * Free an SMacro
765 */
766static void free_smacro(SMacro *s)
767{
768 free_smacro_members(s);
769 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800770}
771
772/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700773 * Free all currently defined macros, and free the hash tables
774 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700775static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700776{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800777 struct hash_iterator it;
778 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700779
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800780 hash_for_each(smt, it, np) {
781 SMacro *tmp;
782 SMacro *s = np->data;
783 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800784 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700785 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700786 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700787 hash_free(smt);
788}
789
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800790static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700791{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800792 struct hash_iterator it;
793 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700794
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800795 hash_for_each(mmt, it, np) {
796 MMacro *tmp;
797 MMacro *m = np->data;
798 nasm_free((void *)np->key);
799 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800800 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700801 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800802 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700803}
804
805static void free_macros(void)
806{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700807 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800808 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700809}
810
811/*
812 * Initialize the hash tables
813 */
814static void init_macros(void)
815{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700816}
817
818/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000819 * Pop the context stack.
820 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000822{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000823 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000824
825 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700826 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000827 nasm_free(c->name);
828 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000829}
830
H. Peter Anvin072771e2008-05-22 13:17:51 -0700831/*
832 * Search for a key in the hash index; adding it if necessary
833 * (in which case we initialize the data pointer to NULL.)
834 */
835static void **
836hash_findi_add(struct hash_table *hash, const char *str)
837{
838 struct hash_insert hi;
839 void **r;
840 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800841 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700842
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800843 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700844 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300845 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700846
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800847 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
848 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700849 return hash_add(&hi, strx, NULL);
850}
851
852/*
853 * Like hash_findi, but returns the data element rather than a pointer
854 * to it. Used only when not adding a new element, hence no third
855 * argument.
856 */
857static void *
858hash_findix(struct hash_table *hash, const char *str)
859{
860 void **p;
861
862 p = hash_findi(hash, str, NULL);
863 return p ? *p : NULL;
864}
865
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400866/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800867 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400868 * if there no more left -- return NULL
869 */
870static char *line_from_stdmac(void)
871{
872 unsigned char c;
873 const unsigned char *p = stdmacpos;
874 char *line, *q;
875 size_t len = 0;
876
877 if (!stdmacpos)
878 return NULL;
879
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700880 /*
881 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
882 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
883 */
884 while ((c = *p++) != 127) {
885 uint8_t ndir = c - 128;
886 if (ndir < 256-96)
887 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400888 else
889 len++;
890 }
891
892 line = nasm_malloc(len + 1);
893 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700894
895 while ((c = *stdmacpos++) != 127) {
896 uint8_t ndir = c - 128;
897 if (ndir < 256-96) {
898 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
899 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400900 *q++ = ' ';
901 } else {
902 *q++ = c;
903 }
904 }
905 stdmacpos = p;
906 *q = '\0';
907
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700908 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700909 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400910 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700911 if (*stdmacnext) {
912 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400913 } else if (do_predef) {
914 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400915
916 /*
917 * Nasty hack: here we push the contents of
918 * `predef' on to the top-level expansion stack,
919 * since this is the most convenient way to
920 * implement the pre-include and pre-define
921 * features.
922 */
923 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700924 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800925 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700926 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800927 l->finishes = NULL;
928
929 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400930 }
931 do_predef = false;
932 }
933 }
934
935 return line;
936}
937
H. Peter Anvin6686de22019-08-10 05:33:14 -0700938/*
939 * Read a line from a file. Return NULL on end of file.
940 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700941static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000942{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700943 int c;
944 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400945 const unsigned int delta = 512;
946 const unsigned int pad = 8;
947 unsigned int nr_cont = 0;
948 bool cont = false;
949 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700950 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000951
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400952 size = delta;
953 p = buffer = nasm_malloc(size);
954
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700955 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700956 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400957
958 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700959 case EOF:
960 if (p == buffer) {
961 nasm_free(buffer);
962 return NULL;
963 }
964 c = 0;
965 break;
966
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400967 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700968 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400969 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700970 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400971 if (cont) {
972 cont = false;
973 continue;
974 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700975 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400976 break;
977
978 case '\n':
979 if (cont) {
980 cont = false;
981 continue;
982 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700983 c = 0;
984 break;
985
986 case 032: /* ^Z = legacy MS-DOS end of file mark */
987 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400988 break;
989
990 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700991 next = fgetc(f);
992 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400993 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400994 cont = true;
995 nr_cont++;
996 continue;
997 }
998 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001000
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +04001001 if (p >= (buffer + size - pad)) {
1002 buffer = nasm_realloc(buffer, size + delta);
1003 p = buffer + size - pad;
1004 size += delta;
1005 }
1006
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07001007 *p++ = c;
1008 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001009
H. Peter Anvinab6f8312019-08-09 22:31:45 -07001010 lineno = src_get_linnum() + istk->lineinc +
1011 (nr_cont * istk->lineinc);
1012 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001013
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001014 return buffer;
1015}
1016
1017/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001018 * Common read routine regardless of source
1019 */
1020static char *read_line(void)
1021{
1022 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001023 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001024
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001025 if (f)
1026 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001027 else
1028 line = line_from_stdmac();
1029
1030 if (!line)
1031 return NULL;
1032
1033 if (!istk->nolist)
1034 lfmt->line(LIST_READ, src_get_linnum(), line);
1035
1036 return line;
1037}
1038
1039/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001040 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001041 * don't need to parse the value out of e.g. numeric tokens: we
1042 * simply split one string into many.
1043 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001044static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001045{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001046 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001047 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001048 Token *list = NULL;
1049 Token *t, **tail = &list;
1050
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001052 char *p = line;
1053 char *ep = NULL; /* End of token, for trimming the end */
1054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 if (*p == '%') {
1056 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001057 if (*p == '+' && !nasm_isdigit(p[1])) {
1058 p++;
1059 type = TOK_PASTE;
1060 } else if (nasm_isdigit(*p) ||
1061 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 do {
1063 p++;
1064 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001065 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 type = TOK_PREPROC_ID;
1067 } else if (*p == '{') {
1068 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001069 while (*p) {
1070 if (*p == '}')
1071 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 p[-1] = *p;
1073 p++;
1074 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001075 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001076 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001077 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 if (*p)
1079 p++;
1080 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001081 } else if (*p == '[') {
1082 int lvl = 1;
1083 line += 2; /* Skip the leading %[ */
1084 p++;
1085 while (lvl && (c = *p++)) {
1086 switch (c) {
1087 case ']':
1088 lvl--;
1089 break;
1090 case '%':
1091 if (*p == '[')
1092 lvl++;
1093 break;
1094 case '\'':
1095 case '\"':
1096 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001097 p = nasm_skip_string(p - 1);
1098 if (*p)
1099 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001100 break;
1101 default:
1102 break;
1103 }
1104 }
1105 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001106 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001107 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001108 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001109 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001110 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001111 type = TOK_INDIRECT;
1112 } else if (*p == '?') {
1113 type = TOK_PREPROC_Q; /* %? */
1114 p++;
1115 if (*p == '?') {
1116 type = TOK_PREPROC_QQ; /* %?? */
1117 p++;
1118 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001119 } else if (*p == '!') {
1120 type = TOK_PREPROC_ID;
1121 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001122 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001123 do {
1124 p++;
1125 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001126 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001127 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001128 p = nasm_skip_string(p);
1129 if (*p)
1130 p++;
1131 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001132 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001133 } else {
1134 /* %! without string or identifier */
1135 type = TOK_OTHER; /* Legacy behavior... */
1136 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001137 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001139 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 do {
1141 p++;
1142 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001143 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 type = TOK_PREPROC_ID;
1145 } else {
1146 type = TOK_OTHER;
1147 if (*p == '%')
1148 p++;
1149 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001150 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 type = TOK_ID;
1152 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001153 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001155 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 /*
1157 * A string token.
1158 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001160 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001161
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 if (*p) {
1163 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001164 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001165 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 /* Handling unterminated strings by UNV */
1167 /* type = -1; */
1168 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001169 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001170 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001171 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001172 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001173 bool is_hex = false;
1174 bool is_float = false;
1175 bool has_e = false;
1176 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001177
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001179 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001181
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001182 if (*p == '$') {
1183 p++;
1184 is_hex = true;
1185 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001186
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001187 for (;;) {
1188 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001189
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001190 if (!is_hex && (c == 'e' || c == 'E')) {
1191 has_e = true;
1192 if (*p == '+' || *p == '-') {
1193 /*
1194 * e can only be followed by +/- if it is either a
1195 * prefixed hex number or a floating-point number
1196 */
1197 p++;
1198 is_float = true;
1199 }
1200 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1201 is_hex = true;
1202 } else if (c == 'P' || c == 'p') {
1203 is_float = true;
1204 if (*p == '+' || *p == '-')
1205 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001206 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001207 ; /* just advance */
1208 else if (c == '.') {
1209 /*
1210 * we need to deal with consequences of the legacy
1211 * parser, like "1.nolist" being two tokens
1212 * (TOK_NUMBER, TOK_ID) here; at least give it
1213 * a shot for now. In the future, we probably need
1214 * a flex-based scanner with proper pattern matching
1215 * to do it as well as it can be done. Nothing in
1216 * the world is going to help the person who wants
1217 * 0x123.p16 interpreted as two tokens, though.
1218 */
1219 r = p;
1220 while (*r == '_')
1221 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001222
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001223 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1224 (!is_hex && (*r == 'e' || *r == 'E')) ||
1225 (*r == 'p' || *r == 'P')) {
1226 p = r;
1227 is_float = true;
1228 } else
1229 break; /* Terminate the token */
1230 } else
1231 break;
1232 }
1233 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001234
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001235 if (p == line+1 && *line == '$') {
1236 type = TOK_OTHER; /* TOKEN_HERE */
1237 } else {
1238 if (has_e && !is_hex) {
1239 /* 1e13 is floating-point, but 1e13h is not */
1240 is_float = true;
1241 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001242
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001243 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1244 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001245 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001247 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 /*
1249 * Whitespace just before end-of-line is discarded by
1250 * pretending it's a comment; whitespace just before a
1251 * comment gets lumped into the comment.
1252 */
1253 if (!*p || *p == ';') {
1254 type = TOK_COMMENT;
1255 while (*p)
1256 p++;
1257 }
1258 } else if (*p == ';') {
1259 type = TOK_COMMENT;
1260 while (*p)
1261 p++;
1262 } else {
1263 /*
1264 * Anything else is an operator of some kind. We check
1265 * for all the double-character operators (>>, <<, //,
1266 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001267 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 */
1269 type = TOK_OTHER;
1270 if ((p[0] == '>' && p[1] == '>') ||
1271 (p[0] == '<' && p[1] == '<') ||
1272 (p[0] == '/' && p[1] == '/') ||
1273 (p[0] == '<' && p[1] == '=') ||
1274 (p[0] == '>' && p[1] == '=') ||
1275 (p[0] == '=' && p[1] == '=') ||
1276 (p[0] == '!' && p[1] == '=') ||
1277 (p[0] == '<' && p[1] == '>') ||
1278 (p[0] == '&' && p[1] == '&') ||
1279 (p[0] == '|' && p[1] == '|') ||
1280 (p[0] == '^' && p[1] == '^')) {
1281 p++;
1282 }
1283 p++;
1284 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001285
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 /* Handling unterminated string by UNV */
1287 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001288 {
1289 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1290 t->text[p-line] = *line;
1291 tail = &t->next;
1292 }
1293 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001295 if (!ep)
1296 ep = p;
1297 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 tail = &t->next;
1299 }
1300 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001301 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001302 return list;
1303}
1304
H. Peter Anvince616072002-04-30 21:02:23 +00001305/*
1306 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001307 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001308 * deleted only all at once by the delete_Blocks function.
1309 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001311{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001312 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001314 /* first, get to the end of the linked list */
1315 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001317 /* now allocate the requested chunk */
1318 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001320 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001321 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001322 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001323}
1324
1325/*
1326 * this function deletes all managed blocks of memory
1327 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001329{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001331
H. Peter Anvin70653092007-10-19 14:42:29 -07001332 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001333 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001334 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001335 * free it.
1336 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001338 if (b->chunk)
1339 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 a = b;
1341 b = b->next;
1342 if (a != &blocks)
1343 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001344 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001345 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001347
1348/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001349 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001350 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001351 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001352static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001353 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001354{
1355 Token *t;
1356 int i;
1357
H. Peter Anvin89cee572009-07-15 09:16:54 -04001358 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1360 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1361 freeTokens[i].next = &freeTokens[i + 1];
1362 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001363 }
1364 t = freeTokens;
1365 freeTokens = t->next;
1366 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001367 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001368 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001369 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 t->text = NULL;
1371 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001372 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001374 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001375 t->text = nasm_malloc(txtlen+1);
1376 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001378 }
1379 return t;
1380}
1381
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001382static Token *dup_Token(Token *next, const Token *src)
1383{
1384 return new_Token(next, src->type, src->text, src->len);
1385}
1386
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001388{
1389 Token *next = t->next;
1390 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001391 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001392 freeTokens = t;
1393 return next;
1394}
1395
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001396/*
1397 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001398 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1399 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001400 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001401static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001402{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001403 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001404 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001405 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001406
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001407 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001408 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001409 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001410 char *v;
1411 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001412
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001413 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001414 if (nasm_isquote(*v))
1415 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001416
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001417 if (v) {
1418 char *p = getenv(v);
1419 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001420 /*!
1421 *!environment [on] nonexistent environment variable
1422 *! warns if a nonexistent environment variable
1423 *! is accessed using the \c{%!} preprocessor
1424 *! construct (see \k{getenv}.) Such environment
1425 *! variables are treated as empty (with this
1426 *! warning issued) starting in NASM 2.15;
1427 *! earlier versions of NASM would treat this as
1428 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001429 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001430 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1431 p = "";
1432 }
1433 t->text = nasm_strdup(p);
1434 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001435 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001436 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001438
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 /* Expand local macros here and not during preprocessing */
1440 if (expand_locals &&
1441 t->type == TOK_PREPROC_ID && t->text &&
1442 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001443 const char *q;
1444 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001445 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001447 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1448 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 nasm_free(t->text);
1450 t->text = p;
1451 }
1452 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001453 if (t->text) {
1454 if (debug_level(2)) {
1455 unsigned long t_len = t->len;
1456 unsigned long s_len = strlen(t->text);
1457 if (t_len != s_len) {
1458 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1459 t->text, t->type, s_len, t_len);
1460 t->len = s_len;
1461 }
1462 }
1463 len += t->len;
1464 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001466 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001467 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001468
H. Peter Anvin734b1882002-04-30 21:01:08 +00001469 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001470
1471 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001472 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001473 memcpy(p, t->text, t->len);
1474 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001475 } else if (t->type == TOK_WHITESPACE) {
1476 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001477 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001478 }
1479 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001480
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001481 return line;
1482}
1483
1484/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001485 * A scanner, suitable for use by the expression evaluator, which
1486 * operates on a line of Tokens. Expects a pointer to a pointer to
1487 * the first token in the line to be passed in as its private_data
1488 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001489 *
1490 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001491 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001492struct ppscan {
1493 Token *tptr;
1494 int ntokens;
1495};
1496
H. Peter Anvine2c80182005-01-15 22:15:51 +00001497static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001498{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001499 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001500 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001501 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001502
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001504 if (pps->ntokens && (tline = pps->tptr)) {
1505 pps->ntokens--;
1506 pps->tptr = tline->next;
1507 } else {
1508 pps->tptr = NULL;
1509 pps->ntokens = 0;
1510 return tokval->t_type = TOKEN_EOS;
1511 }
1512 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001513
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001514 tokval->t_charptr = tline->text;
1515
H. Peter Anvin76690a12002-04-30 20:52:49 +00001516 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001518 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001520
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001522 p = tokval->t_charptr = tline->text;
1523 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 tokval->t_charptr++;
1525 return tokval->t_type = TOKEN_ID;
1526 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001527
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001528 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 if (r >= p+MAX_KEYWORD)
1530 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001531 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001532 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001533 *s = '\0';
1534 /* right, so we have an identifier sitting in temp storage. now,
1535 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001536 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001537 }
1538
H. Peter Anvine2c80182005-01-15 22:15:51 +00001539 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001540 bool rn_error;
1541 tokval->t_integer = readnum(tline->text, &rn_error);
1542 tokval->t_charptr = tline->text;
1543 if (rn_error)
1544 return tokval->t_type = TOKEN_ERRNUM;
1545 else
1546 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001547 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001548
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001549 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001550 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001551 }
1552
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001554 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001555
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001556 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001557 tokval->t_charptr = tline->text;
1558 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001559
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001560 if (ep[0] != bq || ep[1] != '\0')
1561 return tokval->t_type = TOKEN_ERRSTR;
1562 else
1563 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001564 }
1565
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 if (tline->type == TOK_OTHER) {
1567 if (!strcmp(tline->text, "<<"))
1568 return tokval->t_type = TOKEN_SHL;
1569 if (!strcmp(tline->text, ">>"))
1570 return tokval->t_type = TOKEN_SHR;
1571 if (!strcmp(tline->text, "//"))
1572 return tokval->t_type = TOKEN_SDIV;
1573 if (!strcmp(tline->text, "%%"))
1574 return tokval->t_type = TOKEN_SMOD;
1575 if (!strcmp(tline->text, "=="))
1576 return tokval->t_type = TOKEN_EQ;
1577 if (!strcmp(tline->text, "<>"))
1578 return tokval->t_type = TOKEN_NE;
1579 if (!strcmp(tline->text, "!="))
1580 return tokval->t_type = TOKEN_NE;
1581 if (!strcmp(tline->text, "<="))
1582 return tokval->t_type = TOKEN_LE;
1583 if (!strcmp(tline->text, ">="))
1584 return tokval->t_type = TOKEN_GE;
1585 if (!strcmp(tline->text, "&&"))
1586 return tokval->t_type = TOKEN_DBL_AND;
1587 if (!strcmp(tline->text, "^^"))
1588 return tokval->t_type = TOKEN_DBL_XOR;
1589 if (!strcmp(tline->text, "||"))
1590 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001591 }
1592
1593 /*
1594 * We have no other options: just return the first character of
1595 * the token text.
1596 */
1597 return tokval->t_type = tline->text[0];
1598}
1599
1600/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001601 * Compare a string to the name of an existing macro; this is a
1602 * simple wrapper which calls either strcmp or nasm_stricmp
1603 * depending on the value of the `casesense' parameter.
1604 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001605static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001606{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001607 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001608}
1609
1610/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001611 * Compare a string to the name of an existing macro; this is a
1612 * simple wrapper which calls either strcmp or nasm_stricmp
1613 * depending on the value of the `casesense' parameter.
1614 */
1615static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1616{
1617 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1618}
1619
1620/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001621 * Return the Context structure associated with a %$ token. Return
1622 * NULL, having _already_ reported an error condition, if the
1623 * context stack isn't deep enough for the supplied number of $
1624 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001625 *
1626 * If "namep" is non-NULL, set it to the pointer to the macro name
1627 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001628 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001629static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001630{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001631 Context *ctx;
1632 int i;
1633
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001634 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001635 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001636
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001637 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001639
H. Peter Anvine2c80182005-01-15 22:15:51 +00001640 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001641 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001643 }
1644
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001645 name += 2;
1646 ctx = cstk;
1647 i = 0;
1648 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001649 name++;
1650 i++;
1651 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001652 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001654 nasm_nonfatal("`%s': context stack is only"
1655 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001657 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001658
1659 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001660 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001661
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001662 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001663}
1664
1665/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001666 * Open an include file. This routine must always return a valid
1667 * file pointer if it returns - it's responsible for throwing an
1668 * ERR_FATAL and bombing out completely if not. It should also try
1669 * the include path one by one until it finds the file or reaches
1670 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001671 *
1672 * Note: for INC_PROBE the function returns NULL at all times;
1673 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001674 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001675enum incopen_mode {
1676 INC_NEEDED, /* File must exist */
1677 INC_OPTIONAL, /* Missing is OK */
1678 INC_PROBE /* Only an existence probe */
1679};
1680
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001681/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001682static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001683 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001684{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001685 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001686 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001687 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001688 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001689 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001690
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001692 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001693 if (omode == INC_PROBE) {
1694 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001695 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001696 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001697 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001698 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001699 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001700 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001701 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001703 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001704
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001705 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001706
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001707 if (!ip) {
1708 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001709 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001710 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001711
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001712 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001713 ip = ip->next;
1714 }
1715}
1716
1717/*
1718 * Open a file, or test for the presence of one (depending on omode),
1719 * considering the include path.
1720 */
1721static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001722 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001723 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001724 enum incopen_mode omode,
1725 enum file_flags fmode)
1726{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001727 struct hash_insert hi;
1728 void **hp;
1729 char *path;
1730 FILE *fp = NULL;
1731
1732 hp = hash_find(&FileHash, file, &hi);
1733 if (hp) {
1734 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001735 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001736 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001737 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001738 } else {
1739 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001740 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001741
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001742 /* Positive or negative result */
1743 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001744
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001745 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001746 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001747 */
1748 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001749 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001750 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001751
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001752 if (!path) {
1753 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001754 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001755 } else {
1756 if (!fp && omode != INC_PROBE)
1757 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001758 }
1759
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001760 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001761 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001762
1763 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001764}
1765
1766/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001767 * Opens an include or input file. Public version, for use by modules
1768 * that get a file:lineno pair and need to look at the file again
1769 * (e.g. the CodeView debug backend). Returns NULL on failure.
1770 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001771FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001772{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001773 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001774}
1775
1776/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001777 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001778 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001779 * return true if _any_ single-line macro of that name is defined.
1780 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001781 * `nparam' or no parameters is defined.
1782 *
1783 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001784 * defined, or nparam is -1, the address of the definition structure
1785 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001786 * is NULL, no action will be taken regarding its contents, and no
1787 * error will occur.
1788 *
1789 * Note that this is also called with nparam zero to resolve
1790 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001791 *
1792 * If you already know which context macro belongs to, you can pass
1793 * the context pointer as first parameter; if you won't but name begins
1794 * with %$ the context will be automatically computed. If all_contexts
1795 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001796 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001797static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001798smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001799 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001800{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001801 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001802 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001803
H. Peter Anvin97a23472007-09-16 17:57:25 -07001804 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001805 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001806 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001807 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001808 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001810 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001811 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001812 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001813 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001814 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001815 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001816
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 while (m) {
1818 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001819 (nparam <= 0 || m->nparam == 0 || nparam == m->nparam ||
1820 (m->greedy && nparam > m->nparam))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 if (defn) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07001822 *defn = (nparam == m->nparam || nparam == -1) ? m : NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001824 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 }
1826 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001827 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001828
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001829 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001830}
1831
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001832/* param should be a natural number [0; INT_MAX] */
1833static int read_param_count(const char *str)
1834{
1835 int result;
1836 bool err;
1837
1838 result = readnum(str, &err);
1839 if (result < 0 || result > INT_MAX) {
1840 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001841 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1842 str, 0, INT_MAX);
1843 } else if (err)
1844 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001845 return result;
1846}
1847
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001848/*
1849 * Count and mark off the parameters in a multi-line macro call.
1850 * This is called both from within the multi-line macro expansion
1851 * code, and also to mark off the default parameters when provided
1852 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001853 *
1854 * Note that we need space in the params array for parameter 0 being
1855 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001856 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001857static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001858{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001859 int paramsize, brace;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001860 int nparam = 0;
1861 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001862
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001863 paramsize = PARAM_DELTA;
1864 params = nasm_malloc(paramsize * sizeof(*params));
1865 params[0] = NULL;
1866
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 while (t) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001868 /* 2 slots for captured label and NULL */
1869 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001871 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 }
1873 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001874 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001876 brace++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001877 params[++nparam] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001878 if (brace) {
1879 while (brace && (t = t->next) != NULL) {
1880 if (tok_is_(t, "{"))
1881 brace++;
1882 else if (tok_is_(t, "}"))
1883 brace--;
1884 }
1885
1886 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 /*
1888 * Now we've found the closing brace, look further
1889 * for the comma.
1890 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001891 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 skip_white_(t);
1893 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001894 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 while (tok_isnt_(t, ","))
1896 t = t->next;
1897 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001899 } else {
1900 while (tok_isnt_(t, ","))
1901 t = t->next;
1902 }
1903 if (t) { /* got a comma/brace */
1904 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001906 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001907
1908 params[nparam+1] = NULL;
1909 *paramsp = params;
1910 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001911}
1912
1913/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001914 * Determine whether one of the various `if' conditions is true or
1915 * not.
1916 *
1917 * We must free the tline we get passed.
1918 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001919static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001920{
H. Peter Anvin70055962007-10-11 00:05:31 -07001921 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001922 Token *t, *tt, *origline;
1923 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001924 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001925 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001926 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001927 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001928 const char *dname = pp_directives[ct];
1929 bool casesense = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001930
1931 origline = tline;
1932
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001933 switch (PP_COND(ct)) {
1934 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001935 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001936 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001938 if (!tline)
1939 break;
1940 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001941 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001942 dname);
1943 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001945 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001946 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 tline = tline->next;
1948 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001950
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001951 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001952 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001953 while (tline) {
1954 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 if (!tline || (tline->type != TOK_ID &&
1956 (tline->type != TOK_PREPROC_ID ||
1957 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001958 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001959 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001960 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001961 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001962 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001963 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001964 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001965 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001967
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001968 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001969 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001970 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001971 while (tline) {
1972 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001973 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001974 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001975 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001976 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001977 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001978 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001979 goto fail;
1980 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001981 p = tline->text;
1982 if (tline->type == TOK_PREPROC_ID)
1983 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001984 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001985 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001986 if (getenv(p))
1987 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001988 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001989 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001990 break;
1991
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001992 case PP_IFIDNI:
1993 casesense = false;
1994 /* fall through */
1995 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001996 tline = expand_smacro(tline);
1997 t = tt = tline;
1998 while (tok_isnt_(tt, ","))
1999 tt = tt->next;
2000 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002001 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002002 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002003 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002004 }
2005 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002006 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
2008 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002009 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002010 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002012 }
2013 if (t->type == TOK_WHITESPACE) {
2014 t = t->next;
2015 continue;
2016 }
2017 if (tt->type == TOK_WHITESPACE) {
2018 tt = tt->next;
2019 continue;
2020 }
2021 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002022 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 break;
2024 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002025 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 size_t l1 = nasm_unquote(t->text, NULL);
2028 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002029
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 if (l1 != l2) {
2031 j = false;
2032 break;
2033 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002034 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002035 j = false;
2036 break;
2037 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002038 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002039 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002040 break;
2041 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002042
H. Peter Anvine2c80182005-01-15 22:15:51 +00002043 t = t->next;
2044 tt = tt->next;
2045 }
2046 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002047 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002048 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002049
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002050 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002051 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002052 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002053 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002054
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002055 skip_white_(tline);
2056 tline = expand_id(tline);
2057 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002058 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 goto fail;
2060 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002061 nasm_zero(searching);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002062 searching.name = nasm_strdup(tline->text);
2063 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002064 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002065 searching.nparam_max = INT_MAX;
2066 tline = expand_smacro(tline->next);
2067 skip_white_(tline);
2068 if (!tline) {
2069 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002070 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002071 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 } else {
2073 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002074 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 }
2076 if (tline && tok_is_(tline->next, "-")) {
2077 tline = tline->next->next;
2078 if (tok_is_(tline, "*"))
2079 searching.nparam_max = INT_MAX;
2080 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002081 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002082 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002083 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002084 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002085 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002086 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002087 searching.nparam_max = searching.nparam_min;
2088 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002089 }
2090 }
2091 if (tline && tok_is_(tline->next, "+")) {
2092 tline = tline->next;
2093 searching.plus = true;
2094 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002095 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2096 while (mmac) {
2097 if (!strcmp(mmac->name, searching.name) &&
2098 (mmac->nparam_min <= searching.nparam_max
2099 || searching.plus)
2100 && (searching.nparam_min <= mmac->nparam_max
2101 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002102 found = true;
2103 break;
2104 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002105 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 }
2107 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002108 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002109 nasm_free(searching.name);
2110 j = found;
2111 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002112 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002113
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002114 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002115 needtype = TOK_ID;
2116 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002117 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 needtype = TOK_NUMBER;
2119 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002120 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002121 needtype = TOK_STRING;
2122 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002123
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002124iftype:
2125 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002126
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 while (tok_type_(t, TOK_WHITESPACE) ||
2128 (needtype == TOK_NUMBER &&
2129 tok_type_(t, TOK_OTHER) &&
2130 (t->text[0] == '-' || t->text[0] == '+') &&
2131 !t->text[1]))
2132 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002133
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002134 j = tok_type_(t, needtype);
2135 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002136
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002137 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002138 t = tline = expand_smacro(tline);
2139 while (tok_type_(t, TOK_WHITESPACE))
2140 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002141
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002142 j = false;
2143 if (t) {
2144 t = t->next; /* Skip the actual token */
2145 while (tok_type_(t, TOK_WHITESPACE))
2146 t = t->next;
2147 j = !t; /* Should be nothing left */
2148 }
2149 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002150
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002151 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002152 t = tline = expand_smacro(tline);
2153 while (tok_type_(t, TOK_WHITESPACE))
2154 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002155
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 j = !t; /* Should be empty */
2157 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002158
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002159 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002160 pps.tptr = tline = expand_smacro(tline);
2161 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002162 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002163 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002164 if (!evalresult)
2165 return -1;
2166 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002167 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002169 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002170 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002171 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002173 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002174 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002175
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002177 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002179 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002180
2181 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002182 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002183
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002184fail:
2185 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002186 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002187}
2188
2189/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002190 * Default smacro expansion routine: just returns a copy of the
2191 * expansion list.
2192 */
2193static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002194smacro_expand_default(const SMacro *s, Token **params, int nparams)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002195{
2196 (void)params;
2197 (void)nparams;
2198
2199 return dup_tlist(s->expansion, NULL);
2200}
2201
2202/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002203 * Emit a macro defintion or undef to the listing file, if
2204 * desired. This is similar to detoken(), but it handles the reverse
2205 * expansion list, does not expand %! or local variable tokens, and
2206 * does some special handling for macro parameters.
2207 */
2208static void
2209list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2210{
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002211 Token *t;
2212 size_t namelen, size;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002213 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002214 char *context_prefix = NULL;
2215 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002216
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002217 namelen = strlen(m->name);
2218 size = namelen + 2; /* Include room for space after name + NUL */
2219
2220 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002221 int context_depth = cstk->depth - ctx->depth + 1;
2222 context_prefix =
2223 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2224 ctx->name ? ctx->name : "",
2225 ctx->number, context_depth, "");
2226
2227 context_len = nasm_last_string_len();
2228 memset(context_prefix + context_len - context_depth,
2229 '$', context_depth);
2230 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002231 }
2232
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002233 if (m->nparam) {
2234 /*
2235 * Space for ( and either , or ) around each
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002236 * parameter, plus up to 4 flags.
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002237 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002238 int i;
2239
2240 size += 1 + 4 * m->nparam;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002241 for (i = 0; i < m->nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002242 size += m->params[i].namelen;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002243 }
2244 }
2245
2246 def = nasm_malloc(size);
2247 p = def+size;
2248 *--p = '\0';
2249
2250 list_for_each(t, m->expansion) {
2251 if (!t->text) {
2252 *--p = ' ';
2253 } else {
2254 p -= t->len;
2255 memcpy(p, t->text, t->len);
2256 }
2257 }
2258
2259 *--p = ' ';
2260
2261 if (m->nparam) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002262 int i;
2263
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002264 *--p = ')';
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002265 for (i = m->nparam-1; i >= 0; i--) {
2266 enum sparmflags flags = m->params[i].flags;
2267 if (flags & SPARM_GREEDY)
2268 *--p = '+';
2269 if (m->params[i].name) {
2270 p -= m->params[i].namelen;
2271 memcpy(p, m->params[i].name, m->params[i].namelen);
2272 }
2273 if (flags & SPARM_NOSTRIP)
2274 *--p = '!';
2275 if (flags & SPARM_STR)
2276 *--p = '&';
2277 if (flags & SPARM_EVAL)
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002278 *--p = '=';
2279 *--p = ',';
2280 }
2281 *p = '('; /* First parameter starts with ( not , */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002282 }
2283
2284 p -= namelen;
2285 memcpy(p, m->name, namelen);
2286
H. Peter Anvin6686de22019-08-10 05:33:14 -07002287 if (context_prefix) {
2288 p -= context_len;
2289 memcpy(p, context_prefix, context_len);
2290 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002291 }
2292
2293 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002294 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002295}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002296
2297/*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002298 * Parse smacro arguments, return argument count. If the tmpl argument
2299 * is set, set the nparam, greedy and params field in the template.
2300 * **tpp is updated to point to the first token after the
2301 * prototype after any whitespace and *tpp to the pointer to it, if
2302 * advanced.
2303 *
2304 * The text values from any argument tokens are "stolen" and the
2305 * corresponding text fields set to NULL.
2306 */
2307static int parse_smacro_template(Token ***tpp, SMacro *tmpl)
2308{
2309 int nparam = 0;
2310 enum sparmflags flags;
2311 struct smac_param *params = NULL;
2312 bool err, done, greedy;
2313 Token **tn = *tpp;
2314 Token *t = *tn;
2315 Token *name;
2316
2317 while (t && t->type == TOK_WHITESPACE) {
2318 tn = &t->next;
2319 t = t->next;
2320 }
2321 if (!tok_is_(t, "("))
2322 goto finish;
2323
2324 if (tmpl) {
2325 Token *tx = t;
2326 Token **txpp = &tx;
2327 int sparam;
2328
2329 /* Count parameters first */
2330 sparam = parse_smacro_template(&txpp, NULL);
2331 if (!sparam)
2332 goto finish; /* No parameters, we're done */
2333 nasm_newn(params, sparam);
2334 }
2335
2336 name = NULL;
2337 flags = 0;
2338 err = done = greedy = false;
2339
2340 while (!done) {
2341 if (!t || !t->type) {
2342 nasm_nonfatal("parameter identifier expected");
2343 break;
2344 }
2345
2346 tn = &t->next;
2347 t = t->next;
2348
2349 switch (t->type) {
2350 case TOK_ID:
2351 if (name)
2352 goto bad;
2353 name = t;
2354 break;
2355
2356 case TOK_OTHER:
2357 if (t->text[1])
2358 goto bad;
2359 switch (t->text[0]) {
2360 case '=':
2361 flags |= SPARM_EVAL;
2362 break;
2363 case '&':
2364 flags |= SPARM_STR;
2365 break;
2366 case '!':
2367 flags |= SPARM_NOSTRIP;
2368 break;
2369 case '+':
2370 flags |= SPARM_GREEDY;
2371 greedy = true;
2372 break;
2373 case ',':
2374 if (greedy)
2375 nasm_nonfatal("greedy parameter must be last");
2376 /* fall through */
2377 case ')':
2378 if (params) {
2379 if (name) {
2380 params[nparam].name = name->text;
2381 params[nparam].namelen = name->len;
2382 name->text = NULL;
2383 }
2384 params[nparam].flags = flags;
2385 nparam++;
2386 }
2387 name = NULL;
2388 flags = 0;
2389 done = t->text[1] == ')';
2390 break;
2391 default:
2392 goto bad;
2393 }
2394 break;
2395
2396 case TOK_WHITESPACE:
2397 break;
2398
2399 default:
2400 bad:
2401 if (!err) {
2402 nasm_nonfatal("garbage `%s' in macro parameter list", t->text);
2403 err = true;
2404 }
2405 break;
2406 }
2407 }
2408
2409 if (!done)
2410 nasm_nonfatal("`)' expected to terminate macro template");
2411
2412finish:
2413 while (t && t->type == TOK_WHITESPACE) {
2414 tn = &t->next;
2415 t = t->next;
2416 }
2417 *tpp = tn;
2418 if (tmpl) {
2419 tmpl->nparam = nparam;
2420 tmpl->greedy = greedy;
2421 tmpl->params = params;
2422 }
2423 return nparam;
2424}
2425
2426/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002427 * Common code for defining an smacro. The tmpl argument, if not NULL,
2428 * contains any macro parameters that aren't explicit arguments;
2429 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002430 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002431static SMacro *define_smacro(const char *mname, bool casesense,
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002432 Token *expansion, SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002433{
2434 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002435 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002436 Context *ctx;
2437 bool defining_alias = false;
2438 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002439
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002440 if (tmpl) {
2441 defining_alias = tmpl->alias;
2442 nparam = tmpl->nparam;
2443 }
2444
2445 while (1) {
2446 ctx = get_ctx(mname, &mname);
2447
2448 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2449 /* Create a new macro */
2450 smtbl = ctx ? &ctx->localmac : &smacros;
2451 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2452 nasm_new(smac);
2453 smac->next = *smhead;
2454 *smhead = smac;
2455 break;
2456 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002457 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002458 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002459 /*
2460 * Some instances of the old code considered this a failure,
2461 * some others didn't. What is the right thing to do here?
2462 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002463 goto fail;
2464 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002465 /*
2466 * We're redefining, so we have to take over an
2467 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002468 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002469 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002470 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002471 break;
2472 } else if (smac->in_progress) {
2473 nasm_nonfatal("macro alias loop");
2474 goto fail;
2475 } else {
2476 /* It is an alias macro; follow the alias link */
2477 SMacro *s;
2478
2479 smac->in_progress = true;
2480 s = define_smacro(smac->expansion->text, casesense,
2481 expansion, tmpl);
2482 smac->in_progress = false;
2483 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002484 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002485 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002486
2487 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002488 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002489 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002490 smac->expand = smacro_expand_default;
2491 if (tmpl) {
2492 smac->nparam = tmpl->nparam;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002493 smac->params = tmpl->params;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002494 smac->alias = tmpl->alias;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002495 smac->greedy = tmpl->greedy;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002496 if (tmpl->expand)
2497 smac->expand = tmpl->expand;
2498 }
2499 if (list_option('m')) {
2500 static const enum preproc_token op[2][2] = {
2501 { PP_DEFINE, PP_IDEFINE },
2502 { PP_DEFALIAS, PP_IDEFALIAS }
2503 };
2504 list_smacro_def(op[!!smac->alias][casesense], ctx, smac);
2505 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002506 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002507
2508fail:
2509 free_tlist(expansion);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002510 if (tmpl)
2511 free_smacro_members(tmpl);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002512 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002513}
2514
2515/*
2516 * Undefine an smacro
2517 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002518static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002519{
2520 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002521 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002522 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002523
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002524 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002525 smtbl = ctx ? &ctx->localmac : &smacros;
2526 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002527
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002528 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002529 /*
2530 * We now have a macro name... go hunt for it.
2531 */
2532 sp = smhead;
2533 while ((s = *sp) != NULL) {
2534 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002535 if (s->alias && !undefalias) {
2536 if (s->in_progress) {
2537 nasm_nonfatal("macro alias loop");
2538 } else {
2539 s->in_progress = true;
2540 undef_smacro(s->expansion->text, false);
2541 s->in_progress = false;
2542 }
2543 } else {
2544 if (list_option('m'))
2545 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2546 ctx, s);
2547 *sp = s->next;
2548 free_smacro(s);
2549 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002550 } else {
2551 sp = &s->next;
2552 }
2553 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002554 }
2555}
2556
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002557/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002558 * Parse a mmacro specification.
2559 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002560static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002561{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002562 tline = tline->next;
2563 skip_white_(tline);
2564 tline = expand_id(tline);
2565 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002566 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002567 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002568 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002569
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002570#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002571 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002572#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002573 def->name = nasm_strdup(tline->text);
2574 def->plus = false;
2575 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002576 def->nparam_min = 0;
2577 def->nparam_max = 0;
2578
H. Peter Anvina26433d2008-07-16 14:40:01 -07002579 tline = expand_smacro(tline->next);
2580 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002581 if (!tok_type_(tline, TOK_NUMBER))
2582 nasm_nonfatal("`%s' expects a parameter count", directive);
2583 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002584 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002585 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 tline = tline->next->next;
2587 if (tok_is_(tline, "*")) {
2588 def->nparam_max = INT_MAX;
2589 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002590 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002592 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002594 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002595 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 }
2597 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002598 }
2599 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 tline = tline->next;
2601 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002602 }
2603 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002604 !nasm_stricmp(tline->next->text, ".nolist")) {
2605 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002606 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002607 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608
H. Peter Anvina26433d2008-07-16 14:40:01 -07002609 /*
2610 * Handle default parameters.
2611 */
2612 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 def->dlist = tline->next;
2614 tline->next = NULL;
2615 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002616 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002617 def->dlist = NULL;
2618 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002619 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002620 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002621
H. Peter Anvin89cee572009-07-15 09:16:54 -04002622 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002623 !def->plus) {
2624 /*
2625 *!macro-defaults [on] macros with more default than optional parameters
2626 *! warns when a macro has more default parameters than optional parameters.
2627 *! See \k{mlmacdef} for why might want to disable this warning.
2628 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002629 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002630 "too many default macro parameters in macro `%s'", def->name);
2631 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002632
H. Peter Anvina26433d2008-07-16 14:40:01 -07002633 return true;
2634}
2635
2636
2637/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002638 * Decode a size directive
2639 */
2640static int parse_size(const char *str) {
2641 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002643 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002645 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002646}
2647
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002648/*
2649 * Process a preprocessor %pragma directive. Currently there are none.
2650 * Gets passed the token list starting with the "preproc" token from
2651 * "%pragma preproc".
2652 */
2653static void do_pragma_preproc(Token *tline)
2654{
2655 /* Skip to the real stuff */
2656 tline = tline->next;
2657 skip_white_(tline);
2658 if (!tline)
2659 return;
2660
2661 (void)tline; /* Nothing else to do at present */
2662}
2663
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002664static bool is_macro_id(const Token *t)
2665{
2666 return t && (t->type == TOK_ID ||
2667 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2668}
2669
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002670static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002671{
2672 char *id;
2673 Token *t = *tp;
2674
2675 t = t->next; /* Skip directive */
2676 skip_white_(t);
2677 t = expand_id(t);
2678
2679 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002680 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002681 err ? err : "macro identifier");
2682 return NULL;
2683 }
2684
2685 id = t->text;
2686 skip_white_(t);
2687 *tp = t;
2688 return id;
2689}
2690
Ed Beroset3ab3f412002-06-11 03:31:49 +00002691/**
2692 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002693 * Find out if a line contains a preprocessor directive, and deal
2694 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002695 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002696 * If a directive _is_ found, it is the responsibility of this routine
2697 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002698 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002699 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002700 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002701 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002702 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002703 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002704static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002705{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002706 enum preproc_token i;
2707 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002708 bool err;
2709 int nparam;
2710 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002711 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002712 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002713 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002714 char *p, *pp;
2715 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002716 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002717 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002718 Include *inc;
2719 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002720 Cond *cond;
2721 MMacro *mmac, **mmhead;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07002722 Token *t = NULL, *tt, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002723 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002724 struct tokenval tokval;
2725 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002726 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002727 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002728 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002729 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002730
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002731 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002732 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002733
H. Peter Anvineba20a72002-04-30 20:53:55 +00002734 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002735 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002736 (tline->text[0] && (tline->text[1] == '%' ||
2737 tline->text[1] == '$' ||
2738 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002740
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002741 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002742 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002743
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002744 casesense = true;
2745 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2746 casesense = false;
2747 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002748 }
2749
2750 /*
2751 * If we're in a non-emitting branch of a condition construct,
2752 * or walking to the end of an already terminated %rep block,
2753 * we should ignore all directives except for condition
2754 * directives.
2755 */
2756 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002757 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2758 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002759 return NO_DIRECTIVE_FOUND;
2760 }
2761
2762 /*
2763 * If we're defining a macro or reading a %rep block, we should
2764 * ignore all directives except for %macro/%imacro (which nest),
2765 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2766 * If we're in a %rep block, another %rep nests, so should be let through.
2767 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002768 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002769 i != PP_ENDMACRO && i != PP_ENDM &&
2770 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2771 return NO_DIRECTIVE_FOUND;
2772 }
2773
2774 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002775 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002776 nested_mac_count++;
2777 return NO_DIRECTIVE_FOUND;
2778 } else if (nested_mac_count > 0) {
2779 if (i == PP_ENDMACRO) {
2780 nested_mac_count--;
2781 return NO_DIRECTIVE_FOUND;
2782 }
2783 }
2784 if (!defining->name) {
2785 if (i == PP_REP) {
2786 nested_rep_count++;
2787 return NO_DIRECTIVE_FOUND;
2788 } else if (nested_rep_count > 0) {
2789 if (i == PP_ENDREP) {
2790 nested_rep_count--;
2791 return NO_DIRECTIVE_FOUND;
2792 }
2793 }
2794 }
2795 }
2796
H. Peter Anvin4169a472007-09-12 01:29:43 +00002797 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002798 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002799 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002801
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002802 case PP_PRAGMA:
2803 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002804 * %pragma namespace options...
2805 *
2806 * The namespace "preproc" is reserved for the preprocessor;
2807 * all other namespaces generate a [pragma] assembly directive.
2808 *
2809 * Invalid %pragmas are ignored and may have different
2810 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002811 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002812 t = tline;
2813 tline = tline->next;
2814 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002815 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002816 while (tok_type_(tline, TOK_WHITESPACE)) {
2817 t = tline;
2818 tline = tline->next;
2819 delete_Token(t);
2820 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002821 if (tok_type_(tline, TOK_ID)) {
2822 if (!nasm_stricmp(tline->text, "preproc")) {
2823 /* Preprocessor pragma */
2824 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002825 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002826 } else {
2827 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002828
2829 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002830 for (t = tline; t->next; t = t->next)
2831 ;
2832 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002833
2834 /* Prepend "[pragma " */
2835 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2836 t = new_Token(t, TOK_ID, "pragma", 6);
2837 t = new_Token(t, TOK_OTHER, "[", 1);
2838 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002839 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002840 }
2841 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002842 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002843
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 case PP_STACKSIZE:
2845 /* Directive to tell NASM what the default stack size is. The
2846 * default is for a 16-bit stack, and this can be overriden with
2847 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 */
2849 tline = tline->next;
2850 if (tline && tline->type == TOK_WHITESPACE)
2851 tline = tline->next;
2852 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002853 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 }
2855 if (nasm_stricmp(tline->text, "flat") == 0) {
2856 /* All subsequent ARG directives are for a 32-bit stack */
2857 StackSize = 4;
2858 StackPointer = "ebp";
2859 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002860 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002861 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2862 /* All subsequent ARG directives are for a 64-bit stack */
2863 StackSize = 8;
2864 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002865 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002866 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002867 } else if (nasm_stricmp(tline->text, "large") == 0) {
2868 /* All subsequent ARG directives are for a 16-bit stack,
2869 * far function call.
2870 */
2871 StackSize = 2;
2872 StackPointer = "bp";
2873 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002874 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 } else if (nasm_stricmp(tline->text, "small") == 0) {
2876 /* All subsequent ARG directives are for a 16-bit stack,
2877 * far function call. We don't support near functions.
2878 */
2879 StackSize = 2;
2880 StackPointer = "bp";
2881 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002882 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002884 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002886 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002887
H. Peter Anvine2c80182005-01-15 22:15:51 +00002888 case PP_ARG:
2889 /* TASM like ARG directive to define arguments to functions, in
2890 * the following form:
2891 *
2892 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2893 */
2894 offset = ArgOffset;
2895 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002896 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002898
H. Peter Anvine2c80182005-01-15 22:15:51 +00002899 /* Find the argument name */
2900 tline = tline->next;
2901 if (tline && tline->type == TOK_WHITESPACE)
2902 tline = tline->next;
2903 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002904 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002905 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002906 }
2907 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002908
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 /* Find the argument size type */
2910 tline = tline->next;
2911 if (!tline || tline->type != TOK_OTHER
2912 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002913 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002914 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002915 }
2916 tline = tline->next;
2917 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002918 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002919 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002921
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002923 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002925 size = parse_size(tt->text);
2926 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002927 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002929 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 }
2931 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002932
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002933 /* Round up to even stack slots */
2934 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002935
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 /* Now define the macro for the argument */
2937 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2938 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002939 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002940 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002941
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 /* Move to the next argument in the list */
2943 tline = tline->next;
2944 if (tline && tline->type == TOK_WHITESPACE)
2945 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002946 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002947 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002948 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002949
H. Peter Anvine2c80182005-01-15 22:15:51 +00002950 case PP_LOCAL:
2951 /* TASM like LOCAL directive to define local variables for a
2952 * function, in the following form:
2953 *
2954 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2955 *
2956 * The '= LocalSize' at the end is ignored by NASM, but is
2957 * required by TASM to define the local parameter size (and used
2958 * by the TASM macro package).
2959 */
2960 offset = LocalOffset;
2961 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002962 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002964
H. Peter Anvine2c80182005-01-15 22:15:51 +00002965 /* Find the argument name */
2966 tline = tline->next;
2967 if (tline && tline->type == TOK_WHITESPACE)
2968 tline = tline->next;
2969 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002970 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002971 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 }
2973 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002974
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 /* Find the argument size type */
2976 tline = tline->next;
2977 if (!tline || tline->type != TOK_OTHER
2978 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002979 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002980 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 }
2982 tline = tline->next;
2983 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002984 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002985 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002987
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002989 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002991 size = parse_size(tt->text);
2992 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002993 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002995 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 }
2997 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002998
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002999 /* Round up to even stack slots */
3000 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003001
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003002 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003003
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003004 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003005 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
3006 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003007 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003008
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 /* Now define the assign to setup the enter_c macro correctly */
3010 snprintf(directive, sizeof(directive),
3011 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07003012 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003013
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 /* Move to the next argument in the list */
3015 tline = tline->next;
3016 if (tline && tline->type == TOK_WHITESPACE)
3017 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08003018 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003019 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003020 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003021
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 case PP_CLEAR:
3023 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003024 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003025 free_macros();
3026 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003027 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003028
H. Peter Anvin418ca702008-05-30 10:42:30 -07003029 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003030 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003031 skip_white_(t);
3032 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003033 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003034 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003035 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003036 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003037 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003038 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003040 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003041 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03003042 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003043 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003044
3045 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003046 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003047 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07003048
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003049 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003050 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003051 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003052 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003054 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003055 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003056 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07003057 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003058 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003059 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003061 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07003062 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08003063 (pp_mode == PP_DEPS)
3064 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003065 if (!inc->fp) {
3066 /* -MG given but file not found */
3067 nasm_free(inc);
3068 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04003069 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003070 inc->lineno = src_set_linnum(0);
3071 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003072 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003073 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003074 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003075 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003076 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003077
H. Peter Anvind2456592008-06-19 15:04:18 -07003078 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003079 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003080 const struct use_package *pkg;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003081
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003082 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003083 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07003084 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003085 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003086 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003087 nasm_unquote_cstr(tline->text, NULL);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003088 pkg = nasm_find_use_package(tline->text);
3089 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003090 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003091 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003092 /*
3093 * Not already included, go ahead and include it.
3094 * Treat it as an include file for the purpose of
3095 * producing a listing.
3096 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07003097 use_loaded[pkg->index] = true;
3098 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07003099 nasm_new(inc);
3100 inc->next = istk;
3101 inc->fname = src_set_fname(NULL);
3102 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07003103 inc->nolist = !list_option('b') || istk->nolist;
3104 istk = inc;
3105 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003106 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003107 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07003108 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07003111 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 tline = tline->next;
3113 skip_white_(tline);
3114 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003115 if (tline) {
3116 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003117 nasm_nonfatal("`%s' expects a context identifier",
3118 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003119 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003120 }
3121 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003122 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003123 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003124 p = nasm_strdup(tline->text);
3125 } else {
3126 p = NULL; /* Anonymous */
3127 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07003128
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003129 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08003130 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003131 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003133 ctx->name = p;
3134 ctx->number = unique++;
3135 cstk = ctx;
3136 } else {
3137 /* %pop or %repl */
3138 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003139 nasm_nonfatal("`%s': context stack is empty",
3140 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003141 } else if (i == PP_POP) {
3142 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003143 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003144 "expected %s",
3145 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003146 else
3147 ctx_pop();
3148 } else {
3149 /* i == PP_REPL */
3150 nasm_free(cstk->name);
3151 cstk->name = p;
3152 p = NULL;
3153 }
3154 nasm_free(p);
3155 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003156 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003157 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003158 severity = ERR_FATAL;
3159 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003161 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003162 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003163 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003164 /*!
3165 *!user [on] %warning directives
3166 *! controls output of \c{%warning} directives (see \k{pperror}).
3167 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003168 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003170
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003172 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003173 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 tline->next = expand_smacro(tline->next);
3175 tline = tline->next;
3176 skip_white_(tline);
3177 t = tline ? tline->next : NULL;
3178 skip_white_(t);
3179 if (tok_type_(tline, TOK_STRING) && !t) {
3180 /* The line contains only a quoted string */
3181 p = tline->text;
3182 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003183 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 } else {
3185 /* Not a quoted string, or more than a quoted string */
3186 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003187 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003188 nasm_free(p);
3189 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003190 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003191 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003192
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003193 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003194 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003195 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003196 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003197 j = if_condition(tline->next, i);
3198 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003199 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003200 cond = nasm_malloc(sizeof(Cond));
3201 cond->next = istk->conds;
3202 cond->state = j;
3203 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003204 if(istk->mstk.mstk)
3205 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003206 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003207
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003208 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003209 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003210 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003211 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003212 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003213 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003214 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003215
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003216 case COND_DONE:
3217 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003218 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003219
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003220 case COND_ELSE_TRUE:
3221 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003222 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003223 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003224 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003225 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003226
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003227 case COND_IF_FALSE:
3228 /*
3229 * IMPORTANT: In the case of %if, we will already have
3230 * called expand_mmac_params(); however, if we're
3231 * processing an %elif we must have been in a
3232 * non-emitting mode, which would have inhibited
3233 * the normal invocation of expand_mmac_params().
3234 * Therefore, we have to do it explicitly here.
3235 */
3236 j = if_condition(expand_mmac_params(tline->next), i);
3237 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003238 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003239 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003240 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003241 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003242
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 case PP_ELSE:
3244 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003245 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003246 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003247 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003248 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003249 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003250 case COND_IF_TRUE:
3251 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003252 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003253 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003254
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003255 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003256 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003257
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003258 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003259 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003260 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003261
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003262 case COND_ELSE_TRUE:
3263 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003264 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003265 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003266 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003267 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003268 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003269 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003270
H. Peter Anvine2c80182005-01-15 22:15:51 +00003271 case PP_ENDIF:
3272 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003273 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003274 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003275 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003276 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003277 cond = istk->conds;
3278 istk->conds = cond->next;
3279 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003280 if(istk->mstk.mstk)
3281 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003282 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003283
H. Peter Anvin8b262472019-02-26 14:00:54 -08003284 case PP_RMACRO:
3285 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003286 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003287 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003288 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003289 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003290 if (i == PP_RMACRO)
3291 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003292 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003293 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003294 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003295 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003296
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003297 src_get(&defining->xline, &defining->fname);
3298
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003299 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3300 while (mmac) {
3301 if (!strcmp(mmac->name, defining->name) &&
3302 (mmac->nparam_min <= defining->nparam_max
3303 || defining->plus)
3304 && (defining->nparam_min <= mmac->nparam_max
3305 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003306 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003307 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003308 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003309 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003310 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003311 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003312 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003313
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 case PP_ENDM:
3315 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003316 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003317 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003318 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003319 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003320 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3321 defining->next = *mmhead;
3322 *mmhead = defining;
3323 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003324 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003325
H. Peter Anvin89cee572009-07-15 09:16:54 -04003326 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003327 /*
3328 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003329 * macro-end marker for a macro with a name. Then we
3330 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003331 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003332 list_for_each(l, istk->expansion)
3333 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003334 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003335
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003336 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003337 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003338 * Remove all conditional entries relative to this
3339 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003340 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003341 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3342 cond = istk->conds;
3343 istk->conds = cond->next;
3344 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003345 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003346 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003347 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003348 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003349 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003350 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003351
H. Peter Anvina26433d2008-07-16 14:40:01 -07003352 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003353 casesense = false;
3354 /* fall through */
3355 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003356 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003357 MMacro **mmac_p;
3358 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003359
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003360 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003361 spec.casesense = casesense;
3362 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003363 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003364 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003365 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3366 while (mmac_p && *mmac_p) {
3367 mmac = *mmac_p;
3368 if (mmac->casesense == spec.casesense &&
3369 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3370 mmac->nparam_min == spec.nparam_min &&
3371 mmac->nparam_max == spec.nparam_max &&
3372 mmac->plus == spec.plus) {
3373 *mmac_p = mmac->next;
3374 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003375 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003376 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003377 }
3378 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003379 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003380 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003381 }
3382
H. Peter Anvine2c80182005-01-15 22:15:51 +00003383 case PP_ROTATE:
3384 if (tline->next && tline->next->type == TOK_WHITESPACE)
3385 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003386 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003387 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003388 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 return DIRECTIVE_FOUND;
3390 }
3391 t = expand_smacro(tline->next);
3392 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003393 pps.tptr = tline = t;
3394 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 tokval.t_type = TOKEN_INVALID;
3396 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003397 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398 free_tlist(tline);
3399 if (!evalresult)
3400 return DIRECTIVE_FOUND;
3401 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003402 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003404 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 return DIRECTIVE_FOUND;
3406 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003407 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003408 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003409 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003410 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003411 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003412 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003413 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003414
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003415 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003416 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003417 rotate += mmac->nparam;
3418
3419 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003420 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003421 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003422
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003424 {
3425 MMacro *tmp_defining;
3426
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003427 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 do {
3429 tline = tline->next;
3430 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003431
H. Peter Anvine2c80182005-01-15 22:15:51 +00003432 if (tok_type_(tline, TOK_ID) &&
3433 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003434 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435 do {
3436 tline = tline->next;
3437 } while (tok_type_(tline, TOK_WHITESPACE));
3438 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003439
H. Peter Anvine2c80182005-01-15 22:15:51 +00003440 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003441 pps.tptr = expand_smacro(tline);
3442 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003443 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003444 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003446 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003447 if (!evalresult)
3448 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003450 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003451 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003452 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003453 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003455 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003456 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003457 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3458 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003459 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003460 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003461 /*!
3462 *!negative-rep [on] regative %rep count
3463 *! warns about negative counts given to the \c{%rep}
3464 *! preprocessor directive.
3465 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003466 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003467 "negative `%%rep' count: %"PRId64, count);
3468 count = 0;
3469 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003470 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003471 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003473 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003474 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003475 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003476 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003477 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003478 defining->nolist = nolist;
3479 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003480 defining->mstk = istk->mstk;
3481 defining->dstk.mstk = tmp_defining;
3482 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003483 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003484 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003485 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003486
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003488 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003489 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003490 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003491 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003492
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 /*
3494 * Now we have a "macro" defined - although it has no name
3495 * and we won't be entering it in the hash tables - we must
3496 * push a macro-end marker for it on to istk->expansion.
3497 * After that, it will take care of propagating itself (a
3498 * macro-end marker line for a macro which is really a %rep
3499 * block will cause the macro to be re-expanded, complete
3500 * with another macro-end marker to ensure the process
3501 * continues) until the whole expansion is forcibly removed
3502 * from istk->expansion by a %exitrep.
3503 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003504 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003505 l->next = istk->expansion;
3506 l->finishes = defining;
3507 l->first = NULL;
3508 istk->expansion = l;
3509
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003510 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003511
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003512 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003513 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003514 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003515
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003517 /*
3518 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003519 * macro-end marker for a macro with no name. Then we set
3520 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003521 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003522 list_for_each(l, istk->expansion)
3523 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003524 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003525
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003526 if (l)
3527 l->finishes->in_progress = 1;
3528 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003529 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003530 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003531
H. Peter Anvin8b262472019-02-26 14:00:54 -08003532 case PP_DEFINE:
3533 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003534 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003535 {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003536 SMacro tmpl;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003537 Token **lastp;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003538
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003539 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003540 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003541
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003542 nasm_zero(tmpl);
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003543 lastp = &tline->next;
3544 nparam = parse_smacro_template(&lastp, &tmpl);
3545 tline = *lastp;
3546 *lastp = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003547
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003548 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003549 macro_start = tline;
3550 if (!is_macro_id(macro_start)) {
3551 nasm_nonfatal("`%s' expects a macro identifier to alias",
3552 dname);
3553 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003555 tt = macro_start->next;
3556 macro_start->next = NULL;
3557 tline = tline->next;
3558 skip_white_(tline);
3559 if (tline && tline->type) {
3560 nasm_warn(WARN_OTHER,
3561 "trailing garbage after aliasing identifier ignored");
3562 }
3563 free_tlist(tt);
3564 tmpl.alias = true;
3565 } else {
3566 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003567 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003568 tline = expand_smacro(tline);
3569
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003570 /* Reverse expansion list and mark parameter tokens */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003571 macro_start = NULL;
3572 t = tline;
3573 while (t) {
3574 if (t->type == TOK_ID) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07003575 for (i = 0; i < nparam; i++) {
3576 if ((size_t)tmpl.params[i].namelen == t->len &&
3577 !memcmp(t->text, tmpl.params[i].name, t->len)) {
3578 t->type = tok_smac_param(i);
3579 break;
3580 }
3581 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003582 }
3583 tt = t->next;
3584 t->next = macro_start;
3585 macro_start = t;
3586 t = tt;
3587 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003589
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 /*
3591 * Good. We now have a macro name, a parameter count, and a
3592 * token list (in reverse order) for an expansion. We ought
3593 * to be OK just to create an SMacro, store it, and let
3594 * free_tlist have the rest of the line (which we have
3595 * carefully re-terminated after chopping off the expansion
3596 * from the end).
3597 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003598 define_smacro(mname, casesense, macro_start, &tmpl);
3599 break;
3600 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003601
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003603 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003604 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003605 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003606 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003607 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003608
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003609 undef_smacro(mname, i == PP_UNDEFALIAS);
3610 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003611
H. Peter Anvin8b262472019-02-26 14:00:54 -08003612 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003613 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003614 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003615
H. Peter Anvin9e200162008-06-04 17:23:14 -07003616 last = tline;
3617 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003618 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003619
3620 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003621 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003622
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003623 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003624 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003625 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003626
3627 /*
3628 * We now have a macro name, an implicit parameter count of
3629 * zero, and a string token to use as an expansion. Create
3630 * and store an SMacro.
3631 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003632 define_smacro(mname, casesense, macro_start, NULL);
3633 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003634
H. Peter Anvin8b262472019-02-26 14:00:54 -08003635 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003636 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003637 goto done;
3638
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003639 last = tline;
3640 tline = expand_smacro(tline->next);
3641 last->next = NULL;
3642
3643 t = tline;
3644 while (tok_type_(t, TOK_WHITESPACE))
3645 t = t->next;
3646 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003647 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003648 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003649 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003650 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003651 }
3652
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003653 /*
3654 * Convert the string to a token stream. Note that smacros
3655 * are stored with the token stream reversed, so we have to
3656 * reverse the output of tokenize().
3657 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003658 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003659 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003660
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003661 /*
3662 * We now have a macro name, an implicit parameter count of
3663 * zero, and a numeric token to use as an expansion. Create
3664 * and store an SMacro.
3665 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003666 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003667 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003668 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003669
H. Peter Anvin418ca702008-05-30 10:42:30 -07003670 case PP_PATHSEARCH:
3671 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003672 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003673
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003674 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003675 goto done;
3676
H. Peter Anvin418ca702008-05-30 10:42:30 -07003677 last = tline;
3678 tline = expand_smacro(tline->next);
3679 last->next = NULL;
3680
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003681 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003682 while (tok_type_(t, TOK_WHITESPACE))
3683 t = t->next;
3684
3685 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003686 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003687 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003688 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003689 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003690 }
3691 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003692 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003693 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003694 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003695 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003696
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003697 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003698 if (!found_path)
3699 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003700 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003701
3702 /*
3703 * We now have a macro name, an implicit parameter count of
3704 * zero, and a string token to use as an expansion. Create
3705 * and store an SMacro.
3706 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003707 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003708 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003709 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003710 }
3711
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003713 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003714 goto done;
3715
H. Peter Anvine2c80182005-01-15 22:15:51 +00003716 last = tline;
3717 tline = expand_smacro(tline->next);
3718 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003719
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720 t = tline;
3721 while (tok_type_(t, TOK_WHITESPACE))
3722 t = t->next;
3723 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003724 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003725 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003726 free_tlist(tline);
3727 free_tlist(origline);
3728 return DIRECTIVE_FOUND;
3729 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003730
H. Peter Anvin8b262472019-02-26 14:00:54 -08003731 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003732
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 /*
3734 * We now have a macro name, an implicit parameter count of
3735 * zero, and a numeric token to use as an expansion. Create
3736 * and store an SMacro.
3737 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003738 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003739 free_tlist(tline);
3740 free_tlist(origline);
3741 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003742
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003743 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003744 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003745 goto done;
3746
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003747 last = tline;
3748 tline = expand_smacro(tline->next);
3749 last->next = NULL;
3750
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003751 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003752 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003753 switch (t->type) {
3754 case TOK_WHITESPACE:
3755 break;
3756 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003757 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003758 break;
3759 case TOK_OTHER:
3760 if (!strcmp(t->text, ",")) /* permit comma separators */
3761 break;
3762 /* else fall through */
3763 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003764 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003765 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003766 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003767 }
3768 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003769
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003770 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003771 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003772 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003773 memcpy(p, t->text, t->len);
3774 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003775 }
3776 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003777
3778 /*
3779 * We now have a macro name, an implicit parameter count of
3780 * zero, and a numeric token to use as an expansion. Create
3781 * and store an SMacro.
3782 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003783 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003784 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003785 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003786 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003787 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003788
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003790 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003791 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003792 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003793
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003794 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003795 goto done;
3796
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 last = tline;
3798 tline = expand_smacro(tline->next);
3799 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003800
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003801 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003802 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003803 while (tok_type_(t, TOK_WHITESPACE))
3804 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003805
H. Peter Anvine2c80182005-01-15 22:15:51 +00003806 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003807 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003808 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003809 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003810 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003811 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003812
H. Peter Anvin8b262472019-02-26 14:00:54 -08003813 pps.tptr = t->next;
3814 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003815 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003816 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003817 if (!evalresult) {
3818 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003819 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003820 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003821 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003822 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003823 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003824 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003825 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003826
H. Peter Anvin8b262472019-02-26 14:00:54 -08003827 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3828 pps.tptr = pps.tptr->next;
3829 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003830 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003831 } else {
3832 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003833 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003834 if (!evalresult) {
3835 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003836 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003837 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003838 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003839 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003840 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003841 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003842 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003843 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003844
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003846
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003847 /* make start and count being in range */
3848 if (start < 0)
3849 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003850 if (count < 0)
3851 count = len + count + 1 - start;
3852 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003853 count = len - start;
3854 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003855 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003856
H. Peter Anvin8b262472019-02-26 14:00:54 -08003857 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003858 macro_start->len = count;
3859 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3860 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003861
H. Peter Anvine2c80182005-01-15 22:15:51 +00003862 /*
3863 * We now have a macro name, an implicit parameter count of
3864 * zero, and a numeric token to use as an expansion. Create
3865 * and store an SMacro.
3866 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003867 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003869 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003870 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003871
H. Peter Anvin8b262472019-02-26 14:00:54 -08003872 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003873 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003874 goto done;
3875
H. Peter Anvine2c80182005-01-15 22:15:51 +00003876 last = tline;
3877 tline = expand_smacro(tline->next);
3878 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003879
H. Peter Anvin8b262472019-02-26 14:00:54 -08003880 pps.tptr = tline;
3881 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003882 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003883 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003884 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003885 if (!evalresult)
3886 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003887
H. Peter Anvine2c80182005-01-15 22:15:51 +00003888 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003889 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003890
H. Peter Anvine2c80182005-01-15 22:15:51 +00003891 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003892 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003893 free_tlist(origline);
3894 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003895 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003896
H. Peter Anvin8b262472019-02-26 14:00:54 -08003897 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003898
H. Peter Anvine2c80182005-01-15 22:15:51 +00003899 /*
3900 * We now have a macro name, an implicit parameter count of
3901 * zero, and a numeric token to use as an expansion. Create
3902 * and store an SMacro.
3903 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003904 define_smacro(mname, casesense, macro_start, NULL);
3905 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003906
H. Peter Anvine2c80182005-01-15 22:15:51 +00003907 case PP_LINE:
3908 /*
3909 * Syntax is `%line nnn[+mmm] [filename]'
3910 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003911 if (unlikely(pp_noline))
3912 goto done;
3913
H. Peter Anvine2c80182005-01-15 22:15:51 +00003914 tline = tline->next;
3915 skip_white_(tline);
3916 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003917 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003918 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003919 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003920 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003921 m = 1;
3922 tline = tline->next;
3923 if (tok_is_(tline, "+")) {
3924 tline = tline->next;
3925 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003926 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003927 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003928 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003929 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003930 tline = tline->next;
3931 }
3932 skip_white_(tline);
3933 src_set_linnum(k);
3934 istk->lineinc = m;
3935 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003936 char *fname = detoken(tline, false);
3937 src_set_fname(fname);
3938 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003939 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003940 break;
3941 }
3942
3943done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003944 free_tlist(origline);
3945 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003946}
3947
3948/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003949 * Ensure that a macro parameter contains a condition code and
3950 * nothing else. Return the condition code index if so, or -1
3951 * otherwise.
3952 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003953static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003954{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003955 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003956
H. Peter Anvin25a99342007-09-22 17:45:45 -07003957 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003958 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003959
H. Peter Anvineba20a72002-04-30 20:53:55 +00003960 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003961 if (!t)
3962 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003963 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003964 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003965 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003966 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003967 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003968 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003969
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003970 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003971}
3972
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003973/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003974 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003975 * pasting, if @handle_explicit passed then explicit pasting
3976 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003977 * The @m array can contain a series of token types which are
3978 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003979 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003980static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003981 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003982{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003983 Token *tok, *next, **prev_next, **prev_nonspace;
3984 bool pasted = false;
3985 char *buf, *p;
3986 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003987
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003988 /*
3989 * The last token before pasting. We need it
3990 * to be able to connect new handled tokens.
3991 * In other words if there were a tokens stream
3992 *
3993 * A -> B -> C -> D
3994 *
3995 * and we've joined tokens B and C, the resulting
3996 * stream should be
3997 *
3998 * A -> BC -> D
3999 */
4000 tok = *head;
4001 prev_next = NULL;
4002
4003 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
4004 prev_nonspace = head;
4005 else
4006 prev_nonspace = NULL;
4007
4008 while (tok && (next = tok->next)) {
4009
4010 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07004011 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004012 /* Zap redundant whitespaces */
4013 while (tok_type_(next, TOK_WHITESPACE))
4014 next = delete_Token(next);
4015 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004016 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004017
4018 case TOK_PASTE:
4019 /* Explicit pasting */
4020 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004021 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004022 next = delete_Token(tok);
4023
4024 while (tok_type_(next, TOK_WHITESPACE))
4025 next = delete_Token(next);
4026
4027 if (!pasted)
4028 pasted = true;
4029
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004030 /* Left pasting token is start of line */
4031 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004032 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004033
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04004034 /*
4035 * No ending token, this might happen in two
4036 * cases
4037 *
4038 * 1) There indeed no right token at all
4039 * 2) There is a bare "%define ID" statement,
4040 * and @ID does expand to whitespace.
4041 *
4042 * So technically we need to do a grammar analysis
4043 * in another stage of parsing, but for now lets don't
4044 * change the behaviour people used to. Simply allow
4045 * whitespace after paste token.
4046 */
4047 if (!next) {
4048 /*
4049 * Zap ending space tokens and that's all.
4050 */
4051 tok = (*prev_nonspace)->next;
4052 while (tok_type_(tok, TOK_WHITESPACE))
4053 tok = delete_Token(tok);
4054 tok = *prev_nonspace;
4055 tok->next = NULL;
4056 break;
4057 }
4058
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004059 tok = *prev_nonspace;
4060 while (tok_type_(tok, TOK_WHITESPACE))
4061 tok = delete_Token(tok);
4062 len = strlen(tok->text);
4063 len += strlen(next->text);
4064
4065 p = buf = nasm_malloc(len + 1);
4066 strcpy(p, tok->text);
4067 p = strchr(p, '\0');
4068 strcpy(p, next->text);
4069
4070 delete_Token(tok);
4071
4072 tok = tokenize(buf);
4073 nasm_free(buf);
4074
4075 *prev_nonspace = tok;
4076 while (tok && tok->next)
4077 tok = tok->next;
4078
4079 tok->next = delete_Token(next);
4080
4081 /* Restart from pasted tokens head */
4082 tok = *prev_nonspace;
4083 break;
4084
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004085 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004086 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004087 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004088 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4089 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004090
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004091 len = 0;
4092 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4093 len += strlen(next->text);
4094 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004095 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004096
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004097 /* No match or no text to process */
4098 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004099 break;
4100
4101 len += strlen(tok->text);
4102 p = buf = nasm_malloc(len + 1);
4103
Adam Majer1a069432017-07-25 11:12:35 +02004104 strcpy(p, tok->text);
4105 p = strchr(p, '\0');
4106 tok = delete_Token(tok);
4107
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004108 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004109 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4110 strcpy(p, tok->text);
4111 p = strchr(p, '\0');
4112 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004113 tok = delete_Token(tok);
4114 }
4115
4116 tok = tokenize(buf);
4117 nasm_free(buf);
4118
4119 if (prev_next)
4120 *prev_next = tok;
4121 else
4122 *head = tok;
4123
4124 /*
4125 * Connect pasted into original stream,
4126 * ie A -> new-tokens -> B
4127 */
4128 while (tok && tok->next)
4129 tok = tok->next;
4130 tok->next = next;
4131
4132 if (!pasted)
4133 pasted = true;
4134
4135 /* Restart from pasted tokens head */
4136 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004137 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004138
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004139 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004140 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004141
4142 prev_next = &tok->next;
4143
4144 if (tok->next &&
4145 !tok_type_(tok->next, TOK_WHITESPACE) &&
4146 !tok_type_(tok->next, TOK_PASTE))
4147 prev_nonspace = prev_next;
4148
4149 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004150 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004151
4152 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004153}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004154
4155/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004156 * Computes the proper rotation of mmacro parameters
4157 */
4158static int mmac_rotate(const MMacro *mac, unsigned int n)
4159{
4160 if (--n < mac->nparam)
4161 n = (n + mac->rotate) % mac->nparam;
4162
4163 return n+1;
4164}
4165
4166/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004167 * expands to a list of tokens from %{x:y}
4168 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004169static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004170{
4171 Token *t = tline, **tt, *tm, *head;
4172 char *pos;
4173 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004174
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004175 pos = strchr(tline->text, ':');
4176 nasm_assert(pos);
4177
4178 lst = atoi(pos + 1);
4179 fst = atoi(tline->text + 1);
4180
4181 /*
4182 * only macros params are accounted so
4183 * if someone passes %0 -- we reject such
4184 * value(s)
4185 */
4186 if (lst == 0 || fst == 0)
4187 goto err;
4188
4189 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004190 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4191 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004192 goto err;
4193
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004194 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4195 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004196
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004197 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004198 fst--, lst--;
4199
4200 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004201 * It will be at least one token. Note we
4202 * need to scan params until separator, otherwise
4203 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004205 j = (fst + mac->rotate) % mac->nparam;
4206 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004207 if (!tm)
4208 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004209 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004210 tt = &head->next, tm = tm->next;
4211 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004212 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004213 *tt = t, tt = &t->next, tm = tm->next;
4214 }
4215
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004216 if (fst < lst) {
4217 for (i = fst + 1; i <= lst; i++) {
4218 t = new_Token(NULL, TOK_OTHER, ",", 0);
4219 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004220 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004221 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004222 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004223 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004224 *tt = t, tt = &t->next, tm = tm->next;
4225 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004226 }
4227 } else {
4228 for (i = fst - 1; i >= lst; i--) {
4229 t = new_Token(NULL, TOK_OTHER, ",", 0);
4230 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004231 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004232 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004233 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004234 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004235 *tt = t, tt = &t->next, tm = tm->next;
4236 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004237 }
4238 }
4239
4240 *last = tt;
4241 return head;
4242
4243err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004244 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004246 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004247}
4248
H. Peter Anvin76690a12002-04-30 20:52:49 +00004249/*
4250 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004251 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004252 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004253 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004254static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004255{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004256 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004257 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004258 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004259
4260 tail = &thead;
4261 thead = NULL;
4262
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004264 bool change;
4265 Token *t = tline;
4266 char *text = t->text;
4267 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004268
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004269 tline = tline->next;
4270 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004271
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004272 switch (type) {
4273 case TOK_PREPROC_ID:
4274 {
4275 Token *tt = NULL;
4276
4277 change = false;
4278
4279 if (!text || !text[0])
4280 break;
4281 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4282 ((text[1] == '+' || text[1] == '-') && text[2])))
4283 break;
4284
4285 change = true;
4286
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004287 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004288 nasm_nonfatal("`%s': not in a macro call", text);
4289 text = NULL;
4290 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004291 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004292
4293 if (strchr(text, ':')) {
4294 /*
4295 * seems we have a parameters range here
4296 */
4297 Token *head, **last;
4298 head = expand_mmac_params_range(mac, t, &last);
4299 if (head) {
4300 *tail = head;
4301 *last = tline;
4302 text = NULL;
4303 }
4304 break;
4305 }
4306
4307 switch (text[1]) {
4308 /*
4309 * We have to make a substitution of one of the
4310 * forms %1, %-1, %+1, %%foo, %0, %00.
4311 */
4312 case '0':
4313 if (!text[2]) {
4314 type = TOK_NUMBER;
4315 text = nasm_asprintf("%d", mac->nparam);
4316 break;
4317 }
4318 if (text[2] != '0' || text[3])
4319 goto invalid;
4320 /* a possible captured label == mac->params[0] */
4321 /* fall through */
4322 default:
4323 {
4324 unsigned long n;
4325 char *ep;
4326
4327 n = strtoul(text + 1, &ep, 10);
4328 if (unlikely(*ep))
4329 goto invalid;
4330
4331 if (n <= mac->nparam) {
4332 n = mmac_rotate(mac, n);
4333 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4334 }
4335 text = NULL;
4336 break;
4337 }
4338 case '%':
4339 type = TOK_ID;
4340 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4341 break;
4342 case '-':
4343 case '+':
4344 {
4345 int cc;
4346 unsigned long n;
4347 char *ep;
4348
4349 text = NULL;
4350
4351 n = strtoul(t->text + 2, &ep, 10);
4352 if (unlikely(*ep))
4353 goto invalid;
4354
4355 if (n && n < mac->nparam) {
4356 n = mmac_rotate(mac, n);
4357 tt = mac->params[n];
4358 }
4359 cc = find_cc(tt);
4360 if (cc == -1) {
4361 nasm_nonfatal("macro parameter `%s' is not a condition code",
4362 text);
4363 text = NULL;
4364 break;
4365 }
4366
4367 type = TOK_ID;
4368 if (text[1] == '-') {
4369 int ncc = inverse_ccs[cc];
4370 if (unlikely(ncc == -1)) {
4371 nasm_nonfatal("condition code `%s' is not invertible",
4372 conditions[cc]);
4373 break;
4374 }
4375 cc = ncc;
4376 }
4377 text = nasm_strdup(conditions[cc]);
4378 break;
4379 }
4380
4381 invalid:
4382 nasm_nonfatal("invalid macro parameter: `%s'", text);
4383 text = NULL;
4384 break;
4385 }
4386 break;
4387 }
4388
4389 case TOK_PREPROC_Q:
4390 if (mac) {
4391 type = TOK_ID;
4392 text = nasm_strdup(mac->iname);
4393 change = true;
4394 }
4395 break;
4396
4397 case TOK_PREPROC_QQ:
4398 if (mac) {
4399 type = TOK_ID;
4400 text = nasm_strdup(mac->name);
4401 change = true;
4402 }
4403 break;
4404
4405 case TOK_INDIRECT:
4406 {
4407 Token *tt;
4408
4409 tt = tokenize(t->text);
4410 tt = expand_mmac_params(tt);
4411 tt = expand_smacro(tt);
4412 /* Why dup_tlist() here? We should own tt... */
4413 dup_tlist(tt, &tail);
4414 text = NULL;
4415 change = true;
4416 break;
4417 }
4418
4419 default:
4420 change = false;
4421 break;
4422 }
4423
4424 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004425 if (!text) {
4426 delete_Token(t);
4427 } else {
4428 *tail = t;
4429 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004430 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004431 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004432 t->type = type;
4433 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004434 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004435 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004436 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004437 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004438 tail = &t->next;
4439 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004440 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004441
H. Peter Anvineba20a72002-04-30 20:53:55 +00004442 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004443
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004444 if (changed) {
4445 const struct tokseq_match t[] = {
4446 {
4447 PP_CONCAT_MASK(TOK_ID) |
4448 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4449 PP_CONCAT_MASK(TOK_ID) |
4450 PP_CONCAT_MASK(TOK_NUMBER) |
4451 PP_CONCAT_MASK(TOK_FLOAT) |
4452 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4453 },
4454 {
4455 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4456 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4457 }
4458 };
4459 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4460 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004461
H. Peter Anvin76690a12002-04-30 20:52:49 +00004462 return thead;
4463}
4464
H. Peter Anvin322bee02019-08-10 01:38:06 -07004465static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004466
H. Peter Anvin76690a12002-04-30 20:52:49 +00004467/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004468 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004469 * a macro at all, it is simply copied to the output and the pointer
4470 * advanced. tpp should be a pointer to a pointer (usually the next
4471 * pointer of the previous token) to the first token. **tpp is updated
4472 * to point to the last token of the expansion, and *tpp updated to
4473 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004474 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004475 * If the expansion is empty, *tpp will be unchanged but **tpp will
4476 * be advanced past the macro call.
4477 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004478 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004479 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004480static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004481{
4482 Token **params = NULL;
4483 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004484 Token *tline = **tpp;
4485 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004486 SMacro *head, *m;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004487 int i;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004488 Token *t, *tup, *ttail;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004489 int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004490
4491 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004492 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004493
4494 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004495
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004496 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004497 smacro_deadman.levels--;
4498
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004499 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004500 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004501 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004502 smacro_deadman.triggered = true;
4503 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004504 goto not_a_macro;
4505 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004506 head = (SMacro *)hash_findix(&smacros, mname);
4507 } else if (tline->type == TOK_PREPROC_ID) {
4508 Context *ctx = get_ctx(mname, &mname);
4509 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4510 } else {
4511 goto not_a_macro;
4512 }
4513
4514 /*
4515 * We've hit an identifier of some sort. First check whether the
4516 * identifier is a single-line macro at all, then think about
4517 * checking for parameters if necessary.
4518 */
4519 list_for_each(m, head) {
4520 if (!mstrcmp(m->name, mname, m->casesense))
4521 break;
4522 }
4523
4524 if (!m) {
4525 goto not_a_macro;
4526 }
4527
4528 /* Parse parameters, if applicable */
4529
4530 params = NULL;
4531 nparam = 0;
4532
4533 if (m->nparam == 0) {
4534 /*
4535 * Simple case: the macro is parameterless.
4536 * Nothing to parse; the expansion code will
4537 * drop the macro name token.
4538 */
4539 } else {
4540 /*
4541 * Complicated case: at least one macro with this name
4542 * exists and takes parameters. We must find the
4543 * parameters in the call, count them, find the SMacro
4544 * that corresponds to that form of the macro call, and
4545 * substitute for the parameters when we expand. What a
4546 * pain.
4547 */
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004548 Token *t;
4549 int paren, brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004550
4551 tline = tline->next;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004552 skip_white_(tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004553 if (!tok_is_(tline, "(")) {
4554 /*
4555 * This macro wasn't called with parameters: ignore
4556 * the call. (Behaviour borrowed from gnu cpp.)
4557 */
4558 goto not_a_macro;
4559 }
4560
4561 paren = 1;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004562 nparam = 1;
4563 t = tline; /* tline points to leading ( */
4564
4565 while (paren) {
4566 t = t->next;
4567
4568 if (!t) {
4569 nasm_nonfatal("macro call expects terminating `)'");
4570 goto not_a_macro;
4571 }
4572
4573 if (tline->type != TOK_OTHER || tline->len != 1)
4574 continue;
4575
4576 switch (tline->text[0]) {
4577 case ',':
4578 if (!brackets)
4579 nparam++;
4580 break;
4581
4582 case '{':
4583 brackets++;
4584 break;
4585
4586 case '}':
4587 if (brackets > 0)
4588 brackets--;
4589 break;
4590
4591 case '(':
4592 if (!brackets)
4593 paren++;
4594 break;
4595
4596 case ')':
4597 if (!brackets)
4598 paren--;
4599 break;
4600
4601 default:
4602 break; /* Normal token */
4603 }
4604 }
4605
4606 /*
4607 * Look for a macro matching in both name and parameter count.
4608 * We already know any matches cannot be anywhere before the
4609 * current position of "m", so there is no reason to
4610 * backtrack.
4611 */
4612 while (1) {
4613 if (!m) {
4614 /*!
4615 *!macro-params-single [on] single-line macro calls with wrong parameter count
4616 *! warns about \i{single-line macros} being invoked
4617 *! with the wrong number of parameters.
4618 */
4619 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4620 "single-line macro `%s' exists, "
4621 "but not taking %d parameter%s",
4622 mname, nparam, (nparam == 1) ? "" : "s");
4623 goto not_a_macro;
4624 }
4625
4626 if (!mstrcmp(m->name, mname, m->casesense)) {
4627 if (m->nparam == nparam)
4628 break; /* It's good */
4629 if (m->greedy && m->nparam < nparam)
4630 break; /* Also good */
4631 }
4632 m = m->next;
4633 }
4634 }
4635
4636 if (m->in_progress)
4637 goto not_a_macro;
4638
4639 /* Expand the macro */
4640 m->in_progress = true;
4641
4642 nparam = m->nparam; /* If greedy, some parameters might be joint */
4643
4644 if (nparam) {
4645 /* Extract parameters */
4646 Token **phead, **pep;
4647 int white = 0;
4648 int brackets = 0;
4649 int paren;
4650 bool bracketed = false;
4651 bool bad_bracket = false;
4652 enum sparmflags flags;
4653
4654 nparam = m->nparam;
4655
4656 paren = 1;
4657 nasm_newn(params, nparam);
4658 i = 0;
4659 flags = m->params[i].flags;
4660 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004661 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004662
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004663 while (paren) {
4664 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004665 char ch;
4666
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004667 tline = tline->next;
4668
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004669 if (!tline)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004670 nasm_nonfatal("macro call expects terminating `)'");
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004671
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004672 ch = 0;
4673 skip = false;
4674
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004675
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004676 switch (tline->type) {
4677 case TOK_OTHER:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004678 if (tline->len == 1)
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004679 ch = tline->text[0];
4680 break;
4681
4682 case TOK_WHITESPACE:
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004683 if (!(flags & SPARM_NOSTRIP)) {
4684 if (brackets || *phead)
4685 white++; /* Keep interior whitespace */
4686 skip = true;
4687 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004688 break;
4689
4690 default:
4691 break;
4692 }
4693
4694 switch (ch) {
4695 case ',':
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004696 if (!brackets && !(flags & SPARM_GREEDY)) {
4697 i++;
4698 nasm_assert(i < nparam);
4699 phead = pep = &params[i];
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004700 *pep = NULL;
4701 bracketed = false;
4702 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004703 flags = m->params[i].flags;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004704 }
4705 break;
4706
4707 case '{':
4708 if (!bracketed) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004709 bracketed = !*phead && !(flags & SPARM_NOSTRIP);
4710 skip = bracketed;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004711 }
4712 brackets++;
4713 break;
4714
4715 case '}':
4716 if (brackets > 0) {
4717 if (!--brackets)
4718 skip = bracketed;
4719 }
4720 break;
4721
4722 case '(':
4723 if (!brackets)
4724 paren++;
4725 break;
4726
4727 case ')':
4728 if (!brackets) {
4729 paren--;
4730 if (!paren) {
4731 skip = true;
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004732 i++; /* Found last argument */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004733 }
4734 }
4735 break;
4736
4737 default:
4738 break; /* Normal token */
4739 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004740
4741 if (!skip) {
4742 Token *t;
4743
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004744 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004745
4746 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004747 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004748 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004749 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004750 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004751 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004752 pep = &t->next;
4753 white = 0;
4754 }
4755 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004756
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004757 nasm_assert(i == nparam);
4758
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004759 /*
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004760 * Possible further processing of parameters. Note that the
4761 * ordering matters here.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004762 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004763 for (i = 0; i < nparam; i++) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004764 enum sparmflags flags = m->params[i].flags;
4765
4766 if (flags & SPARM_EVAL) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004767 /* Evaluate this parameter as a number */
4768 struct ppscan pps;
4769 struct tokenval tokval;
4770 expr *evalresult;
4771 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004772
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004773 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4774 pps.ntokens = -1;
4775 tokval.t_type = TOKEN_INVALID;
4776 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004777
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004778 free_tlist(eval_param);
4779 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004780
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004781 if (!evalresult) {
4782 /* Nothing meaningful to do */
4783 } else if (tokval.t_type) {
4784 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4785 } else if (!is_simple(evalresult)) {
4786 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4787 } else {
4788 params[i] = make_tok_num(reloc_value(evalresult));
4789 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004790 }
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004791
4792 if (flags & SPARM_STR) {
4793 /* Convert expansion to a quoted string */
4794 char *arg;
4795 Token *qs;
4796
4797 qs = expand_smacro_noreset(params[i]);
4798 arg = detoken(qs, false);
4799 free_tlist(qs);
4800 params[i] = make_tok_qstr(arg);
4801 nasm_free(arg);
4802 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004803 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004804 }
4805
4806 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004807 tline = tline->next; /* Remove the macro call from the input */
4808 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004809
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004810 /* Note: we own the expansion this returns. */
4811 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004812
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004813 tup = NULL;
4814 ttail = NULL; /* Pointer to the last token of the expansion */
4815 while (t) {
4816 enum pp_token_type type = t->type;
4817 Token *tnext = t->next;
4818 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004819
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004820 switch (type) {
4821 case TOK_PREPROC_Q:
4822 case TOK_PREPROC_QQ:
4823 t->type = TOK_ID;
4824 nasm_free(t->text);
4825 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4826 t->len = nasm_last_string_len();
4827 t->next = tline;
4828 break;
4829
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004830 case TOK_ID:
4831 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004832 /*
4833 * Chain this into the target line *before* expanding,
4834 * that way we pick up any arguments to the new macro call,
4835 * if applicable.
4836 */
4837 t->next = tline;
4838 tp = &t;
4839 expand_one_smacro(&tp);
4840 if (t == tline)
4841 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004842 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004843
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004844 default:
4845 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07004846 int param = smac_nparam(t->type);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004847 nasm_assert(!tup && param < nparam);
4848 delete_Token(t);
4849 t = NULL;
4850 tup = tnext;
4851 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004852 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004853 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004854 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004855 }
4856
4857 if (t) {
4858 tline = t;
4859 if (!ttail)
4860 ttail = t;
4861 }
4862
4863 if (tnext) {
4864 t = tnext;
4865 } else {
4866 t = tup;
4867 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004868 }
4869 }
4870
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004871 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004872 if (ttail)
4873 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004874
4875 m->in_progress = false;
4876
4877 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004878 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004879 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004880
4881 /*
4882 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004883 * and then advance to the next input token. Note that this is
4884 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004885 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004886not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004887 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004888 m = NULL;
4889done:
4890 smacro_deadman.levels++;
4891 if (unlikely(params))
4892 free_tlist_array(params, nparam);
4893 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004894}
4895
4896/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004897 * Expand all single-line macro calls made in the given line.
4898 * Return the expanded version of the line. The original is deemed
4899 * to be destroyed in the process. (In reality we'll just move
4900 * Tokens from input to output a lot of the time, rather than
4901 * actually bothering to destroy and replicate.)
4902 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004903static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004904{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004905 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07004906 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4907 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004908 return expand_smacro_noreset(tline);
4909}
4910
4911static Token *expand_smacro_noreset(Token * tline)
4912{
4913 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004914 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004915 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004916
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004917 /*
4918 * Trick: we should avoid changing the start token pointer since it can
4919 * be contained in "next" field of other token. Because of this
4920 * we allocate a copy of first token and work with it; at the end of
4921 * routine we copy it back
4922 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004923 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004924 tline = new_Token(org_tline->next, org_tline->type,
4925 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004926 nasm_free(org_tline->text);
4927 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004928 }
4929
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004930
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004931 /*
4932 * Pretend that we always end up doing expansion on the first pass;
4933 * that way %+ get processed. However, if we process %+ before the
4934 * first pass, we end up with things like MACRO %+ TAIL trying to
4935 * look up the macro "MACROTAIL", which we don't want.
4936 */
4937 expanded = true;
4938 thead = tline;
4939 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004940 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004941 {
4942 PP_CONCAT_MASK(TOK_ID) |
4943 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4944 PP_CONCAT_MASK(TOK_ID) |
4945 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4946 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4947 }
4948 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004949
4950 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004951 while ((t = *tail)) /* main token loop */
4952 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004953
4954 if (!expanded) {
4955 tline = thead;
4956 break; /* Done! */
4957 }
4958
4959 /*
4960 * Now scan the entire line and look for successive TOK_IDs
4961 * that resulted after expansion (they can't be produced by
4962 * tokenize()). The successive TOK_IDs should be concatenated.
4963 * Also we look for %+ tokens and concatenate the tokens
4964 * before and after them (without white spaces in between).
4965 */
4966 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004967
4968 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004969 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004970
H. Peter Anvine2c80182005-01-15 22:15:51 +00004971 if (org_tline) {
4972 if (thead) {
4973 *org_tline = *thead;
4974 /* since we just gave text to org_line, don't free it */
4975 thead->text = NULL;
4976 delete_Token(thead);
4977 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004978 /*
4979 * The expression expanded to empty line;
4980 * we can't return NULL because of the "trick" above.
4981 * Just set the line to a single WHITESPACE token.
4982 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004983 memset(org_tline, 0, sizeof(*org_tline));
4984 org_tline->text = NULL;
4985 org_tline->type = TOK_WHITESPACE;
4986 }
4987 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004988 }
4989
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004990 return thead;
4991}
4992
4993/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004994 * Similar to expand_smacro but used exclusively with macro identifiers
4995 * right before they are fetched in. The reason is that there can be
4996 * identifiers consisting of several subparts. We consider that if there
4997 * are more than one element forming the name, user wants a expansion,
4998 * otherwise it will be left as-is. Example:
4999 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005000 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005001 *
5002 * the identifier %$abc will be left as-is so that the handler for %define
5003 * will suck it and define the corresponding value. Other case:
5004 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005005 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005006 *
5007 * In this case user wants name to be expanded *before* %define starts
5008 * working, so we'll expand %$abc into something (if it has a value;
5009 * otherwise it will be left as-is) then concatenate all successive
5010 * PP_IDs into one.
5011 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005012static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005013{
5014 Token *cur, *oldnext = NULL;
5015
H. Peter Anvin734b1882002-04-30 21:01:08 +00005016 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005017 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005018
5019 cur = tline;
5020 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005021 (cur->next->type == TOK_ID ||
5022 cur->next->type == TOK_PREPROC_ID
5023 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005024 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005025
5026 /* If identifier consists of just one token, don't expand */
5027 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005028 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005029
H. Peter Anvine2c80182005-01-15 22:15:51 +00005030 if (cur) {
5031 oldnext = cur->next; /* Detach the tail past identifier */
5032 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005033 }
5034
H. Peter Anvin734b1882002-04-30 21:01:08 +00005035 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005036
H. Peter Anvine2c80182005-01-15 22:15:51 +00005037 if (cur) {
5038 /* expand_smacro possibly changhed tline; re-scan for EOL */
5039 cur = tline;
5040 while (cur && cur->next)
5041 cur = cur->next;
5042 if (cur)
5043 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005044 }
5045
5046 return tline;
5047}
5048
5049/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005050 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005051 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005052 * to check for an initial label - that's taken care of in
5053 * expand_mmacro - but must check numbers of parameters. Guaranteed
5054 * to be called with tline->type == TOK_ID, so the putative macro
5055 * name is easy to find.
5056 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005057static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005058{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005059 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005060 Token **params;
5061 int nparam;
5062
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005063 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005064
5065 /*
5066 * Efficiency: first we see if any macro exists with the given
5067 * name. If not, we can return NULL immediately. _Then_ we
5068 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005069 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005070 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005071 list_for_each(m, head)
5072 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005073 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005074 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005075 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005076
5077 /*
5078 * OK, we have a potential macro. Count and demarcate the
5079 * parameters.
5080 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00005081 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005082
5083 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005084 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005085 * structure that handles this number.
5086 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005087 while (m) {
5088 if (m->nparam_min <= nparam
5089 && (m->plus || nparam <= m->nparam_max)) {
5090 /*
5091 * This one is right. Just check if cycle removal
5092 * prohibits us using it before we actually celebrate...
5093 */
5094 if (m->in_progress > m->max_depth) {
5095 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005096 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005097 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005098 }
5099 nasm_free(params);
5100 return NULL;
5101 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005102 /*
5103 * It's right, and we can use it. Add its default
5104 * parameters to the end of our list if necessary.
5105 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005106 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005107 params =
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005108 nasm_realloc(params, sizeof(*params) *
5109 (m->nparam_min + m->ndefs + 2));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005110 while (nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005111 nparam++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005112 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00005113 }
5114 }
5115 /*
5116 * If we've gone over the maximum parameter count (and
5117 * we're in Plus mode), ignore parameters beyond
5118 * nparam_max.
5119 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005120 if (m->plus && nparam > m->nparam_max)
5121 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005122
5123 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005124 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005125 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005126 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005127 }
5128 /*
5129 * This one wasn't right: look for the next one with the
5130 * same name.
5131 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 list_for_each(m, m->next)
5133 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00005134 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005135 }
5136
5137 /*
5138 * After all that, we didn't find one with the right number of
5139 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005140 *!
5141 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5142 *! warns about \i{multi-line macros} being invoked
5143 *! with the wrong number of parameters. See \k{mlmacover} for an
5144 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005145 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005146 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5147 "multi-line macro `%s' exists, but not taking %d parameter%s",
5148 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005149 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005150 return NULL;
5151}
5152
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005153
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005154#if 0
5155
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005156/*
5157 * Save MMacro invocation specific fields in
5158 * preparation for a recursive macro expansion
5159 */
5160static void push_mmacro(MMacro *m)
5161{
5162 MMacroInvocation *i;
5163
5164 i = nasm_malloc(sizeof(MMacroInvocation));
5165 i->prev = m->prev;
5166 i->params = m->params;
5167 i->iline = m->iline;
5168 i->nparam = m->nparam;
5169 i->rotate = m->rotate;
5170 i->paramlen = m->paramlen;
5171 i->unique = m->unique;
5172 i->condcnt = m->condcnt;
5173 m->prev = i;
5174}
5175
5176
5177/*
5178 * Restore MMacro invocation specific fields that were
5179 * saved during a previous recursive macro expansion
5180 */
5181static void pop_mmacro(MMacro *m)
5182{
5183 MMacroInvocation *i;
5184
5185 if (m->prev) {
5186 i = m->prev;
5187 m->prev = i->prev;
5188 m->params = i->params;
5189 m->iline = i->iline;
5190 m->nparam = i->nparam;
5191 m->rotate = i->rotate;
5192 m->paramlen = i->paramlen;
5193 m->unique = i->unique;
5194 m->condcnt = i->condcnt;
5195 nasm_free(i);
5196 }
5197}
5198
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005199#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005200
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005201/*
5202 * Expand the multi-line macro call made by the given line, if
5203 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005204 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005205 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005206static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005207{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005208 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005209 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005210 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005211 Token **params, *t, *tt;
5212 MMacro *m;
5213 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005214 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005215 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005216 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005217
5218 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005219 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005220 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005221 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005222 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005223 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005224 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005225 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005226 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005227 Token *last;
5228 /*
5229 * We have an id which isn't a macro call. We'll assume
5230 * it might be a label; we'll also check to see if a
5231 * colon follows it. Then, if there's another id after
5232 * that lot, we'll check it again for macro-hood.
5233 */
5234 label = last = t;
5235 t = t->next;
5236 if (tok_type_(t, TOK_WHITESPACE))
5237 last = t, t = t->next;
5238 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005239 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005240 last = t, t = t->next;
5241 if (tok_type_(t, TOK_WHITESPACE))
5242 last = t, t = t->next;
5243 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005244 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005245 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005246 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005247 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005248 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005249 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005250
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005251 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5252 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5253 if (!mmacro_deadman.triggered) {
5254 nasm_nonfatal("interminable multiline macro recursion");
5255 mmacro_deadman.triggered = true;
5256 }
5257 return 0;
5258 }
5259
5260 mmacro_deadman.total++;
5261 mmacro_deadman.levels++;
5262
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005263 /*
5264 * Fix up the parameters: this involves stripping leading and
5265 * trailing whitespace, then stripping braces if they are
5266 * present.
5267 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005268 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005269
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005270 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005271 int brace = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005272 int comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005273
H. Peter Anvine2c80182005-01-15 22:15:51 +00005274 skip_white_(t);
5275 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005276 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005277 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005278 while (t) {
5279 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5280 break; /* ... because we have hit a comma */
5281 if (comma && t->type == TOK_WHITESPACE
5282 && tok_is_(t->next, ","))
5283 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005284 if (brace && t->type == TOK_OTHER) {
5285 if (t->text[0] == '{')
5286 brace++; /* ... or a nested opening brace */
5287 else if (t->text[0] == '}')
5288 if (!--brace)
5289 break; /* ... or a brace */
5290 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005291 t = t->next;
5292 paramlen[i]++;
5293 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005294 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005295 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005296 }
5297
5298 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005299 * OK, we have a MMacro structure together with a set of
5300 * parameters. We must now go through the expansion and push
5301 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005302 * parameter tokens and macro-local tokens doesn't get done
5303 * until the single-line macro substitution process; this is
5304 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005305 * later through %rotate and give the right semantics for
5306 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005307 *
5308 * First, push an end marker on to istk->expansion, mark this
5309 * macro as in progress, and set up its invocation-specific
5310 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005311 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005312 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005313 ll->next = istk->expansion;
5314 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005315 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005316
5317 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005318 * Save the previous MMacro expansion in the case of
5319 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005320 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005321#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005322 if (m->max_depth && m->in_progress)
5323 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005324#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005325
5326 m->in_progress ++;
5327 m->params = params;
5328 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005329 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005330 m->nparam = nparam;
5331 m->rotate = 0;
5332 m->paramlen = paramlen;
5333 m->unique = unique++;
5334 m->lineno = 0;
5335 m->condcnt = 0;
5336
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005337 m->mstk = istk->mstk;
5338 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005339
5340 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005341 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005342 ll->next = istk->expansion;
5343 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005344 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005345 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005346
5347 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005348 * If we had a label, and this macro definition does not include
5349 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005350 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005351 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005352 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005353 /*
5354 * We had a label. If this macro contains an %00 parameter,
5355 * save the value as a special parameter (which is what it
5356 * is), otherwise push it as the first line of the macro
5357 * expansion.
5358 */
5359 if (m->capture_label) {
5360 params[0] = dup_Token(NULL, label);
5361 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005362 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005363 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005364 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005365 ll->finishes = NULL;
5366 ll->next = istk->expansion;
5367 istk->expansion = ll;
5368 ll->first = startline;
5369 if (!dont_prepend) {
5370 while (label->next)
5371 label = label->next;
5372 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005373 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005374 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005375 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005376
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005377 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005378
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005379 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005380}
5381
H. Peter Anvin130736c2016-02-17 20:27:41 -08005382/*
5383 * This function adds macro names to error messages, and suppresses
5384 * them if necessary.
5385 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005386static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005387{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005388 /*
5389 * If we're in a dead branch of IF or something like it, ignore the error.
5390 * However, because %else etc are evaluated in the state context
5391 * of the previous branch, errors might get lost:
5392 * %if 0 ... %else trailing garbage ... %endif
5393 * So %else etc should set the ERR_PP_PRECOND flag.
5394 */
5395 if ((severity & ERR_MASK) < ERR_FATAL &&
5396 istk && istk->conds &&
5397 ((severity & ERR_PP_PRECOND) ?
5398 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005399 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005400 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005401
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005402 /* This doesn't make sense with the macro stack unwinding */
5403 if (0) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005404 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005405
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005406 /* get %macro name */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005407 if (!(severity & ERR_NOFILE) && istk && istk->mstk.mmac) {
5408 MMacro *mmac = istk->mstk.mmac;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005409 char *buf;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005410
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005411 nasm_set_verror(real_verror);
5412 buf = nasm_vasprintf(fmt, arg);
5413 nasm_error(severity, "(%s:%"PRId32") %s",
5414 mmac->name, mmac->lineno - delta, buf);
5415 nasm_set_verror(pp_verror);
5416 nasm_free(buf);
5417 return;
5418 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005419 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005420 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005421}
5422
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005423static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005424stdmac_file(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005425{
5426 (void)s;
5427 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005428 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005429
5430 return make_tok_qstr(src_get_fname());
5431}
5432
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005433static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005434stdmac_line(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005435{
5436 (void)s;
5437 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005438 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005439
5440 return make_tok_num(src_get_linnum());
5441}
5442
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005443static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005444stdmac_bits(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005445{
5446 (void)s;
5447 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005448 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005449
5450 return make_tok_num(globalbits);
5451}
5452
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005453static Token *
H. Peter Anvin (Intel)62cf4aa2019-08-20 00:05:41 -07005454stdmac_ptr(const SMacro *s, Token **params, int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005455{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005456 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005457 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5458 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5459 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005460
5461 (void)s;
5462 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005463 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005464
5465 switch (globalbits) {
5466 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005467 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005468 break;
5469 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005470 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005471 break;
5472 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005473 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005474 break;
5475 default:
5476 panic();
5477 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005478
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005479 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005480}
5481
H. Peter Anvin8b262472019-02-26 14:00:54 -08005482/* Add magic standard macros */
5483struct magic_macros {
5484 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005485 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005486 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005487};
5488static const struct magic_macros magic_macros[] =
5489{
5490 { "__FILE__", 0, stdmac_file },
5491 { "__LINE__", 0, stdmac_line },
5492 { "__BITS__", 0, stdmac_bits },
5493 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005494 { NULL, 0, NULL }
5495};
5496
5497static void pp_add_magic_stdmac(void)
5498{
5499 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005500 SMacro tmpl;
5501
5502 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005503
5504 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005505 tmpl.nparam = m->nparam;
5506 tmpl.expand = m->func;
5507 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005508 }
5509}
5510
H. Peter Anvin734b1882002-04-30 21:01:08 +00005511static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005512pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005513{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005514 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005515 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005516
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005517 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005518 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005519 nested_mac_count = 0;
5520 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005521 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005522 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005523 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005524 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005525
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005526 if (!use_loaded)
5527 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5528 memset(use_loaded, 0, use_package_count * sizeof(bool));
5529
H. Peter Anvin6686de22019-08-10 05:33:14 -07005530 /* First set up the top level input file */
5531 nasm_new(istk);
5532 istk->fp = nasm_open_read(file, NF_TEXT);
5533 src_set(0, file);
5534 istk->lineinc = 1;
5535 if (!istk->fp)
5536 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5537
5538 strlist_add(deplist, file);
5539
5540 /*
5541 * Set up the stdmac packages as a virtual include file,
5542 * indicated by a null file pointer.
5543 */
5544 nasm_new(inc);
5545 inc->next = istk;
5546 inc->fname = src_set_fname(NULL);
5547 inc->nolist = !list_option('b');
5548 istk = inc;
5549 lfmt->uplevel(LIST_INCLUDE, 0);
5550
H. Peter Anvin8b262472019-02-26 14:00:54 -08005551 pp_add_magic_stdmac();
5552
H. Peter Anvinf7606612016-07-13 14:23:48 -07005553 if (tasm_compatible_mode)
5554 pp_add_stdmac(nasm_stdmac_tasm);
5555
5556 pp_add_stdmac(nasm_stdmac_nasm);
5557 pp_add_stdmac(nasm_stdmac_version);
5558
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005559 if (extrastdmac)
5560 pp_add_stdmac(extrastdmac);
5561
H. Peter Anvinf7606612016-07-13 14:23:48 -07005562 stdmacpos = stdmacros[0];
5563 stdmacnext = &stdmacros[1];
5564
H. Peter Anvind2456592008-06-19 15:04:18 -07005565 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005566
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005567 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005568 * Define the __PASS__ macro. This is defined here unlike all the
5569 * other builtins, because it is special -- it varies between
5570 * passes -- but there is really no particular reason to make it
5571 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005572 *
5573 * 0 = dependencies only
5574 * 1 = preparatory passes
5575 * 2 = final pass
5576 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005577 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005578 switch (mode) {
5579 case PP_NORMAL:
5580 apass = pass_final() ? 2 : 1;
5581 break;
5582 case PP_DEPS:
5583 apass = 0;
5584 break;
5585 case PP_PREPROC:
5586 apass = 3;
5587 break;
5588 default:
5589 panic();
5590 }
5591
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005592 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005593}
5594
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005595static void pp_init(void)
5596{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005597}
5598
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005599/*
5600 * Get a line of tokens. If we popped the macro expansion/include stack,
5601 * we return a pointer to the dummy token tok_pop; at that point if
5602 * istk is NULL then we have reached end of input;
5603 */
5604static Token tok_pop; /* Dummy token placeholder */
5605
5606static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005607{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005608 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005609 Line *l = istk->expansion;
5610 Token *tline = NULL;
5611 Token *dtline;
5612
H. Peter Anvine2c80182005-01-15 22:15:51 +00005613 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005614 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005615 * buffer or from the input file.
5616 */
5617 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005618 while (l && l->finishes) {
5619 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005620
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005621 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005622 /*
5623 * This is a macro-end marker for a macro with no
5624 * name, which means it's not really a macro at all
5625 * but a %rep block, and the `in_progress' field is
5626 * more than 1, meaning that we still need to
5627 * repeat. (1 means the natural last repetition; 0
5628 * means termination by %exitrep.) We have
5629 * therefore expanded up to the %endrep, and must
5630 * push the whole block on to the expansion buffer
5631 * again. We don't bother to remove the macro-end
5632 * marker: we'd only have to generate another one
5633 * if we did.
5634 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005635 fm->in_progress--;
5636 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005637 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005638 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005639
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005640 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005641 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005642 tail = &ll->first;
5643
5644 list_for_each(t, l->first) {
5645 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005646 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005647 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005648 }
5649 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005650 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005651 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005652 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005653 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005654 MMacro *m = istk->mstk.mstk;
5655
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005656 /*
5657 * Check whether a `%rep' was started and not ended
5658 * within this macro expansion. This can happen and
5659 * should be detected. It's a fatal error because
5660 * I'm too confused to work out how to recover
5661 * sensibly from it.
5662 */
5663 if (defining) {
5664 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005665 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005666 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005667 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005668 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005669 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005670
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005671 /*
5672 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005673 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005674 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005675 istk->mstk = m->mstk;
5676 if (m->name) {
5677 /*
5678 * This was a real macro call, not a %rep, and
5679 * therefore the parameter information needs to
5680 * be freed and the iteration count/nesting
5681 * depth adjusted.
5682 */
5683
5684 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005685 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005686 * If all mmacro processing done,
5687 * clear all counters and the deadman
5688 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005689 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005690 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005691 }
5692
Adam Majer91e72402017-07-25 10:42:01 +02005693#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005694 if (m->prev) {
5695 pop_mmacro(m);
5696 fm->in_progress --;
5697 } else
Adam Majer91e72402017-07-25 10:42:01 +02005698#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005699 {
5700 nasm_free(m->params);
5701 free_tlist(m->iline);
5702 nasm_free(m->paramlen);
5703 fm->in_progress = 0;
5704 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005705 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005706
5707 /*
5708 * FIXME It is incorrect to always free_mmacro here.
5709 * It leads to usage-after-free.
5710 *
5711 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5712 */
5713#if 0
5714 else
5715 free_mmacro(m);
5716#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005717 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005718 istk->expansion = l->next;
5719 nasm_free(l);
5720 lfmt->downlevel(LIST_MACRO);
5721 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005722 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005723
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005724 do { /* until we get a line we can use */
5725 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005726
5727 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005728 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005729 int32_t lineno;
5730
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005731 if (istk->mstk.mstk) {
5732 istk->mstk.mstk->lineno++;
5733 if (istk->mstk.mstk->fname)
5734 lineno = istk->mstk.mstk->lineno +
5735 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005736 else
5737 lineno = 0; /* Defined at init time or builtin */
5738 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005739 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005740 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005741
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005742 tline = l->first;
5743 istk->expansion = l->next;
5744 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005745
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005746 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005747 if (!istk->nolist)
5748 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005749 nasm_free(line);
5750 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005751 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005752 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005753 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005754 } else {
5755 /*
5756 * The current file has ended; work down the istk
5757 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005758 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005759 if (i->fp)
5760 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005761 if (i->conds) {
5762 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005763 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005764 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005765 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005766 if (i->next)
5767 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005768 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005769 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005770 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005771 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005772 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005773 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005774
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005775 /*
5776 * We must expand MMacro parameters and MMacro-local labels
5777 * _before_ we plunge into directive processing, to cope
5778 * with things like `%define something %1' such as STRUC
5779 * uses. Unless we're _defining_ a MMacro, in which case
5780 * those tokens should be left alone to go into the
5781 * definition; and unless we're in a non-emitting
5782 * condition, in which case we don't want to meddle with
5783 * anything.
5784 */
5785 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005786 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005787 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005788 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005789
H. Peter Anvine2c80182005-01-15 22:15:51 +00005790 /*
5791 * Check the line to see if it's a preprocessor directive.
5792 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005793 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5794 if (dtline)
5795 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005796 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005797 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005798 * We're defining a multi-line macro. We emit nothing
5799 * at all, and just
5800 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005801 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005802 MMacro *mmac = defining->dstk.mmac;
5803
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005804 Line *l = nasm_malloc(sizeof(Line));
5805 l->next = defining->expansion;
5806 l->first = tline;
5807 l->finishes = NULL;
5808 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005809
5810 /*
5811 * Remember if this mmacro expansion contains %00:
5812 * if it does, we will have to handle leading labels
5813 * specially.
5814 */
5815 if (mmac) {
5816 const Token *t;
5817 list_for_each(t, tline) {
5818 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
5819 mmac->capture_label = true;
5820 }
5821 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005822 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005823 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005824 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005825 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005826 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005827 * directive so we keep our place correctly.
5828 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005829 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005830 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005831 /*
5832 * We're in a %rep block which has been terminated, so
5833 * we're walking through to the %endrep without
5834 * emitting anything. Emit nothing at all, not even a
5835 * blank line: when we emerge from the %rep block we'll
5836 * give a line-number directive so we keep our place
5837 * correctly.
5838 */
5839 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005840 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005841 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005842 if (!expand_mmacro(tline))
5843 return tline;
5844 }
5845 }
5846}
5847
5848static char *pp_getline(void)
5849{
5850 char *line = NULL;
5851 Token *tline;
5852
5853 real_verror = nasm_set_verror(pp_verror);
5854
5855 while (true) {
5856 tline = pp_tokline();
5857 if (tline == &tok_pop) {
5858 /*
5859 * We popped the macro/include stack. If istk is empty,
5860 * we are at end of input, otherwise just loop back.
5861 */
5862 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005863 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005864 } else {
5865 /*
5866 * De-tokenize the line and emit it.
5867 */
5868 line = detoken(tline, true);
5869 free_tlist(tline);
5870 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005871 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005872 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005873
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005874 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005875 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005876 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005877 nasm_free(buf);
5878 }
5879
H. Peter Anvin130736c2016-02-17 20:27:41 -08005880 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005881 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005882}
5883
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005884static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005885{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005886 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005887
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005888 if (defining) {
5889 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005890 nasm_nonfatal("end of file while still defining macro `%s'",
5891 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005892 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005893 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005894 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005895
5896 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005897 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005898 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005899
5900 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005901
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005902 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005903 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005904 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005905 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005906 Include *i = istk;
5907 istk = istk->next;
5908 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005909 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005910 }
5911 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005912 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005913 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005914}
5915
5916static void pp_cleanup_session(void)
5917{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005918 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005919 free_llist(predef);
5920 predef = NULL;
5921 delete_Blocks();
5922 freeTokens = NULL;
5923 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005924}
5925
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005926static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005927{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005928 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005929}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005930
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005931static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005932{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005933 Token *inc, *space, *name;
5934 Line *l;
5935
H. Peter Anvin734b1882002-04-30 21:01:08 +00005936 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5937 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5938 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005939
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005940 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005941 l->next = predef;
5942 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005943 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005944 predef = l;
5945}
5946
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005947static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005948{
5949 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005950 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005951 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005952
H. Peter Anvin130736c2016-02-17 20:27:41 -08005953 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005954
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005955 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005956 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5957 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005958 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005959 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005960 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005961 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005962 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005963
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005964 if (space->next->type != TOK_PREPROC_ID &&
5965 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005966 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005967
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005968 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005969 l->next = predef;
5970 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005971 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005972 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005973
5974 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005975}
5976
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005977static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005978{
5979 Token *def, *space;
5980 Line *l;
5981
H. Peter Anvin734b1882002-04-30 21:01:08 +00005982 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5983 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005984 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005985
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005986 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005987 l->next = predef;
5988 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005989 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005990 predef = l;
5991}
5992
H. Peter Anvin05990342018-06-11 13:32:42 -07005993/* Insert an early preprocessor command that doesn't need special handling */
5994static void pp_pre_command(const char *what, char *string)
5995{
5996 char *cmd;
5997 Token *def, *space;
5998 Line *l;
5999
6000 def = tokenize(string);
6001 if (what) {
6002 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
6003 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
6004 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
6005 }
6006
6007 l = nasm_malloc(sizeof(Line));
6008 l->next = predef;
6009 l->first = def;
6010 l->finishes = NULL;
6011 predef = l;
6012}
6013
H. Peter Anvinf7606612016-07-13 14:23:48 -07006014static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006015{
H. Peter Anvinf7606612016-07-13 14:23:48 -07006016 macros_t **mp;
6017
6018 /* Find the end of the list and avoid duplicates */
6019 for (mp = stdmacros; *mp; mp++) {
6020 if (*mp == macros)
6021 return; /* Nothing to do */
6022 }
6023
6024 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
6025
6026 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00006027}
6028
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006029static void pp_extra_stdmac(macros_t *macros)
6030{
6031 extrastdmac = macros;
6032}
6033
H. Peter Anvin8b262472019-02-26 14:00:54 -08006034static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00006035{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04006036 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08006037 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
6038 return new_Token(NULL, TOK_NUMBER, numbuf, len);
6039}
6040
6041static Token *make_tok_qstr(const char *str)
6042{
6043 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07006044 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08006045 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00006046}
6047
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006048static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07006049{
6050 if (!m)
6051 return;
6052
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006053 /* We need to print the mstk.mmac list in reverse order */
6054 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07006055
6056 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07006057 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08006058 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07006059 }
6060}
6061
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08006062static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006063{
H. Peter Anvinddb29062018-12-11 00:06:29 -08006064 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006065
H. Peter Anvinddb29062018-12-11 00:06:29 -08006066 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
6067 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006068
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03006069 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07006070 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006071
H. Peter Anvinddb29062018-12-11 00:06:29 -08006072 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006073}
6074
H. Peter Anvine7469712016-02-18 02:20:59 -08006075const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07006076 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006077 pp_reset,
6078 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08006079 pp_cleanup_pass,
6080 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03006081 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04006082 pp_pre_define,
6083 pp_pre_undefine,
6084 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07006085 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07006086 pp_include_path,
6087 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006088};