blob: 585843a47933b429516406e5b95a9decae91caa5 [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)1c21a532019-08-09 02:34:21 -0700101typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params,
102 unsigned int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800108 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800109 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700110 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700111 ExpandSMacro expand;
112 intorptr expandpvt;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800113 bool *eval_param;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800114 unsigned int nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800115 bool casesense;
116 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700117 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000118};
119
120/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800121 * Store the definition of a multi-line macro. This is also used to
122 * store the interiors of `%rep...%endrep' blocks, which are
123 * effectively self-re-invoking multi-line macros which simply
124 * don't have a name or bother to appear in the hash tables. %rep
125 * blocks are signified by having a NULL `name' field.
126 *
127 * In a MMacro describing a `%rep' block, the `in_progress' field
128 * isn't merely boolean, but gives the number of repeats left to
129 * run.
130 *
131 * The `next' field is used for storing MMacros in hash tables; the
132 * `next_active' field is for stacking them on istk entries.
133 *
134 * When a MMacro is being expanded, `params', `iline', `nparam',
135 * `paramlen', `rotate' and `unique' are local to the invocation.
136 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700137
138/*
139 * Expansion stack. Note that .mmac can point back to the macro itself,
140 * whereas .mstk cannot.
141 */
142struct mstk {
143 MMacro *mstk; /* Any expansion, real macro or not */
144 MMacro *mmac; /* Highest level actual mmacro */
145};
146
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800147struct MMacro {
148 MMacro *next;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700149#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800150 MMacroInvocation *prev; /* previous invocation */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700151#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800152 char *name;
153 int nparam_min, nparam_max;
154 bool casesense;
155 bool plus; /* is the last parameter greedy? */
156 bool nolist; /* is this macro listing-inhibited? */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700157 bool capture_label; /* macro definition has %00; capture label */
158 int32_t in_progress; /* is this macro currently being expanded? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800159 int32_t max_depth; /* maximum number of recursive expansions allowed */
160 Token *dlist; /* All defaults as one list */
161 Token **defaults; /* Parameter default pointers */
162 int ndefs; /* number of default parameters */
163 Line *expansion;
164
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700165 struct mstk mstk; /* Macro expansion stack */
166 struct mstk dstk; /* Macro definitions stack */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800167 Token **params; /* actual parameters */
168 Token *iline; /* invocation line */
169 unsigned int nparam, rotate;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700170 char *iname; /* name invoked as */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800171 int *paramlen;
172 uint64_t unique;
173 int lineno; /* Current line number on expansion */
174 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700175
H. Peter Anvin274cda82016-05-10 02:56:29 -0700176 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700177 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800178};
179
180
181/* Store the definition of a multi-line macro, as defined in a
182 * previous recursive macro expansion.
183 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700184#if 0
185
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800186struct MMacroInvocation {
187 MMacroInvocation *prev; /* previous invocation */
188 Token **params; /* actual parameters */
189 Token *iline; /* invocation line */
190 unsigned int nparam, rotate;
191 int *paramlen;
192 uint64_t unique;
193 uint64_t condcnt;
194};
195
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700196#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800197
198/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000199 * The context stack is composed of a linked list of these.
200 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000201struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800202 Context *next;
203 char *name;
204 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700205 uint64_t number;
206 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000207};
208
209/*
210 * This is the internal form which we break input lines up into.
211 * Typically stored in linked lists.
212 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800213 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
214 * not necessarily used as-is, but is also used to encode the number
215 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000216 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800217 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700218 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000219 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800220 * tok_smac_param(0) but the one representing `y' will be
221 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000222 *
223 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
224 * which doesn't need quotes around it. Used in the pre-include
225 * mechanism as an alternative to trying to find a sensible type of
226 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000227 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000228enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800229 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
230 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800231 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700232 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800233 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300234 TOK_PASTE, /* %+ */
235 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800236 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300237 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000238};
239
H. Peter Anvin8b262472019-02-26 14:00:54 -0800240static inline enum pp_token_type tok_smac_param(int param)
241{
242 return TOK_SMAC_START_PARAMS + param;
243}
244static int smac_nparam(enum pp_token_type toktype)
245{
246 return toktype - TOK_SMAC_START_PARAMS;
247}
248static bool is_smac_param(enum pp_token_type toktype)
249{
250 return toktype >= TOK_SMAC_START_PARAMS;
251}
252
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400253#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400254#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400255
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400256struct tokseq_match {
257 int mask_head;
258 int mask_tail;
259};
260
H. Peter Anvine2c80182005-01-15 22:15:51 +0000261struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800262 Token *next;
263 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700264 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800265 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000266};
267
268/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800269 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270 * these, which is essentially a container to allow several linked
271 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700272 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000273 * Note that in this module, linked lists are treated as stacks
274 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800275 * `expansion' field in MMacro structures, so that the linked list,
276 * if walked, would give the macro lines in reverse order; this
277 * means that we can walk the list when expanding a macro, and thus
278 * push the lines on to the `expansion' field in _istk_ in reverse
279 * order (so that when popped back off they are in the right
280 * order). It may seem cockeyed, and it relies on my design having
281 * an even number of steps in, but it works...
282 *
283 * Some of these structures, rather than being actual lines, are
284 * markers delimiting the end of the expansion of a given macro.
285 * This is for use in the cycle-tracking and %rep-handling code.
286 * Such structures have `finishes' non-NULL, and `first' NULL. All
287 * others have `finishes' NULL, but `first' may still be NULL if
288 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000290struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800291 Line *next;
292 MMacro *finishes;
293 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500294};
295
296/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000297 * To handle an arbitrary level of file inclusion, we maintain a
298 * stack (ie linked list) of these things.
299 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000300struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800301 Include *next;
302 FILE *fp;
303 Cond *conds;
304 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700305 const char *fname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700306 struct mstk mstk;
H. Peter Anvin6686de22019-08-10 05:33:14 -0700307 int lineno, lineinc;
308 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000309};
310
311/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700312 * File real name hash, so we don't have to re-search the include
313 * path for every pass (and potentially more than that if a file
314 * is used more than once.)
315 */
316struct hash_table FileHash;
317
318/*
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700319 * Counters to trap on insane macro recursion or processing.
320 * Note: for smacros these count *down*, for mmacros they count *up*.
321 */
322struct deadman {
323 int64_t total; /* Total number of macros/tokens */
324 int64_t levels; /* Descent depth across all macros */
325 bool triggered; /* Already triggered, no need for error msg */
326};
327
328static struct deadman smacro_deadman, mmacro_deadman;
329
330/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000331 * Conditional assembly: we maintain a separate stack of these for
332 * each level of file inclusion. (The only reason we keep the
333 * stacks separate is to ensure that a stray `%endif' in a file
334 * included from within the true branch of a `%if' won't terminate
335 * it and cause confusion: instead, rightly, it'll cause an error.)
336 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700337enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338 /*
339 * These states are for use just after %if or %elif: IF_TRUE
340 * means the condition has evaluated to truth so we are
341 * currently emitting, whereas IF_FALSE means we are not
342 * currently emitting but will start doing so if a %else comes
343 * up. In these states, all directives are admissible: %elif,
344 * %else and %endif. (And of course %if.)
345 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800346 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 /*
348 * These states come up after a %else: ELSE_TRUE means we're
349 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
350 * any %elif or %else will cause an error.
351 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800352 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200354 * These states mean that we're not emitting now, and also that
355 * nothing until %endif will be emitted at all. COND_DONE is
356 * used when we've had our moment of emission
357 * and have now started seeing %elifs. COND_NEVER is used when
358 * the condition construct in question is contained within a
359 * non-emitting branch of a larger condition construct,
360 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800362 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700364struct Cond {
365 Cond *next;
366 enum cond_state state;
367};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800368#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000369
H. Peter Anvin70653092007-10-19 14:42:29 -0700370/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000371 * These defines are used as the possible return values for do_directive
372 */
373#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300374#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000375
Keith Kanios852f1ee2009-07-12 00:19:55 -0500376/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000377 * Condition codes. Note that we use c_ prefix not C_ because C_ is
378 * used in nasm.h for the "real" condition codes. At _this_ level,
379 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
380 * ones, so we need a different enum...
381 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700382static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000383 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
384 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000385 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000386};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700387enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000388 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
389 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 -0700390 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
391 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000392};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700393static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000394 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
395 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 +0000396 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000397};
398
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800399/*
400 * Directive names.
401 */
402/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
403static int is_condition(enum preproc_token arg)
404{
405 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
406}
407
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000408/* For TASM compatibility we need to be able to recognise TASM compatible
409 * conditional compilation directives. Using the NASM pre-processor does
410 * not work, so we look for them specifically from the following list and
411 * then jam in the equivalent NASM directive into the input stream.
412 */
413
H. Peter Anvine2c80182005-01-15 22:15:51 +0000414enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000415 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
416 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
417};
418
H. Peter Anvin476d2862007-10-02 22:04:15 -0700419static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000420 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
421 "ifndef", "include", "local"
422};
423
424static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700425static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000426static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800427static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000428
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000429static Context *cstk;
430static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300431static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000432
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300433static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000434
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300435static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000436
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800437static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700438static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800439static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000440
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000441/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800442 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000443 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800444static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000445
446/*
447 * The current set of single-line macros we have defined.
448 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700449static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000450
451/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800452 * The multi-line macro we are currently defining, or the %rep
453 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000454 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800455static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000456
Charles Crayned4200be2008-07-12 16:42:33 -0700457static uint64_t nested_mac_count;
458static uint64_t nested_rep_count;
459
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000460/*
461 * The number of macro parameters to allocate space for at a time.
462 */
463#define PARAM_DELTA 16
464
465/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700466 * The standard macro set: defined in macros.c in a set of arrays.
467 * This gives our position in any macro set, while we are processing it.
468 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000469 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700470static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700471static macros_t **stdmacnext;
472static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300473static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000474
475/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700476 * Map of which %use packages have been loaded
477 */
478static bool *use_loaded;
479
480/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000481 * Tokens are allocated in blocks to improve speed
482 */
483#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800484static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000485struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000486 Blocks *next;
487 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000488};
489
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800490static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000491
492/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000493 * Forward declarations.
494 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700495static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000496static Token *expand_mmac_params(Token * tline);
497static Token *expand_smacro(Token * tline);
498static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400499static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800500static Token *make_tok_num(int64_t val);
501static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800502static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800503static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000504static void *new_Block(size_t size);
505static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700506static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700507 const char *text, size_t txtlen);
508static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000509static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000510
511/*
512 * Macros for safe checking of token pointers, avoid *(NULL)
513 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300514#define tok_type_(x,t) ((x) && (x)->type == (t))
515#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
516#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
517#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000518
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400519/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700520 * In-place reverse a list of tokens.
521 */
522static Token *reverse_tokens(Token *t)
523{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800524 Token *prev = NULL;
525 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700526
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800527 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400528 next = t->next;
529 t->next = prev;
530 prev = t;
531 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800532 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700533
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800534 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700535}
536
537/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300538 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000539 * front of them. We do it here because I could not find any other
540 * place to do it for the moment, and it is a hack (ideally it would
541 * be nice to be able to use the NASM pre-processor to do it).
542 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000543static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000544{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000545 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400546 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000547
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400548 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000549
550 /* Binary search for the directive name */
551 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400552 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400553 q = nasm_skip_word(p);
554 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000555 if (len) {
556 oldchar = p[len];
557 p[len] = 0;
558 while (j - i > 1) {
559 k = (j + i) / 2;
560 m = nasm_stricmp(p, tasm_directives[k]);
561 if (m == 0) {
562 /* We have found a directive, so jam a % in front of it
563 * so that NASM will then recognise it as one if it's own.
564 */
565 p[len] = oldchar;
566 len = strlen(p);
567 oldline = line;
568 line = nasm_malloc(len + 2);
569 line[0] = '%';
570 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700571 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300572 * NASM does not recognise IFDIFI, so we convert
573 * it to %if 0. This is not used in NASM
574 * compatible code, but does need to parse for the
575 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700577 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000578 } else {
579 memcpy(line + 1, p, len + 1);
580 }
581 nasm_free(oldline);
582 return line;
583 } else if (m < 0) {
584 j = k;
585 } else
586 i = k;
587 }
588 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000589 }
590 return line;
591}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000592
H. Peter Anvin76690a12002-04-30 20:52:49 +0000593/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000594 * The pre-preprocessing stage... This function translates line
595 * number indications as they emerge from GNU cpp (`# lineno "file"
596 * flags') into NASM preprocessor line number indications (`%line
597 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000598 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000599static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000600{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000601 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000602 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 if (line[0] == '#' && line[1] == ' ') {
605 oldline = line;
606 fname = oldline + 2;
607 lineno = atoi(fname);
608 fname += strspn(fname, "0123456789 ");
609 if (*fname == '"')
610 fname++;
611 fnlen = strcspn(fname, "\"");
612 line = nasm_malloc(20 + fnlen);
613 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
614 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000615 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000616 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000618 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000619}
620
621/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000622 * Free a linked list of tokens.
623 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000625{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400626 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000628}
629
630/*
631 * Free a linked list of lines.
632 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000633static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000634{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400635 Line *l, *tmp;
636 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637 free_tlist(l->first);
638 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000639 }
640}
641
642/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700643 * Free an array of linked lists of tokens
644 */
645static void free_tlist_array(Token **array, size_t nlists)
646{
647 Token **listp = array;
648
649 while (nlists--)
650 free_tlist(*listp++);
651
652 nasm_free(array);
653}
654
655/*
656 * Duplicate a linked list of tokens.
657 */
658static Token *dup_tlist(const Token *list, Token ***tailp)
659{
660 Token *newlist = NULL;
661 Token **tailpp = &newlist;
662 const Token *t;
663
664 list_for_each(t, list) {
665 Token *nt;
666 *tailpp = nt = dup_Token(NULL, t);
667 tailpp = &nt->next;
668 }
669
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700670 if (tailp) {
671 **tailp = newlist;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700672 *tailp = tailpp;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700673 }
674
675 return newlist;
676}
677
678/*
679 * Duplicate a linked list of tokens with a maximum count
680 */
681static Token *dup_tlistn(const Token *list, size_t cnt, Token ***tailp)
682{
683 Token *newlist = NULL;
684 Token **tailpp = &newlist;
685 const Token *t;
686
687 list_for_each(t, list) {
688 Token *nt;
689 if (!cnt--)
690 break;
691 *tailpp = nt = dup_Token(NULL, t);
692 tailpp = &nt->next;
693 }
694
695 if (tailp) {
696 **tailp = newlist;
697 *tailp = tailpp;
698 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700699
700 return newlist;
701}
702
703/*
704 * Duplicate a linked list of tokens in reverse order
705 */
706static Token *dup_tlist_reverse(const Token *list, Token *tail)
707{
708 const Token *t;
709
710 list_for_each(t, list)
711 tail = dup_Token(tail, t);
712
713 return tail;
714}
715
716/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800717 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000718 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800719static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000720{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800721 nasm_free(m->name);
722 free_tlist(m->dlist);
723 nasm_free(m->defaults);
724 free_llist(m->expansion);
725 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000726}
727
728/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700729 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800730 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700731static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800732{
733 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700734 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800735 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700736}
737
738static void clear_smacro(SMacro *s)
739{
740 free_smacro_members(s);
741 /* Wipe everything except the next pointer */
742 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
743}
744
745/*
746 * Free an SMacro
747 */
748static void free_smacro(SMacro *s)
749{
750 free_smacro_members(s);
751 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800752}
753
754/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700755 * Free all currently defined macros, and free the hash tables
756 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700757static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700758{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800759 struct hash_iterator it;
760 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700761
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800762 hash_for_each(smt, it, np) {
763 SMacro *tmp;
764 SMacro *s = np->data;
765 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800766 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700767 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700768 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700769 hash_free(smt);
770}
771
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800772static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700773{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800774 struct hash_iterator it;
775 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700776
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800777 hash_for_each(mmt, it, np) {
778 MMacro *tmp;
779 MMacro *m = np->data;
780 nasm_free((void *)np->key);
781 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800782 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700783 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800784 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700785}
786
787static void free_macros(void)
788{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700789 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800790 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700791}
792
793/*
794 * Initialize the hash tables
795 */
796static void init_macros(void)
797{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700798}
799
800/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000801 * Pop the context stack.
802 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000804{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000805 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000806
807 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700808 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000809 nasm_free(c->name);
810 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000811}
812
H. Peter Anvin072771e2008-05-22 13:17:51 -0700813/*
814 * Search for a key in the hash index; adding it if necessary
815 * (in which case we initialize the data pointer to NULL.)
816 */
817static void **
818hash_findi_add(struct hash_table *hash, const char *str)
819{
820 struct hash_insert hi;
821 void **r;
822 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800823 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700824
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800825 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700826 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300827 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700828
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800829 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
830 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700831 return hash_add(&hi, strx, NULL);
832}
833
834/*
835 * Like hash_findi, but returns the data element rather than a pointer
836 * to it. Used only when not adding a new element, hence no third
837 * argument.
838 */
839static void *
840hash_findix(struct hash_table *hash, const char *str)
841{
842 void **p;
843
844 p = hash_findi(hash, str, NULL);
845 return p ? *p : NULL;
846}
847
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400848/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800849 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400850 * if there no more left -- return NULL
851 */
852static char *line_from_stdmac(void)
853{
854 unsigned char c;
855 const unsigned char *p = stdmacpos;
856 char *line, *q;
857 size_t len = 0;
858
859 if (!stdmacpos)
860 return NULL;
861
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700862 /*
863 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
864 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
865 */
866 while ((c = *p++) != 127) {
867 uint8_t ndir = c - 128;
868 if (ndir < 256-96)
869 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400870 else
871 len++;
872 }
873
874 line = nasm_malloc(len + 1);
875 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700876
877 while ((c = *stdmacpos++) != 127) {
878 uint8_t ndir = c - 128;
879 if (ndir < 256-96) {
880 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
881 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400882 *q++ = ' ';
883 } else {
884 *q++ = c;
885 }
886 }
887 stdmacpos = p;
888 *q = '\0';
889
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700890 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700891 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400892 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700893 if (*stdmacnext) {
894 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400895 } else if (do_predef) {
896 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400897
898 /*
899 * Nasty hack: here we push the contents of
900 * `predef' on to the top-level expansion stack,
901 * since this is the most convenient way to
902 * implement the pre-include and pre-define
903 * features.
904 */
905 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700906 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800907 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700908 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800909 l->finishes = NULL;
910
911 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400912 }
913 do_predef = false;
914 }
915 }
916
917 return line;
918}
919
H. Peter Anvin6686de22019-08-10 05:33:14 -0700920/*
921 * Read a line from a file. Return NULL on end of file.
922 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700923static char *line_from_file(FILE *f)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000924{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700925 int c;
926 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400927 const unsigned int delta = 512;
928 const unsigned int pad = 8;
929 unsigned int nr_cont = 0;
930 bool cont = false;
931 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700932 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000933
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400934 size = delta;
935 p = buffer = nasm_malloc(size);
936
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700937 do {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700938 c = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400939
940 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700941 case EOF:
942 if (p == buffer) {
943 nasm_free(buffer);
944 return NULL;
945 }
946 c = 0;
947 break;
948
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400949 case '\r':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700950 next = fgetc(f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400951 if (next != '\n')
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700952 ungetc(next, f);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400953 if (cont) {
954 cont = false;
955 continue;
956 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700957 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400958 break;
959
960 case '\n':
961 if (cont) {
962 cont = false;
963 continue;
964 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700965 c = 0;
966 break;
967
968 case 032: /* ^Z = legacy MS-DOS end of file mark */
969 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400970 break;
971
972 case '\\':
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -0700973 next = fgetc(f);
974 ungetc(next, f);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400975 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400976 cont = true;
977 nr_cont++;
978 continue;
979 }
980 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400982
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400983 if (p >= (buffer + size - pad)) {
984 buffer = nasm_realloc(buffer, size + delta);
985 p = buffer + size - pad;
986 size += delta;
987 }
988
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700989 *p++ = c;
990 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000991
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700992 lineno = src_get_linnum() + istk->lineinc +
993 (nr_cont * istk->lineinc);
994 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000995
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000996 return buffer;
997}
998
999/*
H. Peter Anvin6686de22019-08-10 05:33:14 -07001000 * Common read routine regardless of source
1001 */
1002static char *read_line(void)
1003{
1004 char *line;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001005 FILE *f = istk->fp;
H. Peter Anvin6686de22019-08-10 05:33:14 -07001006
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001007 if (f)
1008 line = line_from_file(f);
H. Peter Anvin6686de22019-08-10 05:33:14 -07001009 else
1010 line = line_from_stdmac();
1011
1012 if (!line)
1013 return NULL;
1014
1015 if (!istk->nolist)
1016 lfmt->line(LIST_READ, src_get_linnum(), line);
1017
1018 return line;
1019}
1020
1021/*
Keith Kaniosb7a89542007-04-12 02:40:54 +00001022 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001023 * don't need to parse the value out of e.g. numeric tokens: we
1024 * simply split one string into many.
1025 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001026static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001027{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001028 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001029 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001030 Token *list = NULL;
1031 Token *t, **tail = &list;
1032
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001034 char *p = line;
1035 char *ep = NULL; /* End of token, for trimming the end */
1036
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 if (*p == '%') {
1038 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001039 if (*p == '+' && !nasm_isdigit(p[1])) {
1040 p++;
1041 type = TOK_PASTE;
1042 } else if (nasm_isdigit(*p) ||
1043 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 do {
1045 p++;
1046 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001047 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 type = TOK_PREPROC_ID;
1049 } else if (*p == '{') {
1050 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001051 while (*p) {
1052 if (*p == '}')
1053 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 p[-1] = *p;
1055 p++;
1056 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001057 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001058 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001059 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 if (*p)
1061 p++;
1062 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001063 } else if (*p == '[') {
1064 int lvl = 1;
1065 line += 2; /* Skip the leading %[ */
1066 p++;
1067 while (lvl && (c = *p++)) {
1068 switch (c) {
1069 case ']':
1070 lvl--;
1071 break;
1072 case '%':
1073 if (*p == '[')
1074 lvl++;
1075 break;
1076 case '\'':
1077 case '\"':
1078 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001079 p = nasm_skip_string(p - 1);
1080 if (*p)
1081 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001082 break;
1083 default:
1084 break;
1085 }
1086 }
1087 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001088 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001089 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001090 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001091 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001092 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001093 type = TOK_INDIRECT;
1094 } else if (*p == '?') {
1095 type = TOK_PREPROC_Q; /* %? */
1096 p++;
1097 if (*p == '?') {
1098 type = TOK_PREPROC_QQ; /* %?? */
1099 p++;
1100 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001101 } else if (*p == '!') {
1102 type = TOK_PREPROC_ID;
1103 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001104 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001105 do {
1106 p++;
1107 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001108 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001109 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001110 p = nasm_skip_string(p);
1111 if (*p)
1112 p++;
1113 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001114 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001115 } else {
1116 /* %! without string or identifier */
1117 type = TOK_OTHER; /* Legacy behavior... */
1118 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001119 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001121 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 do {
1123 p++;
1124 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001125 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 type = TOK_PREPROC_ID;
1127 } else {
1128 type = TOK_OTHER;
1129 if (*p == '%')
1130 p++;
1131 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001132 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 type = TOK_ID;
1134 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001135 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001137 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 /*
1139 * A string token.
1140 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001142 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001143
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 if (*p) {
1145 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001146 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001147 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 /* Handling unterminated strings by UNV */
1149 /* type = -1; */
1150 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001151 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001152 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001153 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001154 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001155 bool is_hex = false;
1156 bool is_float = false;
1157 bool has_e = false;
1158 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001159
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001161 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001163
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001164 if (*p == '$') {
1165 p++;
1166 is_hex = true;
1167 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001168
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001169 for (;;) {
1170 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001171
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001172 if (!is_hex && (c == 'e' || c == 'E')) {
1173 has_e = true;
1174 if (*p == '+' || *p == '-') {
1175 /*
1176 * e can only be followed by +/- if it is either a
1177 * prefixed hex number or a floating-point number
1178 */
1179 p++;
1180 is_float = true;
1181 }
1182 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1183 is_hex = true;
1184 } else if (c == 'P' || c == 'p') {
1185 is_float = true;
1186 if (*p == '+' || *p == '-')
1187 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001188 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001189 ; /* just advance */
1190 else if (c == '.') {
1191 /*
1192 * we need to deal with consequences of the legacy
1193 * parser, like "1.nolist" being two tokens
1194 * (TOK_NUMBER, TOK_ID) here; at least give it
1195 * a shot for now. In the future, we probably need
1196 * a flex-based scanner with proper pattern matching
1197 * to do it as well as it can be done. Nothing in
1198 * the world is going to help the person who wants
1199 * 0x123.p16 interpreted as two tokens, though.
1200 */
1201 r = p;
1202 while (*r == '_')
1203 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001204
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001205 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1206 (!is_hex && (*r == 'e' || *r == 'E')) ||
1207 (*r == 'p' || *r == 'P')) {
1208 p = r;
1209 is_float = true;
1210 } else
1211 break; /* Terminate the token */
1212 } else
1213 break;
1214 }
1215 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001216
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001217 if (p == line+1 && *line == '$') {
1218 type = TOK_OTHER; /* TOKEN_HERE */
1219 } else {
1220 if (has_e && !is_hex) {
1221 /* 1e13 is floating-point, but 1e13h is not */
1222 is_float = true;
1223 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001224
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001225 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1226 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001227 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001229 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 /*
1231 * Whitespace just before end-of-line is discarded by
1232 * pretending it's a comment; whitespace just before a
1233 * comment gets lumped into the comment.
1234 */
1235 if (!*p || *p == ';') {
1236 type = TOK_COMMENT;
1237 while (*p)
1238 p++;
1239 }
1240 } else if (*p == ';') {
1241 type = TOK_COMMENT;
1242 while (*p)
1243 p++;
1244 } else {
1245 /*
1246 * Anything else is an operator of some kind. We check
1247 * for all the double-character operators (>>, <<, //,
1248 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001249 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 */
1251 type = TOK_OTHER;
1252 if ((p[0] == '>' && p[1] == '>') ||
1253 (p[0] == '<' && p[1] == '<') ||
1254 (p[0] == '/' && p[1] == '/') ||
1255 (p[0] == '<' && p[1] == '=') ||
1256 (p[0] == '>' && p[1] == '=') ||
1257 (p[0] == '=' && p[1] == '=') ||
1258 (p[0] == '!' && p[1] == '=') ||
1259 (p[0] == '<' && p[1] == '>') ||
1260 (p[0] == '&' && p[1] == '&') ||
1261 (p[0] == '|' && p[1] == '|') ||
1262 (p[0] == '^' && p[1] == '^')) {
1263 p++;
1264 }
1265 p++;
1266 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001267
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 /* Handling unterminated string by UNV */
1269 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001270 {
1271 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1272 t->text[p-line] = *line;
1273 tail = &t->next;
1274 }
1275 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001277 if (!ep)
1278 ep = p;
1279 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 tail = &t->next;
1281 }
1282 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001283 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001284 return list;
1285}
1286
H. Peter Anvince616072002-04-30 21:02:23 +00001287/*
1288 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001289 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001290 * deleted only all at once by the delete_Blocks function.
1291 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001293{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001294 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001296 /* first, get to the end of the linked list */
1297 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001299 /* now allocate the requested chunk */
1300 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001302 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001303 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001304 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001305}
1306
1307/*
1308 * this function deletes all managed blocks of memory
1309 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001311{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001312 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001313
H. Peter Anvin70653092007-10-19 14:42:29 -07001314 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001315 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001316 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001317 * free it.
1318 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001320 if (b->chunk)
1321 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 a = b;
1323 b = b->next;
1324 if (a != &blocks)
1325 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001326 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001327 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001329
1330/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001331 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001332 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001333 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001334static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001335 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001336{
1337 Token *t;
1338 int i;
1339
H. Peter Anvin89cee572009-07-15 09:16:54 -04001340 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1342 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1343 freeTokens[i].next = &freeTokens[i + 1];
1344 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001345 }
1346 t = freeTokens;
1347 freeTokens = t->next;
1348 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001349 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001350 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001351 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 t->text = NULL;
1353 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001354 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001355 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001356 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001357 t->text = nasm_malloc(txtlen+1);
1358 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001360 }
1361 return t;
1362}
1363
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001364static Token *dup_Token(Token *next, const Token *src)
1365{
1366 return new_Token(next, src->type, src->text, src->len);
1367}
1368
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001370{
1371 Token *next = t->next;
1372 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001373 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001374 freeTokens = t;
1375 return next;
1376}
1377
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001378/*
1379 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001380 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1381 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001382 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001383static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001384{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001385 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001386 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001387 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001388
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001389 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001390 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001391 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001392 char *v;
1393 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001394
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001395 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001396 if (nasm_isquote(*v))
1397 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001398
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001399 if (v) {
1400 char *p = getenv(v);
1401 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001402 /*!
1403 *!environment [on] nonexistent environment variable
1404 *! warns if a nonexistent environment variable
1405 *! is accessed using the \c{%!} preprocessor
1406 *! construct (see \k{getenv}.) Such environment
1407 *! variables are treated as empty (with this
1408 *! warning issued) starting in NASM 2.15;
1409 *! earlier versions of NASM would treat this as
1410 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001411 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001412 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1413 p = "";
1414 }
1415 t->text = nasm_strdup(p);
1416 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001417 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001418 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001420
H. Peter Anvine2c80182005-01-15 22:15:51 +00001421 /* Expand local macros here and not during preprocessing */
1422 if (expand_locals &&
1423 t->type == TOK_PREPROC_ID && t->text &&
1424 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001425 const char *q;
1426 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001427 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001429 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1430 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 nasm_free(t->text);
1432 t->text = p;
1433 }
1434 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001435 if (t->text) {
1436 if (debug_level(2)) {
1437 unsigned long t_len = t->len;
1438 unsigned long s_len = strlen(t->text);
1439 if (t_len != s_len) {
1440 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1441 t->text, t->type, s_len, t_len);
1442 t->len = s_len;
1443 }
1444 }
1445 len += t->len;
1446 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001448 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001449 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001450
H. Peter Anvin734b1882002-04-30 21:01:08 +00001451 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001452
1453 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001454 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001455 memcpy(p, t->text, t->len);
1456 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001457 } else if (t->type == TOK_WHITESPACE) {
1458 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001460 }
1461 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001462
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 return line;
1464}
1465
1466/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001467 * A scanner, suitable for use by the expression evaluator, which
1468 * operates on a line of Tokens. Expects a pointer to a pointer to
1469 * the first token in the line to be passed in as its private_data
1470 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001471 *
1472 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001473 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001474struct ppscan {
1475 Token *tptr;
1476 int ntokens;
1477};
1478
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001480{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001481 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001482 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001483 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001484
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001486 if (pps->ntokens && (tline = pps->tptr)) {
1487 pps->ntokens--;
1488 pps->tptr = tline->next;
1489 } else {
1490 pps->tptr = NULL;
1491 pps->ntokens = 0;
1492 return tokval->t_type = TOKEN_EOS;
1493 }
1494 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001495
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001496 tokval->t_charptr = tline->text;
1497
H. Peter Anvin76690a12002-04-30 20:52:49 +00001498 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001500 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001501 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001502
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001504 p = tokval->t_charptr = tline->text;
1505 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 tokval->t_charptr++;
1507 return tokval->t_type = TOKEN_ID;
1508 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001509
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001510 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001511 if (r >= p+MAX_KEYWORD)
1512 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001513 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001514 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001515 *s = '\0';
1516 /* right, so we have an identifier sitting in temp storage. now,
1517 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001518 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001519 }
1520
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001522 bool rn_error;
1523 tokval->t_integer = readnum(tline->text, &rn_error);
1524 tokval->t_charptr = tline->text;
1525 if (rn_error)
1526 return tokval->t_type = TOKEN_ERRNUM;
1527 else
1528 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001529 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001530
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001531 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001532 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001533 }
1534
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001536 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001537
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001538 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001539 tokval->t_charptr = tline->text;
1540 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001541
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001542 if (ep[0] != bq || ep[1] != '\0')
1543 return tokval->t_type = TOKEN_ERRSTR;
1544 else
1545 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001546 }
1547
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 if (tline->type == TOK_OTHER) {
1549 if (!strcmp(tline->text, "<<"))
1550 return tokval->t_type = TOKEN_SHL;
1551 if (!strcmp(tline->text, ">>"))
1552 return tokval->t_type = TOKEN_SHR;
1553 if (!strcmp(tline->text, "//"))
1554 return tokval->t_type = TOKEN_SDIV;
1555 if (!strcmp(tline->text, "%%"))
1556 return tokval->t_type = TOKEN_SMOD;
1557 if (!strcmp(tline->text, "=="))
1558 return tokval->t_type = TOKEN_EQ;
1559 if (!strcmp(tline->text, "<>"))
1560 return tokval->t_type = TOKEN_NE;
1561 if (!strcmp(tline->text, "!="))
1562 return tokval->t_type = TOKEN_NE;
1563 if (!strcmp(tline->text, "<="))
1564 return tokval->t_type = TOKEN_LE;
1565 if (!strcmp(tline->text, ">="))
1566 return tokval->t_type = TOKEN_GE;
1567 if (!strcmp(tline->text, "&&"))
1568 return tokval->t_type = TOKEN_DBL_AND;
1569 if (!strcmp(tline->text, "^^"))
1570 return tokval->t_type = TOKEN_DBL_XOR;
1571 if (!strcmp(tline->text, "||"))
1572 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001573 }
1574
1575 /*
1576 * We have no other options: just return the first character of
1577 * the token text.
1578 */
1579 return tokval->t_type = tline->text[0];
1580}
1581
1582/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001583 * Compare a string to the name of an existing macro; this is a
1584 * simple wrapper which calls either strcmp or nasm_stricmp
1585 * depending on the value of the `casesense' parameter.
1586 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001587static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001588{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001589 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001590}
1591
1592/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001593 * Compare a string to the name of an existing macro; this is a
1594 * simple wrapper which calls either strcmp or nasm_stricmp
1595 * depending on the value of the `casesense' parameter.
1596 */
1597static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1598{
1599 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1600}
1601
1602/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001603 * Return the Context structure associated with a %$ token. Return
1604 * NULL, having _already_ reported an error condition, if the
1605 * context stack isn't deep enough for the supplied number of $
1606 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001607 *
1608 * If "namep" is non-NULL, set it to the pointer to the macro name
1609 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001610 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001611static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001613 Context *ctx;
1614 int i;
1615
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001616 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001617 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001618
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001619 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001621
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001623 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001625 }
1626
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001627 name += 2;
1628 ctx = cstk;
1629 i = 0;
1630 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001631 name++;
1632 i++;
1633 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001634 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001636 nasm_nonfatal("`%s': context stack is only"
1637 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001639 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001640
1641 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001642 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001643
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001644 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645}
1646
1647/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001648 * Open an include file. This routine must always return a valid
1649 * file pointer if it returns - it's responsible for throwing an
1650 * ERR_FATAL and bombing out completely if not. It should also try
1651 * the include path one by one until it finds the file or reaches
1652 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001653 *
1654 * Note: for INC_PROBE the function returns NULL at all times;
1655 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001656 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001657enum incopen_mode {
1658 INC_NEEDED, /* File must exist */
1659 INC_OPTIONAL, /* Missing is OK */
1660 INC_PROBE /* Only an existence probe */
1661};
1662
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001663/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001664static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001665 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001666{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001667 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001668 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001669 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001670 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001671 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001672
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001674 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001675 if (omode == INC_PROBE) {
1676 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001677 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001678 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001679 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001680 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001681 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001682 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001683 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001685 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001686
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001687 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001688
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001689 if (!ip) {
1690 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001691 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001692 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001693
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001694 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001695 ip = ip->next;
1696 }
1697}
1698
1699/*
1700 * Open a file, or test for the presence of one (depending on omode),
1701 * considering the include path.
1702 */
1703static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001704 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001705 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001706 enum incopen_mode omode,
1707 enum file_flags fmode)
1708{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001709 struct hash_insert hi;
1710 void **hp;
1711 char *path;
1712 FILE *fp = NULL;
1713
1714 hp = hash_find(&FileHash, file, &hi);
1715 if (hp) {
1716 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001717 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001718 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001719 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001720 } else {
1721 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001722 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001723
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001724 /* Positive or negative result */
1725 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001726
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001727 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001728 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001729 */
1730 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001731 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001732 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001733
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001734 if (!path) {
1735 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001736 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001737 } else {
1738 if (!fp && omode != INC_PROBE)
1739 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001740 }
1741
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001742 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001743 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001744
1745 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001746}
1747
1748/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001749 * Opens an include or input file. Public version, for use by modules
1750 * that get a file:lineno pair and need to look at the file again
1751 * (e.g. the CodeView debug backend). Returns NULL on failure.
1752 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001753FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001754{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001755 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001756}
1757
1758/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001759 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001760 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001761 * return true if _any_ single-line macro of that name is defined.
1762 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001763 * `nparam' or no parameters is defined.
1764 *
1765 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001766 * defined, or nparam is -1, the address of the definition structure
1767 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001768 * is NULL, no action will be taken regarding its contents, and no
1769 * error will occur.
1770 *
1771 * Note that this is also called with nparam zero to resolve
1772 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001773 *
1774 * If you already know which context macro belongs to, you can pass
1775 * the context pointer as first parameter; if you won't but name begins
1776 * with %$ the context will be automatically computed. If all_contexts
1777 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001778 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001779static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001780smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001781 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001782{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001783 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001784 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001785
H. Peter Anvin97a23472007-09-16 17:57:25 -07001786 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001787 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001788 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001789 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001790 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001792 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001793 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001794 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001795 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001796 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001797 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001798
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 while (m) {
1800 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001801 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001803 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 *defn = m;
1805 else
1806 *defn = NULL;
1807 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001808 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 }
1810 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001811 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001812
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001813 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001814}
1815
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001816/* param should be a natural number [0; INT_MAX] */
1817static int read_param_count(const char *str)
1818{
1819 int result;
1820 bool err;
1821
1822 result = readnum(str, &err);
1823 if (result < 0 || result > INT_MAX) {
1824 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001825 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1826 str, 0, INT_MAX);
1827 } else if (err)
1828 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001829 return result;
1830}
1831
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001832/*
1833 * Count and mark off the parameters in a multi-line macro call.
1834 * This is called both from within the multi-line macro expansion
1835 * code, and also to mark off the default parameters when provided
1836 * in a %macro definition line.
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001837 *
1838 * Note that we need space in the params array for parameter 0 being
1839 * a possible captured label as well as the final NULL.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001840 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001841static void count_mmac_params(Token * t, int *nparamp, Token ***paramsp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001842{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001843 int paramsize, brace;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001844 int nparam = 0;
1845 Token **params;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001846
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001847 paramsize = PARAM_DELTA;
1848 params = nasm_malloc(paramsize * sizeof(*params));
1849 params[0] = NULL;
1850
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 while (t) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001852 /* 2 slots for captured label and NULL */
1853 if (nparam+2 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 paramsize += PARAM_DELTA;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001855 params = nasm_realloc(params, sizeof(*params) * paramsize);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 }
1857 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001858 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001860 brace++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001861 params[++nparam] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001862 if (brace) {
1863 while (brace && (t = t->next) != NULL) {
1864 if (tok_is_(t, "{"))
1865 brace++;
1866 else if (tok_is_(t, "}"))
1867 brace--;
1868 }
1869
1870 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 /*
1872 * Now we've found the closing brace, look further
1873 * for the comma.
1874 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001875 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 skip_white_(t);
1877 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001878 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 while (tok_isnt_(t, ","))
1880 t = t->next;
1881 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001883 } else {
1884 while (tok_isnt_(t, ","))
1885 t = t->next;
1886 }
1887 if (t) { /* got a comma/brace */
1888 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001890 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07001891
1892 params[nparam+1] = NULL;
1893 *paramsp = params;
1894 *nparamp = nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001895}
1896
1897/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001898 * Determine whether one of the various `if' conditions is true or
1899 * not.
1900 *
1901 * We must free the tline we get passed.
1902 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001903static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001904{
H. Peter Anvin70055962007-10-11 00:05:31 -07001905 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001906 Token *t, *tt, *origline;
1907 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001908 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001909 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001910 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001911 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001912 const char *dname = pp_directives[ct];
1913 bool casesense = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001914
1915 origline = tline;
1916
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001917 switch (PP_COND(ct)) {
1918 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001919 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001920 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001922 if (!tline)
1923 break;
1924 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001925 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001926 dname);
1927 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001929 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001930 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 tline = tline->next;
1932 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001934
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001935 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001936 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001937 while (tline) {
1938 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 if (!tline || (tline->type != TOK_ID &&
1940 (tline->type != TOK_PREPROC_ID ||
1941 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001942 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001943 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001946 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001947 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001949 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001951
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001952 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001953 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001954 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001955 while (tline) {
1956 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001957 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001958 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001959 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001960 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001961 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001962 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001963 goto fail;
1964 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001965 p = tline->text;
1966 if (tline->type == TOK_PREPROC_ID)
1967 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001968 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001969 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001970 if (getenv(p))
1971 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001972 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001973 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001974 break;
1975
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001976 case PP_IFIDNI:
1977 casesense = false;
1978 /* fall through */
1979 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001980 tline = expand_smacro(tline);
1981 t = tt = tline;
1982 while (tok_isnt_(tt, ","))
1983 tt = tt->next;
1984 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001985 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001986 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 }
1989 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001990 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1992 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001993 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001994 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001996 }
1997 if (t->type == TOK_WHITESPACE) {
1998 t = t->next;
1999 continue;
2000 }
2001 if (tt->type == TOK_WHITESPACE) {
2002 tt = tt->next;
2003 continue;
2004 }
2005 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002006 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 break;
2008 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07002009 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002010 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 size_t l1 = nasm_unquote(t->text, NULL);
2012 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07002013
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 if (l1 != l2) {
2015 j = false;
2016 break;
2017 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002018 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002019 j = false;
2020 break;
2021 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002022 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002023 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002024 break;
2025 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00002026
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 t = t->next;
2028 tt = tt->next;
2029 }
2030 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002031 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002033
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002034 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04002035 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002037 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00002038
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039 skip_white_(tline);
2040 tline = expand_id(tline);
2041 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002042 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002043 goto fail;
2044 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002045 nasm_zero(searching);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 searching.name = nasm_strdup(tline->text);
2047 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002048 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002049 searching.nparam_max = INT_MAX;
2050 tline = expand_smacro(tline->next);
2051 skip_white_(tline);
2052 if (!tline) {
2053 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002054 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002055 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056 } else {
2057 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002058 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 }
2060 if (tline && tok_is_(tline->next, "-")) {
2061 tline = tline->next->next;
2062 if (tok_is_(tline, "*"))
2063 searching.nparam_max = INT_MAX;
2064 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002065 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002066 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002067 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002068 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002069 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002070 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002071 searching.nparam_max = searching.nparam_min;
2072 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 }
2074 }
2075 if (tline && tok_is_(tline->next, "+")) {
2076 tline = tline->next;
2077 searching.plus = true;
2078 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002079 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2080 while (mmac) {
2081 if (!strcmp(mmac->name, searching.name) &&
2082 (mmac->nparam_min <= searching.nparam_max
2083 || searching.plus)
2084 && (searching.nparam_min <= mmac->nparam_max
2085 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 found = true;
2087 break;
2088 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002089 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 }
2091 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002092 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 nasm_free(searching.name);
2094 j = found;
2095 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002096 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002097
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002098 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002099 needtype = TOK_ID;
2100 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002101 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002102 needtype = TOK_NUMBER;
2103 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002104 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 needtype = TOK_STRING;
2106 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002107
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108iftype:
2109 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002110
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002111 while (tok_type_(t, TOK_WHITESPACE) ||
2112 (needtype == TOK_NUMBER &&
2113 tok_type_(t, TOK_OTHER) &&
2114 (t->text[0] == '-' || t->text[0] == '+') &&
2115 !t->text[1]))
2116 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002117
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002118 j = tok_type_(t, needtype);
2119 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002120
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002121 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 t = tline = expand_smacro(tline);
2123 while (tok_type_(t, TOK_WHITESPACE))
2124 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002125
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002126 j = false;
2127 if (t) {
2128 t = t->next; /* Skip the actual token */
2129 while (tok_type_(t, TOK_WHITESPACE))
2130 t = t->next;
2131 j = !t; /* Should be nothing left */
2132 }
2133 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002134
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002135 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 t = tline = expand_smacro(tline);
2137 while (tok_type_(t, TOK_WHITESPACE))
2138 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002139
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002140 j = !t; /* Should be empty */
2141 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002142
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002143 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002144 pps.tptr = tline = expand_smacro(tline);
2145 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002146 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002147 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148 if (!evalresult)
2149 return -1;
2150 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002151 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002152 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002153 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002154 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002155 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002156 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002157 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002158 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002159
H. Peter Anvine2c80182005-01-15 22:15:51 +00002160 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002161 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002162 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002163 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002164
2165 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002166 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002167
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002168fail:
2169 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002170 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002171}
2172
2173/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002174 * Default smacro expansion routine: just returns a copy of the
2175 * expansion list.
2176 */
2177static Token *
2178smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2179{
2180 (void)params;
2181 (void)nparams;
2182
2183 return dup_tlist(s->expansion, NULL);
2184}
2185
2186/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002187 * Emit a macro defintion or undef to the listing file, if
2188 * desired. This is similar to detoken(), but it handles the reverse
2189 * expansion list, does not expand %! or local variable tokens, and
2190 * does some special handling for macro parameters.
2191 */
2192static void
2193list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2194{
2195 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2196 const Token **param_names;
2197 Token *junk_names = NULL;
2198 Token *t;
2199 size_t namelen, size;
2200 unsigned int i;
2201 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002202 char *context_prefix = NULL;
2203 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002204
2205 nasm_newn(param_names, m->nparam);
2206
2207 namelen = strlen(m->name);
2208 size = namelen + 2; /* Include room for space after name + NUL */
2209
2210 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002211 int context_depth = cstk->depth - ctx->depth + 1;
2212 context_prefix =
2213 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2214 ctx->name ? ctx->name : "",
2215 ctx->number, context_depth, "");
2216
2217 context_len = nasm_last_string_len();
2218 memset(context_prefix + context_len - context_depth,
2219 '$', context_depth);
2220 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002221 }
2222
2223 list_for_each(t, m->expansion) {
2224 if (!t->text) {
2225 size++; /* Whitespace, presumably */
2226 } else {
2227 size += t->len;
2228 if (is_smac_param(t->type))
2229 param_names[smac_nparam(t->type)] = t;
2230 }
2231 }
2232
2233 if (m->nparam) {
2234 /*
2235 * Space for ( and either , or ) around each
2236 * parameter, plus an optional =.
2237 */
2238 size += 1 + 2 * m->nparam;
2239 for (i = 0; i < m->nparam; i++) {
2240 if (!param_names[i])
2241 param_names[i] = &unused_arg_name;
2242 size += param_names[i]->len;
2243 }
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) {
2262 *--p = ')';
2263 for (i = m->nparam; i--;) {
2264 p -= param_names[i]->len;
2265 memcpy(p, param_names[i]->text, param_names[i]->len);
2266 if (m->eval_param && m->eval_param[i])
2267 *--p = '=';
2268 *--p = ',';
2269 }
2270 *p = '('; /* First parameter starts with ( not , */
2271
2272 free_tlist(junk_names);
2273 }
2274
2275 p -= namelen;
2276 memcpy(p, m->name, namelen);
2277
H. Peter Anvin6686de22019-08-10 05:33:14 -07002278 if (context_prefix) {
2279 p -= context_len;
2280 memcpy(p, context_prefix, context_len);
2281 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002282 }
2283
2284 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002285 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002286}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002287
2288/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002289 * Common code for defining an smacro. The tmpl argument, if not NULL,
2290 * contains any macro parameters that aren't explicit arguments;
2291 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002292 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002293static SMacro *define_smacro(const char *mname, bool casesense,
2294 Token *expansion, const SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002295{
2296 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002297 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002298 Context *ctx;
2299 bool defining_alias = false;
2300 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002301
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002302 if (tmpl) {
2303 defining_alias = tmpl->alias;
2304 nparam = tmpl->nparam;
2305 }
2306
2307 while (1) {
2308 ctx = get_ctx(mname, &mname);
2309
2310 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2311 /* Create a new macro */
2312 smtbl = ctx ? &ctx->localmac : &smacros;
2313 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2314 nasm_new(smac);
2315 smac->next = *smhead;
2316 *smhead = smac;
2317 break;
2318 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002319 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002320 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002321 /*
2322 * Some instances of the old code considered this a failure,
2323 * some others didn't. What is the right thing to do here?
2324 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002325 goto fail;
2326 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002327 /*
2328 * We're redefining, so we have to take over an
2329 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002330 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002331 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002332 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002333 break;
2334 } else if (smac->in_progress) {
2335 nasm_nonfatal("macro alias loop");
2336 goto fail;
2337 } else {
2338 /* It is an alias macro; follow the alias link */
2339 SMacro *s;
2340
2341 smac->in_progress = true;
2342 s = define_smacro(smac->expansion->text, casesense,
2343 expansion, tmpl);
2344 smac->in_progress = false;
2345 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002346 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002347 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002348
2349 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002350 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002351 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002352 smac->expand = smacro_expand_default;
2353 if (tmpl) {
2354 smac->nparam = tmpl->nparam;
2355 smac->eval_param = tmpl->eval_param;
2356 smac->alias = tmpl->alias;
2357 if (tmpl->expand)
2358 smac->expand = tmpl->expand;
2359 }
2360 if (list_option('m')) {
2361 static const enum preproc_token op[2][2] = {
2362 { PP_DEFINE, PP_IDEFINE },
2363 { PP_DEFALIAS, PP_IDEFALIAS }
2364 };
2365 list_smacro_def(op[!!smac->alias][casesense], ctx, smac);
2366 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002367 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002368
2369fail:
2370 free_tlist(expansion);
2371 if (tmpl && tmpl->eval_param)
2372 nasm_free(tmpl->eval_param);
2373 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002374}
2375
2376/*
2377 * Undefine an smacro
2378 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002379static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002380{
2381 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002382 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002383 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002384
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002385 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002386 smtbl = ctx ? &ctx->localmac : &smacros;
2387 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002388
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002389 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002390 /*
2391 * We now have a macro name... go hunt for it.
2392 */
2393 sp = smhead;
2394 while ((s = *sp) != NULL) {
2395 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002396 if (s->alias && !undefalias) {
2397 if (s->in_progress) {
2398 nasm_nonfatal("macro alias loop");
2399 } else {
2400 s->in_progress = true;
2401 undef_smacro(s->expansion->text, false);
2402 s->in_progress = false;
2403 }
2404 } else {
2405 if (list_option('m'))
2406 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2407 ctx, s);
2408 *sp = s->next;
2409 free_smacro(s);
2410 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002411 } else {
2412 sp = &s->next;
2413 }
2414 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002415 }
2416}
2417
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002418/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002419 * Parse a mmacro specification.
2420 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002421static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002422{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002423 tline = tline->next;
2424 skip_white_(tline);
2425 tline = expand_id(tline);
2426 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002427 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002428 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002429 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002430
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002431#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002432 def->prev = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002433#endif
H. Peter Anvina26433d2008-07-16 14:40:01 -07002434 def->name = nasm_strdup(tline->text);
2435 def->plus = false;
2436 def->nolist = false;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002437 def->nparam_min = 0;
2438 def->nparam_max = 0;
2439
H. Peter Anvina26433d2008-07-16 14:40:01 -07002440 tline = expand_smacro(tline->next);
2441 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002442 if (!tok_type_(tline, TOK_NUMBER))
2443 nasm_nonfatal("`%s' expects a parameter count", directive);
2444 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002445 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002446 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002447 tline = tline->next->next;
2448 if (tok_is_(tline, "*")) {
2449 def->nparam_max = INT_MAX;
2450 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002451 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002452 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002453 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002454 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002455 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002456 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002457 }
2458 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002459 }
2460 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002461 tline = tline->next;
2462 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002463 }
2464 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002465 !nasm_stricmp(tline->next->text, ".nolist")) {
2466 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002467 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002468 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002469
H. Peter Anvina26433d2008-07-16 14:40:01 -07002470 /*
2471 * Handle default parameters.
2472 */
2473 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002474 def->dlist = tline->next;
2475 tline->next = NULL;
2476 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002477 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002478 def->dlist = NULL;
2479 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002480 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002481 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002482
H. Peter Anvin89cee572009-07-15 09:16:54 -04002483 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002484 !def->plus) {
2485 /*
2486 *!macro-defaults [on] macros with more default than optional parameters
2487 *! warns when a macro has more default parameters than optional parameters.
2488 *! See \k{mlmacdef} for why might want to disable this warning.
2489 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002490 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002491 "too many default macro parameters in macro `%s'", def->name);
2492 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002493
H. Peter Anvina26433d2008-07-16 14:40:01 -07002494 return true;
2495}
2496
2497
2498/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002499 * Decode a size directive
2500 */
2501static int parse_size(const char *str) {
2502 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002503 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002504 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002505 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002506 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002507}
2508
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002509/*
2510 * Process a preprocessor %pragma directive. Currently there are none.
2511 * Gets passed the token list starting with the "preproc" token from
2512 * "%pragma preproc".
2513 */
2514static void do_pragma_preproc(Token *tline)
2515{
2516 /* Skip to the real stuff */
2517 tline = tline->next;
2518 skip_white_(tline);
2519 if (!tline)
2520 return;
2521
2522 (void)tline; /* Nothing else to do at present */
2523}
2524
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002525static bool is_macro_id(const Token *t)
2526{
2527 return t && (t->type == TOK_ID ||
2528 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2529}
2530
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002531static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002532{
2533 char *id;
2534 Token *t = *tp;
2535
2536 t = t->next; /* Skip directive */
2537 skip_white_(t);
2538 t = expand_id(t);
2539
2540 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002541 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002542 err ? err : "macro identifier");
2543 return NULL;
2544 }
2545
2546 id = t->text;
2547 skip_white_(t);
2548 *tp = t;
2549 return id;
2550}
2551
Ed Beroset3ab3f412002-06-11 03:31:49 +00002552/**
2553 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002554 * Find out if a line contains a preprocessor directive, and deal
2555 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002556 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002557 * If a directive _is_ found, it is the responsibility of this routine
2558 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002559 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002560 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002561 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002562 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002563 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002564 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002565static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002566{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002567 enum preproc_token i;
2568 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002569 bool err;
2570 int nparam;
2571 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002572 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002573 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002574 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002575 char *p, *pp;
2576 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002577 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002578 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002579 Include *inc;
2580 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002581 Cond *cond;
2582 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002583 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002584 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002585 struct tokenval tokval;
2586 expr *evalresult;
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002587 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002588 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002589 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002590 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002591
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002592 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002593 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002594
H. Peter Anvineba20a72002-04-30 20:53:55 +00002595 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002596 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002597 (tline->text[0] && (tline->text[1] == '%' ||
2598 tline->text[1] == '$' ||
2599 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002601
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002602 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002603 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002604
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002605 casesense = true;
2606 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2607 casesense = false;
2608 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002609 }
2610
2611 /*
2612 * If we're in a non-emitting branch of a condition construct,
2613 * or walking to the end of an already terminated %rep block,
2614 * we should ignore all directives except for condition
2615 * directives.
2616 */
2617 if (((istk->conds && !emitting(istk->conds->state)) ||
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07002618 (istk->mstk.mstk && !istk->mstk.mstk->in_progress)) &&
2619 !is_condition(i)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002620 return NO_DIRECTIVE_FOUND;
2621 }
2622
2623 /*
2624 * If we're defining a macro or reading a %rep block, we should
2625 * ignore all directives except for %macro/%imacro (which nest),
2626 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2627 * If we're in a %rep block, another %rep nests, so should be let through.
2628 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002629 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002630 i != PP_ENDMACRO && i != PP_ENDM &&
2631 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2632 return NO_DIRECTIVE_FOUND;
2633 }
2634
2635 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002636 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002637 nested_mac_count++;
2638 return NO_DIRECTIVE_FOUND;
2639 } else if (nested_mac_count > 0) {
2640 if (i == PP_ENDMACRO) {
2641 nested_mac_count--;
2642 return NO_DIRECTIVE_FOUND;
2643 }
2644 }
2645 if (!defining->name) {
2646 if (i == PP_REP) {
2647 nested_rep_count++;
2648 return NO_DIRECTIVE_FOUND;
2649 } else if (nested_rep_count > 0) {
2650 if (i == PP_ENDREP) {
2651 nested_rep_count--;
2652 return NO_DIRECTIVE_FOUND;
2653 }
2654 }
2655 }
2656 }
2657
H. Peter Anvin4169a472007-09-12 01:29:43 +00002658 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002659 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002660 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002661 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002662
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002663 case PP_PRAGMA:
2664 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002665 * %pragma namespace options...
2666 *
2667 * The namespace "preproc" is reserved for the preprocessor;
2668 * all other namespaces generate a [pragma] assembly directive.
2669 *
2670 * Invalid %pragmas are ignored and may have different
2671 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002672 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002673 t = tline;
2674 tline = tline->next;
2675 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002676 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002677 while (tok_type_(tline, TOK_WHITESPACE)) {
2678 t = tline;
2679 tline = tline->next;
2680 delete_Token(t);
2681 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002682 if (tok_type_(tline, TOK_ID)) {
2683 if (!nasm_stricmp(tline->text, "preproc")) {
2684 /* Preprocessor pragma */
2685 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002686 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002687 } else {
2688 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002689
2690 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002691 for (t = tline; t->next; t = t->next)
2692 ;
2693 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002694
2695 /* Prepend "[pragma " */
2696 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2697 t = new_Token(t, TOK_ID, "pragma", 6);
2698 t = new_Token(t, TOK_OTHER, "[", 1);
2699 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002700 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002701 }
2702 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002703 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002704
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 case PP_STACKSIZE:
2706 /* Directive to tell NASM what the default stack size is. The
2707 * default is for a 16-bit stack, and this can be overriden with
2708 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 */
2710 tline = tline->next;
2711 if (tline && tline->type == TOK_WHITESPACE)
2712 tline = tline->next;
2713 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002714 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 }
2716 if (nasm_stricmp(tline->text, "flat") == 0) {
2717 /* All subsequent ARG directives are for a 32-bit stack */
2718 StackSize = 4;
2719 StackPointer = "ebp";
2720 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002721 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002722 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2723 /* All subsequent ARG directives are for a 64-bit stack */
2724 StackSize = 8;
2725 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002726 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002727 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 } else if (nasm_stricmp(tline->text, "large") == 0) {
2729 /* All subsequent ARG directives are for a 16-bit stack,
2730 * far function call.
2731 */
2732 StackSize = 2;
2733 StackPointer = "bp";
2734 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002735 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002736 } else if (nasm_stricmp(tline->text, "small") == 0) {
2737 /* All subsequent ARG directives are for a 16-bit stack,
2738 * far function call. We don't support near functions.
2739 */
2740 StackSize = 2;
2741 StackPointer = "bp";
2742 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002743 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002744 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002745 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002747 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002748
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 case PP_ARG:
2750 /* TASM like ARG directive to define arguments to functions, in
2751 * the following form:
2752 *
2753 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2754 */
2755 offset = ArgOffset;
2756 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002757 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002758 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002759
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 /* Find the argument name */
2761 tline = tline->next;
2762 if (tline && tline->type == TOK_WHITESPACE)
2763 tline = tline->next;
2764 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002765 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002766 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 }
2768 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002769
H. Peter Anvine2c80182005-01-15 22:15:51 +00002770 /* Find the argument size type */
2771 tline = tline->next;
2772 if (!tline || tline->type != TOK_OTHER
2773 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002774 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002775 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 }
2777 tline = tline->next;
2778 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002779 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002780 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002782
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002784 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002786 size = parse_size(tt->text);
2787 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002788 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002789 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002790 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002791 }
2792 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002793
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 /* Round up to even stack slots */
2795 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002796
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 /* Now define the macro for the argument */
2798 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2799 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002800 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002802
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 /* Move to the next argument in the list */
2804 tline = tline->next;
2805 if (tline && tline->type == TOK_WHITESPACE)
2806 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002807 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002808 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002809 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002810
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 case PP_LOCAL:
2812 /* TASM like LOCAL directive to define local variables for a
2813 * function, in the following form:
2814 *
2815 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2816 *
2817 * The '= LocalSize' at the end is ignored by NASM, but is
2818 * required by TASM to define the local parameter size (and used
2819 * by the TASM macro package).
2820 */
2821 offset = LocalOffset;
2822 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002823 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002824 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002825
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 /* Find the argument name */
2827 tline = tline->next;
2828 if (tline && tline->type == TOK_WHITESPACE)
2829 tline = tline->next;
2830 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002831 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002832 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 }
2834 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002835
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 /* Find the argument size type */
2837 tline = tline->next;
2838 if (!tline || tline->type != TOK_OTHER
2839 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002840 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002841 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002842 }
2843 tline = tline->next;
2844 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002845 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002846 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002848
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002850 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002852 size = parse_size(tt->text);
2853 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002854 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002856 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 }
2858 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002859
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002860 /* Round up to even stack slots */
2861 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002862
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002863 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002864
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002865 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2867 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002868 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002869
H. Peter Anvine2c80182005-01-15 22:15:51 +00002870 /* Now define the assign to setup the enter_c macro correctly */
2871 snprintf(directive, sizeof(directive),
2872 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002873 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002874
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 /* Move to the next argument in the list */
2876 tline = tline->next;
2877 if (tline && tline->type == TOK_WHITESPACE)
2878 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002879 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002880 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002881 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002882
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 case PP_CLEAR:
2884 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002885 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002886 free_macros();
2887 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002888 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002889
H. Peter Anvin418ca702008-05-30 10:42:30 -07002890 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002891 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002892 skip_white_(t);
2893 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002894 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002895 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002896 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002897 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002898 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002899 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002900 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002901 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002902 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002903 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002904 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002905
2906 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002907 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002908 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002909
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002910 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002911 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002912 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002913 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002914 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002915 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002916 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002917 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002918 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002919 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002920 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002921 inc->next = istk;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002922 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002923 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002924 (pp_mode == PP_DEPS)
2925 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002926 if (!inc->fp) {
2927 /* -MG given but file not found */
2928 nasm_free(inc);
2929 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002930 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002931 inc->lineno = src_set_linnum(0);
2932 inc->lineinc = 1;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002933 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002934 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002935 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002936 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002937 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002938
H. Peter Anvind2456592008-06-19 15:04:18 -07002939 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002940 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07002941 const struct use_package *pkg;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002942
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002943 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002944 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002945 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002946 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002947 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002948 nasm_unquote_cstr(tline->text, NULL);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07002949 pkg = nasm_find_use_package(tline->text);
2950 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002951 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07002952 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002953 /*
2954 * Not already included, go ahead and include it.
2955 * Treat it as an include file for the purpose of
2956 * producing a listing.
2957 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07002958 use_loaded[pkg->index] = true;
2959 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002960 nasm_new(inc);
2961 inc->next = istk;
2962 inc->fname = src_set_fname(NULL);
2963 inc->lineno = src_set_linnum(0);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002964 inc->nolist = !list_option('b') || istk->nolist;
2965 istk = inc;
2966 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002967 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002968 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002969 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002972 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 tline = tline->next;
2974 skip_white_(tline);
2975 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002976 if (tline) {
2977 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002978 nasm_nonfatal("`%s' expects a context identifier",
2979 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002980 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002981 }
2982 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002983 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002984 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002985 p = nasm_strdup(tline->text);
2986 } else {
2987 p = NULL; /* Anonymous */
2988 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002989
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002990 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002991 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002992 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002993 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002994 ctx->name = p;
2995 ctx->number = unique++;
2996 cstk = ctx;
2997 } else {
2998 /* %pop or %repl */
2999 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003000 nasm_nonfatal("`%s': context stack is empty",
3001 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003002 } else if (i == PP_POP) {
3003 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003004 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08003005 "expected %s",
3006 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003007 else
3008 ctx_pop();
3009 } else {
3010 /* i == PP_REPL */
3011 nasm_free(cstk->name);
3012 cstk->name = p;
3013 p = NULL;
3014 }
3015 nasm_free(p);
3016 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003017 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003018 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003019 severity = ERR_FATAL;
3020 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003022 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003023 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003024 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003025 /*!
3026 *!user [on] %warning directives
3027 *! controls output of \c{%warning} directives (see \k{pperror}).
3028 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003029 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003030 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07003031
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003032issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07003033 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003034 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003035 tline->next = expand_smacro(tline->next);
3036 tline = tline->next;
3037 skip_white_(tline);
3038 t = tline ? tline->next : NULL;
3039 skip_white_(t);
3040 if (tok_type_(tline, TOK_STRING) && !t) {
3041 /* The line contains only a quoted string */
3042 p = tline->text;
3043 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08003044 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003045 } else {
3046 /* Not a quoted string, or more than a quoted string */
3047 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003048 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003049 nasm_free(p);
3050 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003051 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003052 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003053
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003054 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003055 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003056 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003057 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003058 j = if_condition(tline->next, i);
3059 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003060 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003061 cond = nasm_malloc(sizeof(Cond));
3062 cond->next = istk->conds;
3063 cond->state = j;
3064 istk->conds = cond;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003065 if(istk->mstk.mstk)
3066 istk->mstk.mstk->condcnt++;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003067 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003068
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003069 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003070 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003071 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003072 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003073 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003074 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003075 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003076
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003077 case COND_DONE:
3078 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003079 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003080
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003081 case COND_ELSE_TRUE:
3082 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003083 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003084 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003085 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003086 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003087
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003088 case COND_IF_FALSE:
3089 /*
3090 * IMPORTANT: In the case of %if, we will already have
3091 * called expand_mmac_params(); however, if we're
3092 * processing an %elif we must have been in a
3093 * non-emitting mode, which would have inhibited
3094 * the normal invocation of expand_mmac_params().
3095 * Therefore, we have to do it explicitly here.
3096 */
3097 j = if_condition(expand_mmac_params(tline->next), i);
3098 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003099 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003100 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003102 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003103
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 case PP_ELSE:
3105 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003106 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003107 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003108 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003109 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003110 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003111 case COND_IF_TRUE:
3112 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003113 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003114 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003115
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003116 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003117 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003118
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003119 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003120 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003121 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003122
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003123 case COND_ELSE_TRUE:
3124 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003125 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003126 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003127 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003128 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003129 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003130 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003131
H. Peter Anvine2c80182005-01-15 22:15:51 +00003132 case PP_ENDIF:
3133 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003134 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003135 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003136 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003137 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003138 cond = istk->conds;
3139 istk->conds = cond->next;
3140 nasm_free(cond);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003141 if(istk->mstk.mstk)
3142 istk->mstk.mstk->condcnt--;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003143 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003144
H. Peter Anvin8b262472019-02-26 14:00:54 -08003145 case PP_RMACRO:
3146 case PP_MACRO:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003147 nasm_assert(!defining);
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003148 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003149 defining->casesense = casesense;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003150 defining->dstk.mmac = defining;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003151 if (i == PP_RMACRO)
3152 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003153 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003154 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003155 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003156 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003157
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003158 src_get(&defining->xline, &defining->fname);
3159
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003160 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3161 while (mmac) {
3162 if (!strcmp(mmac->name, defining->name) &&
3163 (mmac->nparam_min <= defining->nparam_max
3164 || defining->plus)
3165 && (defining->nparam_min <= mmac->nparam_max
3166 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003167 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003168 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003169 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003170 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003171 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003172 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003173 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003174
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 case PP_ENDM:
3176 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003177 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003178 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003179 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003180 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003181 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3182 defining->next = *mmhead;
3183 *mmhead = defining;
3184 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003185 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003186
H. Peter Anvin89cee572009-07-15 09:16:54 -04003187 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003188 /*
3189 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003190 * macro-end marker for a macro with a name. Then we
3191 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003192 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003193 list_for_each(l, istk->expansion)
3194 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003195 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003196
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003197 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003198 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003199 * Remove all conditional entries relative to this
3200 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003201 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003202 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3203 cond = istk->conds;
3204 istk->conds = cond->next;
3205 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003206 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003207 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003208 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003209 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003210 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003211 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003212
H. Peter Anvina26433d2008-07-16 14:40:01 -07003213 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003214 casesense = false;
3215 /* fall through */
3216 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003217 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003218 MMacro **mmac_p;
3219 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003220
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003221 nasm_zero(spec);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003222 spec.casesense = casesense;
3223 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003224 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003225 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003226 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3227 while (mmac_p && *mmac_p) {
3228 mmac = *mmac_p;
3229 if (mmac->casesense == spec.casesense &&
3230 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3231 mmac->nparam_min == spec.nparam_min &&
3232 mmac->nparam_max == spec.nparam_max &&
3233 mmac->plus == spec.plus) {
3234 *mmac_p = mmac->next;
3235 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003236 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003237 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003238 }
3239 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003240 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003241 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003242 }
3243
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 case PP_ROTATE:
3245 if (tline->next && tline->next->type == TOK_WHITESPACE)
3246 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003247 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003249 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003250 return DIRECTIVE_FOUND;
3251 }
3252 t = expand_smacro(tline->next);
3253 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003254 pps.tptr = tline = t;
3255 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003256 tokval.t_type = TOKEN_INVALID;
3257 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003258 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003259 free_tlist(tline);
3260 if (!evalresult)
3261 return DIRECTIVE_FOUND;
3262 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003263 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003264 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003265 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 return DIRECTIVE_FOUND;
3267 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003268 mmac = istk->mstk.mmac;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003269 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003270 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003271 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003272 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003273 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003274 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003275
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003276 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003277 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003278 rotate += mmac->nparam;
3279
3280 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003281 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003282 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003283
H. Peter Anvine2c80182005-01-15 22:15:51 +00003284 case PP_REP:
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003285 {
3286 MMacro *tmp_defining;
3287
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003288 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289 do {
3290 tline = tline->next;
3291 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003292
H. Peter Anvine2c80182005-01-15 22:15:51 +00003293 if (tok_type_(tline, TOK_ID) &&
3294 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003295 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003296 do {
3297 tline = tline->next;
3298 } while (tok_type_(tline, TOK_WHITESPACE));
3299 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003300
H. Peter Anvine2c80182005-01-15 22:15:51 +00003301 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003302 pps.tptr = expand_smacro(tline);
3303 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003304 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003305 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003306 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003307 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003308 if (!evalresult)
3309 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003311 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003313 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003314 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003315 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003316 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003317 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003318 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3319 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003320 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003321 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003322 /*!
3323 *!negative-rep [on] regative %rep count
3324 *! warns about negative counts given to the \c{%rep}
3325 *! preprocessor directive.
3326 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003327 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003328 "negative `%%rep' count: %"PRId64, count);
3329 count = 0;
3330 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003331 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003332 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003333 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003334 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003335 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003336 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003337 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003338 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003339 defining->nolist = nolist;
3340 defining->in_progress = count;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003341 defining->mstk = istk->mstk;
3342 defining->dstk.mstk = tmp_defining;
3343 defining->dstk.mmac = tmp_defining ? tmp_defining->dstk.mmac : NULL;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003344 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003345 break;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003346 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003347
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003349 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003350 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003351 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003353
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 /*
3355 * Now we have a "macro" defined - although it has no name
3356 * and we won't be entering it in the hash tables - we must
3357 * push a macro-end marker for it on to istk->expansion.
3358 * After that, it will take care of propagating itself (a
3359 * macro-end marker line for a macro which is really a %rep
3360 * block will cause the macro to be re-expanded, complete
3361 * with another macro-end marker to ensure the process
3362 * continues) until the whole expansion is forcibly removed
3363 * from istk->expansion by a %exitrep.
3364 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003365 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003366 l->next = istk->expansion;
3367 l->finishes = defining;
3368 l->first = NULL;
3369 istk->expansion = l;
3370
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003371 istk->mstk.mstk = defining;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003372
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003373 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07003374 defining = defining->dstk.mstk;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003375 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003376
H. Peter Anvine2c80182005-01-15 22:15:51 +00003377 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003378 /*
3379 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003380 * macro-end marker for a macro with no name. Then we set
3381 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003382 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003383 list_for_each(l, istk->expansion)
3384 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003385 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003386
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003387 if (l)
3388 l->finishes->in_progress = 1;
3389 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003390 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003391 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003392
H. Peter Anvin8b262472019-02-26 14:00:54 -08003393 case PP_DEFINE:
3394 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003395 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003396 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003397 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003398 bool *eval_params = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003399 SMacro tmpl;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003400
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003401 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003402 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003403
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003404 nasm_zero(tmpl);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 last = tline;
3406 param_start = tline = tline->next;
3407 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003408
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 if (tok_is_(tline, "(")) {
3410 /*
3411 * This macro has parameters.
3412 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003413
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 tline = tline->next;
3415 while (1) {
3416 skip_white_(tline);
3417 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003418 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003419 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003420 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003421 if (tok_is_(tline, "=")) {
3422 have_eval_params = true;
3423 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003424 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003425 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003426
3427 /*
3428 * "*" means an unnamed, ignored argument; it can never
3429 * match anything because "*" is not TOK_ID, and so
3430 * none of the expansion entries will be converted
3431 * to parameters.
3432 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003433 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003434 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3435 /*
3436 * Empty name; duplicate the termination
3437 * token and use the first copy as a placeholder.
3438 * It will never match anything, since the
3439 * associated text doesn't correspond to a TOK_ID.
3440 */
3441 tline = dup_Token(tline, tline);
3442 } else {
3443 nasm_nonfatal("`%s': parameter identifier expected",
3444 tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003445 goto done;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003446 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003448 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 tline = tline->next;
3450 skip_white_(tline);
3451 if (tok_is_(tline, ",")) {
3452 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003453 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003454 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003455 nasm_nonfatal("`)' expected to terminate macro template");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003456 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 }
3458 break;
3459 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003460 }
3461 last = tline;
3462 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003463
3464 if (have_eval_params) {
3465 /* Create evaluated parameters table */
3466 bool is_eval = false;
3467
3468 nasm_newn(eval_params, nparam);
3469 list_for_each(tt, param_start) {
3470 if (is_smac_param(tt->type))
3471 eval_params[smac_nparam(tt->type)] = is_eval;
3472 is_eval = tok_is_(tt, "=");
3473 }
3474 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003475 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003476
H. Peter Anvine2c80182005-01-15 22:15:51 +00003477 if (tok_type_(tline, TOK_WHITESPACE))
3478 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003479 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003480
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003481 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003482 macro_start = tline;
3483 if (!is_macro_id(macro_start)) {
3484 nasm_nonfatal("`%s' expects a macro identifier to alias",
3485 dname);
3486 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003488 tt = macro_start->next;
3489 macro_start->next = NULL;
3490 tline = tline->next;
3491 skip_white_(tline);
3492 if (tline && tline->type) {
3493 nasm_warn(WARN_OTHER,
3494 "trailing garbage after aliasing identifier ignored");
3495 }
3496 free_tlist(tt);
3497 tmpl.alias = true;
3498 } else {
3499 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003500 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003501 tline = expand_smacro(tline);
3502
3503 macro_start = NULL;
3504 t = tline;
3505 while (t) {
3506 if (t->type == TOK_ID) {
3507 list_for_each(tt, param_start)
3508 if (is_smac_param(tt->type) &&
3509 tt->len == t->len &&
3510 !memcmp(tt->text, t->text, tt->len))
3511 t->type = tt->type;
3512 }
3513 tt = t->next;
3514 t->next = macro_start;
3515 macro_start = t;
3516 t = tt;
3517 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003519
H. Peter Anvine2c80182005-01-15 22:15:51 +00003520 /*
3521 * Good. We now have a macro name, a parameter count, and a
3522 * token list (in reverse order) for an expansion. We ought
3523 * to be OK just to create an SMacro, store it, and let
3524 * free_tlist have the rest of the line (which we have
3525 * carefully re-terminated after chopping off the expansion
3526 * from the end).
3527 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003528 tmpl.nparam = nparam;
3529 tmpl.eval_param = eval_params;
3530 define_smacro(mname, casesense, macro_start, &tmpl);
3531 break;
3532 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003533
H. Peter Anvine2c80182005-01-15 22:15:51 +00003534 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003535 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003536 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003537 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003538 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003539 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003540
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003541 undef_smacro(mname, i == PP_UNDEFALIAS);
3542 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003543
H. Peter Anvin8b262472019-02-26 14:00:54 -08003544 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003545 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003546 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003547
H. Peter Anvin9e200162008-06-04 17:23:14 -07003548 last = tline;
3549 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003550 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003551
3552 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003553 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003554
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003555 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003556 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003557 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003558
3559 /*
3560 * We now have a macro name, an implicit parameter count of
3561 * zero, and a string token to use as an expansion. Create
3562 * and store an SMacro.
3563 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003564 define_smacro(mname, casesense, macro_start, NULL);
3565 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003566
H. Peter Anvin8b262472019-02-26 14:00:54 -08003567 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003568 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003569 goto done;
3570
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003571 last = tline;
3572 tline = expand_smacro(tline->next);
3573 last->next = NULL;
3574
3575 t = tline;
3576 while (tok_type_(t, TOK_WHITESPACE))
3577 t = t->next;
3578 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003579 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003580 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003581 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003582 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003583 }
3584
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003585 /*
3586 * Convert the string to a token stream. Note that smacros
3587 * are stored with the token stream reversed, so we have to
3588 * reverse the output of tokenize().
3589 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003590 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003591 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003592
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003593 /*
3594 * We now have a macro name, an implicit parameter count of
3595 * zero, and a numeric token to use as an expansion. Create
3596 * and store an SMacro.
3597 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003598 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003599 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003600 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003601
H. Peter Anvin418ca702008-05-30 10:42:30 -07003602 case PP_PATHSEARCH:
3603 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003604 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003605
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003606 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003607 goto done;
3608
H. Peter Anvin418ca702008-05-30 10:42:30 -07003609 last = tline;
3610 tline = expand_smacro(tline->next);
3611 last->next = NULL;
3612
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003613 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003614 while (tok_type_(t, TOK_WHITESPACE))
3615 t = t->next;
3616
3617 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003618 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003619 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003620 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003621 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003622 }
3623 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003624 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003625 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003626 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003627 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003628
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003629 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003630 if (!found_path)
3631 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003632 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003633
3634 /*
3635 * We now have a macro name, an implicit parameter count of
3636 * zero, and a string token to use as an expansion. Create
3637 * and store an SMacro.
3638 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003639 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003640 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003641 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003642 }
3643
H. Peter Anvine2c80182005-01-15 22:15:51 +00003644 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003645 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003646 goto done;
3647
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 last = tline;
3649 tline = expand_smacro(tline->next);
3650 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003651
H. Peter Anvine2c80182005-01-15 22:15:51 +00003652 t = tline;
3653 while (tok_type_(t, TOK_WHITESPACE))
3654 t = t->next;
3655 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003656 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003657 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 free_tlist(tline);
3659 free_tlist(origline);
3660 return DIRECTIVE_FOUND;
3661 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003662
H. Peter Anvin8b262472019-02-26 14:00:54 -08003663 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003664
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 /*
3666 * We now have a macro name, an implicit parameter count of
3667 * zero, and a numeric token to use as an expansion. Create
3668 * and store an SMacro.
3669 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003670 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671 free_tlist(tline);
3672 free_tlist(origline);
3673 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003674
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003675 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003676 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003677 goto done;
3678
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003679 last = tline;
3680 tline = expand_smacro(tline->next);
3681 last->next = NULL;
3682
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003683 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003684 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003685 switch (t->type) {
3686 case TOK_WHITESPACE:
3687 break;
3688 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003689 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003690 break;
3691 case TOK_OTHER:
3692 if (!strcmp(t->text, ",")) /* permit comma separators */
3693 break;
3694 /* else fall through */
3695 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003696 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003697 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003698 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699 }
3700 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003701
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003702 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003703 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003704 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003705 memcpy(p, t->text, t->len);
3706 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003707 }
3708 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003709
3710 /*
3711 * We now have a macro name, an implicit parameter count of
3712 * zero, and a numeric token to use as an expansion. Create
3713 * and store an SMacro.
3714 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003715 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003716 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003717 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003718 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003719 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003720
H. Peter Anvine2c80182005-01-15 22:15:51 +00003721 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003722 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003723 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003724 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003725
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003726 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003727 goto done;
3728
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 last = tline;
3730 tline = expand_smacro(tline->next);
3731 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003732
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003733 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003734 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003735 while (tok_type_(t, TOK_WHITESPACE))
3736 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003737
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003739 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003740 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003741 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003742 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003743 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003744
H. Peter Anvin8b262472019-02-26 14:00:54 -08003745 pps.tptr = t->next;
3746 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003747 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003748 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 if (!evalresult) {
3750 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003751 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003752 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003753 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003754 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003755 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003756 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003757 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003758
H. Peter Anvin8b262472019-02-26 14:00:54 -08003759 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3760 pps.tptr = pps.tptr->next;
3761 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003762 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003763 } else {
3764 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003765 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003766 if (!evalresult) {
3767 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003768 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003769 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003770 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003771 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003772 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003773 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003774 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003775 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003776
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003777 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003778
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003779 /* make start and count being in range */
3780 if (start < 0)
3781 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003782 if (count < 0)
3783 count = len + count + 1 - start;
3784 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003785 count = len - start;
3786 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003787 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003788
H. Peter Anvin8b262472019-02-26 14:00:54 -08003789 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003790 macro_start->len = count;
3791 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3792 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003793
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 /*
3795 * We now have a macro name, an implicit parameter count of
3796 * zero, and a numeric token to use as an expansion. Create
3797 * and store an SMacro.
3798 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003799 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003800 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003801 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003802 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003803
H. Peter Anvin8b262472019-02-26 14:00:54 -08003804 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003805 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003806 goto done;
3807
H. Peter Anvine2c80182005-01-15 22:15:51 +00003808 last = tline;
3809 tline = expand_smacro(tline->next);
3810 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003811
H. Peter Anvin8b262472019-02-26 14:00:54 -08003812 pps.tptr = tline;
3813 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003815 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003816 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003817 if (!evalresult)
3818 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003819
H. Peter Anvine2c80182005-01-15 22:15:51 +00003820 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003821 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003822
H. Peter Anvine2c80182005-01-15 22:15:51 +00003823 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003824 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003825 free_tlist(origline);
3826 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003827 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003828
H. Peter Anvin8b262472019-02-26 14:00:54 -08003829 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003830
H. Peter Anvine2c80182005-01-15 22:15:51 +00003831 /*
3832 * We now have a macro name, an implicit parameter count of
3833 * zero, and a numeric token to use as an expansion. Create
3834 * and store an SMacro.
3835 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003836 define_smacro(mname, casesense, macro_start, NULL);
3837 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003838
H. Peter Anvine2c80182005-01-15 22:15:51 +00003839 case PP_LINE:
3840 /*
3841 * Syntax is `%line nnn[+mmm] [filename]'
3842 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003843 if (unlikely(pp_noline))
3844 goto done;
3845
H. Peter Anvine2c80182005-01-15 22:15:51 +00003846 tline = tline->next;
3847 skip_white_(tline);
3848 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003849 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003850 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003851 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003852 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003853 m = 1;
3854 tline = tline->next;
3855 if (tok_is_(tline, "+")) {
3856 tline = tline->next;
3857 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003858 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003859 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003860 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003861 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003862 tline = tline->next;
3863 }
3864 skip_white_(tline);
3865 src_set_linnum(k);
3866 istk->lineinc = m;
3867 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003868 char *fname = detoken(tline, false);
3869 src_set_fname(fname);
3870 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003871 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003872 break;
3873 }
3874
3875done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003876 free_tlist(origline);
3877 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003878}
3879
3880/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003881 * Ensure that a macro parameter contains a condition code and
3882 * nothing else. Return the condition code index if so, or -1
3883 * otherwise.
3884 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003885static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003886{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003887 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003888
H. Peter Anvin25a99342007-09-22 17:45:45 -07003889 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003890 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003891
H. Peter Anvineba20a72002-04-30 20:53:55 +00003892 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003893 if (!t)
3894 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003895 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003896 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003897 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003898 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003899 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003900 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003901
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003902 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003903}
3904
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003905/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003906 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003907 * pasting, if @handle_explicit passed then explicit pasting
3908 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003909 * The @m array can contain a series of token types which are
3910 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003911 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003912static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003913 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003914{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003915 Token *tok, *next, **prev_next, **prev_nonspace;
3916 bool pasted = false;
3917 char *buf, *p;
3918 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003919
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003920 /*
3921 * The last token before pasting. We need it
3922 * to be able to connect new handled tokens.
3923 * In other words if there were a tokens stream
3924 *
3925 * A -> B -> C -> D
3926 *
3927 * and we've joined tokens B and C, the resulting
3928 * stream should be
3929 *
3930 * A -> BC -> D
3931 */
3932 tok = *head;
3933 prev_next = NULL;
3934
3935 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3936 prev_nonspace = head;
3937 else
3938 prev_nonspace = NULL;
3939
3940 while (tok && (next = tok->next)) {
3941
3942 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003943 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003944 /* Zap redundant whitespaces */
3945 while (tok_type_(next, TOK_WHITESPACE))
3946 next = delete_Token(next);
3947 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003948 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003949
3950 case TOK_PASTE:
3951 /* Explicit pasting */
3952 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003953 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003954 next = delete_Token(tok);
3955
3956 while (tok_type_(next, TOK_WHITESPACE))
3957 next = delete_Token(next);
3958
3959 if (!pasted)
3960 pasted = true;
3961
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003962 /* Left pasting token is start of line */
3963 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003964 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003965
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003966 /*
3967 * No ending token, this might happen in two
3968 * cases
3969 *
3970 * 1) There indeed no right token at all
3971 * 2) There is a bare "%define ID" statement,
3972 * and @ID does expand to whitespace.
3973 *
3974 * So technically we need to do a grammar analysis
3975 * in another stage of parsing, but for now lets don't
3976 * change the behaviour people used to. Simply allow
3977 * whitespace after paste token.
3978 */
3979 if (!next) {
3980 /*
3981 * Zap ending space tokens and that's all.
3982 */
3983 tok = (*prev_nonspace)->next;
3984 while (tok_type_(tok, TOK_WHITESPACE))
3985 tok = delete_Token(tok);
3986 tok = *prev_nonspace;
3987 tok->next = NULL;
3988 break;
3989 }
3990
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003991 tok = *prev_nonspace;
3992 while (tok_type_(tok, TOK_WHITESPACE))
3993 tok = delete_Token(tok);
3994 len = strlen(tok->text);
3995 len += strlen(next->text);
3996
3997 p = buf = nasm_malloc(len + 1);
3998 strcpy(p, tok->text);
3999 p = strchr(p, '\0');
4000 strcpy(p, next->text);
4001
4002 delete_Token(tok);
4003
4004 tok = tokenize(buf);
4005 nasm_free(buf);
4006
4007 *prev_nonspace = tok;
4008 while (tok && tok->next)
4009 tok = tok->next;
4010
4011 tok->next = delete_Token(next);
4012
4013 /* Restart from pasted tokens head */
4014 tok = *prev_nonspace;
4015 break;
4016
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004017 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004018 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004019 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004020 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4021 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004022
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004023 len = 0;
4024 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4025 len += strlen(next->text);
4026 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004027 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004028
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004029 /* No match or no text to process */
4030 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004031 break;
4032
4033 len += strlen(tok->text);
4034 p = buf = nasm_malloc(len + 1);
4035
Adam Majer1a069432017-07-25 11:12:35 +02004036 strcpy(p, tok->text);
4037 p = strchr(p, '\0');
4038 tok = delete_Token(tok);
4039
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004040 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004041 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4042 strcpy(p, tok->text);
4043 p = strchr(p, '\0');
4044 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004045 tok = delete_Token(tok);
4046 }
4047
4048 tok = tokenize(buf);
4049 nasm_free(buf);
4050
4051 if (prev_next)
4052 *prev_next = tok;
4053 else
4054 *head = tok;
4055
4056 /*
4057 * Connect pasted into original stream,
4058 * ie A -> new-tokens -> B
4059 */
4060 while (tok && tok->next)
4061 tok = tok->next;
4062 tok->next = next;
4063
4064 if (!pasted)
4065 pasted = true;
4066
4067 /* Restart from pasted tokens head */
4068 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004069 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004070
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004071 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004072 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004073
4074 prev_next = &tok->next;
4075
4076 if (tok->next &&
4077 !tok_type_(tok->next, TOK_WHITESPACE) &&
4078 !tok_type_(tok->next, TOK_PASTE))
4079 prev_nonspace = prev_next;
4080
4081 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004082 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004083
4084 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004085}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004086
4087/*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004088 * Computes the proper rotation of mmacro parameters
4089 */
4090static int mmac_rotate(const MMacro *mac, unsigned int n)
4091{
4092 if (--n < mac->nparam)
4093 n = (n + mac->rotate) % mac->nparam;
4094
4095 return n+1;
4096}
4097
4098/*
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004099 * expands to a list of tokens from %{x:y}
4100 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004101static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004102{
4103 Token *t = tline, **tt, *tm, *head;
4104 char *pos;
4105 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004106
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004107 pos = strchr(tline->text, ':');
4108 nasm_assert(pos);
4109
4110 lst = atoi(pos + 1);
4111 fst = atoi(tline->text + 1);
4112
4113 /*
4114 * only macros params are accounted so
4115 * if someone passes %0 -- we reject such
4116 * value(s)
4117 */
4118 if (lst == 0 || fst == 0)
4119 goto err;
4120
4121 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004122 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4123 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004124 goto err;
4125
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004126 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4127 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004128
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004129 /* count from zero */
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004130 fst--, lst--;
4131
4132 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004133 * It will be at least one token. Note we
4134 * need to scan params until separator, otherwise
4135 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004136 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004137 j = (fst + mac->rotate) % mac->nparam;
4138 tm = mac->params[j+1];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004139 if (!tm)
4140 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004141 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004142 tt = &head->next, tm = tm->next;
4143 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004144 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004145 *tt = t, tt = &t->next, tm = tm->next;
4146 }
4147
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004148 if (fst < lst) {
4149 for (i = fst + 1; i <= lst; i++) {
4150 t = new_Token(NULL, TOK_OTHER, ",", 0);
4151 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004152 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004153 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004154 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004155 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004156 *tt = t, tt = &t->next, tm = tm->next;
4157 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004158 }
4159 } else {
4160 for (i = fst - 1; i >= lst; i--) {
4161 t = new_Token(NULL, TOK_OTHER, ",", 0);
4162 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004163 j = (i + mac->rotate) % mac->nparam;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004164 tm = mac->params[j+1];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004165 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004166 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004167 *tt = t, tt = &t->next, tm = tm->next;
4168 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004169 }
4170 }
4171
4172 *last = tt;
4173 return head;
4174
4175err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004176 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004177 &tline->text[1]);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004178 return NULL;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004179}
4180
H. Peter Anvin76690a12002-04-30 20:52:49 +00004181/*
4182 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004183 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004184 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004186static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004187{
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004188 Token **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004189 bool changed = false;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004190 MMacro *mac = istk->mstk.mmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004191
4192 tail = &thead;
4193 thead = NULL;
4194
H. Peter Anvine2c80182005-01-15 22:15:51 +00004195 while (tline) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004196 bool change;
4197 Token *t = tline;
4198 char *text = t->text;
4199 int type = t->type;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004200
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004201 tline = tline->next;
4202 t->next = NULL;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004203
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004204 switch (type) {
4205 case TOK_PREPROC_ID:
4206 {
4207 Token *tt = NULL;
4208
4209 change = false;
4210
4211 if (!text || !text[0])
4212 break;
4213 if (!(nasm_isdigit(text[1]) || text[1] == '%' ||
4214 ((text[1] == '+' || text[1] == '-') && text[2])))
4215 break;
4216
4217 change = true;
4218
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004219 if (!mac) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004220 nasm_nonfatal("`%s': not in a macro call", text);
4221 text = NULL;
4222 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004223 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004224
4225 if (strchr(text, ':')) {
4226 /*
4227 * seems we have a parameters range here
4228 */
4229 Token *head, **last;
4230 head = expand_mmac_params_range(mac, t, &last);
4231 if (head) {
4232 *tail = head;
4233 *last = tline;
4234 text = NULL;
4235 }
4236 break;
4237 }
4238
4239 switch (text[1]) {
4240 /*
4241 * We have to make a substitution of one of the
4242 * forms %1, %-1, %+1, %%foo, %0, %00.
4243 */
4244 case '0':
4245 if (!text[2]) {
4246 type = TOK_NUMBER;
4247 text = nasm_asprintf("%d", mac->nparam);
4248 break;
4249 }
4250 if (text[2] != '0' || text[3])
4251 goto invalid;
4252 /* a possible captured label == mac->params[0] */
4253 /* fall through */
4254 default:
4255 {
4256 unsigned long n;
4257 char *ep;
4258
4259 n = strtoul(text + 1, &ep, 10);
4260 if (unlikely(*ep))
4261 goto invalid;
4262
4263 if (n <= mac->nparam) {
4264 n = mmac_rotate(mac, n);
4265 dup_tlistn(mac->params[n], mac->paramlen[n], &tail);
4266 }
4267 text = NULL;
4268 break;
4269 }
4270 case '%':
4271 type = TOK_ID;
4272 text = nasm_asprintf("..@%"PRIu64".%s", mac->unique, text+2);
4273 break;
4274 case '-':
4275 case '+':
4276 {
4277 int cc;
4278 unsigned long n;
4279 char *ep;
4280
4281 text = NULL;
4282
4283 n = strtoul(t->text + 2, &ep, 10);
4284 if (unlikely(*ep))
4285 goto invalid;
4286
4287 if (n && n < mac->nparam) {
4288 n = mmac_rotate(mac, n);
4289 tt = mac->params[n];
4290 }
4291 cc = find_cc(tt);
4292 if (cc == -1) {
4293 nasm_nonfatal("macro parameter `%s' is not a condition code",
4294 text);
4295 text = NULL;
4296 break;
4297 }
4298
4299 type = TOK_ID;
4300 if (text[1] == '-') {
4301 int ncc = inverse_ccs[cc];
4302 if (unlikely(ncc == -1)) {
4303 nasm_nonfatal("condition code `%s' is not invertible",
4304 conditions[cc]);
4305 break;
4306 }
4307 cc = ncc;
4308 }
4309 text = nasm_strdup(conditions[cc]);
4310 break;
4311 }
4312
4313 invalid:
4314 nasm_nonfatal("invalid macro parameter: `%s'", text);
4315 text = NULL;
4316 break;
4317 }
4318 break;
4319 }
4320
4321 case TOK_PREPROC_Q:
4322 if (mac) {
4323 type = TOK_ID;
4324 text = nasm_strdup(mac->iname);
4325 change = true;
4326 }
4327 break;
4328
4329 case TOK_PREPROC_QQ:
4330 if (mac) {
4331 type = TOK_ID;
4332 text = nasm_strdup(mac->name);
4333 change = true;
4334 }
4335 break;
4336
4337 case TOK_INDIRECT:
4338 {
4339 Token *tt;
4340
4341 tt = tokenize(t->text);
4342 tt = expand_mmac_params(tt);
4343 tt = expand_smacro(tt);
4344 /* Why dup_tlist() here? We should own tt... */
4345 dup_tlist(tt, &tail);
4346 text = NULL;
4347 change = true;
4348 break;
4349 }
4350
4351 default:
4352 change = false;
4353 break;
4354 }
4355
4356 if (change) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004357 if (!text) {
4358 delete_Token(t);
4359 } else {
4360 *tail = t;
4361 tail = &t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004362 nasm_free(t->text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004363 t->len = strlen(text);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004364 t->type = type;
4365 t->text = text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004366 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004367 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004368 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004369 *tail = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004370 tail = &t->next;
4371 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004372 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004373
H. Peter Anvineba20a72002-04-30 20:53:55 +00004374 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004375
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004376 if (changed) {
4377 const struct tokseq_match t[] = {
4378 {
4379 PP_CONCAT_MASK(TOK_ID) |
4380 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4381 PP_CONCAT_MASK(TOK_ID) |
4382 PP_CONCAT_MASK(TOK_NUMBER) |
4383 PP_CONCAT_MASK(TOK_FLOAT) |
4384 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4385 },
4386 {
4387 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4388 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4389 }
4390 };
4391 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4392 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004393
H. Peter Anvin76690a12002-04-30 20:52:49 +00004394 return thead;
4395}
4396
H. Peter Anvin322bee02019-08-10 01:38:06 -07004397static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004398
H. Peter Anvin76690a12002-04-30 20:52:49 +00004399/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004400 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004401 * a macro at all, it is simply copied to the output and the pointer
4402 * advanced. tpp should be a pointer to a pointer (usually the next
4403 * pointer of the previous token) to the first token. **tpp is updated
4404 * to point to the last token of the expansion, and *tpp updated to
4405 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004406 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004407 * If the expansion is empty, *tpp will be unchanged but **tpp will
4408 * be advanced past the macro call.
4409 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004410 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004411 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004412static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004413{
4414 Token **params = NULL;
4415 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004416 Token *tline = **tpp;
4417 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004418 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004419 unsigned int i;
4420 Token *t, *tup, *ttail;
4421 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004422
4423 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004424 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004425
4426 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004427
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004428 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004429 smacro_deadman.levels--;
4430
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004431 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004432 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004433 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004434 smacro_deadman.triggered = true;
4435 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004436 goto not_a_macro;
4437 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004438 head = (SMacro *)hash_findix(&smacros, mname);
4439 } else if (tline->type == TOK_PREPROC_ID) {
4440 Context *ctx = get_ctx(mname, &mname);
4441 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4442 } else {
4443 goto not_a_macro;
4444 }
4445
4446 /*
4447 * We've hit an identifier of some sort. First check whether the
4448 * identifier is a single-line macro at all, then think about
4449 * checking for parameters if necessary.
4450 */
4451 list_for_each(m, head) {
4452 if (!mstrcmp(m->name, mname, m->casesense))
4453 break;
4454 }
4455
4456 if (!m) {
4457 goto not_a_macro;
4458 }
4459
4460 /* Parse parameters, if applicable */
4461
4462 params = NULL;
4463 nparam = 0;
4464
4465 if (m->nparam == 0) {
4466 /*
4467 * Simple case: the macro is parameterless.
4468 * Nothing to parse; the expansion code will
4469 * drop the macro name token.
4470 */
4471 } else {
4472 /*
4473 * Complicated case: at least one macro with this name
4474 * exists and takes parameters. We must find the
4475 * parameters in the call, count them, find the SMacro
4476 * that corresponds to that form of the macro call, and
4477 * substitute for the parameters when we expand. What a
4478 * pain.
4479 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004480 Token **phead, **pep;
4481 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004482 int white = 0;
4483 int brackets = 0;
4484 bool bracketed = false;
4485 bool bad_bracket = false;
4486 unsigned int sparam = PARAM_DELTA;
4487
4488 tline = tline->next;
4489
4490 while (tok_type_(tline, TOK_WHITESPACE)) {
4491 tline = tline->next;
4492 }
4493 if (!tok_is_(tline, "(")) {
4494 /*
4495 * This macro wasn't called with parameters: ignore
4496 * the call. (Behaviour borrowed from gnu cpp.)
4497 */
4498 goto not_a_macro;
4499 }
4500
4501 paren = 1;
4502 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004503 nasm_newn(params, sparam);
4504 phead = pep = &params[0];
4505 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004506
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004507 while (paren) {
4508 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004509 char ch;
4510
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004511 tline = tline->next;
4512
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004513 if (!tline) {
4514 nasm_nonfatal("macro call expects terminating `)'");
4515 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004516 }
4517
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004518 ch = 0;
4519 skip = false;
4520
4521 switch (tline->type) {
4522 case TOK_OTHER:
4523 if (!tline->text[1])
4524 ch = tline->text[0];
4525 break;
4526
4527 case TOK_WHITESPACE:
4528 if (brackets || *phead)
4529 white++; /* Keep interior whitespace */
4530 skip = true;
4531 break;
4532
4533 default:
4534 break;
4535 }
4536
4537 switch (ch) {
4538 case ',':
4539 if (!brackets) {
4540 if (++nparam >= sparam) {
4541 sparam += PARAM_DELTA;
4542 params = nasm_realloc(params, sparam * sizeof *params);
4543 }
4544 pep = &params[nparam];
4545 *pep = NULL;
4546 bracketed = false;
4547 skip = true;
4548 }
4549 break;
4550
4551 case '{':
4552 if (!bracketed) {
4553 bracketed = !*phead;
4554 skip = true;
4555 }
4556 brackets++;
4557 break;
4558
4559 case '}':
4560 if (brackets > 0) {
4561 if (!--brackets)
4562 skip = bracketed;
4563 }
4564 break;
4565
4566 case '(':
4567 if (!brackets)
4568 paren++;
4569 break;
4570
4571 case ')':
4572 if (!brackets) {
4573 paren--;
4574 if (!paren) {
4575 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004576 /* Count the final argument */
4577 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004578 }
4579 }
4580 break;
4581
4582 default:
4583 break; /* Normal token */
4584 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004585
4586 if (!skip) {
4587 Token *t;
4588
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004589 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004590
4591 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004592 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004593 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004594 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004595 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004596 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004597 pep = &t->next;
4598 white = 0;
4599 }
4600 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004601
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004602 /*
4603 * Look for a macro matching in both name and parameter count.
4604 * We already know any matches cannot be anywhere before the
4605 * current position of "m", so there is no reason to
4606 * backtrack.
4607 */
4608 while (1) {
4609 if (!m) {
4610 /*!
4611 *!macro-params-single [on] single-line macro calls with wrong parameter count
4612 *! warns about \i{single-line macros} being invoked
4613 *! with the wrong number of parameters.
4614 */
4615 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4616 "single-line macro `%s' exists, "
4617 "but not taking %d parameter%s",
4618 mname, nparam, (nparam == 1) ? "" : "s");
4619 goto not_a_macro;
4620 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004621
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004622 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4623 break; /* It's good */
4624
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004625 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004626 }
4627 }
4628
4629 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004630 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004631
4632 /* Expand each parameter */
4633 m->in_progress = true;
4634
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004635 if (m->eval_param) {
4636 for (i = 0; i < nparam; i++) {
4637 if (m->eval_param[i]) {
4638 /* Evaluate this parameter as a number */
4639 struct ppscan pps;
4640 struct tokenval tokval;
4641 expr *evalresult;
4642 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004643
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004644 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4645 pps.ntokens = -1;
4646 tokval.t_type = TOKEN_INVALID;
4647 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004648
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004649 free_tlist(eval_param);
4650 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004651
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004652 if (!evalresult) {
4653 /* Nothing meaningful to do */
4654 } else if (tokval.t_type) {
4655 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4656 } else if (!is_simple(evalresult)) {
4657 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4658 } else {
4659 params[i] = make_tok_num(reloc_value(evalresult));
4660 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004661 }
4662 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004663 }
4664
4665 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004666 tline = tline->next; /* Remove the macro call from the input */
4667 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004668
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004669 /* Note: we own the expansion this returns. */
4670 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004671
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004672 tup = NULL;
4673 ttail = NULL; /* Pointer to the last token of the expansion */
4674 while (t) {
4675 enum pp_token_type type = t->type;
4676 Token *tnext = t->next;
4677 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004678
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004679 switch (type) {
4680 case TOK_PREPROC_Q:
4681 case TOK_PREPROC_QQ:
4682 t->type = TOK_ID;
4683 nasm_free(t->text);
4684 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4685 t->len = nasm_last_string_len();
4686 t->next = tline;
4687 break;
4688
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004689 case TOK_ID:
4690 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004691 /*
4692 * Chain this into the target line *before* expanding,
4693 * that way we pick up any arguments to the new macro call,
4694 * if applicable.
4695 */
4696 t->next = tline;
4697 tp = &t;
4698 expand_one_smacro(&tp);
4699 if (t == tline)
4700 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004701 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004702
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004703 default:
4704 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004705 unsigned int param = smac_nparam(t->type);
4706 nasm_assert(!tup && param < nparam);
4707 delete_Token(t);
4708 t = NULL;
4709 tup = tnext;
4710 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004711 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004712 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004713 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004714 }
4715
4716 if (t) {
4717 tline = t;
4718 if (!ttail)
4719 ttail = t;
4720 }
4721
4722 if (tnext) {
4723 t = tnext;
4724 } else {
4725 t = tup;
4726 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004727 }
4728 }
4729
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004730 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004731 if (ttail)
4732 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004733
4734 m->in_progress = false;
4735
4736 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004737 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004738 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004739
4740 /*
4741 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004742 * and then advance to the next input token. Note that this is
4743 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004744 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004745not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004746 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004747 m = NULL;
4748done:
4749 smacro_deadman.levels++;
4750 if (unlikely(params))
4751 free_tlist_array(params, nparam);
4752 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004753}
4754
4755/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004756 * Expand all single-line macro calls made in the given line.
4757 * Return the expanded version of the line. The original is deemed
4758 * to be destroyed in the process. (In reality we'll just move
4759 * Tokens from input to output a lot of the time, rather than
4760 * actually bothering to destroy and replicate.)
4761 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004762static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004763{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004764 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07004765 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4766 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004767 return expand_smacro_noreset(tline);
4768}
4769
4770static Token *expand_smacro_noreset(Token * tline)
4771{
4772 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004773 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004774 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004775
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004776 /*
4777 * Trick: we should avoid changing the start token pointer since it can
4778 * be contained in "next" field of other token. Because of this
4779 * we allocate a copy of first token and work with it; at the end of
4780 * routine we copy it back
4781 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004782 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004783 tline = new_Token(org_tline->next, org_tline->type,
4784 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004785 nasm_free(org_tline->text);
4786 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004787 }
4788
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004789
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004790 /*
4791 * Pretend that we always end up doing expansion on the first pass;
4792 * that way %+ get processed. However, if we process %+ before the
4793 * first pass, we end up with things like MACRO %+ TAIL trying to
4794 * look up the macro "MACROTAIL", which we don't want.
4795 */
4796 expanded = true;
4797 thead = tline;
4798 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004799 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004800 {
4801 PP_CONCAT_MASK(TOK_ID) |
4802 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4803 PP_CONCAT_MASK(TOK_ID) |
4804 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4805 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4806 }
4807 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004808
4809 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004810 while ((t = *tail)) /* main token loop */
4811 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004812
4813 if (!expanded) {
4814 tline = thead;
4815 break; /* Done! */
4816 }
4817
4818 /*
4819 * Now scan the entire line and look for successive TOK_IDs
4820 * that resulted after expansion (they can't be produced by
4821 * tokenize()). The successive TOK_IDs should be concatenated.
4822 * Also we look for %+ tokens and concatenate the tokens
4823 * before and after them (without white spaces in between).
4824 */
4825 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004826
4827 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004828 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004829
H. Peter Anvine2c80182005-01-15 22:15:51 +00004830 if (org_tline) {
4831 if (thead) {
4832 *org_tline = *thead;
4833 /* since we just gave text to org_line, don't free it */
4834 thead->text = NULL;
4835 delete_Token(thead);
4836 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004837 /*
4838 * The expression expanded to empty line;
4839 * we can't return NULL because of the "trick" above.
4840 * Just set the line to a single WHITESPACE token.
4841 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004842 memset(org_tline, 0, sizeof(*org_tline));
4843 org_tline->text = NULL;
4844 org_tline->type = TOK_WHITESPACE;
4845 }
4846 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004847 }
4848
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004849 return thead;
4850}
4851
4852/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004853 * Similar to expand_smacro but used exclusively with macro identifiers
4854 * right before they are fetched in. The reason is that there can be
4855 * identifiers consisting of several subparts. We consider that if there
4856 * are more than one element forming the name, user wants a expansion,
4857 * otherwise it will be left as-is. Example:
4858 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004859 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004860 *
4861 * the identifier %$abc will be left as-is so that the handler for %define
4862 * will suck it and define the corresponding value. Other case:
4863 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004864 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004865 *
4866 * In this case user wants name to be expanded *before* %define starts
4867 * working, so we'll expand %$abc into something (if it has a value;
4868 * otherwise it will be left as-is) then concatenate all successive
4869 * PP_IDs into one.
4870 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004871static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004872{
4873 Token *cur, *oldnext = NULL;
4874
H. Peter Anvin734b1882002-04-30 21:01:08 +00004875 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004876 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004877
4878 cur = tline;
4879 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004880 (cur->next->type == TOK_ID ||
4881 cur->next->type == TOK_PREPROC_ID
4882 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004883 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004884
4885 /* If identifier consists of just one token, don't expand */
4886 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004887 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004888
H. Peter Anvine2c80182005-01-15 22:15:51 +00004889 if (cur) {
4890 oldnext = cur->next; /* Detach the tail past identifier */
4891 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004892 }
4893
H. Peter Anvin734b1882002-04-30 21:01:08 +00004894 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004895
H. Peter Anvine2c80182005-01-15 22:15:51 +00004896 if (cur) {
4897 /* expand_smacro possibly changhed tline; re-scan for EOL */
4898 cur = tline;
4899 while (cur && cur->next)
4900 cur = cur->next;
4901 if (cur)
4902 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004903 }
4904
4905 return tline;
4906}
4907
4908/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004909 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004910 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004911 * to check for an initial label - that's taken care of in
4912 * expand_mmacro - but must check numbers of parameters. Guaranteed
4913 * to be called with tline->type == TOK_ID, so the putative macro
4914 * name is easy to find.
4915 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004916static MMacro *is_mmacro(Token * tline, int *nparamp, Token ***params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004917{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004918 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004919 Token **params;
4920 int nparam;
4921
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004922 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004923
4924 /*
4925 * Efficiency: first we see if any macro exists with the given
4926 * name. If not, we can return NULL immediately. _Then_ we
4927 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004928 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004929 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004930 list_for_each(m, head)
4931 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004932 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004933 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004934 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004935
4936 /*
4937 * OK, we have a potential macro. Count and demarcate the
4938 * parameters.
4939 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004940 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004941
4942 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004943 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004944 * structure that handles this number.
4945 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004946 while (m) {
4947 if (m->nparam_min <= nparam
4948 && (m->plus || nparam <= m->nparam_max)) {
4949 /*
4950 * This one is right. Just check if cycle removal
4951 * prohibits us using it before we actually celebrate...
4952 */
4953 if (m->in_progress > m->max_depth) {
4954 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004955 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004956 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004957 }
4958 nasm_free(params);
4959 return NULL;
4960 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004961 /*
4962 * It's right, and we can use it. Add its default
4963 * parameters to the end of our list if necessary.
4964 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004965 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004966 params =
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004967 nasm_realloc(params, sizeof(*params) *
4968 (m->nparam_min + m->ndefs + 2));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004969 while (nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004970 nparam++;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004971 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004972 }
4973 }
4974 /*
4975 * If we've gone over the maximum parameter count (and
4976 * we're in Plus mode), ignore parameters beyond
4977 * nparam_max.
4978 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004979 if (m->plus && nparam > m->nparam_max)
4980 nparam = m->nparam_max;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004981
4982 /* Done! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004983 *params_array = params;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07004984 *nparamp = nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004985 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004986 }
4987 /*
4988 * This one wasn't right: look for the next one with the
4989 * same name.
4990 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004991 list_for_each(m, m->next)
4992 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004993 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004994 }
4995
4996 /*
4997 * After all that, we didn't find one with the right number of
4998 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004999 *!
5000 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
5001 *! warns about \i{multi-line macros} being invoked
5002 *! with the wrong number of parameters. See \k{mlmacover} for an
5003 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005004 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005005 nasm_warn(WARN_MACRO_PARAMS_MULTI,
5006 "multi-line macro `%s' exists, but not taking %d parameter%s",
5007 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00005008 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005009 return NULL;
5010}
5011
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005012
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005013#if 0
5014
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005015/*
5016 * Save MMacro invocation specific fields in
5017 * preparation for a recursive macro expansion
5018 */
5019static void push_mmacro(MMacro *m)
5020{
5021 MMacroInvocation *i;
5022
5023 i = nasm_malloc(sizeof(MMacroInvocation));
5024 i->prev = m->prev;
5025 i->params = m->params;
5026 i->iline = m->iline;
5027 i->nparam = m->nparam;
5028 i->rotate = m->rotate;
5029 i->paramlen = m->paramlen;
5030 i->unique = m->unique;
5031 i->condcnt = m->condcnt;
5032 m->prev = i;
5033}
5034
5035
5036/*
5037 * Restore MMacro invocation specific fields that were
5038 * saved during a previous recursive macro expansion
5039 */
5040static void pop_mmacro(MMacro *m)
5041{
5042 MMacroInvocation *i;
5043
5044 if (m->prev) {
5045 i = m->prev;
5046 m->prev = i->prev;
5047 m->params = i->params;
5048 m->iline = i->iline;
5049 m->nparam = i->nparam;
5050 m->rotate = i->rotate;
5051 m->paramlen = i->paramlen;
5052 m->unique = i->unique;
5053 m->condcnt = i->condcnt;
5054 nasm_free(i);
5055 }
5056}
5057
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005058#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005059
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005060/*
5061 * Expand the multi-line macro call made by the given line, if
5062 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005063 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005064 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005065static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005066{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005067 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005068 Token *label = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005069 bool dont_prepend = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 Token **params, *t, *tt;
5071 MMacro *m;
5072 Line *l, *ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005073 int i, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005074 const char *mname;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005075 int nparam = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005076
5077 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005078 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005079 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005080 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005081 return 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005082 m = is_mmacro(t, &nparam, &params);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005084 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005085 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005086 Token *last;
5087 /*
5088 * We have an id which isn't a macro call. We'll assume
5089 * it might be a label; we'll also check to see if a
5090 * colon follows it. Then, if there's another id after
5091 * that lot, we'll check it again for macro-hood.
5092 */
5093 label = last = t;
5094 t = t->next;
5095 if (tok_type_(t, TOK_WHITESPACE))
5096 last = t, t = t->next;
5097 if (tok_is_(t, ":")) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005098 dont_prepend = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005099 last = t, t = t->next;
5100 if (tok_type_(t, TOK_WHITESPACE))
5101 last = t, t = t->next;
5102 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005103 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &nparam, &params)))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005104 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005105 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005106 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005107 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005108 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005109
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005110 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5111 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5112 if (!mmacro_deadman.triggered) {
5113 nasm_nonfatal("interminable multiline macro recursion");
5114 mmacro_deadman.triggered = true;
5115 }
5116 return 0;
5117 }
5118
5119 mmacro_deadman.total++;
5120 mmacro_deadman.levels++;
5121
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005122 /*
5123 * Fix up the parameters: this involves stripping leading and
5124 * trailing whitespace, then stripping braces if they are
5125 * present.
5126 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005127 nasm_newn(paramlen, nparam+1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005128
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005129 for (i = 1; (t = params[i]); i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005130 int brace = 0;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005131 int comma = !m->plus || i < nparam;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005132
H. Peter Anvine2c80182005-01-15 22:15:51 +00005133 skip_white_(t);
5134 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005135 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005136 params[i] = t;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005137 while (t) {
5138 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5139 break; /* ... because we have hit a comma */
5140 if (comma && t->type == TOK_WHITESPACE
5141 && tok_is_(t->next, ","))
5142 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005143 if (brace && t->type == TOK_OTHER) {
5144 if (t->text[0] == '{')
5145 brace++; /* ... or a nested opening brace */
5146 else if (t->text[0] == '}')
5147 if (!--brace)
5148 break; /* ... or a brace */
5149 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005150 t = t->next;
5151 paramlen[i]++;
5152 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005153 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005154 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005155 }
5156
5157 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005158 * OK, we have a MMacro structure together with a set of
5159 * parameters. We must now go through the expansion and push
5160 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005161 * parameter tokens and macro-local tokens doesn't get done
5162 * until the single-line macro substitution process; this is
5163 * because delaying them allows us to change the semantics
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005164 * later through %rotate and give the right semantics for
5165 * nested mmacros.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005166 *
5167 * First, push an end marker on to istk->expansion, mark this
5168 * macro as in progress, and set up its invocation-specific
5169 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005170 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005171 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005172 ll->next = istk->expansion;
5173 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005174 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005175
5176 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005177 * Save the previous MMacro expansion in the case of
5178 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005179 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005180#if 0
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005181 if (m->max_depth && m->in_progress)
5182 push_mmacro(m);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005183#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005184
5185 m->in_progress ++;
5186 m->params = params;
5187 m->iline = tline;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005188 m->iname = nasm_strdup(mname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005189 m->nparam = nparam;
5190 m->rotate = 0;
5191 m->paramlen = paramlen;
5192 m->unique = unique++;
5193 m->lineno = 0;
5194 m->condcnt = 0;
5195
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005196 m->mstk = istk->mstk;
5197 istk->mstk.mstk = istk->mstk.mmac = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005198
5199 list_for_each(l, m->expansion) {
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005200 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005201 ll->next = istk->expansion;
5202 istk->expansion = ll;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005203 ll->first = dup_tlist(l->first, NULL);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005204 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005205
5206 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005207 * If we had a label, and this macro definition does not include
5208 * a %00, push it on as the first line of, ot
H. Peter Anvineba20a72002-04-30 20:53:55 +00005209 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005210 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005211 if (label) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005212 /*
5213 * We had a label. If this macro contains an %00 parameter,
5214 * save the value as a special parameter (which is what it
5215 * is), otherwise push it as the first line of the macro
5216 * expansion.
5217 */
5218 if (m->capture_label) {
5219 params[0] = dup_Token(NULL, label);
5220 paramlen[0] = 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005221 free_tlist(startline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005222 } else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005223 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005224 ll->finishes = NULL;
5225 ll->next = istk->expansion;
5226 istk->expansion = ll;
5227 ll->first = startline;
5228 if (!dont_prepend) {
5229 while (label->next)
5230 label = label->next;
5231 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005232 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005233 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005234 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005235
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005236 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005237
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005238 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005239}
5240
H. Peter Anvin130736c2016-02-17 20:27:41 -08005241/*
5242 * This function adds macro names to error messages, and suppresses
5243 * them if necessary.
5244 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005245static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005246{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005247 /*
5248 * If we're in a dead branch of IF or something like it, ignore the error.
5249 * However, because %else etc are evaluated in the state context
5250 * of the previous branch, errors might get lost:
5251 * %if 0 ... %else trailing garbage ... %endif
5252 * So %else etc should set the ERR_PP_PRECOND flag.
5253 */
5254 if ((severity & ERR_MASK) < ERR_FATAL &&
5255 istk && istk->conds &&
5256 ((severity & ERR_PP_PRECOND) ?
5257 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005258 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005259 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005260
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005261 /* This doesn't make sense with the macro stack unwinding */
5262 if (0) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005263 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005264
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005265 /* get %macro name */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005266 if (!(severity & ERR_NOFILE) && istk && istk->mstk.mmac) {
5267 MMacro *mmac = istk->mstk.mmac;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005268 char *buf;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005269
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005270 nasm_set_verror(real_verror);
5271 buf = nasm_vasprintf(fmt, arg);
5272 nasm_error(severity, "(%s:%"PRId32") %s",
5273 mmac->name, mmac->lineno - delta, buf);
5274 nasm_set_verror(pp_verror);
5275 nasm_free(buf);
5276 return;
5277 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005278 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005279 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005280}
5281
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005282static Token *
5283stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005284{
5285 (void)s;
5286 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005287 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005288
5289 return make_tok_qstr(src_get_fname());
5290}
5291
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005292static Token *
5293stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005294{
5295 (void)s;
5296 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005297 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005298
5299 return make_tok_num(src_get_linnum());
5300}
5301
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005302static Token *
5303stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005304{
5305 (void)s;
5306 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005307 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005308
5309 return make_tok_num(globalbits);
5310}
5311
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005312static Token *
5313stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005314{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005315 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005316 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5317 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5318 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005319
5320 (void)s;
5321 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005322 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005323
5324 switch (globalbits) {
5325 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005326 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005327 break;
5328 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005329 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005330 break;
5331 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005332 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005333 break;
5334 default:
5335 panic();
5336 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005337
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005338 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005339}
5340
H. Peter Anvin8b262472019-02-26 14:00:54 -08005341/* Add magic standard macros */
5342struct magic_macros {
5343 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005344 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005345 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005346};
5347static const struct magic_macros magic_macros[] =
5348{
5349 { "__FILE__", 0, stdmac_file },
5350 { "__LINE__", 0, stdmac_line },
5351 { "__BITS__", 0, stdmac_bits },
5352 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005353 { NULL, 0, NULL }
5354};
5355
5356static void pp_add_magic_stdmac(void)
5357{
5358 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005359 SMacro tmpl;
5360
5361 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005362
5363 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005364 tmpl.nparam = m->nparam;
5365 tmpl.expand = m->func;
5366 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005367 }
5368}
5369
H. Peter Anvin734b1882002-04-30 21:01:08 +00005370static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005371pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005372{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005373 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005374 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005375
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005376 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005377 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005378 nested_mac_count = 0;
5379 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005380 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005381 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005382 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005383 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005384
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005385 if (!use_loaded)
5386 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5387 memset(use_loaded, 0, use_package_count * sizeof(bool));
5388
H. Peter Anvin6686de22019-08-10 05:33:14 -07005389 /* First set up the top level input file */
5390 nasm_new(istk);
5391 istk->fp = nasm_open_read(file, NF_TEXT);
5392 src_set(0, file);
5393 istk->lineinc = 1;
5394 if (!istk->fp)
5395 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5396
5397 strlist_add(deplist, file);
5398
5399 /*
5400 * Set up the stdmac packages as a virtual include file,
5401 * indicated by a null file pointer.
5402 */
5403 nasm_new(inc);
5404 inc->next = istk;
5405 inc->fname = src_set_fname(NULL);
5406 inc->nolist = !list_option('b');
5407 istk = inc;
5408 lfmt->uplevel(LIST_INCLUDE, 0);
5409
H. Peter Anvin8b262472019-02-26 14:00:54 -08005410 pp_add_magic_stdmac();
5411
H. Peter Anvinf7606612016-07-13 14:23:48 -07005412 if (tasm_compatible_mode)
5413 pp_add_stdmac(nasm_stdmac_tasm);
5414
5415 pp_add_stdmac(nasm_stdmac_nasm);
5416 pp_add_stdmac(nasm_stdmac_version);
5417
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005418 if (extrastdmac)
5419 pp_add_stdmac(extrastdmac);
5420
H. Peter Anvinf7606612016-07-13 14:23:48 -07005421 stdmacpos = stdmacros[0];
5422 stdmacnext = &stdmacros[1];
5423
H. Peter Anvind2456592008-06-19 15:04:18 -07005424 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005425
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005426 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005427 * Define the __PASS__ macro. This is defined here unlike all the
5428 * other builtins, because it is special -- it varies between
5429 * passes -- but there is really no particular reason to make it
5430 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005431 *
5432 * 0 = dependencies only
5433 * 1 = preparatory passes
5434 * 2 = final pass
5435 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005436 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005437 switch (mode) {
5438 case PP_NORMAL:
5439 apass = pass_final() ? 2 : 1;
5440 break;
5441 case PP_DEPS:
5442 apass = 0;
5443 break;
5444 case PP_PREPROC:
5445 apass = 3;
5446 break;
5447 default:
5448 panic();
5449 }
5450
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005451 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005452}
5453
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005454static void pp_init(void)
5455{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005456}
5457
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005458/*
5459 * Get a line of tokens. If we popped the macro expansion/include stack,
5460 * we return a pointer to the dummy token tok_pop; at that point if
5461 * istk is NULL then we have reached end of input;
5462 */
5463static Token tok_pop; /* Dummy token placeholder */
5464
5465static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005466{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005467 while (true) {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005468 Line *l = istk->expansion;
5469 Token *tline = NULL;
5470 Token *dtline;
5471
H. Peter Anvine2c80182005-01-15 22:15:51 +00005472 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005473 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005474 * buffer or from the input file.
5475 */
5476 tline = NULL;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005477 while (l && l->finishes) {
5478 MMacro *fm = l->finishes;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005479
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005480 if (!fm->name && fm->in_progress > 1) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005481 /*
5482 * This is a macro-end marker for a macro with no
5483 * name, which means it's not really a macro at all
5484 * but a %rep block, and the `in_progress' field is
5485 * more than 1, meaning that we still need to
5486 * repeat. (1 means the natural last repetition; 0
5487 * means termination by %exitrep.) We have
5488 * therefore expanded up to the %endrep, and must
5489 * push the whole block on to the expansion buffer
5490 * again. We don't bother to remove the macro-end
5491 * marker: we'd only have to generate another one
5492 * if we did.
5493 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005494 fm->in_progress--;
5495 list_for_each(l, fm->expansion) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005496 Token *t, *tt, **tail;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005497 Line *ll;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005498
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005499 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005500 ll->next = istk->expansion;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005501 tail = &ll->first;
5502
5503 list_for_each(t, l->first) {
5504 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005505 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005506 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005507 }
5508 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005509 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005510 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005511 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005512 } else {
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005513 MMacro *m = istk->mstk.mstk;
5514
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005515 /*
5516 * Check whether a `%rep' was started and not ended
5517 * within this macro expansion. This can happen and
5518 * should be detected. It's a fatal error because
5519 * I'm too confused to work out how to recover
5520 * sensibly from it.
5521 */
5522 if (defining) {
5523 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005524 nasm_panic("defining with name in expansion");
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005525 else if (m->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005526 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005527 " expansion of macro `%s'", m->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005528 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005529
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005530 /*
5531 * FIXME: investigate the relationship at this point between
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005532 * istk->mstk.mstk and fm
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005533 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005534 istk->mstk = m->mstk;
5535 if (m->name) {
5536 /*
5537 * This was a real macro call, not a %rep, and
5538 * therefore the parameter information needs to
5539 * be freed and the iteration count/nesting
5540 * depth adjusted.
5541 */
5542
5543 if (!--mmacro_deadman.levels) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005544 /*
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005545 * If all mmacro processing done,
5546 * clear all counters and the deadman
5547 * message trigger.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005548 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005549 nasm_zero(mmacro_deadman); /* Clear all counters */
Adam Majer91e72402017-07-25 10:42:01 +02005550 }
5551
Adam Majer91e72402017-07-25 10:42:01 +02005552#if 0
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005553 if (m->prev) {
5554 pop_mmacro(m);
5555 fm->in_progress --;
5556 } else
Adam Majer91e72402017-07-25 10:42:01 +02005557#endif
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005558 {
5559 nasm_free(m->params);
5560 free_tlist(m->iline);
5561 nasm_free(m->paramlen);
5562 fm->in_progress = 0;
5563 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005564 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005565
5566 /*
5567 * FIXME It is incorrect to always free_mmacro here.
5568 * It leads to usage-after-free.
5569 *
5570 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5571 */
5572#if 0
5573 else
5574 free_mmacro(m);
5575#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005576 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005577 istk->expansion = l->next;
5578 nasm_free(l);
5579 lfmt->downlevel(LIST_MACRO);
5580 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005581 }
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005582
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005583 do { /* until we get a line we can use */
5584 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005585
5586 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005587 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005588 int32_t lineno;
5589
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005590 if (istk->mstk.mstk) {
5591 istk->mstk.mstk->lineno++;
5592 if (istk->mstk.mstk->fname)
5593 lineno = istk->mstk.mstk->lineno +
5594 istk->mstk.mstk->xline;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005595 else
5596 lineno = 0; /* Defined at init time or builtin */
5597 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005598 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005599 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005600
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005601 tline = l->first;
5602 istk->expansion = l->next;
5603 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005604
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005605 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005606 if (!istk->nolist)
5607 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005608 nasm_free(line);
5609 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005610 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005611 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005612 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005613 } else {
5614 /*
5615 * The current file has ended; work down the istk
5616 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005617 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005618 if (i->fp)
5619 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005620 if (i->conds) {
5621 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005622 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005623 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005624 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005625 if (i->next)
5626 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005627 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005628 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005629 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005630 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005631 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005632 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005633
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005634 /*
5635 * We must expand MMacro parameters and MMacro-local labels
5636 * _before_ we plunge into directive processing, to cope
5637 * with things like `%define something %1' such as STRUC
5638 * uses. Unless we're _defining_ a MMacro, in which case
5639 * those tokens should be left alone to go into the
5640 * definition; and unless we're in a non-emitting
5641 * condition, in which case we don't want to meddle with
5642 * anything.
5643 */
5644 if (!defining && !(istk->conds && !emitting(istk->conds->state))
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005645 && !(istk->mstk.mstk && !istk->mstk.mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005646 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005647 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005648
H. Peter Anvine2c80182005-01-15 22:15:51 +00005649 /*
5650 * Check the line to see if it's a preprocessor directive.
5651 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005652 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5653 if (dtline)
5654 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005655 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005656 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005657 * We're defining a multi-line macro. We emit nothing
5658 * at all, and just
5659 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005660 */
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005661 MMacro *mmac = defining->dstk.mmac;
5662
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005663 Line *l = nasm_malloc(sizeof(Line));
5664 l->next = defining->expansion;
5665 l->first = tline;
5666 l->finishes = NULL;
5667 defining->expansion = l;
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005668
5669 /*
5670 * Remember if this mmacro expansion contains %00:
5671 * if it does, we will have to handle leading labels
5672 * specially.
5673 */
5674 if (mmac) {
5675 const Token *t;
5676 list_for_each(t, tline) {
5677 if (t->type == TOK_PREPROC_ID && !strcmp(t->text, "%00"))
5678 mmac->capture_label = true;
5679 }
5680 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005681 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005682 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005683 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005684 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005685 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005686 * directive so we keep our place correctly.
5687 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005688 free_tlist(tline);
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005689 } else if (istk->mstk.mstk && !istk->mstk.mstk->in_progress) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005690 /*
5691 * We're in a %rep block which has been terminated, so
5692 * we're walking through to the %endrep without
5693 * emitting anything. Emit nothing at all, not even a
5694 * blank line: when we emerge from the %rep block we'll
5695 * give a line-number directive so we keep our place
5696 * correctly.
5697 */
5698 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005699 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005700 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005701 if (!expand_mmacro(tline))
5702 return tline;
5703 }
5704 }
5705}
5706
5707static char *pp_getline(void)
5708{
5709 char *line = NULL;
5710 Token *tline;
5711
5712 real_verror = nasm_set_verror(pp_verror);
5713
5714 while (true) {
5715 tline = pp_tokline();
5716 if (tline == &tok_pop) {
5717 /*
5718 * We popped the macro/include stack. If istk is empty,
5719 * we are at end of input, otherwise just loop back.
5720 */
5721 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005722 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005723 } else {
5724 /*
5725 * De-tokenize the line and emit it.
5726 */
5727 line = detoken(tline, true);
5728 free_tlist(tline);
5729 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005730 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005731 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005732
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005733 if (list_option('e') && istk && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005734 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005735 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005736 nasm_free(buf);
5737 }
5738
H. Peter Anvin130736c2016-02-17 20:27:41 -08005739 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005740 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005741}
5742
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005743static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005744{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005745 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005746
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005747 if (defining) {
5748 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005749 nasm_nonfatal("end of file while still defining macro `%s'",
5750 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005751 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005752 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005753 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005754
5755 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005756 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005757 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005758
5759 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005760
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005761 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005762 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005763 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005764 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005765 Include *i = istk;
5766 istk = istk->next;
5767 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005768 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005769 }
5770 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005771 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005772 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005773}
5774
5775static void pp_cleanup_session(void)
5776{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005777 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005778 free_llist(predef);
5779 predef = NULL;
5780 delete_Blocks();
5781 freeTokens = NULL;
5782 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005783}
5784
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005785static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005786{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005787 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005788}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005789
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005790static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005791{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005792 Token *inc, *space, *name;
5793 Line *l;
5794
H. Peter Anvin734b1882002-04-30 21:01:08 +00005795 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5796 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5797 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005798
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005799 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005800 l->next = predef;
5801 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005802 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005803 predef = l;
5804}
5805
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005806static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005807{
5808 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005809 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005810 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005811
H. Peter Anvin130736c2016-02-17 20:27:41 -08005812 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005813
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005814 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005815 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5816 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005817 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005818 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005819 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005820 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005821 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005822
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005823 if (space->next->type != TOK_PREPROC_ID &&
5824 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005825 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005826
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005827 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005828 l->next = predef;
5829 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005830 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005831 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005832
5833 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005834}
5835
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005836static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005837{
5838 Token *def, *space;
5839 Line *l;
5840
H. Peter Anvin734b1882002-04-30 21:01:08 +00005841 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5842 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005843 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005844
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005845 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005846 l->next = predef;
5847 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005848 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005849 predef = l;
5850}
5851
H. Peter Anvin05990342018-06-11 13:32:42 -07005852/* Insert an early preprocessor command that doesn't need special handling */
5853static void pp_pre_command(const char *what, char *string)
5854{
5855 char *cmd;
5856 Token *def, *space;
5857 Line *l;
5858
5859 def = tokenize(string);
5860 if (what) {
5861 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5862 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5863 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5864 }
5865
5866 l = nasm_malloc(sizeof(Line));
5867 l->next = predef;
5868 l->first = def;
5869 l->finishes = NULL;
5870 predef = l;
5871}
5872
H. Peter Anvinf7606612016-07-13 14:23:48 -07005873static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005874{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005875 macros_t **mp;
5876
5877 /* Find the end of the list and avoid duplicates */
5878 for (mp = stdmacros; *mp; mp++) {
5879 if (*mp == macros)
5880 return; /* Nothing to do */
5881 }
5882
5883 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5884
5885 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005886}
5887
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005888static void pp_extra_stdmac(macros_t *macros)
5889{
5890 extrastdmac = macros;
5891}
5892
H. Peter Anvin8b262472019-02-26 14:00:54 -08005893static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005894{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005895 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005896 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5897 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5898}
5899
5900static Token *make_tok_qstr(const char *str)
5901{
5902 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005903 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005904 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005905}
5906
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005907static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005908{
5909 if (!m)
5910 return;
5911
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005912 /* We need to print the mstk.mmac list in reverse order */
5913 pp_list_one_macro(m->mstk.mmac, severity);
H. Peter Anvin37368952016-05-09 14:10:32 -07005914
5915 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005916 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005917 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005918 }
5919}
5920
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005921static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005922{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005923 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005924
H. Peter Anvinddb29062018-12-11 00:06:29 -08005925 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5926 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005927
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005928 if (istk)
H. Peter Anvin (Intel)de7acc32019-08-19 17:52:55 -07005929 pp_list_one_macro(istk->mstk.mmac, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005930
H. Peter Anvinddb29062018-12-11 00:06:29 -08005931 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005932}
5933
H. Peter Anvine7469712016-02-18 02:20:59 -08005934const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005935 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005936 pp_reset,
5937 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005938 pp_cleanup_pass,
5939 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005940 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005941 pp_pre_define,
5942 pp_pre_undefine,
5943 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005944 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005945 pp_include_path,
5946 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005947};