blob: 56dd4a396b3863d1dbc250da25f89c087b9b44ef [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 Anvind7ed89e2002-04-30 20:52:08 +0000117};
118
119/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800120 * Store the definition of a multi-line macro. This is also used to
121 * store the interiors of `%rep...%endrep' blocks, which are
122 * effectively self-re-invoking multi-line macros which simply
123 * don't have a name or bother to appear in the hash tables. %rep
124 * blocks are signified by having a NULL `name' field.
125 *
126 * In a MMacro describing a `%rep' block, the `in_progress' field
127 * isn't merely boolean, but gives the number of repeats left to
128 * run.
129 *
130 * The `next' field is used for storing MMacros in hash tables; the
131 * `next_active' field is for stacking them on istk entries.
132 *
133 * When a MMacro is being expanded, `params', `iline', `nparam',
134 * `paramlen', `rotate' and `unique' are local to the invocation.
135 */
136struct MMacro {
137 MMacro *next;
138 MMacroInvocation *prev; /* previous invocation */
139 char *name;
140 int nparam_min, nparam_max;
141 bool casesense;
142 bool plus; /* is the last parameter greedy? */
143 bool nolist; /* is this macro listing-inhibited? */
144 int64_t in_progress; /* is this macro currently being expanded? */
145 int32_t max_depth; /* maximum number of recursive expansions allowed */
146 Token *dlist; /* All defaults as one list */
147 Token **defaults; /* Parameter default pointers */
148 int ndefs; /* number of default parameters */
149 Line *expansion;
150
151 MMacro *next_active;
152 MMacro *rep_nest; /* used for nesting %rep */
153 Token **params; /* actual parameters */
154 Token *iline; /* invocation line */
155 unsigned int nparam, rotate;
156 int *paramlen;
157 uint64_t unique;
158 int lineno; /* Current line number on expansion */
159 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700160
H. Peter Anvin274cda82016-05-10 02:56:29 -0700161 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700162 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800163};
164
165
166/* Store the definition of a multi-line macro, as defined in a
167 * previous recursive macro expansion.
168 */
169struct MMacroInvocation {
170 MMacroInvocation *prev; /* previous invocation */
171 Token **params; /* actual parameters */
172 Token *iline; /* invocation line */
173 unsigned int nparam, rotate;
174 int *paramlen;
175 uint64_t unique;
176 uint64_t condcnt;
177};
178
179
180/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000181 * The context stack is composed of a linked list of these.
182 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000183struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800184 Context *next;
185 char *name;
186 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700187 uint64_t number;
188 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000189};
190
191/*
192 * This is the internal form which we break input lines up into.
193 * Typically stored in linked lists.
194 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800195 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
196 * not necessarily used as-is, but is also used to encode the number
197 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000198 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800199 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700200 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000201 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800202 * tok_smac_param(0) but the one representing `y' will be
203 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000204 *
205 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
206 * which doesn't need quotes around it. Used in the pre-include
207 * mechanism as an alternative to trying to find a sensible type of
208 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000209 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000210enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
212 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800213 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700214 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800215 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300216 TOK_PASTE, /* %+ */
217 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800218 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300219 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000220};
221
H. Peter Anvin8b262472019-02-26 14:00:54 -0800222static inline enum pp_token_type tok_smac_param(int param)
223{
224 return TOK_SMAC_START_PARAMS + param;
225}
226static int smac_nparam(enum pp_token_type toktype)
227{
228 return toktype - TOK_SMAC_START_PARAMS;
229}
230static bool is_smac_param(enum pp_token_type toktype)
231{
232 return toktype >= TOK_SMAC_START_PARAMS;
233}
234
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400235#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400236#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400237
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400238struct tokseq_match {
239 int mask_head;
240 int mask_tail;
241};
242
H. Peter Anvine2c80182005-01-15 22:15:51 +0000243struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800244 Token *next;
245 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700246 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800247 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248};
249
250/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800251 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000252 * these, which is essentially a container to allow several linked
253 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700254 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000255 * Note that in this module, linked lists are treated as stacks
256 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800257 * `expansion' field in MMacro structures, so that the linked list,
258 * if walked, would give the macro lines in reverse order; this
259 * means that we can walk the list when expanding a macro, and thus
260 * push the lines on to the `expansion' field in _istk_ in reverse
261 * order (so that when popped back off they are in the right
262 * order). It may seem cockeyed, and it relies on my design having
263 * an even number of steps in, but it works...
264 *
265 * Some of these structures, rather than being actual lines, are
266 * markers delimiting the end of the expansion of a given macro.
267 * This is for use in the cycle-tracking and %rep-handling code.
268 * Such structures have `finishes' non-NULL, and `first' NULL. All
269 * others have `finishes' NULL, but `first' may still be NULL if
270 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800273 Line *next;
274 MMacro *finishes;
275 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500276};
277
278/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000279 * To handle an arbitrary level of file inclusion, we maintain a
280 * stack (ie linked list) of these things.
281 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000282struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800283 Include *next;
284 FILE *fp;
285 Cond *conds;
286 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700287 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800288 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvin6686de22019-08-10 05:33:14 -0700289 int lineno, lineinc;
290 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000291};
292
293/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700294 * File real name hash, so we don't have to re-search the include
295 * path for every pass (and potentially more than that if a file
296 * is used more than once.)
297 */
298struct hash_table FileHash;
299
300/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000301 * Conditional assembly: we maintain a separate stack of these for
302 * each level of file inclusion. (The only reason we keep the
303 * stacks separate is to ensure that a stray `%endif' in a file
304 * included from within the true branch of a `%if' won't terminate
305 * it and cause confusion: instead, rightly, it'll cause an error.)
306 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800307struct Cond {
308 Cond *next;
309 int state;
310};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000311enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000312 /*
313 * These states are for use just after %if or %elif: IF_TRUE
314 * means the condition has evaluated to truth so we are
315 * currently emitting, whereas IF_FALSE means we are not
316 * currently emitting but will start doing so if a %else comes
317 * up. In these states, all directives are admissible: %elif,
318 * %else and %endif. (And of course %if.)
319 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800320 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321 /*
322 * These states come up after a %else: ELSE_TRUE means we're
323 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
324 * any %elif or %else will cause an error.
325 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800326 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000327 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200328 * These states mean that we're not emitting now, and also that
329 * nothing until %endif will be emitted at all. COND_DONE is
330 * used when we've had our moment of emission
331 * and have now started seeing %elifs. COND_NEVER is used when
332 * the condition construct in question is contained within a
333 * non-emitting branch of a larger condition construct,
334 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800336 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800338#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000339
H. Peter Anvin70653092007-10-19 14:42:29 -0700340/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000341 * These defines are used as the possible return values for do_directive
342 */
343#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300344#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000345
Keith Kanios852f1ee2009-07-12 00:19:55 -0500346/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000347 * Condition codes. Note that we use c_ prefix not C_ because C_ is
348 * used in nasm.h for the "real" condition codes. At _this_ level,
349 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
350 * ones, so we need a different enum...
351 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700352static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000353 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
354 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000355 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000356};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700357enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
359 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 -0700360 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
361 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000362};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700363static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
365 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 +0000366 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000367};
368
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800369/*
370 * Directive names.
371 */
372/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
373static int is_condition(enum preproc_token arg)
374{
375 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
376}
377
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000378/* For TASM compatibility we need to be able to recognise TASM compatible
379 * conditional compilation directives. Using the NASM pre-processor does
380 * not work, so we look for them specifically from the following list and
381 * then jam in the equivalent NASM directive into the input stream.
382 */
383
H. Peter Anvine2c80182005-01-15 22:15:51 +0000384enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000385 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
386 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
387};
388
H. Peter Anvin476d2862007-10-02 22:04:15 -0700389static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000390 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
391 "ifndef", "include", "local"
392};
393
394static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700395static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000396static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800397static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000398
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000399static Context *cstk;
400static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300401static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000402
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300403static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000404
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300405static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800407static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700408static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800409static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000410
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000411/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800412 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000413 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800414static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415
416/*
417 * The current set of single-line macros we have defined.
418 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700419static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000420
421/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800422 * The multi-line macro we are currently defining, or the %rep
423 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800425static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426
Charles Crayned4200be2008-07-12 16:42:33 -0700427static uint64_t nested_mac_count;
428static uint64_t nested_rep_count;
429
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000430/*
431 * The number of macro parameters to allocate space for at a time.
432 */
433#define PARAM_DELTA 16
434
435/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700436 * The standard macro set: defined in macros.c in a set of arrays.
437 * This gives our position in any macro set, while we are processing it.
438 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000439 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700440static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700441static macros_t **stdmacnext;
442static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300443static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000444
445/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000446 * Tokens are allocated in blocks to improve speed
447 */
448#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800449static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000450struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 Blocks *next;
452 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000453};
454
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800455static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000456
457/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000458 * Forward declarations.
459 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700460static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000461static Token *expand_mmac_params(Token * tline);
462static Token *expand_smacro(Token * tline);
463static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400464static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800465static Token *make_tok_num(int64_t val);
466static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800467static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800468static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000469static void *new_Block(size_t size);
470static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700471static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700472 const char *text, size_t txtlen);
473static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000474static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000475
476/*
477 * Macros for safe checking of token pointers, avoid *(NULL)
478 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300479#define tok_type_(x,t) ((x) && (x)->type == (t))
480#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
481#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
482#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000483
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400484/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700485 * In-place reverse a list of tokens.
486 */
487static Token *reverse_tokens(Token *t)
488{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800489 Token *prev = NULL;
490 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700491
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800492 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400493 next = t->next;
494 t->next = prev;
495 prev = t;
496 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800497 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700498
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800499 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700500}
501
502/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300503 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000504 * front of them. We do it here because I could not find any other
505 * place to do it for the moment, and it is a hack (ideally it would
506 * be nice to be able to use the NASM pre-processor to do it).
507 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000508static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000509{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000510 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400511 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000512
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400513 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000514
515 /* Binary search for the directive name */
516 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400517 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400518 q = nasm_skip_word(p);
519 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 if (len) {
521 oldchar = p[len];
522 p[len] = 0;
523 while (j - i > 1) {
524 k = (j + i) / 2;
525 m = nasm_stricmp(p, tasm_directives[k]);
526 if (m == 0) {
527 /* We have found a directive, so jam a % in front of it
528 * so that NASM will then recognise it as one if it's own.
529 */
530 p[len] = oldchar;
531 len = strlen(p);
532 oldline = line;
533 line = nasm_malloc(len + 2);
534 line[0] = '%';
535 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700536 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300537 * NASM does not recognise IFDIFI, so we convert
538 * it to %if 0. This is not used in NASM
539 * compatible code, but does need to parse for the
540 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700542 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 } else {
544 memcpy(line + 1, p, len + 1);
545 }
546 nasm_free(oldline);
547 return line;
548 } else if (m < 0) {
549 j = k;
550 } else
551 i = k;
552 }
553 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000554 }
555 return line;
556}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000557
H. Peter Anvin76690a12002-04-30 20:52:49 +0000558/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000559 * The pre-preprocessing stage... This function translates line
560 * number indications as they emerge from GNU cpp (`# lineno "file"
561 * flags') into NASM preprocessor line number indications (`%line
562 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000563 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000564static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000565{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000566 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000567 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000568
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 if (line[0] == '#' && line[1] == ' ') {
570 oldline = line;
571 fname = oldline + 2;
572 lineno = atoi(fname);
573 fname += strspn(fname, "0123456789 ");
574 if (*fname == '"')
575 fname++;
576 fnlen = strcspn(fname, "\"");
577 line = nasm_malloc(20 + fnlen);
578 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
579 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000580 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000581 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000582 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000583 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584}
585
586/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587 * Free a linked list of tokens.
588 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000590{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400591 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000593}
594
595/*
596 * Free a linked list of lines.
597 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000599{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400600 Line *l, *tmp;
601 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 free_tlist(l->first);
603 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000604 }
605}
606
607/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700608 * Free an array of linked lists of tokens
609 */
610static void free_tlist_array(Token **array, size_t nlists)
611{
612 Token **listp = array;
613
614 while (nlists--)
615 free_tlist(*listp++);
616
617 nasm_free(array);
618}
619
620/*
621 * Duplicate a linked list of tokens.
622 */
623static Token *dup_tlist(const Token *list, Token ***tailp)
624{
625 Token *newlist = NULL;
626 Token **tailpp = &newlist;
627 const Token *t;
628
629 list_for_each(t, list) {
630 Token *nt;
631 *tailpp = nt = dup_Token(NULL, t);
632 tailpp = &nt->next;
633 }
634
635 if (tailp)
636 *tailp = tailpp;
637
638 return newlist;
639}
640
641/*
642 * Duplicate a linked list of tokens in reverse order
643 */
644static Token *dup_tlist_reverse(const Token *list, Token *tail)
645{
646 const Token *t;
647
648 list_for_each(t, list)
649 tail = dup_Token(tail, t);
650
651 return tail;
652}
653
654/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800655 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000656 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800657static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000658{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659 nasm_free(m->name);
660 free_tlist(m->dlist);
661 nasm_free(m->defaults);
662 free_llist(m->expansion);
663 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000664}
665
666/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700667 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800668 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700669static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800670{
671 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700672 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800673 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700674}
675
676static void clear_smacro(SMacro *s)
677{
678 free_smacro_members(s);
679 /* Wipe everything except the next pointer */
680 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
681}
682
683/*
684 * Free an SMacro
685 */
686static void free_smacro(SMacro *s)
687{
688 free_smacro_members(s);
689 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800690}
691
692/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700693 * Free all currently defined macros, and free the hash tables
694 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700695static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700696{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800697 struct hash_iterator it;
698 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700699
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800700 hash_for_each(smt, it, np) {
701 SMacro *tmp;
702 SMacro *s = np->data;
703 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800704 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700705 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700706 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700707 hash_free(smt);
708}
709
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800710static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700711{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800712 struct hash_iterator it;
713 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700714
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800715 hash_for_each(mmt, it, np) {
716 MMacro *tmp;
717 MMacro *m = np->data;
718 nasm_free((void *)np->key);
719 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800720 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700721 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800722 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700723}
724
725static void free_macros(void)
726{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700727 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800728 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700729}
730
731/*
732 * Initialize the hash tables
733 */
734static void init_macros(void)
735{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700736}
737
738/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000739 * Pop the context stack.
740 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000742{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000743 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000744
745 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700746 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000747 nasm_free(c->name);
748 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000749}
750
H. Peter Anvin072771e2008-05-22 13:17:51 -0700751/*
752 * Search for a key in the hash index; adding it if necessary
753 * (in which case we initialize the data pointer to NULL.)
754 */
755static void **
756hash_findi_add(struct hash_table *hash, const char *str)
757{
758 struct hash_insert hi;
759 void **r;
760 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800761 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700762
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800763 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700764 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300765 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700766
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800767 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
768 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700769 return hash_add(&hi, strx, NULL);
770}
771
772/*
773 * Like hash_findi, but returns the data element rather than a pointer
774 * to it. Used only when not adding a new element, hence no third
775 * argument.
776 */
777static void *
778hash_findix(struct hash_table *hash, const char *str)
779{
780 void **p;
781
782 p = hash_findi(hash, str, NULL);
783 return p ? *p : NULL;
784}
785
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400786/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800787 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400788 * if there no more left -- return NULL
789 */
790static char *line_from_stdmac(void)
791{
792 unsigned char c;
793 const unsigned char *p = stdmacpos;
794 char *line, *q;
795 size_t len = 0;
796
797 if (!stdmacpos)
798 return NULL;
799
800 while ((c = *p++)) {
801 if (c >= 0x80)
802 len += pp_directives_len[c - 0x80] + 1;
803 else
804 len++;
805 }
806
807 line = nasm_malloc(len + 1);
808 q = line;
809 while ((c = *stdmacpos++)) {
810 if (c >= 0x80) {
811 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
812 q += pp_directives_len[c - 0x80];
813 *q++ = ' ';
814 } else {
815 *q++ = c;
816 }
817 }
818 stdmacpos = p;
819 *q = '\0';
820
821 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700822 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400823 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700824 if (*stdmacnext) {
825 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400826 } else if (do_predef) {
827 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400828
829 /*
830 * Nasty hack: here we push the contents of
831 * `predef' on to the top-level expansion stack,
832 * since this is the most convenient way to
833 * implement the pre-include and pre-define
834 * features.
835 */
836 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700837 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800838 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700839 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800840 l->finishes = NULL;
841
842 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400843 }
844 do_predef = false;
845 }
846 }
847
848 return line;
849}
850
H. Peter Anvin6686de22019-08-10 05:33:14 -0700851/*
852 * Read a line from a file. Return NULL on end of file.
853 */
854static char *line_from_file(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000855{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700856 int c;
857 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400858 const unsigned int delta = 512;
859 const unsigned int pad = 8;
860 unsigned int nr_cont = 0;
861 bool cont = false;
862 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700863 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000864
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400865 size = delta;
866 p = buffer = nasm_malloc(size);
867
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700868 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400869 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400870
871 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700872 case EOF:
873 if (p == buffer) {
874 nasm_free(buffer);
875 return NULL;
876 }
877 c = 0;
878 break;
879
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400880 case '\r':
881 next = fgetc(istk->fp);
882 if (next != '\n')
883 ungetc(next, istk->fp);
884 if (cont) {
885 cont = false;
886 continue;
887 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700888 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400889 break;
890
891 case '\n':
892 if (cont) {
893 cont = false;
894 continue;
895 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700896 c = 0;
897 break;
898
899 case 032: /* ^Z = legacy MS-DOS end of file mark */
900 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400901 break;
902
903 case '\\':
904 next = fgetc(istk->fp);
905 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400906 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400907 cont = true;
908 nr_cont++;
909 continue;
910 }
911 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000912 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400913
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400914 if (p >= (buffer + size - pad)) {
915 buffer = nasm_realloc(buffer, size + delta);
916 p = buffer + size - pad;
917 size += delta;
918 }
919
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700920 *p++ = c;
921 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000922
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700923 lineno = src_get_linnum() + istk->lineinc +
924 (nr_cont * istk->lineinc);
925 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000926
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000927 return buffer;
928}
929
930/*
H. Peter Anvin6686de22019-08-10 05:33:14 -0700931 * Common read routine regardless of source
932 */
933static char *read_line(void)
934{
935 char *line;
936
937 if (istk->fp)
938 line = line_from_file();
939 else
940 line = line_from_stdmac();
941
942 if (!line)
943 return NULL;
944
945 if (!istk->nolist)
946 lfmt->line(LIST_READ, src_get_linnum(), line);
947
948 return line;
949}
950
951/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000952 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000953 * don't need to parse the value out of e.g. numeric tokens: we
954 * simply split one string into many.
955 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000956static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000957{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700958 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000959 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000960 Token *list = NULL;
961 Token *t, **tail = &list;
962
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700964 char *p = line;
965 char *ep = NULL; /* End of token, for trimming the end */
966
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 if (*p == '%') {
968 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300969 if (*p == '+' && !nasm_isdigit(p[1])) {
970 p++;
971 type = TOK_PASTE;
972 } else if (nasm_isdigit(*p) ||
973 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 do {
975 p++;
976 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700977 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 type = TOK_PREPROC_ID;
979 } else if (*p == '{') {
980 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800981 while (*p) {
982 if (*p == '}')
983 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 p[-1] = *p;
985 p++;
986 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800987 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800988 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700989 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 if (*p)
991 p++;
992 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300993 } else if (*p == '[') {
994 int lvl = 1;
995 line += 2; /* Skip the leading %[ */
996 p++;
997 while (lvl && (c = *p++)) {
998 switch (c) {
999 case ']':
1000 lvl--;
1001 break;
1002 case '%':
1003 if (*p == '[')
1004 lvl++;
1005 break;
1006 case '\'':
1007 case '\"':
1008 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001009 p = nasm_skip_string(p - 1);
1010 if (*p)
1011 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001012 break;
1013 default:
1014 break;
1015 }
1016 }
1017 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001018 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001019 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001020 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001021 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001022 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001023 type = TOK_INDIRECT;
1024 } else if (*p == '?') {
1025 type = TOK_PREPROC_Q; /* %? */
1026 p++;
1027 if (*p == '?') {
1028 type = TOK_PREPROC_QQ; /* %?? */
1029 p++;
1030 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001031 } else if (*p == '!') {
1032 type = TOK_PREPROC_ID;
1033 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001034 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001035 do {
1036 p++;
1037 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001038 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001039 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001040 p = nasm_skip_string(p);
1041 if (*p)
1042 p++;
1043 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001044 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001045 } else {
1046 /* %! without string or identifier */
1047 type = TOK_OTHER; /* Legacy behavior... */
1048 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001049 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001051 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 do {
1053 p++;
1054 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001055 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 type = TOK_PREPROC_ID;
1057 } else {
1058 type = TOK_OTHER;
1059 if (*p == '%')
1060 p++;
1061 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001062 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 type = TOK_ID;
1064 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001065 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001067 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 /*
1069 * A string token.
1070 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001072 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 if (*p) {
1075 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001076 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001077 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 /* Handling unterminated strings by UNV */
1079 /* type = -1; */
1080 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001081 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001082 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001083 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001084 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001085 bool is_hex = false;
1086 bool is_float = false;
1087 bool has_e = false;
1088 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001091 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001094 if (*p == '$') {
1095 p++;
1096 is_hex = true;
1097 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001098
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001099 for (;;) {
1100 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001101
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001102 if (!is_hex && (c == 'e' || c == 'E')) {
1103 has_e = true;
1104 if (*p == '+' || *p == '-') {
1105 /*
1106 * e can only be followed by +/- if it is either a
1107 * prefixed hex number or a floating-point number
1108 */
1109 p++;
1110 is_float = true;
1111 }
1112 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1113 is_hex = true;
1114 } else if (c == 'P' || c == 'p') {
1115 is_float = true;
1116 if (*p == '+' || *p == '-')
1117 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001118 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001119 ; /* just advance */
1120 else if (c == '.') {
1121 /*
1122 * we need to deal with consequences of the legacy
1123 * parser, like "1.nolist" being two tokens
1124 * (TOK_NUMBER, TOK_ID) here; at least give it
1125 * a shot for now. In the future, we probably need
1126 * a flex-based scanner with proper pattern matching
1127 * to do it as well as it can be done. Nothing in
1128 * the world is going to help the person who wants
1129 * 0x123.p16 interpreted as two tokens, though.
1130 */
1131 r = p;
1132 while (*r == '_')
1133 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001134
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001135 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1136 (!is_hex && (*r == 'e' || *r == 'E')) ||
1137 (*r == 'p' || *r == 'P')) {
1138 p = r;
1139 is_float = true;
1140 } else
1141 break; /* Terminate the token */
1142 } else
1143 break;
1144 }
1145 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001146
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001147 if (p == line+1 && *line == '$') {
1148 type = TOK_OTHER; /* TOKEN_HERE */
1149 } else {
1150 if (has_e && !is_hex) {
1151 /* 1e13 is floating-point, but 1e13h is not */
1152 is_float = true;
1153 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001154
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001155 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1156 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001157 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001159 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 /*
1161 * Whitespace just before end-of-line is discarded by
1162 * pretending it's a comment; whitespace just before a
1163 * comment gets lumped into the comment.
1164 */
1165 if (!*p || *p == ';') {
1166 type = TOK_COMMENT;
1167 while (*p)
1168 p++;
1169 }
1170 } else if (*p == ';') {
1171 type = TOK_COMMENT;
1172 while (*p)
1173 p++;
1174 } else {
1175 /*
1176 * Anything else is an operator of some kind. We check
1177 * for all the double-character operators (>>, <<, //,
1178 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001179 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 */
1181 type = TOK_OTHER;
1182 if ((p[0] == '>' && p[1] == '>') ||
1183 (p[0] == '<' && p[1] == '<') ||
1184 (p[0] == '/' && p[1] == '/') ||
1185 (p[0] == '<' && p[1] == '=') ||
1186 (p[0] == '>' && p[1] == '=') ||
1187 (p[0] == '=' && p[1] == '=') ||
1188 (p[0] == '!' && p[1] == '=') ||
1189 (p[0] == '<' && p[1] == '>') ||
1190 (p[0] == '&' && p[1] == '&') ||
1191 (p[0] == '|' && p[1] == '|') ||
1192 (p[0] == '^' && p[1] == '^')) {
1193 p++;
1194 }
1195 p++;
1196 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001197
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 /* Handling unterminated string by UNV */
1199 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001200 {
1201 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1202 t->text[p-line] = *line;
1203 tail = &t->next;
1204 }
1205 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001207 if (!ep)
1208 ep = p;
1209 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 tail = &t->next;
1211 }
1212 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001213 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001214 return list;
1215}
1216
H. Peter Anvince616072002-04-30 21:02:23 +00001217/*
1218 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001219 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001220 * deleted only all at once by the delete_Blocks function.
1221 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001223{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001224 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001226 /* first, get to the end of the linked list */
1227 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001229 /* now allocate the requested chunk */
1230 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001232 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001233 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001234 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001235}
1236
1237/*
1238 * this function deletes all managed blocks of memory
1239 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001241{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001243
H. Peter Anvin70653092007-10-19 14:42:29 -07001244 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001245 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001246 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001247 * free it.
1248 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001250 if (b->chunk)
1251 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 a = b;
1253 b = b->next;
1254 if (a != &blocks)
1255 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001256 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001257 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001259
1260/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001261 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001262 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001263 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001264static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001265 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001266{
1267 Token *t;
1268 int i;
1269
H. Peter Anvin89cee572009-07-15 09:16:54 -04001270 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1272 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1273 freeTokens[i].next = &freeTokens[i + 1];
1274 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001275 }
1276 t = freeTokens;
1277 freeTokens = t->next;
1278 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001279 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001280 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001281 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 t->text = NULL;
1283 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001284 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001286 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001287 t->text = nasm_malloc(txtlen+1);
1288 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001290 }
1291 return t;
1292}
1293
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001294static Token *dup_Token(Token *next, const Token *src)
1295{
1296 return new_Token(next, src->type, src->text, src->len);
1297}
1298
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001300{
1301 Token *next = t->next;
1302 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001303 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001304 freeTokens = t;
1305 return next;
1306}
1307
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308/*
1309 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001310 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1311 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001312 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001313static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001314{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001315 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001316 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001317 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001318
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001319 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001320 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001321 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001322 char *v;
1323 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001324
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001325 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001326 if (nasm_isquote(*v))
1327 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001328
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001329 if (v) {
1330 char *p = getenv(v);
1331 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001332 /*!
1333 *!environment [on] nonexistent environment variable
1334 *! warns if a nonexistent environment variable
1335 *! is accessed using the \c{%!} preprocessor
1336 *! construct (see \k{getenv}.) Such environment
1337 *! variables are treated as empty (with this
1338 *! warning issued) starting in NASM 2.15;
1339 *! earlier versions of NASM would treat this as
1340 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001341 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001342 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1343 p = "";
1344 }
1345 t->text = nasm_strdup(p);
1346 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001347 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001348 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001350
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 /* Expand local macros here and not during preprocessing */
1352 if (expand_locals &&
1353 t->type == TOK_PREPROC_ID && t->text &&
1354 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 const char *q;
1356 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001357 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001359 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1360 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 nasm_free(t->text);
1362 t->text = p;
1363 }
1364 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001365 if (t->text) {
1366 if (debug_level(2)) {
1367 unsigned long t_len = t->len;
1368 unsigned long s_len = strlen(t->text);
1369 if (t_len != s_len) {
1370 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1371 t->text, t->type, s_len, t_len);
1372 t->len = s_len;
1373 }
1374 }
1375 len += t->len;
1376 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001378 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001379 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001380
H. Peter Anvin734b1882002-04-30 21:01:08 +00001381 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001382
1383 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001384 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001385 memcpy(p, t->text, t->len);
1386 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001387 } else if (t->type == TOK_WHITESPACE) {
1388 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001390 }
1391 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001392
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001393 return line;
1394}
1395
1396/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001397 * A scanner, suitable for use by the expression evaluator, which
1398 * operates on a line of Tokens. Expects a pointer to a pointer to
1399 * the first token in the line to be passed in as its private_data
1400 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001401 *
1402 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001403 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001404struct ppscan {
1405 Token *tptr;
1406 int ntokens;
1407};
1408
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001410{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001411 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001412 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001413 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001414
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001416 if (pps->ntokens && (tline = pps->tptr)) {
1417 pps->ntokens--;
1418 pps->tptr = tline->next;
1419 } else {
1420 pps->tptr = NULL;
1421 pps->ntokens = 0;
1422 return tokval->t_type = TOKEN_EOS;
1423 }
1424 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001425
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001426 tokval->t_charptr = tline->text;
1427
H. Peter Anvin76690a12002-04-30 20:52:49 +00001428 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001429 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001430 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001431 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001432
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001434 p = tokval->t_charptr = tline->text;
1435 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 tokval->t_charptr++;
1437 return tokval->t_type = TOKEN_ID;
1438 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001439
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001440 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001441 if (r >= p+MAX_KEYWORD)
1442 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001443 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001444 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001445 *s = '\0';
1446 /* right, so we have an identifier sitting in temp storage. now,
1447 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001448 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001449 }
1450
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001452 bool rn_error;
1453 tokval->t_integer = readnum(tline->text, &rn_error);
1454 tokval->t_charptr = tline->text;
1455 if (rn_error)
1456 return tokval->t_type = TOKEN_ERRNUM;
1457 else
1458 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001459 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001460
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001461 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001462 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001463 }
1464
H. Peter Anvine2c80182005-01-15 22:15:51 +00001465 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001466 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001467
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001468 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001469 tokval->t_charptr = tline->text;
1470 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001471
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001472 if (ep[0] != bq || ep[1] != '\0')
1473 return tokval->t_type = TOKEN_ERRSTR;
1474 else
1475 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001476 }
1477
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 if (tline->type == TOK_OTHER) {
1479 if (!strcmp(tline->text, "<<"))
1480 return tokval->t_type = TOKEN_SHL;
1481 if (!strcmp(tline->text, ">>"))
1482 return tokval->t_type = TOKEN_SHR;
1483 if (!strcmp(tline->text, "//"))
1484 return tokval->t_type = TOKEN_SDIV;
1485 if (!strcmp(tline->text, "%%"))
1486 return tokval->t_type = TOKEN_SMOD;
1487 if (!strcmp(tline->text, "=="))
1488 return tokval->t_type = TOKEN_EQ;
1489 if (!strcmp(tline->text, "<>"))
1490 return tokval->t_type = TOKEN_NE;
1491 if (!strcmp(tline->text, "!="))
1492 return tokval->t_type = TOKEN_NE;
1493 if (!strcmp(tline->text, "<="))
1494 return tokval->t_type = TOKEN_LE;
1495 if (!strcmp(tline->text, ">="))
1496 return tokval->t_type = TOKEN_GE;
1497 if (!strcmp(tline->text, "&&"))
1498 return tokval->t_type = TOKEN_DBL_AND;
1499 if (!strcmp(tline->text, "^^"))
1500 return tokval->t_type = TOKEN_DBL_XOR;
1501 if (!strcmp(tline->text, "||"))
1502 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001503 }
1504
1505 /*
1506 * We have no other options: just return the first character of
1507 * the token text.
1508 */
1509 return tokval->t_type = tline->text[0];
1510}
1511
1512/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001513 * Compare a string to the name of an existing macro; this is a
1514 * simple wrapper which calls either strcmp or nasm_stricmp
1515 * depending on the value of the `casesense' parameter.
1516 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001517static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001518{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001519 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001520}
1521
1522/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001523 * Compare a string to the name of an existing macro; this is a
1524 * simple wrapper which calls either strcmp or nasm_stricmp
1525 * depending on the value of the `casesense' parameter.
1526 */
1527static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1528{
1529 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1530}
1531
1532/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001533 * Return the Context structure associated with a %$ token. Return
1534 * NULL, having _already_ reported an error condition, if the
1535 * context stack isn't deep enough for the supplied number of $
1536 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001537 *
1538 * If "namep" is non-NULL, set it to the pointer to the macro name
1539 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001540 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001541static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001542{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001543 Context *ctx;
1544 int i;
1545
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001546 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001547 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001548
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001549 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001551
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001553 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001555 }
1556
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001557 name += 2;
1558 ctx = cstk;
1559 i = 0;
1560 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001561 name++;
1562 i++;
1563 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001564 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001566 nasm_nonfatal("`%s': context stack is only"
1567 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001569 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001570
1571 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001572 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001573
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001574 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001575}
1576
1577/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001578 * Open an include file. This routine must always return a valid
1579 * file pointer if it returns - it's responsible for throwing an
1580 * ERR_FATAL and bombing out completely if not. It should also try
1581 * the include path one by one until it finds the file or reaches
1582 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001583 *
1584 * Note: for INC_PROBE the function returns NULL at all times;
1585 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001586 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001587enum incopen_mode {
1588 INC_NEEDED, /* File must exist */
1589 INC_OPTIONAL, /* Missing is OK */
1590 INC_PROBE /* Only an existence probe */
1591};
1592
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001593/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001594static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001595 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001596{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001597 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001598 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001599 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001600 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001601 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001602
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001604 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001605 if (omode == INC_PROBE) {
1606 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001607 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001608 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001609 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001610 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001611 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001612 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001613 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001615 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001616
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001617 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001618
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001619 if (!ip) {
1620 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001621 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001622 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001623
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001624 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001625 ip = ip->next;
1626 }
1627}
1628
1629/*
1630 * Open a file, or test for the presence of one (depending on omode),
1631 * considering the include path.
1632 */
1633static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001634 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001635 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001636 enum incopen_mode omode,
1637 enum file_flags fmode)
1638{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001639 struct hash_insert hi;
1640 void **hp;
1641 char *path;
1642 FILE *fp = NULL;
1643
1644 hp = hash_find(&FileHash, file, &hi);
1645 if (hp) {
1646 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001647 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001648 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001649 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001650 } else {
1651 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001652 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001653
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001654 /* Positive or negative result */
1655 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001656
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001657 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001658 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001659 */
1660 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001661 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001662 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001663
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001664 if (!path) {
1665 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001666 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001667 } else {
1668 if (!fp && omode != INC_PROBE)
1669 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001670 }
1671
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001672 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001673 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001674
1675 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001676}
1677
1678/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001679 * Opens an include or input file. Public version, for use by modules
1680 * that get a file:lineno pair and need to look at the file again
1681 * (e.g. the CodeView debug backend). Returns NULL on failure.
1682 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001683FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001684{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001685 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001686}
1687
1688/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001689 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001690 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001691 * return true if _any_ single-line macro of that name is defined.
1692 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001693 * `nparam' or no parameters is defined.
1694 *
1695 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001696 * defined, or nparam is -1, the address of the definition structure
1697 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001698 * is NULL, no action will be taken regarding its contents, and no
1699 * error will occur.
1700 *
1701 * Note that this is also called with nparam zero to resolve
1702 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001703 *
1704 * If you already know which context macro belongs to, you can pass
1705 * the context pointer as first parameter; if you won't but name begins
1706 * with %$ the context will be automatically computed. If all_contexts
1707 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001708 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001709static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001710smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001711 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001712{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001713 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001714 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001715
H. Peter Anvin97a23472007-09-16 17:57:25 -07001716 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001717 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001718 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001719 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001720 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001722 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001723 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001724 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001725 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001726 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001727 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001728
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 while (m) {
1730 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001731 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001733 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 *defn = m;
1735 else
1736 *defn = NULL;
1737 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001738 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001739 }
1740 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001741 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001742
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001743 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001744}
1745
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001746/* param should be a natural number [0; INT_MAX] */
1747static int read_param_count(const char *str)
1748{
1749 int result;
1750 bool err;
1751
1752 result = readnum(str, &err);
1753 if (result < 0 || result > INT_MAX) {
1754 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001755 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1756 str, 0, INT_MAX);
1757 } else if (err)
1758 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001759 return result;
1760}
1761
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001762/*
1763 * Count and mark off the parameters in a multi-line macro call.
1764 * This is called both from within the multi-line macro expansion
1765 * code, and also to mark off the default parameters when provided
1766 * in a %macro definition line.
1767 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001769{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001770 int paramsize, brace;
1771
1772 *nparam = paramsize = 0;
1773 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001775 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001776 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 paramsize += PARAM_DELTA;
1778 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1779 }
1780 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001781 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001783 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001785 if (brace) {
1786 while (brace && (t = t->next) != NULL) {
1787 if (tok_is_(t, "{"))
1788 brace++;
1789 else if (tok_is_(t, "}"))
1790 brace--;
1791 }
1792
1793 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 /*
1795 * Now we've found the closing brace, look further
1796 * for the comma.
1797 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001798 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 skip_white_(t);
1800 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001801 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 while (tok_isnt_(t, ","))
1803 t = t->next;
1804 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001806 } else {
1807 while (tok_isnt_(t, ","))
1808 t = t->next;
1809 }
1810 if (t) { /* got a comma/brace */
1811 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001813 }
1814}
1815
1816/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001817 * Determine whether one of the various `if' conditions is true or
1818 * not.
1819 *
1820 * We must free the tline we get passed.
1821 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001822static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001823{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001824 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001825 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001826 Token *t, *tt, *origline;
1827 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001828 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001829 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001830 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001831 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001832
1833 origline = tline;
1834
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001836 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001837 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001838 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001840 if (!tline)
1841 break;
1842 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001843 nasm_nonfatal("`%s' expects context identifiers",
1844 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 free_tlist(origline);
1846 return -1;
1847 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001848 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001849 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 tline = tline->next;
1851 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001852 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001853
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001854 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001855 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001856 while (tline) {
1857 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 if (!tline || (tline->type != TOK_ID &&
1859 (tline->type != TOK_PREPROC_ID ||
1860 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001861 nasm_nonfatal("`%s' expects macro identifiers",
1862 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001863 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001865 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001866 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001868 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001869 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001870
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001871 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001872 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001873 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001874 while (tline) {
1875 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001876 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001877 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001878 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001879 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001880 nasm_nonfatal("`%s' expects environment variable names",
1881 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001882 goto fail;
1883 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001884 p = tline->text;
1885 if (tline->type == TOK_PREPROC_ID)
1886 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001887 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001888 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001889 if (getenv(p))
1890 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001891 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001892 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001893 break;
1894
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001895 case PPC_IFIDN:
1896 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 tline = expand_smacro(tline);
1898 t = tt = tline;
1899 while (tok_isnt_(tt, ","))
1900 tt = tt->next;
1901 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001902 nasm_nonfatal("`%s' expects two comma-separated arguments",
1903 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001904 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 }
1906 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001907 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1909 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001910 nasm_nonfatal("`%s': more than one comma on line",
1911 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 }
1914 if (t->type == TOK_WHITESPACE) {
1915 t = t->next;
1916 continue;
1917 }
1918 if (tt->type == TOK_WHITESPACE) {
1919 tt = tt->next;
1920 continue;
1921 }
1922 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001923 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 break;
1925 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001926 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 size_t l1 = nasm_unquote(t->text, NULL);
1929 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001930
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 if (l1 != l2) {
1932 j = false;
1933 break;
1934 }
1935 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1936 j = false;
1937 break;
1938 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001939 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001940 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 break;
1942 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001943
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 t = t->next;
1945 tt = tt->next;
1946 }
1947 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001948 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001950
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001951 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001952 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001953 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001954 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001955
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001956 skip_white_(tline);
1957 tline = expand_id(tline);
1958 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001959 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001960 goto fail;
1961 }
1962 searching.name = nasm_strdup(tline->text);
1963 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001964 searching.plus = false;
1965 searching.nolist = false;
1966 searching.in_progress = 0;
1967 searching.max_depth = 0;
1968 searching.rep_nest = NULL;
1969 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001970 searching.nparam_max = INT_MAX;
1971 tline = expand_smacro(tline->next);
1972 skip_white_(tline);
1973 if (!tline) {
1974 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001975 nasm_nonfatal("`%s' expects a parameter count or nothing",
1976 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001977 } else {
1978 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001979 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 }
1981 if (tline && tok_is_(tline->next, "-")) {
1982 tline = tline->next->next;
1983 if (tok_is_(tline, "*"))
1984 searching.nparam_max = INT_MAX;
1985 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001986 nasm_nonfatal("`%s' expects a parameter count after `-'",
1987 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001989 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001990 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001991 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001992 searching.nparam_max = searching.nparam_min;
1993 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001994 }
1995 }
1996 if (tline && tok_is_(tline->next, "+")) {
1997 tline = tline->next;
1998 searching.plus = true;
1999 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002000 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2001 while (mmac) {
2002 if (!strcmp(mmac->name, searching.name) &&
2003 (mmac->nparam_min <= searching.nparam_max
2004 || searching.plus)
2005 && (searching.nparam_min <= mmac->nparam_max
2006 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 found = true;
2008 break;
2009 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002010 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011 }
2012 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002013 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 nasm_free(searching.name);
2015 j = found;
2016 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002017 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002018
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002019 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002020 needtype = TOK_ID;
2021 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002022 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 needtype = TOK_NUMBER;
2024 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002025 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 needtype = TOK_STRING;
2027 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002028
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029iftype:
2030 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002031
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032 while (tok_type_(t, TOK_WHITESPACE) ||
2033 (needtype == TOK_NUMBER &&
2034 tok_type_(t, TOK_OTHER) &&
2035 (t->text[0] == '-' || t->text[0] == '+') &&
2036 !t->text[1]))
2037 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002038
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039 j = tok_type_(t, needtype);
2040 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002041
2042 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002043 t = tline = expand_smacro(tline);
2044 while (tok_type_(t, TOK_WHITESPACE))
2045 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002046
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002047 j = false;
2048 if (t) {
2049 t = t->next; /* Skip the actual token */
2050 while (tok_type_(t, TOK_WHITESPACE))
2051 t = t->next;
2052 j = !t; /* Should be nothing left */
2053 }
2054 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002055
H. Peter Anvin134b9462008-02-16 17:01:40 -08002056 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 t = tline = expand_smacro(tline);
2058 while (tok_type_(t, TOK_WHITESPACE))
2059 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002060
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002061 j = !t; /* Should be empty */
2062 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002063
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002064 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002065 pps.tptr = tline = expand_smacro(tline);
2066 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002067 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002068 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 if (!evalresult)
2070 return -1;
2071 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002072 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002073 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002074 nasm_nonfatal("non-constant value given to `%s'",
2075 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002076 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002078 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002079 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002080
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002082 nasm_fatal("preprocessor directive `%s' not yet implemented",
2083 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002085 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002086
2087 free_tlist(origline);
2088 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002089
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002090fail:
2091 free_tlist(origline);
2092 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002093}
2094
2095/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002096 * Default smacro expansion routine: just returns a copy of the
2097 * expansion list.
2098 */
2099static Token *
2100smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2101{
2102 (void)params;
2103 (void)nparams;
2104
2105 return dup_tlist(s->expansion, NULL);
2106}
2107
2108/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002109 * Emit a macro defintion or undef to the listing file, if
2110 * desired. This is similar to detoken(), but it handles the reverse
2111 * expansion list, does not expand %! or local variable tokens, and
2112 * does some special handling for macro parameters.
2113 */
2114static void
2115list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2116{
2117 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2118 const Token **param_names;
2119 Token *junk_names = NULL;
2120 Token *t;
2121 size_t namelen, size;
2122 unsigned int i;
2123 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002124 char *context_prefix = NULL;
2125 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002126
2127 nasm_newn(param_names, m->nparam);
2128
2129 namelen = strlen(m->name);
2130 size = namelen + 2; /* Include room for space after name + NUL */
2131
2132 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002133 int context_depth = cstk->depth - ctx->depth + 1;
2134 context_prefix =
2135 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2136 ctx->name ? ctx->name : "",
2137 ctx->number, context_depth, "");
2138
2139 context_len = nasm_last_string_len();
2140 memset(context_prefix + context_len - context_depth,
2141 '$', context_depth);
2142 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002143 }
2144
2145 list_for_each(t, m->expansion) {
2146 if (!t->text) {
2147 size++; /* Whitespace, presumably */
2148 } else {
2149 size += t->len;
2150 if (is_smac_param(t->type))
2151 param_names[smac_nparam(t->type)] = t;
2152 }
2153 }
2154
2155 if (m->nparam) {
2156 /*
2157 * Space for ( and either , or ) around each
2158 * parameter, plus an optional =.
2159 */
2160 size += 1 + 2 * m->nparam;
2161 for (i = 0; i < m->nparam; i++) {
2162 if (!param_names[i])
2163 param_names[i] = &unused_arg_name;
2164 size += param_names[i]->len;
2165 }
2166 }
2167
2168 def = nasm_malloc(size);
2169 p = def+size;
2170 *--p = '\0';
2171
2172 list_for_each(t, m->expansion) {
2173 if (!t->text) {
2174 *--p = ' ';
2175 } else {
2176 p -= t->len;
2177 memcpy(p, t->text, t->len);
2178 }
2179 }
2180
2181 *--p = ' ';
2182
2183 if (m->nparam) {
2184 *--p = ')';
2185 for (i = m->nparam; i--;) {
2186 p -= param_names[i]->len;
2187 memcpy(p, param_names[i]->text, param_names[i]->len);
2188 if (m->eval_param && m->eval_param[i])
2189 *--p = '=';
2190 *--p = ',';
2191 }
2192 *p = '('; /* First parameter starts with ( not , */
2193
2194 free_tlist(junk_names);
2195 }
2196
2197 p -= namelen;
2198 memcpy(p, m->name, namelen);
2199
H. Peter Anvin6686de22019-08-10 05:33:14 -07002200 if (context_prefix) {
2201 p -= context_len;
2202 memcpy(p, context_prefix, context_len);
2203 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002204 }
2205
2206 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002207 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002208}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002209
2210/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002211 * Common code for defining an smacro
2212 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002213static SMacro *define_smacro(Context *ctx, const char *mname,
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002214 bool casesense, int nparam,
2215 bool *eval_param, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002216{
2217 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002218 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002219
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002220 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002221 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002222 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002223 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002224 /*
2225 * Some instances of the old code considered this a failure,
2226 * some others didn't. What is the right thing to do here?
2227 */
2228 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002229 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002230 } else {
2231 /*
2232 * We're redefining, so we have to take over an
2233 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002234 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002235 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002236 clear_smacro(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002237 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002238 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002239 smtbl = ctx ? &ctx->localmac : &smacros;
2240 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002241 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002242 smac->next = *smhead;
2243 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002244 }
2245 smac->name = nasm_strdup(mname);
2246 smac->casesense = casesense;
2247 smac->nparam = nparam;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002248 smac->eval_param = eval_param;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002249 smac->expansion = expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002250 smac->expand = smacro_expand_default;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002251
2252 if (list_option('m'))
2253 list_smacro_def(casesense ? PP_DEFINE : PP_IDEFINE, ctx, smac);
2254
H. Peter Anvin8b262472019-02-26 14:00:54 -08002255 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002256}
2257
2258/*
2259 * Undefine an smacro
2260 */
2261static void undef_smacro(Context *ctx, const char *mname)
2262{
2263 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002264 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002265
H. Peter Anvin166c2472008-05-28 12:28:58 -07002266 smtbl = ctx ? &ctx->localmac : &smacros;
2267 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002268
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002269 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002270 /*
2271 * We now have a macro name... go hunt for it.
2272 */
2273 sp = smhead;
2274 while ((s = *sp) != NULL) {
2275 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002276 if (list_option('m'))
2277 list_smacro_def(PP_UNDEF, ctx, s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002278 *sp = s->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002279 free_smacro(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002280 } else {
2281 sp = &s->next;
2282 }
2283 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002284 }
2285}
2286
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002287/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002288 * Parse a mmacro specification.
2289 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002290static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002291{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002292 tline = tline->next;
2293 skip_white_(tline);
2294 tline = expand_id(tline);
2295 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002296 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002297 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002298 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002299
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002300 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002301 def->name = nasm_strdup(tline->text);
2302 def->plus = false;
2303 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002304 def->in_progress = 0;
2305 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002306 def->nparam_min = 0;
2307 def->nparam_max = 0;
2308
H. Peter Anvina26433d2008-07-16 14:40:01 -07002309 tline = expand_smacro(tline->next);
2310 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002311 if (!tok_type_(tline, TOK_NUMBER))
2312 nasm_nonfatal("`%s' expects a parameter count", directive);
2313 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002314 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002315 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002316 tline = tline->next->next;
2317 if (tok_is_(tline, "*")) {
2318 def->nparam_max = INT_MAX;
2319 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002320 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002321 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002322 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002323 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002324 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002325 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002326 }
2327 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002328 }
2329 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002330 tline = tline->next;
2331 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002332 }
2333 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002334 !nasm_stricmp(tline->next->text, ".nolist")) {
2335 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002336 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002337 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002338
H. Peter Anvina26433d2008-07-16 14:40:01 -07002339 /*
2340 * Handle default parameters.
2341 */
2342 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002343 def->dlist = tline->next;
2344 tline->next = NULL;
2345 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002346 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002347 def->dlist = NULL;
2348 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002349 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002350 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002351
H. Peter Anvin89cee572009-07-15 09:16:54 -04002352 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002353 !def->plus) {
2354 /*
2355 *!macro-defaults [on] macros with more default than optional parameters
2356 *! warns when a macro has more default parameters than optional parameters.
2357 *! See \k{mlmacdef} for why might want to disable this warning.
2358 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002359 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002360 "too many default macro parameters in macro `%s'", def->name);
2361 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002362
H. Peter Anvina26433d2008-07-16 14:40:01 -07002363 return true;
2364}
2365
2366
2367/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002368 * Decode a size directive
2369 */
2370static int parse_size(const char *str) {
2371 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002372 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002373 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002374 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002375 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002376}
2377
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002378/*
2379 * Process a preprocessor %pragma directive. Currently there are none.
2380 * Gets passed the token list starting with the "preproc" token from
2381 * "%pragma preproc".
2382 */
2383static void do_pragma_preproc(Token *tline)
2384{
2385 /* Skip to the real stuff */
2386 tline = tline->next;
2387 skip_white_(tline);
2388 if (!tline)
2389 return;
2390
2391 (void)tline; /* Nothing else to do at present */
2392}
2393
Ed Beroset3ab3f412002-06-11 03:31:49 +00002394/**
2395 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002396 * Find out if a line contains a preprocessor directive, and deal
2397 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002398 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002399 * If a directive _is_ found, it is the responsibility of this routine
2400 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002401 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002402 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002403 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002404 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002405 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002406 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002407static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002408{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002409 enum preproc_token i;
2410 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002411 bool err;
2412 int nparam;
2413 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002414 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002415 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002416 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002417 char *p, *pp;
2418 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002419 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002420 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002421 Include *inc;
2422 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002423 Cond *cond;
2424 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002425 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002426 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002427 struct tokenval tokval;
2428 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002429 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002430 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002431 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002432 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002433 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002434
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002435 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002436 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002437
H. Peter Anvineba20a72002-04-30 20:53:55 +00002438 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002439 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002440 (tline->text[0] && (tline->text[1] == '%' ||
2441 tline->text[1] == '$' ||
2442 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002444
H. Peter Anvin4169a472007-09-12 01:29:43 +00002445 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002446
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002447 /*
2448 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2449 * since they are known to be buggy at moment, we need to fix them
2450 * in future release (2.09-2.10)
2451 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002452 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002453 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2454 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002455 }
2456
2457 /*
2458 * If we're in a non-emitting branch of a condition construct,
2459 * or walking to the end of an already terminated %rep block,
2460 * we should ignore all directives except for condition
2461 * directives.
2462 */
2463 if (((istk->conds && !emitting(istk->conds->state)) ||
2464 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2465 return NO_DIRECTIVE_FOUND;
2466 }
2467
2468 /*
2469 * If we're defining a macro or reading a %rep block, we should
2470 * ignore all directives except for %macro/%imacro (which nest),
2471 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2472 * If we're in a %rep block, another %rep nests, so should be let through.
2473 */
2474 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2475 i != PP_RMACRO && i != PP_IRMACRO &&
2476 i != PP_ENDMACRO && i != PP_ENDM &&
2477 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2478 return NO_DIRECTIVE_FOUND;
2479 }
2480
2481 if (defining) {
2482 if (i == PP_MACRO || i == PP_IMACRO ||
2483 i == PP_RMACRO || i == PP_IRMACRO) {
2484 nested_mac_count++;
2485 return NO_DIRECTIVE_FOUND;
2486 } else if (nested_mac_count > 0) {
2487 if (i == PP_ENDMACRO) {
2488 nested_mac_count--;
2489 return NO_DIRECTIVE_FOUND;
2490 }
2491 }
2492 if (!defining->name) {
2493 if (i == PP_REP) {
2494 nested_rep_count++;
2495 return NO_DIRECTIVE_FOUND;
2496 } else if (nested_rep_count > 0) {
2497 if (i == PP_ENDREP) {
2498 nested_rep_count--;
2499 return NO_DIRECTIVE_FOUND;
2500 }
2501 }
2502 }
2503 }
2504
H. Peter Anvin8b262472019-02-26 14:00:54 -08002505 dname = pp_directives[i]; /* Directive name, for error messages */
2506 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002507 switch (i) {
2508 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002509 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002511
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002512 case PP_PRAGMA:
2513 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002514 * %pragma namespace options...
2515 *
2516 * The namespace "preproc" is reserved for the preprocessor;
2517 * all other namespaces generate a [pragma] assembly directive.
2518 *
2519 * Invalid %pragmas are ignored and may have different
2520 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002521 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002522 tline = tline->next;
2523 skip_white_(tline);
2524 tline = expand_smacro(tline);
2525 if (tok_type_(tline, TOK_ID)) {
2526 if (!nasm_stricmp(tline->text, "preproc")) {
2527 /* Preprocessor pragma */
2528 do_pragma_preproc(tline);
2529 } else {
2530 /* Build the assembler directive */
2531 t = new_Token(NULL, TOK_OTHER, "[", 1);
2532 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2533 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2534 tline = t;
2535 for (t = tline; t->next; t = t->next)
2536 ;
2537 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002538 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002539 }
2540 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002541 free_tlist(origline);
2542 return DIRECTIVE_FOUND;
2543
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 case PP_STACKSIZE:
2545 /* Directive to tell NASM what the default stack size is. The
2546 * default is for a 16-bit stack, and this can be overriden with
2547 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 */
2549 tline = tline->next;
2550 if (tline && tline->type == TOK_WHITESPACE)
2551 tline = tline->next;
2552 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002553 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002554 free_tlist(origline);
2555 return DIRECTIVE_FOUND;
2556 }
2557 if (nasm_stricmp(tline->text, "flat") == 0) {
2558 /* All subsequent ARG directives are for a 32-bit stack */
2559 StackSize = 4;
2560 StackPointer = "ebp";
2561 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002562 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002563 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2564 /* All subsequent ARG directives are for a 64-bit stack */
2565 StackSize = 8;
2566 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002567 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002568 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 } else if (nasm_stricmp(tline->text, "large") == 0) {
2570 /* All subsequent ARG directives are for a 16-bit stack,
2571 * far function call.
2572 */
2573 StackSize = 2;
2574 StackPointer = "bp";
2575 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002576 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 } else if (nasm_stricmp(tline->text, "small") == 0) {
2578 /* All subsequent ARG directives are for a 16-bit stack,
2579 * far function call. We don't support near functions.
2580 */
2581 StackSize = 2;
2582 StackPointer = "bp";
2583 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002584 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002586 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 free_tlist(origline);
2588 return DIRECTIVE_FOUND;
2589 }
2590 free_tlist(origline);
2591 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002592
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 case PP_ARG:
2594 /* TASM like ARG directive to define arguments to functions, in
2595 * the following form:
2596 *
2597 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2598 */
2599 offset = ArgOffset;
2600 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002601 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002603
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 /* Find the argument name */
2605 tline = tline->next;
2606 if (tline && tline->type == TOK_WHITESPACE)
2607 tline = tline->next;
2608 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002609 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 free_tlist(origline);
2611 return DIRECTIVE_FOUND;
2612 }
2613 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002614
H. Peter Anvine2c80182005-01-15 22:15:51 +00002615 /* Find the argument size type */
2616 tline = tline->next;
2617 if (!tline || tline->type != TOK_OTHER
2618 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002619 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002620 free_tlist(origline);
2621 return DIRECTIVE_FOUND;
2622 }
2623 tline = tline->next;
2624 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002625 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002626 free_tlist(origline);
2627 return DIRECTIVE_FOUND;
2628 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002629
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002631 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002632 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 size = parse_size(tt->text);
2634 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002635 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 free_tlist(tt);
2637 free_tlist(origline);
2638 return DIRECTIVE_FOUND;
2639 }
2640 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002641
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002642 /* Round up to even stack slots */
2643 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 /* Now define the macro for the argument */
2646 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2647 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002648 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002650
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 /* Move to the next argument in the list */
2652 tline = tline->next;
2653 if (tline && tline->type == TOK_WHITESPACE)
2654 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002655 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002656 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002659
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 case PP_LOCAL:
2661 /* TASM like LOCAL directive to define local variables for a
2662 * function, in the following form:
2663 *
2664 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2665 *
2666 * The '= LocalSize' at the end is ignored by NASM, but is
2667 * required by TASM to define the local parameter size (and used
2668 * by the TASM macro package).
2669 */
2670 offset = LocalOffset;
2671 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002672 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002674
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 /* Find the argument name */
2676 tline = tline->next;
2677 if (tline && tline->type == TOK_WHITESPACE)
2678 tline = tline->next;
2679 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002680 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 free_tlist(origline);
2682 return DIRECTIVE_FOUND;
2683 }
2684 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002685
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 /* Find the argument size type */
2687 tline = tline->next;
2688 if (!tline || tline->type != TOK_OTHER
2689 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002690 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 free_tlist(origline);
2692 return DIRECTIVE_FOUND;
2693 }
2694 tline = tline->next;
2695 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002696 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 free_tlist(origline);
2698 return DIRECTIVE_FOUND;
2699 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002700
H. Peter Anvine2c80182005-01-15 22:15:51 +00002701 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002702 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002703 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 size = parse_size(tt->text);
2705 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002706 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 free_tlist(tt);
2708 free_tlist(origline);
2709 return DIRECTIVE_FOUND;
2710 }
2711 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002712
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 /* Round up to even stack slots */
2714 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002715
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002717
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2720 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002721 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002722
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 /* Now define the assign to setup the enter_c macro correctly */
2724 snprintf(directive, sizeof(directive),
2725 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002726 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002727
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 /* Move to the next argument in the list */
2729 tline = tline->next;
2730 if (tline && tline->type == TOK_WHITESPACE)
2731 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002732 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 free_tlist(origline);
2735 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 case PP_CLEAR:
2738 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002739 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002740 free_macros();
2741 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 free_tlist(origline);
2743 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002744
H. Peter Anvin418ca702008-05-30 10:42:30 -07002745 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002746 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002747 skip_white_(t);
2748 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002749 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002750 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002751 free_tlist(origline);
2752 return DIRECTIVE_FOUND; /* but we did _something_ */
2753 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002754 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002755 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002756 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002757 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002758 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002759 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002761 return DIRECTIVE_FOUND;
2762
2763 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002764 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002765 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002766
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002767 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002769 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002770 free_tlist(origline);
2771 return DIRECTIVE_FOUND; /* but we did _something_ */
2772 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002773 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002774 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002775 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002776 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002777 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002778 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002780 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002781 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002782 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002783 (pp_mode == PP_DEPS)
2784 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002785 if (!inc->fp) {
2786 /* -MG given but file not found */
2787 nasm_free(inc);
2788 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002789 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002790 inc->lineno = src_set_linnum(0);
2791 inc->lineinc = 1;
2792 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002793 inc->mstk = NULL;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002794 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002795 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002796 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 }
2798 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002800
H. Peter Anvind2456592008-06-19 15:04:18 -07002801 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002802 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002803 static macros_t *use_pkg;
2804 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002805
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002806 tline = tline->next;
2807 skip_white_(tline);
2808 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002809
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002810 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 tline->type != TOK_INTERNAL_STRING &&
2812 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002813 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002814 free_tlist(origline);
2815 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002816 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002817 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002818 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002820 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002821 use_pkg = nasm_stdmac_find_package(tline->text);
2822 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002823 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002824 else
2825 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002826 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002827 /*
2828 * Not already included, go ahead and include it.
2829 * Treat it as an include file for the purpose of
2830 * producing a listing.
2831 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002832 stdmacpos = use_pkg;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002833 nasm_new(inc);
2834 inc->next = istk;
2835 inc->fname = src_set_fname(NULL);
2836 inc->lineno = src_set_linnum(0);
2837 inc->lineinc = 0;
2838 inc->expansion = NULL;
2839 inc->mstk = NULL;
2840 inc->nolist = !list_option('b') || istk->nolist;
2841 istk = inc;
2842 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002843 }
2844 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002845 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002846 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002849 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 tline = tline->next;
2851 skip_white_(tline);
2852 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002853 if (tline) {
2854 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002855 nasm_nonfatal("`%s' expects a context identifier",
2856 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002857 free_tlist(origline);
2858 return DIRECTIVE_FOUND; /* but we did _something_ */
2859 }
2860 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002861 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002862 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002863 p = nasm_strdup(tline->text);
2864 } else {
2865 p = NULL; /* Anonymous */
2866 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002867
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002868 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002869 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002870 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002871 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002872 ctx->name = p;
2873 ctx->number = unique++;
2874 cstk = ctx;
2875 } else {
2876 /* %pop or %repl */
2877 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002878 nasm_nonfatal("`%s': context stack is empty",
2879 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002880 } else if (i == PP_POP) {
2881 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002882 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002883 "expected %s",
2884 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002885 else
2886 ctx_pop();
2887 } else {
2888 /* i == PP_REPL */
2889 nasm_free(cstk->name);
2890 cstk->name = p;
2891 p = NULL;
2892 }
2893 nasm_free(p);
2894 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002895 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002896 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002897 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002898 severity = ERR_FATAL;
2899 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002901 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002902 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002903 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002904 /*!
2905 *!user [on] %warning directives
2906 *! controls output of \c{%warning} directives (see \k{pperror}).
2907 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002908 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002909 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002910
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002911issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002912 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002913 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002914 tline->next = expand_smacro(tline->next);
2915 tline = tline->next;
2916 skip_white_(tline);
2917 t = tline ? tline->next : NULL;
2918 skip_white_(t);
2919 if (tok_type_(tline, TOK_STRING) && !t) {
2920 /* The line contains only a quoted string */
2921 p = tline->text;
2922 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002923 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002924 } else {
2925 /* Not a quoted string, or more than a quoted string */
2926 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002927 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002928 nasm_free(p);
2929 }
2930 free_tlist(origline);
2931 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002932 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002933
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002934 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002935 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002936 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002937 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002938 j = if_condition(tline->next, i);
2939 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002940 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002941 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002942 cond = nasm_malloc(sizeof(Cond));
2943 cond->next = istk->conds;
2944 cond->state = j;
2945 istk->conds = cond;
2946 if(istk->mstk)
2947 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002948 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002950
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002951 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002952 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002953 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002954 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002955 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002956 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002957 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002958
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002959 case COND_DONE:
2960 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002961 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002962
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002963 case COND_ELSE_TRUE:
2964 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002965 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002966 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002967 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002968 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002969
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002970 case COND_IF_FALSE:
2971 /*
2972 * IMPORTANT: In the case of %if, we will already have
2973 * called expand_mmac_params(); however, if we're
2974 * processing an %elif we must have been in a
2975 * non-emitting mode, which would have inhibited
2976 * the normal invocation of expand_mmac_params().
2977 * Therefore, we have to do it explicitly here.
2978 */
2979 j = if_condition(expand_mmac_params(tline->next), i);
2980 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002981 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002982 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2983 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002984 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002985 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002987
H. Peter Anvine2c80182005-01-15 22:15:51 +00002988 case PP_ELSE:
2989 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002990 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002991 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002992 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002993 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002994 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002995 case COND_IF_TRUE:
2996 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002997 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002998 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002999
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003000 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003001 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003002
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003003 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003004 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003005 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003006
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003007 case COND_ELSE_TRUE:
3008 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003009 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003010 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003011 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003012 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003013 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 free_tlist(origline);
3015 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003016
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 case PP_ENDIF:
3018 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003019 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003020 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003021 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003022 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003023 cond = istk->conds;
3024 istk->conds = cond->next;
3025 nasm_free(cond);
3026 if(istk->mstk)
3027 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 free_tlist(origline);
3029 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003030
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04003031 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003032 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003033 casesense = false;
3034 /* fall through */
3035 case PP_RMACRO:
3036 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003037 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003038 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 return DIRECTIVE_FOUND;
3040 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003041 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin322bee02019-08-10 01:38:06 -07003042 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003043 defining->casesense = casesense;
3044 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003045 nasm_free(defining);
3046 defining = NULL;
3047 return DIRECTIVE_FOUND;
3048 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003049
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003050 src_get(&defining->xline, &defining->fname);
3051
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003052 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3053 while (mmac) {
3054 if (!strcmp(mmac->name, defining->name) &&
3055 (mmac->nparam_min <= defining->nparam_max
3056 || defining->plus)
3057 && (defining->nparam_min <= mmac->nparam_max
3058 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003059 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003060 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003061 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003062 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003063 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003064 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 free_tlist(origline);
3066 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003067
H. Peter Anvine2c80182005-01-15 22:15:51 +00003068 case PP_ENDM:
3069 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003070 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003071 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003072 return DIRECTIVE_FOUND;
3073 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003074 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3075 defining->next = *mmhead;
3076 *mmhead = defining;
3077 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003078 free_tlist(origline);
3079 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003080
H. Peter Anvin89cee572009-07-15 09:16:54 -04003081 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003082 /*
3083 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003084 * macro-end marker for a macro with a name. Then we
3085 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003086 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003087 list_for_each(l, istk->expansion)
3088 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003089 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003090
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003091 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003092 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003093 * Remove all conditional entries relative to this
3094 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003095 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003096 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3097 cond = istk->conds;
3098 istk->conds = cond->next;
3099 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003100 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003101 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003102 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003103 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003104 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003105 free_tlist(origline);
3106 return DIRECTIVE_FOUND;
3107
H. Peter Anvina26433d2008-07-16 14:40:01 -07003108 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003109 casesense = false;
3110 /* fall through */
3111 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003112 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003113 MMacro **mmac_p;
3114 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003115
H. Peter Anvin8b262472019-02-26 14:00:54 -08003116 spec.casesense = casesense;
3117 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003118 return DIRECTIVE_FOUND;
3119 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003120 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3121 while (mmac_p && *mmac_p) {
3122 mmac = *mmac_p;
3123 if (mmac->casesense == spec.casesense &&
3124 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3125 mmac->nparam_min == spec.nparam_min &&
3126 mmac->nparam_max == spec.nparam_max &&
3127 mmac->plus == spec.plus) {
3128 *mmac_p = mmac->next;
3129 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003130 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003131 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003132 }
3133 }
3134 free_tlist(origline);
3135 free_tlist(spec.dlist);
3136 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003137 }
3138
H. Peter Anvine2c80182005-01-15 22:15:51 +00003139 case PP_ROTATE:
3140 if (tline->next && tline->next->type == TOK_WHITESPACE)
3141 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003142 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003144 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 return DIRECTIVE_FOUND;
3146 }
3147 t = expand_smacro(tline->next);
3148 tline->next = NULL;
3149 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003150 pps.tptr = tline = t;
3151 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003152 tokval.t_type = TOKEN_INVALID;
3153 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003154 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003155 free_tlist(tline);
3156 if (!evalresult)
3157 return DIRECTIVE_FOUND;
3158 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003159 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003161 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 return DIRECTIVE_FOUND;
3163 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003164 mmac = istk->mstk;
3165 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3166 mmac = mmac->next_active;
3167 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003168 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003169 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003170 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003171 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003172 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003173
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003174 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003175 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003176 rotate += mmac->nparam;
3177
3178 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003179 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003180 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003181
H. Peter Anvine2c80182005-01-15 22:15:51 +00003182 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003183 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003184 do {
3185 tline = tline->next;
3186 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003187
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 if (tok_type_(tline, TOK_ID) &&
3189 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003190 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 do {
3192 tline = tline->next;
3193 } while (tok_type_(tline, TOK_WHITESPACE));
3194 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003195
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003197 pps.tptr = expand_smacro(tline);
3198 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003199 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003200 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003202 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003203 if (!evalresult) {
3204 free_tlist(origline);
3205 return DIRECTIVE_FOUND;
3206 }
3207 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003208 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003209 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003210 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003211 return DIRECTIVE_FOUND;
3212 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003213 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003214 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003215 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3216 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003217 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003218 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003219 /*!
3220 *!negative-rep [on] regative %rep count
3221 *! warns about negative counts given to the \c{%rep}
3222 *! preprocessor directive.
3223 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003224 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003225 "negative `%%rep' count: %"PRId64, count);
3226 count = 0;
3227 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003228 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003229 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003231 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003232 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003233 }
3234 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003235
3236 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003237 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003238 defining->nolist = nolist;
3239 defining->in_progress = count;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003240 defining->next_active = istk->mstk;
3241 defining->rep_nest = tmp_defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003242 src_get(&defining->xline, &defining->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003244
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003246 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003247 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 return DIRECTIVE_FOUND;
3249 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003250
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 /*
3252 * Now we have a "macro" defined - although it has no name
3253 * and we won't be entering it in the hash tables - we must
3254 * push a macro-end marker for it on to istk->expansion.
3255 * After that, it will take care of propagating itself (a
3256 * macro-end marker line for a macro which is really a %rep
3257 * block will cause the macro to be re-expanded, complete
3258 * with another macro-end marker to ensure the process
3259 * continues) until the whole expansion is forcibly removed
3260 * from istk->expansion by a %exitrep.
3261 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003262 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003263 l->next = istk->expansion;
3264 l->finishes = defining;
3265 l->first = NULL;
3266 istk->expansion = l;
3267
3268 istk->mstk = defining;
3269
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003270 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003271 tmp_defining = defining;
3272 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 free_tlist(origline);
3274 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003275
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003277 /*
3278 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003279 * macro-end marker for a macro with no name. Then we set
3280 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003281 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003282 list_for_each(l, istk->expansion)
3283 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003284 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003285
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003286 if (l)
3287 l->finishes->in_progress = 1;
3288 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003289 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003290 free_tlist(origline);
3291 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003292
H. Peter Anvine2c80182005-01-15 22:15:51 +00003293 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003294 case PP_IXDEFINE:
3295 casesense = false;
3296 /* fall through */
3297 case PP_DEFINE:
3298 case PP_XDEFINE:
3299 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003300 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003301 bool *eval_params = NULL;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003302
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 tline = tline->next;
3304 skip_white_(tline);
3305 tline = expand_id(tline);
3306 if (!tline || (tline->type != TOK_ID &&
3307 (tline->type != TOK_PREPROC_ID ||
3308 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003309 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003310 free_tlist(origline);
3311 return DIRECTIVE_FOUND;
3312 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003313
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003314 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003315 last = tline;
3316 param_start = tline = tline->next;
3317 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003318
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 if (tok_is_(tline, "(")) {
3320 /*
3321 * This macro has parameters.
3322 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003323
H. Peter Anvine2c80182005-01-15 22:15:51 +00003324 tline = tline->next;
3325 while (1) {
3326 skip_white_(tline);
3327 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003328 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329 free_tlist(origline);
3330 return DIRECTIVE_FOUND;
3331 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003332 if (tok_is_(tline, "=")) {
3333 have_eval_params = true;
3334 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003335 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003336 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003337
3338 /*
3339 * "*" means an unnamed, ignored argument; it can never
3340 * match anything because "*" is not TOK_ID, and so
3341 * none of the expansion entries will be converted
3342 * to parameters.
3343 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003344 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003345 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3346 /*
3347 * Empty name; duplicate the termination
3348 * token and use the first copy as a placeholder.
3349 * It will never match anything, since the
3350 * associated text doesn't correspond to a TOK_ID.
3351 */
3352 tline = dup_Token(tline, tline);
3353 } else {
3354 nasm_nonfatal("`%s': parameter identifier expected",
3355 tline->text);
3356 free_tlist(origline);
3357 return DIRECTIVE_FOUND;
3358 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003359 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003360 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 tline = tline->next;
3362 skip_white_(tline);
3363 if (tok_is_(tline, ",")) {
3364 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003365 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003366 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003367 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 free_tlist(origline);
3369 return DIRECTIVE_FOUND;
3370 }
3371 break;
3372 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003373 }
3374 last = tline;
3375 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003376
3377 if (have_eval_params) {
3378 /* Create evaluated parameters table */
3379 bool is_eval = false;
3380
3381 nasm_newn(eval_params, nparam);
3382 list_for_each(tt, param_start) {
3383 if (is_smac_param(tt->type))
3384 eval_params[smac_nparam(tt->type)] = is_eval;
3385 is_eval = tok_is_(tt, "=");
3386 }
3387 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003388 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003389
H. Peter Anvine2c80182005-01-15 22:15:51 +00003390 if (tok_type_(tline, TOK_WHITESPACE))
3391 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003393
3394 /* Expand the macro definition now for %xdefine and %ixdefine */
3395 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3396 tline = expand_smacro(tline);
3397
3398 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 t = tline;
3400 while (t) {
3401 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003402 list_for_each(tt, param_start)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003403 if (is_smac_param(tt->type) &&
3404 tt->len == t->len &&
3405 !memcmp(tt->text, t->text, tt->len))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 t->type = tt->type;
3407 }
3408 tt = t->next;
3409 t->next = macro_start;
3410 macro_start = t;
3411 t = tt;
3412 }
3413 /*
3414 * Good. We now have a macro name, a parameter count, and a
3415 * token list (in reverse order) for an expansion. We ought
3416 * to be OK just to create an SMacro, store it, and let
3417 * free_tlist have the rest of the line (which we have
3418 * carefully re-terminated after chopping off the expansion
3419 * from the end).
3420 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003421 define_smacro(ctx, mname, casesense, nparam, eval_params, macro_start);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003422
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 free_tlist(origline);
3424 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003425 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003426
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 case PP_UNDEF:
3428 tline = tline->next;
3429 skip_white_(tline);
3430 tline = expand_id(tline);
3431 if (!tline || (tline->type != TOK_ID &&
3432 (tline->type != TOK_PREPROC_ID ||
3433 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003434 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435 free_tlist(origline);
3436 return DIRECTIVE_FOUND;
3437 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003438 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003439 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003440
H. Peter Anvine2c80182005-01-15 22:15:51 +00003441 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003442 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003443 undef_smacro(ctx, mname);
3444 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003446
H. Peter Anvin9e200162008-06-04 17:23:14 -07003447 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003448 casesense = false;
3449 /* fall through */
3450 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003451 tline = tline->next;
3452 skip_white_(tline);
3453 tline = expand_id(tline);
3454 if (!tline || (tline->type != TOK_ID &&
3455 (tline->type != TOK_PREPROC_ID ||
3456 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003457 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003458 free_tlist(origline);
3459 return DIRECTIVE_FOUND;
3460 }
3461
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003462 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003463 last = tline;
3464 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003465 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003466
3467 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003468 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003469
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003471 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003473
3474 /*
3475 * We now have a macro name, an implicit parameter count of
3476 * zero, and a string token to use as an expansion. Create
3477 * and store an SMacro.
3478 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003479 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003480 free_tlist(origline);
3481 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003483 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003484 casesense = false;
3485 /* fall through */
3486 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003487 tline = tline->next;
3488 skip_white_(tline);
3489 tline = expand_id(tline);
3490 if (!tline || (tline->type != TOK_ID &&
3491 (tline->type != TOK_PREPROC_ID ||
3492 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003493 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
3496 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003497 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003498 last = tline;
3499 tline = expand_smacro(tline->next);
3500 last->next = NULL;
3501
3502 t = tline;
3503 while (tok_type_(t, TOK_WHITESPACE))
3504 t = t->next;
3505 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003506 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003507 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003508 free_tlist(tline);
3509 free_tlist(origline);
3510 return DIRECTIVE_FOUND;
3511 }
3512
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003513 /*
3514 * Convert the string to a token stream. Note that smacros
3515 * are stored with the token stream reversed, so we have to
3516 * reverse the output of tokenize().
3517 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003518 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003519 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003520
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003521 /*
3522 * We now have a macro name, an implicit parameter count of
3523 * zero, and a numeric token to use as an expansion. Create
3524 * and store an SMacro.
3525 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003526 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003527 free_tlist(tline);
3528 free_tlist(origline);
3529 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003530
H. Peter Anvin8b262472019-02-26 14:00:54 -08003531 case PP_IPATHSEARCH:
3532 casesense = false;
3533 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003534 case PP_PATHSEARCH:
3535 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003536 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003537
H. Peter Anvin418ca702008-05-30 10:42:30 -07003538 tline = tline->next;
3539 skip_white_(tline);
3540 tline = expand_id(tline);
3541 if (!tline || (tline->type != TOK_ID &&
3542 (tline->type != TOK_PREPROC_ID ||
3543 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003544 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003545 free_tlist(origline);
3546 return DIRECTIVE_FOUND;
3547 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003548 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003549 last = tline;
3550 tline = expand_smacro(tline->next);
3551 last->next = NULL;
3552
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003553 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003554 while (tok_type_(t, TOK_WHITESPACE))
3555 t = t->next;
3556
3557 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003558 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003559 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003561 free_tlist(origline);
3562 return DIRECTIVE_FOUND; /* but we did _something_ */
3563 }
3564 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003565 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003566 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003567 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003568 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003569
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003570 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003571 if (!found_path)
3572 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003573 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003574
3575 /*
3576 * We now have a macro name, an implicit parameter count of
3577 * zero, and a string token to use as an expansion. Create
3578 * and store an SMacro.
3579 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003580 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003581 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003582 free_tlist(origline);
3583 return DIRECTIVE_FOUND;
3584 }
3585
H. Peter Anvin8b262472019-02-26 14:00:54 -08003586 case PP_ISTRLEN:
3587 casesense = false;
3588 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 case PP_STRLEN:
3590 tline = tline->next;
3591 skip_white_(tline);
3592 tline = expand_id(tline);
3593 if (!tline || (tline->type != TOK_ID &&
3594 (tline->type != TOK_PREPROC_ID ||
3595 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003596 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 free_tlist(origline);
3598 return DIRECTIVE_FOUND;
3599 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003600 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 last = tline;
3602 tline = expand_smacro(tline->next);
3603 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003604
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 t = tline;
3606 while (tok_type_(t, TOK_WHITESPACE))
3607 t = t->next;
3608 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003609 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003610 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 free_tlist(tline);
3612 free_tlist(origline);
3613 return DIRECTIVE_FOUND;
3614 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003615
H. Peter Anvin8b262472019-02-26 14:00:54 -08003616 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003617
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 /*
3619 * We now have a macro name, an implicit parameter count of
3620 * zero, and a numeric token to use as an expansion. Create
3621 * and store an SMacro.
3622 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003623 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003624 free_tlist(tline);
3625 free_tlist(origline);
3626 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003627
H. Peter Anvin8b262472019-02-26 14:00:54 -08003628 case PP_ISTRCAT:
3629 casesense = false;
3630 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003631 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003632 tline = tline->next;
3633 skip_white_(tline);
3634 tline = expand_id(tline);
3635 if (!tline || (tline->type != TOK_ID &&
3636 (tline->type != TOK_PREPROC_ID ||
3637 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003638 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003639 free_tlist(origline);
3640 return DIRECTIVE_FOUND;
3641 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003642 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003643 last = tline;
3644 tline = expand_smacro(tline->next);
3645 last->next = NULL;
3646
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003647 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003648 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003649 switch (t->type) {
3650 case TOK_WHITESPACE:
3651 break;
3652 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003653 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003654 break;
3655 case TOK_OTHER:
3656 if (!strcmp(t->text, ",")) /* permit comma separators */
3657 break;
3658 /* else fall through */
3659 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003660 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003661 free_tlist(tline);
3662 free_tlist(origline);
3663 return DIRECTIVE_FOUND;
3664 }
3665 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003666
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003667 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003668 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003669 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003670 memcpy(p, t->text, t->len);
3671 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003672 }
3673 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003674
3675 /*
3676 * We now have a macro name, an implicit parameter count of
3677 * zero, and a numeric token to use as an expansion. Create
3678 * and store an SMacro.
3679 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003680 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003681 nasm_free(pp);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003682 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003683 free_tlist(tline);
3684 free_tlist(origline);
3685 return DIRECTIVE_FOUND;
3686
H. Peter Anvin8b262472019-02-26 14:00:54 -08003687 case PP_ISUBSTR:
3688 casesense = false;
3689 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003690 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003691 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003692 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003693 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003694
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 tline = tline->next;
3696 skip_white_(tline);
3697 tline = expand_id(tline);
3698 if (!tline || (tline->type != TOK_ID &&
3699 (tline->type != TOK_PREPROC_ID ||
3700 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003701 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003702 free_tlist(origline);
3703 return DIRECTIVE_FOUND;
3704 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003705 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706 last = tline;
3707 tline = expand_smacro(tline->next);
3708 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003709
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003710 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003711 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 while (tok_type_(t, TOK_WHITESPACE))
3713 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003714
H. Peter Anvine2c80182005-01-15 22:15:51 +00003715 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003716 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003717 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003718 free_tlist(tline);
3719 free_tlist(origline);
3720 return DIRECTIVE_FOUND;
3721 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003722
H. Peter Anvin8b262472019-02-26 14:00:54 -08003723 pps.tptr = t->next;
3724 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003725 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003726 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003727 if (!evalresult) {
3728 free_tlist(tline);
3729 free_tlist(origline);
3730 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003731 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003732 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003733 free_tlist(tline);
3734 free_tlist(origline);
3735 return DIRECTIVE_FOUND;
3736 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003737 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003738
H. Peter Anvin8b262472019-02-26 14:00:54 -08003739 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3740 pps.tptr = pps.tptr->next;
3741 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003742 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003743 } else {
3744 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003745 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003746 if (!evalresult) {
3747 free_tlist(tline);
3748 free_tlist(origline);
3749 return DIRECTIVE_FOUND;
3750 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003751 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003752 free_tlist(tline);
3753 free_tlist(origline);
3754 return DIRECTIVE_FOUND;
3755 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003756 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003757 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003758
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003759 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003760
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003761 /* make start and count being in range */
3762 if (start < 0)
3763 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003764 if (count < 0)
3765 count = len + count + 1 - start;
3766 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003767 count = len - start;
3768 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003769 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003770
H. Peter Anvin8b262472019-02-26 14:00:54 -08003771 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003772 macro_start->len = count;
3773 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3774 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003775
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 /*
3777 * We now have a macro name, an implicit parameter count of
3778 * zero, and a numeric token to use as an expansion. Create
3779 * and store an SMacro.
3780 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003781 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 free_tlist(tline);
3783 free_tlist(origline);
3784 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003785 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003786
H. Peter Anvine2c80182005-01-15 22:15:51 +00003787 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003788 casesense = false;
3789 /* fall through */
3790 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 tline = tline->next;
3792 skip_white_(tline);
3793 tline = expand_id(tline);
3794 if (!tline || (tline->type != TOK_ID &&
3795 (tline->type != TOK_PREPROC_ID ||
3796 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003797 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 free_tlist(origline);
3799 return DIRECTIVE_FOUND;
3800 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003801 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003802 last = tline;
3803 tline = expand_smacro(tline->next);
3804 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003805
H. Peter Anvin8b262472019-02-26 14:00:54 -08003806 pps.tptr = tline;
3807 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003808 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003809 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 free_tlist(tline);
3811 if (!evalresult) {
3812 free_tlist(origline);
3813 return DIRECTIVE_FOUND;
3814 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003815
H. Peter Anvine2c80182005-01-15 22:15:51 +00003816 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003817 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003818
H. Peter Anvine2c80182005-01-15 22:15:51 +00003819 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003820 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003821 free_tlist(origline);
3822 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003823 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003824
H. Peter Anvin8b262472019-02-26 14:00:54 -08003825 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003826
H. Peter Anvine2c80182005-01-15 22:15:51 +00003827 /*
3828 * We now have a macro name, an implicit parameter count of
3829 * zero, and a numeric token to use as an expansion. Create
3830 * and store an SMacro.
3831 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003832 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003833 free_tlist(origline);
3834 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003835
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 case PP_LINE:
3837 /*
3838 * Syntax is `%line nnn[+mmm] [filename]'
3839 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003840 if (unlikely(pp_noline)) {
3841 free_tlist(origline);
3842 return DIRECTIVE_FOUND;
3843 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844 tline = tline->next;
3845 skip_white_(tline);
3846 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003847 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003848 free_tlist(origline);
3849 return DIRECTIVE_FOUND;
3850 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003851 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003852 m = 1;
3853 tline = tline->next;
3854 if (tok_is_(tline, "+")) {
3855 tline = tline->next;
3856 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003857 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003858 free_tlist(origline);
3859 return DIRECTIVE_FOUND;
3860 }
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 }
3872 free_tlist(origline);
3873 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003874
H. Peter Anvine2c80182005-01-15 22:15:51 +00003875 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003876 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003877 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003878 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003879}
3880
3881/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003882 * Ensure that a macro parameter contains a condition code and
3883 * nothing else. Return the condition code index if so, or -1
3884 * otherwise.
3885 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003886static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003887{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003888 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003889
H. Peter Anvin25a99342007-09-22 17:45:45 -07003890 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003891 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003892
H. Peter Anvineba20a72002-04-30 20:53:55 +00003893 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003894 if (!t)
3895 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003896 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003897 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003898 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003899 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003900 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003901 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003902
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003903 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003904}
3905
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003906/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003907 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003908 * pasting, if @handle_explicit passed then explicit pasting
3909 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003910 * The @m array can contain a series of token types which are
3911 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003912 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003913static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003914 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003915{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003916 Token *tok, *next, **prev_next, **prev_nonspace;
3917 bool pasted = false;
3918 char *buf, *p;
3919 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003920
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003921 /*
3922 * The last token before pasting. We need it
3923 * to be able to connect new handled tokens.
3924 * In other words if there were a tokens stream
3925 *
3926 * A -> B -> C -> D
3927 *
3928 * and we've joined tokens B and C, the resulting
3929 * stream should be
3930 *
3931 * A -> BC -> D
3932 */
3933 tok = *head;
3934 prev_next = NULL;
3935
3936 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3937 prev_nonspace = head;
3938 else
3939 prev_nonspace = NULL;
3940
3941 while (tok && (next = tok->next)) {
3942
3943 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003944 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003945 /* Zap redundant whitespaces */
3946 while (tok_type_(next, TOK_WHITESPACE))
3947 next = delete_Token(next);
3948 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003949 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003950
3951 case TOK_PASTE:
3952 /* Explicit pasting */
3953 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003954 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003955 next = delete_Token(tok);
3956
3957 while (tok_type_(next, TOK_WHITESPACE))
3958 next = delete_Token(next);
3959
3960 if (!pasted)
3961 pasted = true;
3962
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003963 /* Left pasting token is start of line */
3964 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003965 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003966
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003967 /*
3968 * No ending token, this might happen in two
3969 * cases
3970 *
3971 * 1) There indeed no right token at all
3972 * 2) There is a bare "%define ID" statement,
3973 * and @ID does expand to whitespace.
3974 *
3975 * So technically we need to do a grammar analysis
3976 * in another stage of parsing, but for now lets don't
3977 * change the behaviour people used to. Simply allow
3978 * whitespace after paste token.
3979 */
3980 if (!next) {
3981 /*
3982 * Zap ending space tokens and that's all.
3983 */
3984 tok = (*prev_nonspace)->next;
3985 while (tok_type_(tok, TOK_WHITESPACE))
3986 tok = delete_Token(tok);
3987 tok = *prev_nonspace;
3988 tok->next = NULL;
3989 break;
3990 }
3991
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003992 tok = *prev_nonspace;
3993 while (tok_type_(tok, TOK_WHITESPACE))
3994 tok = delete_Token(tok);
3995 len = strlen(tok->text);
3996 len += strlen(next->text);
3997
3998 p = buf = nasm_malloc(len + 1);
3999 strcpy(p, tok->text);
4000 p = strchr(p, '\0');
4001 strcpy(p, next->text);
4002
4003 delete_Token(tok);
4004
4005 tok = tokenize(buf);
4006 nasm_free(buf);
4007
4008 *prev_nonspace = tok;
4009 while (tok && tok->next)
4010 tok = tok->next;
4011
4012 tok->next = delete_Token(next);
4013
4014 /* Restart from pasted tokens head */
4015 tok = *prev_nonspace;
4016 break;
4017
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004018 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004019 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004020 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004021 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
4022 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004023
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004024 len = 0;
4025 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
4026 len += strlen(next->text);
4027 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004028 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004029
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03004030 /* No match or no text to process */
4031 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004032 break;
4033
4034 len += strlen(tok->text);
4035 p = buf = nasm_malloc(len + 1);
4036
Adam Majer1a069432017-07-25 11:12:35 +02004037 strcpy(p, tok->text);
4038 p = strchr(p, '\0');
4039 tok = delete_Token(tok);
4040
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004041 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004042 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4043 strcpy(p, tok->text);
4044 p = strchr(p, '\0');
4045 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004046 tok = delete_Token(tok);
4047 }
4048
4049 tok = tokenize(buf);
4050 nasm_free(buf);
4051
4052 if (prev_next)
4053 *prev_next = tok;
4054 else
4055 *head = tok;
4056
4057 /*
4058 * Connect pasted into original stream,
4059 * ie A -> new-tokens -> B
4060 */
4061 while (tok && tok->next)
4062 tok = tok->next;
4063 tok->next = next;
4064
4065 if (!pasted)
4066 pasted = true;
4067
4068 /* Restart from pasted tokens head */
4069 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004070 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004071
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004072 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004073 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004074
4075 prev_next = &tok->next;
4076
4077 if (tok->next &&
4078 !tok_type_(tok->next, TOK_WHITESPACE) &&
4079 !tok_type_(tok->next, TOK_PASTE))
4080 prev_nonspace = prev_next;
4081
4082 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004083 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004084
4085 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004086}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004087
4088/*
4089 * expands to a list of tokens from %{x:y}
4090 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004091static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004092{
4093 Token *t = tline, **tt, *tm, *head;
4094 char *pos;
4095 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004096
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004097 pos = strchr(tline->text, ':');
4098 nasm_assert(pos);
4099
4100 lst = atoi(pos + 1);
4101 fst = atoi(tline->text + 1);
4102
4103 /*
4104 * only macros params are accounted so
4105 * if someone passes %0 -- we reject such
4106 * value(s)
4107 */
4108 if (lst == 0 || fst == 0)
4109 goto err;
4110
4111 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004112 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4113 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004114 goto err;
4115
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004116 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4117 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004118
4119 /* counted from zero */
4120 fst--, lst--;
4121
4122 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004123 * It will be at least one token. Note we
4124 * need to scan params until separator, otherwise
4125 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004126 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004127 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004128 if (!tm)
4129 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004130 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004131 tt = &head->next, tm = tm->next;
4132 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004133 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004134 *tt = t, tt = &t->next, tm = tm->next;
4135 }
4136
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004137 if (fst < lst) {
4138 for (i = fst + 1; i <= lst; i++) {
4139 t = new_Token(NULL, TOK_OTHER, ",", 0);
4140 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004141 j = (i + mac->rotate) % mac->nparam;
4142 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004143 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 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004147 }
4148 } else {
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;
4153 tm = mac->params[j];
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 }
4160
4161 *last = tt;
4162 return head;
4163
4164err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004165 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004166 &tline->text[1]);
4167 return tline;
4168}
4169
H. Peter Anvin76690a12002-04-30 20:52:49 +00004170/*
4171 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004172 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004173 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004174 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004175static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004176{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004177 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004178 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004179 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004180
4181 tail = &thead;
4182 thead = NULL;
4183
H. Peter Anvine2c80182005-01-15 22:15:51 +00004184 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004185 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004186 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4187 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4188 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004189 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004190 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004191 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004192 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004193 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004194 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004195
H. Peter Anvine2c80182005-01-15 22:15:51 +00004196 t = tline;
4197 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004198
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004199 mac = istk->mstk;
4200 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4201 mac = mac->next_active;
4202 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004203 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204 } else {
4205 pos = strchr(t->text, ':');
4206 if (!pos) {
4207 switch (t->text[1]) {
4208 /*
4209 * We have to make a substitution of one of the
4210 * forms %1, %-1, %+1, %%foo, %0.
4211 */
4212 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004213 type = TOK_NUMBER;
4214 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4215 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004216 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004217 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004218 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004219 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004220 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004221 text = nasm_strcat(tmpbuf, t->text + 2);
4222 break;
4223 case '-':
4224 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004225 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004226 tt = NULL;
4227 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004228 if (mac->nparam > 1)
4229 n = (n + mac->rotate) % mac->nparam;
4230 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004231 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004232 cc = find_cc(tt);
4233 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004234 nasm_nonfatal("macro parameter %d is not a condition code",
4235 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004236 text = NULL;
4237 } else {
4238 type = TOK_ID;
4239 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004240 nasm_nonfatal("condition code `%s' is not invertible",
4241 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004242 text = NULL;
4243 } else
4244 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4245 }
4246 break;
4247 case '+':
4248 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004249 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004250 tt = NULL;
4251 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004252 if (mac->nparam > 1)
4253 n = (n + mac->rotate) % mac->nparam;
4254 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004255 }
4256 cc = find_cc(tt);
4257 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004258 nasm_nonfatal("macro parameter %d is not a condition code",
4259 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004260 text = NULL;
4261 } else {
4262 type = TOK_ID;
4263 text = nasm_strdup(conditions[cc]);
4264 }
4265 break;
4266 default:
4267 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004268 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004269 tt = NULL;
4270 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004271 if (mac->nparam > 1)
4272 n = (n + mac->rotate) % mac->nparam;
4273 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004274 }
4275 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004276 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004277 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004278 tail = &(*tail)->next;
4279 tt = tt->next;
4280 }
4281 }
4282 text = NULL; /* we've done it here */
4283 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004284 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004285 } else {
4286 /*
4287 * seems we have a parameters range here
4288 */
4289 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004290 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004291 if (head != t) {
4292 *tail = head;
4293 *last = tline;
4294 tline = head;
4295 text = NULL;
4296 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004297 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004298 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004299 if (!text) {
4300 delete_Token(t);
4301 } else {
4302 *tail = t;
4303 tail = &t->next;
4304 t->type = type;
4305 nasm_free(t->text);
4306 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004307 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004308 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004309 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004310 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004311 } else if (tline->type == TOK_INDIRECT) {
4312 t = tline;
4313 tline = tline->next;
4314 tt = tokenize(t->text);
4315 tt = expand_mmac_params(tt);
4316 tt = expand_smacro(tt);
4317 *tail = tt;
4318 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004319 tail = &tt->next;
4320 tt = tt->next;
4321 }
4322 delete_Token(t);
4323 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004324 } else {
4325 t = *tail = tline;
4326 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004327 tail = &t->next;
4328 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004329 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004330 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004331
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004332 if (changed) {
4333 const struct tokseq_match t[] = {
4334 {
4335 PP_CONCAT_MASK(TOK_ID) |
4336 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4337 PP_CONCAT_MASK(TOK_ID) |
4338 PP_CONCAT_MASK(TOK_NUMBER) |
4339 PP_CONCAT_MASK(TOK_FLOAT) |
4340 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4341 },
4342 {
4343 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4344 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4345 }
4346 };
4347 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4348 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004349
H. Peter Anvin76690a12002-04-30 20:52:49 +00004350 return thead;
4351}
4352
H. Peter Anvin322bee02019-08-10 01:38:06 -07004353static Token *expand_smacro_noreset(Token * tline);
4354static struct {
4355 int64_t tokens;
4356 int64_t levels;
4357 bool triggered;
4358} smacro_deadman;
4359
H. Peter Anvin76690a12002-04-30 20:52:49 +00004360/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004361 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004362 * a macro at all, it is simply copied to the output and the pointer
4363 * advanced. tpp should be a pointer to a pointer (usually the next
4364 * pointer of the previous token) to the first token. **tpp is updated
4365 * to point to the last token of the expansion, and *tpp updated to
4366 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004367 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004368 * If the expansion is empty, *tpp will be unchanged but **tpp will
4369 * be advanced past the macro call.
4370 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004371 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004372 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004373static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004374{
4375 Token **params = NULL;
4376 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004377 Token *tline = **tpp;
4378 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004379 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004380 unsigned int i;
4381 Token *t, *tup, *ttail;
4382 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004383
4384 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004385 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004386
4387 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004388
H. Peter Anvin322bee02019-08-10 01:38:06 -07004389 smacro_deadman.tokens--;
4390 smacro_deadman.levels--;
4391
4392 if (unlikely(smacro_deadman.tokens < 0 || smacro_deadman.levels < 0)) {
4393 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004394 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004395 smacro_deadman.triggered = true;
4396 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004397 goto not_a_macro;
4398 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004399 head = (SMacro *)hash_findix(&smacros, mname);
4400 } else if (tline->type == TOK_PREPROC_ID) {
4401 Context *ctx = get_ctx(mname, &mname);
4402 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4403 } else {
4404 goto not_a_macro;
4405 }
4406
4407 /*
4408 * We've hit an identifier of some sort. First check whether the
4409 * identifier is a single-line macro at all, then think about
4410 * checking for parameters if necessary.
4411 */
4412 list_for_each(m, head) {
4413 if (!mstrcmp(m->name, mname, m->casesense))
4414 break;
4415 }
4416
4417 if (!m) {
4418 goto not_a_macro;
4419 }
4420
4421 /* Parse parameters, if applicable */
4422
4423 params = NULL;
4424 nparam = 0;
4425
4426 if (m->nparam == 0) {
4427 /*
4428 * Simple case: the macro is parameterless.
4429 * Nothing to parse; the expansion code will
4430 * drop the macro name token.
4431 */
4432 } else {
4433 /*
4434 * Complicated case: at least one macro with this name
4435 * exists and takes parameters. We must find the
4436 * parameters in the call, count them, find the SMacro
4437 * that corresponds to that form of the macro call, and
4438 * substitute for the parameters when we expand. What a
4439 * pain.
4440 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004441 Token **phead, **pep;
4442 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004443 int white = 0;
4444 int brackets = 0;
4445 bool bracketed = false;
4446 bool bad_bracket = false;
4447 unsigned int sparam = PARAM_DELTA;
4448
4449 tline = tline->next;
4450
4451 while (tok_type_(tline, TOK_WHITESPACE)) {
4452 tline = tline->next;
4453 }
4454 if (!tok_is_(tline, "(")) {
4455 /*
4456 * This macro wasn't called with parameters: ignore
4457 * the call. (Behaviour borrowed from gnu cpp.)
4458 */
4459 goto not_a_macro;
4460 }
4461
4462 paren = 1;
4463 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004464 nasm_newn(params, sparam);
4465 phead = pep = &params[0];
4466 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004467
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004468 while (paren) {
4469 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004470 char ch;
4471
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004472 tline = tline->next;
4473
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004474 if (!tline) {
4475 nasm_nonfatal("macro call expects terminating `)'");
4476 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004477 }
4478
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004479 ch = 0;
4480 skip = false;
4481
4482 switch (tline->type) {
4483 case TOK_OTHER:
4484 if (!tline->text[1])
4485 ch = tline->text[0];
4486 break;
4487
4488 case TOK_WHITESPACE:
4489 if (brackets || *phead)
4490 white++; /* Keep interior whitespace */
4491 skip = true;
4492 break;
4493
4494 default:
4495 break;
4496 }
4497
4498 switch (ch) {
4499 case ',':
4500 if (!brackets) {
4501 if (++nparam >= sparam) {
4502 sparam += PARAM_DELTA;
4503 params = nasm_realloc(params, sparam * sizeof *params);
4504 }
4505 pep = &params[nparam];
4506 *pep = NULL;
4507 bracketed = false;
4508 skip = true;
4509 }
4510 break;
4511
4512 case '{':
4513 if (!bracketed) {
4514 bracketed = !*phead;
4515 skip = true;
4516 }
4517 brackets++;
4518 break;
4519
4520 case '}':
4521 if (brackets > 0) {
4522 if (!--brackets)
4523 skip = bracketed;
4524 }
4525 break;
4526
4527 case '(':
4528 if (!brackets)
4529 paren++;
4530 break;
4531
4532 case ')':
4533 if (!brackets) {
4534 paren--;
4535 if (!paren) {
4536 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004537 /* Count the final argument */
4538 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004539 }
4540 }
4541 break;
4542
4543 default:
4544 break; /* Normal token */
4545 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004546
4547 if (!skip) {
4548 Token *t;
4549
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004550 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004551
4552 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004553 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004554 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004555 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004556 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004557 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004558 pep = &t->next;
4559 white = 0;
4560 }
4561 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004562
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004563 /*
4564 * Look for a macro matching in both name and parameter count.
4565 * We already know any matches cannot be anywhere before the
4566 * current position of "m", so there is no reason to
4567 * backtrack.
4568 */
4569 while (1) {
4570 if (!m) {
4571 /*!
4572 *!macro-params-single [on] single-line macro calls with wrong parameter count
4573 *! warns about \i{single-line macros} being invoked
4574 *! with the wrong number of parameters.
4575 */
4576 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4577 "single-line macro `%s' exists, "
4578 "but not taking %d parameter%s",
4579 mname, nparam, (nparam == 1) ? "" : "s");
4580 goto not_a_macro;
4581 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004582
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004583 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4584 break; /* It's good */
4585
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004586 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004587 }
4588 }
4589
4590 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004591 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004592
4593 /* Expand each parameter */
4594 m->in_progress = true;
4595
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004596 if (m->eval_param) {
4597 for (i = 0; i < nparam; i++) {
4598 if (m->eval_param[i]) {
4599 /* Evaluate this parameter as a number */
4600 struct ppscan pps;
4601 struct tokenval tokval;
4602 expr *evalresult;
4603 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004604
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004605 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4606 pps.ntokens = -1;
4607 tokval.t_type = TOKEN_INVALID;
4608 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004609
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004610 free_tlist(eval_param);
4611 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004612
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004613 if (!evalresult) {
4614 /* Nothing meaningful to do */
4615 } else if (tokval.t_type) {
4616 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4617 } else if (!is_simple(evalresult)) {
4618 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4619 } else {
4620 params[i] = make_tok_num(reloc_value(evalresult));
4621 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004622 }
4623 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004624 }
4625
4626 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004627 tline = tline->next; /* Remove the macro call from the input */
4628 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004629
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004630 /* Note: we own the expansion this returns. */
4631 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004632
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004633 tup = NULL;
4634 ttail = NULL; /* Pointer to the last token of the expansion */
4635 while (t) {
4636 enum pp_token_type type = t->type;
4637 Token *tnext = t->next;
4638 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004639
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004640 switch (type) {
4641 case TOK_PREPROC_Q:
4642 case TOK_PREPROC_QQ:
4643 t->type = TOK_ID;
4644 nasm_free(t->text);
4645 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4646 t->len = nasm_last_string_len();
4647 t->next = tline;
4648 break;
4649
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004650 case TOK_ID:
4651 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004652 /*
4653 * Chain this into the target line *before* expanding,
4654 * that way we pick up any arguments to the new macro call,
4655 * if applicable.
4656 */
4657 t->next = tline;
4658 tp = &t;
4659 expand_one_smacro(&tp);
4660 if (t == tline)
4661 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004662 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004663
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004664 default:
4665 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004666 unsigned int param = smac_nparam(t->type);
4667 nasm_assert(!tup && param < nparam);
4668 delete_Token(t);
4669 t = NULL;
4670 tup = tnext;
4671 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004672 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004673 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004674 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004675 }
4676
4677 if (t) {
4678 tline = t;
4679 if (!ttail)
4680 ttail = t;
4681 }
4682
4683 if (tnext) {
4684 t = tnext;
4685 } else {
4686 t = tup;
4687 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004688 }
4689 }
4690
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004691 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004692 if (ttail)
4693 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004694
4695 m->in_progress = false;
4696
4697 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004698 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004699 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004700
4701 /*
4702 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004703 * and then advance to the next input token. Note that this is
4704 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004705 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004706not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004707 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004708 m = NULL;
4709done:
4710 smacro_deadman.levels++;
4711 if (unlikely(params))
4712 free_tlist_array(params, nparam);
4713 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004714}
4715
4716/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004717 * Expand all single-line macro calls made in the given line.
4718 * Return the expanded version of the line. The original is deemed
4719 * to be destroyed in the process. (In reality we'll just move
4720 * Tokens from input to output a lot of the time, rather than
4721 * actually bothering to destroy and replicate.)
4722 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004723static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004724{
H. Peter Anvin322bee02019-08-10 01:38:06 -07004725 smacro_deadman.tokens = nasm_limit[LIMIT_MACRO_TOKENS];
4726 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4727 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004728 return expand_smacro_noreset(tline);
4729}
4730
4731static Token *expand_smacro_noreset(Token * tline)
4732{
4733 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004734 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004735 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004736
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004737 /*
4738 * Trick: we should avoid changing the start token pointer since it can
4739 * be contained in "next" field of other token. Because of this
4740 * we allocate a copy of first token and work with it; at the end of
4741 * routine we copy it back
4742 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004743 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004744 tline = new_Token(org_tline->next, org_tline->type,
4745 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004746 nasm_free(org_tline->text);
4747 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004748 }
4749
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004750
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004751 /*
4752 * Pretend that we always end up doing expansion on the first pass;
4753 * that way %+ get processed. However, if we process %+ before the
4754 * first pass, we end up with things like MACRO %+ TAIL trying to
4755 * look up the macro "MACROTAIL", which we don't want.
4756 */
4757 expanded = true;
4758 thead = tline;
4759 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004760 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004761 {
4762 PP_CONCAT_MASK(TOK_ID) |
4763 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4764 PP_CONCAT_MASK(TOK_ID) |
4765 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4766 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4767 }
4768 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004769
4770 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004771 while ((t = *tail)) /* main token loop */
4772 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004773
4774 if (!expanded) {
4775 tline = thead;
4776 break; /* Done! */
4777 }
4778
4779 /*
4780 * Now scan the entire line and look for successive TOK_IDs
4781 * that resulted after expansion (they can't be produced by
4782 * tokenize()). The successive TOK_IDs should be concatenated.
4783 * Also we look for %+ tokens and concatenate the tokens
4784 * before and after them (without white spaces in between).
4785 */
4786 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004787
4788 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004789 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004790
H. Peter Anvine2c80182005-01-15 22:15:51 +00004791 if (org_tline) {
4792 if (thead) {
4793 *org_tline = *thead;
4794 /* since we just gave text to org_line, don't free it */
4795 thead->text = NULL;
4796 delete_Token(thead);
4797 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004798 /*
4799 * The expression expanded to empty line;
4800 * we can't return NULL because of the "trick" above.
4801 * Just set the line to a single WHITESPACE token.
4802 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004803 memset(org_tline, 0, sizeof(*org_tline));
4804 org_tline->text = NULL;
4805 org_tline->type = TOK_WHITESPACE;
4806 }
4807 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004808 }
4809
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004810 return thead;
4811}
4812
4813/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004814 * Similar to expand_smacro but used exclusively with macro identifiers
4815 * right before they are fetched in. The reason is that there can be
4816 * identifiers consisting of several subparts. We consider that if there
4817 * are more than one element forming the name, user wants a expansion,
4818 * otherwise it will be left as-is. Example:
4819 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004820 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004821 *
4822 * the identifier %$abc will be left as-is so that the handler for %define
4823 * will suck it and define the corresponding value. Other case:
4824 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004825 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004826 *
4827 * In this case user wants name to be expanded *before* %define starts
4828 * working, so we'll expand %$abc into something (if it has a value;
4829 * otherwise it will be left as-is) then concatenate all successive
4830 * PP_IDs into one.
4831 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004832static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004833{
4834 Token *cur, *oldnext = NULL;
4835
H. Peter Anvin734b1882002-04-30 21:01:08 +00004836 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004837 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004838
4839 cur = tline;
4840 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004841 (cur->next->type == TOK_ID ||
4842 cur->next->type == TOK_PREPROC_ID
4843 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004844 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004845
4846 /* If identifier consists of just one token, don't expand */
4847 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004848 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004849
H. Peter Anvine2c80182005-01-15 22:15:51 +00004850 if (cur) {
4851 oldnext = cur->next; /* Detach the tail past identifier */
4852 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004853 }
4854
H. Peter Anvin734b1882002-04-30 21:01:08 +00004855 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004856
H. Peter Anvine2c80182005-01-15 22:15:51 +00004857 if (cur) {
4858 /* expand_smacro possibly changhed tline; re-scan for EOL */
4859 cur = tline;
4860 while (cur && cur->next)
4861 cur = cur->next;
4862 if (cur)
4863 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004864 }
4865
4866 return tline;
4867}
4868
4869/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004870 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004872 * to check for an initial label - that's taken care of in
4873 * expand_mmacro - but must check numbers of parameters. Guaranteed
4874 * to be called with tline->type == TOK_ID, so the putative macro
4875 * name is easy to find.
4876 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004877static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004878{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004879 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004880 Token **params;
4881 int nparam;
4882
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004883 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004884
4885 /*
4886 * Efficiency: first we see if any macro exists with the given
4887 * name. If not, we can return NULL immediately. _Then_ we
4888 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004889 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004890 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004891 list_for_each(m, head)
4892 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004893 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004894 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004895 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004896
4897 /*
4898 * OK, we have a potential macro. Count and demarcate the
4899 * parameters.
4900 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004901 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004902
4903 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004904 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004905 * structure that handles this number.
4906 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004907 while (m) {
4908 if (m->nparam_min <= nparam
4909 && (m->plus || nparam <= m->nparam_max)) {
4910 /*
4911 * This one is right. Just check if cycle removal
4912 * prohibits us using it before we actually celebrate...
4913 */
4914 if (m->in_progress > m->max_depth) {
4915 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004916 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004917 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004918 }
4919 nasm_free(params);
4920 return NULL;
4921 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004922 /*
4923 * It's right, and we can use it. Add its default
4924 * parameters to the end of our list if necessary.
4925 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004926 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004927 params =
4928 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004929 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004930 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004931 while (nparam < m->nparam_min + m->ndefs) {
4932 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004933 nparam++;
4934 }
4935 }
4936 /*
4937 * If we've gone over the maximum parameter count (and
4938 * we're in Plus mode), ignore parameters beyond
4939 * nparam_max.
4940 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004941 if (m->plus && nparam > m->nparam_max)
4942 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004943 /*
4944 * Then terminate the parameter list, and leave.
4945 */
4946 if (!params) { /* need this special case */
4947 params = nasm_malloc(sizeof(*params));
4948 nparam = 0;
4949 }
4950 params[nparam] = NULL;
4951 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004952 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004953 }
4954 /*
4955 * This one wasn't right: look for the next one with the
4956 * same name.
4957 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004958 list_for_each(m, m->next)
4959 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004960 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004961 }
4962
4963 /*
4964 * After all that, we didn't find one with the right number of
4965 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004966 *!
4967 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4968 *! warns about \i{multi-line macros} being invoked
4969 *! with the wrong number of parameters. See \k{mlmacover} for an
4970 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004971 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004972 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4973 "multi-line macro `%s' exists, but not taking %d parameter%s",
4974 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004975 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004976 return NULL;
4977}
4978
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004979
4980/*
4981 * Save MMacro invocation specific fields in
4982 * preparation for a recursive macro expansion
4983 */
4984static void push_mmacro(MMacro *m)
4985{
4986 MMacroInvocation *i;
4987
4988 i = nasm_malloc(sizeof(MMacroInvocation));
4989 i->prev = m->prev;
4990 i->params = m->params;
4991 i->iline = m->iline;
4992 i->nparam = m->nparam;
4993 i->rotate = m->rotate;
4994 i->paramlen = m->paramlen;
4995 i->unique = m->unique;
4996 i->condcnt = m->condcnt;
4997 m->prev = i;
4998}
4999
5000
5001/*
5002 * Restore MMacro invocation specific fields that were
5003 * saved during a previous recursive macro expansion
5004 */
5005static void pop_mmacro(MMacro *m)
5006{
5007 MMacroInvocation *i;
5008
5009 if (m->prev) {
5010 i = m->prev;
5011 m->prev = i->prev;
5012 m->params = i->params;
5013 m->iline = i->iline;
5014 m->nparam = i->nparam;
5015 m->rotate = i->rotate;
5016 m->paramlen = i->paramlen;
5017 m->unique = i->unique;
5018 m->condcnt = i->condcnt;
5019 nasm_free(i);
5020 }
5021}
5022
5023
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005024/*
5025 * Expand the multi-line macro call made by the given line, if
5026 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005027 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005028 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005029static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005030{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005031 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005032 Token *label = NULL;
5033 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005034 Token **params, *t, *tt;
5035 MMacro *m;
5036 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005037 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005038 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005039
5040 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005041 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005042 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005043 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005044 return 0;
5045 m = is_mmacro(t, &params);
5046 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005047 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005048 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005049 Token *last;
5050 /*
5051 * We have an id which isn't a macro call. We'll assume
5052 * it might be a label; we'll also check to see if a
5053 * colon follows it. Then, if there's another id after
5054 * that lot, we'll check it again for macro-hood.
5055 */
5056 label = last = t;
5057 t = t->next;
5058 if (tok_type_(t, TOK_WHITESPACE))
5059 last = t, t = t->next;
5060 if (tok_is_(t, ":")) {
5061 dont_prepend = 1;
5062 last = t, t = t->next;
5063 if (tok_type_(t, TOK_WHITESPACE))
5064 last = t, t = t->next;
5065 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005066 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
5067 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005068 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005069 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005070 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005071 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005072
5073 /*
5074 * Fix up the parameters: this involves stripping leading and
5075 * trailing whitespace, then stripping braces if they are
5076 * present.
5077 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005078 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005079 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005080
H. Peter Anvine2c80182005-01-15 22:15:51 +00005081 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005082 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005084
H. Peter Anvine2c80182005-01-15 22:15:51 +00005085 t = params[i];
5086 skip_white_(t);
5087 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005088 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005089 params[i] = t;
5090 paramlen[i] = 0;
5091 while (t) {
5092 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5093 break; /* ... because we have hit a comma */
5094 if (comma && t->type == TOK_WHITESPACE
5095 && tok_is_(t->next, ","))
5096 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005097 if (brace && t->type == TOK_OTHER) {
5098 if (t->text[0] == '{')
5099 brace++; /* ... or a nested opening brace */
5100 else if (t->text[0] == '}')
5101 if (!--brace)
5102 break; /* ... or a brace */
5103 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005104 t = t->next;
5105 paramlen[i]++;
5106 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005107 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005108 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005109 }
5110
5111 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005112 * OK, we have a MMacro structure together with a set of
5113 * parameters. We must now go through the expansion and push
5114 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005115 * parameter tokens and macro-local tokens doesn't get done
5116 * until the single-line macro substitution process; this is
5117 * because delaying them allows us to change the semantics
5118 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005119 *
5120 * First, push an end marker on to istk->expansion, mark this
5121 * macro as in progress, and set up its invocation-specific
5122 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005123 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005124 ll = nasm_malloc(sizeof(Line));
5125 ll->next = istk->expansion;
5126 ll->finishes = m;
5127 ll->first = NULL;
5128 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005129
5130 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005131 * Save the previous MMacro expansion in the case of
5132 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005133 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005134 if (m->max_depth && m->in_progress)
5135 push_mmacro(m);
5136
5137 m->in_progress ++;
5138 m->params = params;
5139 m->iline = tline;
5140 m->nparam = nparam;
5141 m->rotate = 0;
5142 m->paramlen = paramlen;
5143 m->unique = unique++;
5144 m->lineno = 0;
5145 m->condcnt = 0;
5146
5147 m->next_active = istk->mstk;
5148 istk->mstk = m;
5149
5150 list_for_each(l, m->expansion) {
5151 Token **tail;
5152
5153 ll = nasm_malloc(sizeof(Line));
5154 ll->finishes = NULL;
5155 ll->next = istk->expansion;
5156 istk->expansion = ll;
5157 tail = &ll->first;
5158
5159 list_for_each(t, l->first) {
5160 Token *x = t;
5161 switch (t->type) {
5162 case TOK_PREPROC_Q:
5163 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005164 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005165 case TOK_PREPROC_QQ:
5166 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
5167 break;
5168 case TOK_PREPROC_ID:
5169 if (t->text[1] == '0' && t->text[2] == '0') {
5170 dont_prepend = -1;
5171 x = label;
5172 if (!x)
5173 continue;
5174 }
5175 /* fall through */
5176 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005177 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005178 break;
5179 }
5180 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005181 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005182 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005183 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005184
5185 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005186 * If we had a label, push it on as the first line of
5187 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005188 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005189 if (label) {
5190 if (dont_prepend < 0)
5191 free_tlist(startline);
5192 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005193 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005194 ll->finishes = NULL;
5195 ll->next = istk->expansion;
5196 istk->expansion = ll;
5197 ll->first = startline;
5198 if (!dont_prepend) {
5199 while (label->next)
5200 label = label->next;
5201 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005202 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005203 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005204 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005205
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005206 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005207
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005208 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005209}
5210
H. Peter Anvin130736c2016-02-17 20:27:41 -08005211/*
5212 * This function adds macro names to error messages, and suppresses
5213 * them if necessary.
5214 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005215static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005216{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005217 /*
5218 * If we're in a dead branch of IF or something like it, ignore the error.
5219 * However, because %else etc are evaluated in the state context
5220 * of the previous branch, errors might get lost:
5221 * %if 0 ... %else trailing garbage ... %endif
5222 * So %else etc should set the ERR_PP_PRECOND flag.
5223 */
5224 if ((severity & ERR_MASK) < ERR_FATAL &&
5225 istk && istk->conds &&
5226 ((severity & ERR_PP_PRECOND) ?
5227 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005228 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005229 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005230
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005231 /* This doesn't make sense with the macro stack unwinding */
5232 if (0) {
5233 MMacro *mmac = NULL;
5234 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005235
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005236 /* get %macro name */
5237 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
5238 mmac = istk->mstk;
5239 /* but %rep blocks should be skipped */
5240 while (mmac && !mmac->name)
5241 mmac = mmac->next_active, delta++;
5242 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005243
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005244 if (mmac) {
5245 char *buf;
5246 nasm_set_verror(real_verror);
5247 buf = nasm_vasprintf(fmt, arg);
5248 nasm_error(severity, "(%s:%"PRId32") %s",
5249 mmac->name, mmac->lineno - delta, buf);
5250 nasm_set_verror(pp_verror);
5251 nasm_free(buf);
5252 return;
5253 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005254 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005255 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005256}
5257
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005258static Token *
5259stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005260{
5261 (void)s;
5262 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005263 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005264
5265 return make_tok_qstr(src_get_fname());
5266}
5267
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005268static Token *
5269stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005270{
5271 (void)s;
5272 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005273 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005274
5275 return make_tok_num(src_get_linnum());
5276}
5277
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005278static Token *
5279stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005280{
5281 (void)s;
5282 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005283 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005284
5285 return make_tok_num(globalbits);
5286}
5287
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005288static Token *
5289stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005290{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005291 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005292 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5293 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5294 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005295
5296 (void)s;
5297 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005298 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005299
5300 switch (globalbits) {
5301 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005302 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005303 break;
5304 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005305 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005306 break;
5307 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005308 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005309 break;
5310 default:
5311 panic();
5312 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005313
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005314 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005315}
5316
H. Peter Anvin8b262472019-02-26 14:00:54 -08005317/* Add magic standard macros */
5318struct magic_macros {
5319 const char *name;
5320 int nparams;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005321 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005322};
5323static const struct magic_macros magic_macros[] =
5324{
5325 { "__FILE__", 0, stdmac_file },
5326 { "__LINE__", 0, stdmac_line },
5327 { "__BITS__", 0, stdmac_bits },
5328 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005329 { NULL, 0, NULL }
5330};
5331
5332static void pp_add_magic_stdmac(void)
5333{
5334 const struct magic_macros *m;
5335 SMacro *s;
5336
5337 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005338 s = define_smacro(NULL, m->name, true, m->nparams, NULL, NULL);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005339 s->expand = m->func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005340 }
5341}
5342
H. Peter Anvin734b1882002-04-30 21:01:08 +00005343static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005344pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005345{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005346 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005347 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005348
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005349 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005350 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005351 nested_mac_count = 0;
5352 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005353 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005354 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005355 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005356 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005357
H. Peter Anvin6686de22019-08-10 05:33:14 -07005358 /* First set up the top level input file */
5359 nasm_new(istk);
5360 istk->fp = nasm_open_read(file, NF_TEXT);
5361 src_set(0, file);
5362 istk->lineinc = 1;
5363 if (!istk->fp)
5364 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5365
5366 strlist_add(deplist, file);
5367
5368 /*
5369 * Set up the stdmac packages as a virtual include file,
5370 * indicated by a null file pointer.
5371 */
5372 nasm_new(inc);
5373 inc->next = istk;
5374 inc->fname = src_set_fname(NULL);
5375 inc->nolist = !list_option('b');
5376 istk = inc;
5377 lfmt->uplevel(LIST_INCLUDE, 0);
5378
H. Peter Anvin8b262472019-02-26 14:00:54 -08005379 pp_add_magic_stdmac();
5380
H. Peter Anvinf7606612016-07-13 14:23:48 -07005381 if (tasm_compatible_mode)
5382 pp_add_stdmac(nasm_stdmac_tasm);
5383
5384 pp_add_stdmac(nasm_stdmac_nasm);
5385 pp_add_stdmac(nasm_stdmac_version);
5386
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005387 if (extrastdmac)
5388 pp_add_stdmac(extrastdmac);
5389
H. Peter Anvinf7606612016-07-13 14:23:48 -07005390 stdmacpos = stdmacros[0];
5391 stdmacnext = &stdmacros[1];
5392
H. Peter Anvind2456592008-06-19 15:04:18 -07005393 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005394
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005395 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005396 * Define the __PASS__ macro. This is defined here unlike all the
5397 * other builtins, because it is special -- it varies between
5398 * passes -- but there is really no particular reason to make it
5399 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005400 *
5401 * 0 = dependencies only
5402 * 1 = preparatory passes
5403 * 2 = final pass
5404 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005405 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005406 switch (mode) {
5407 case PP_NORMAL:
5408 apass = pass_final() ? 2 : 1;
5409 break;
5410 case PP_DEPS:
5411 apass = 0;
5412 break;
5413 case PP_PREPROC:
5414 apass = 3;
5415 break;
5416 default:
5417 panic();
5418 }
5419
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005420 define_smacro(NULL, "__PASS__", true, 0, NULL, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005421}
5422
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005423static void pp_init(void)
5424{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005425}
5426
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005427/*
5428 * Get a line of tokens. If we popped the macro expansion/include stack,
5429 * we return a pointer to the dummy token tok_pop; at that point if
5430 * istk is NULL then we have reached end of input;
5431 */
5432static Token tok_pop; /* Dummy token placeholder */
5433
5434static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005435{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005436 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005437
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005438 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005439 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005440 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005441 * buffer or from the input file.
5442 */
5443 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005444 while (istk->expansion && istk->expansion->finishes) {
5445 Line *l = istk->expansion;
5446 if (!l->finishes->name && l->finishes->in_progress > 1) {
5447 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005448
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005449 /*
5450 * This is a macro-end marker for a macro with no
5451 * name, which means it's not really a macro at all
5452 * but a %rep block, and the `in_progress' field is
5453 * more than 1, meaning that we still need to
5454 * repeat. (1 means the natural last repetition; 0
5455 * means termination by %exitrep.) We have
5456 * therefore expanded up to the %endrep, and must
5457 * push the whole block on to the expansion buffer
5458 * again. We don't bother to remove the macro-end
5459 * marker: we'd only have to generate another one
5460 * if we did.
5461 */
5462 l->finishes->in_progress--;
5463 list_for_each(l, l->finishes->expansion) {
5464 Token *t, *tt, **tail;
5465
5466 ll = nasm_malloc(sizeof(Line));
5467 ll->next = istk->expansion;
5468 ll->finishes = NULL;
5469 ll->first = NULL;
5470 tail = &ll->first;
5471
5472 list_for_each(t, l->first) {
5473 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005474 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005475 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005476 }
5477 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005478 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005479 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005480 } else {
5481 /*
5482 * Check whether a `%rep' was started and not ended
5483 * within this macro expansion. This can happen and
5484 * should be detected. It's a fatal error because
5485 * I'm too confused to work out how to recover
5486 * sensibly from it.
5487 */
5488 if (defining) {
5489 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005490 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005491 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005492 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005493 " expansion of macro `%s'",
5494 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005495 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005496
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005497 /*
5498 * FIXME: investigate the relationship at this point between
5499 * istk->mstk and l->finishes
5500 */
5501 {
5502 MMacro *m = istk->mstk;
5503 istk->mstk = m->next_active;
5504 if (m->name) {
5505 /*
5506 * This was a real macro call, not a %rep, and
5507 * therefore the parameter information needs to
5508 * be freed.
5509 */
5510 if (m->prev) {
5511 pop_mmacro(m);
5512 l->finishes->in_progress --;
5513 } else {
5514 nasm_free(m->params);
5515 free_tlist(m->iline);
5516 nasm_free(m->paramlen);
5517 l->finishes->in_progress = 0;
5518 }
Adam Majer91e72402017-07-25 10:42:01 +02005519 }
5520
5521 /*
5522 * FIXME It is incorrect to always free_mmacro here.
5523 * It leads to usage-after-free.
5524 *
5525 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5526 */
5527#if 0
5528 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005529 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005530#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005531 }
5532 istk->expansion = l->next;
5533 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005534 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005535 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005536 }
5537 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005538 do { /* until we get a line we can use */
5539 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005540
5541 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005542 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005543 int32_t lineno;
5544
H. Peter Anvin6686de22019-08-10 05:33:14 -07005545 if (istk->mstk) {
5546 istk->mstk->lineno++;
5547 if (istk->mstk->fname)
5548 lineno = istk->mstk->lineno + istk->mstk->xline;
5549 else
5550 lineno = 0; /* Defined at init time or builtin */
5551 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005552 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005553 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005554
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005555 tline = l->first;
5556 istk->expansion = l->next;
5557 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005558
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005559 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005560 if (!istk->nolist)
5561 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005562 nasm_free(line);
5563 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005564 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005565 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005566 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005567 } else {
5568 /*
5569 * The current file has ended; work down the istk
5570 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005571 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005572 if (i->fp)
5573 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005574 if (i->conds) {
5575 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005576 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005577 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005578 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005579 if (i->next)
5580 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005581 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005582 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005583 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005584 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005585 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005586 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005587
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005588 /*
5589 * We must expand MMacro parameters and MMacro-local labels
5590 * _before_ we plunge into directive processing, to cope
5591 * with things like `%define something %1' such as STRUC
5592 * uses. Unless we're _defining_ a MMacro, in which case
5593 * those tokens should be left alone to go into the
5594 * definition; and unless we're in a non-emitting
5595 * condition, in which case we don't want to meddle with
5596 * anything.
5597 */
5598 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5599 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005600 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005601 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005602
H. Peter Anvine2c80182005-01-15 22:15:51 +00005603 /*
5604 * Check the line to see if it's a preprocessor directive.
5605 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005606 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5607 if (dtline)
5608 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005609 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005610 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005611 * We're defining a multi-line macro. We emit nothing
5612 * at all, and just
5613 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005614 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005615 Line *l = nasm_malloc(sizeof(Line));
5616 l->next = defining->expansion;
5617 l->first = tline;
5618 l->finishes = NULL;
5619 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005620 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005621 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005622 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005623 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005624 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005625 * directive so we keep our place correctly.
5626 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005627 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005628 } else if (istk->mstk && !istk->mstk->in_progress) {
5629 /*
5630 * We're in a %rep block which has been terminated, so
5631 * we're walking through to the %endrep without
5632 * emitting anything. Emit nothing at all, not even a
5633 * blank line: when we emerge from the %rep block we'll
5634 * give a line-number directive so we keep our place
5635 * correctly.
5636 */
5637 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005638 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005639 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005640 if (!expand_mmacro(tline))
5641 return tline;
5642 }
5643 }
5644}
5645
5646static char *pp_getline(void)
5647{
5648 char *line = NULL;
5649 Token *tline;
5650
5651 real_verror = nasm_set_verror(pp_verror);
5652
5653 while (true) {
5654 tline = pp_tokline();
5655 if (tline == &tok_pop) {
5656 /*
5657 * We popped the macro/include stack. If istk is empty,
5658 * we are at end of input, otherwise just loop back.
5659 */
5660 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005661 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005662 } else {
5663 /*
5664 * De-tokenize the line and emit it.
5665 */
5666 line = detoken(tline, true);
5667 free_tlist(tline);
5668 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005669 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005670 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005671
H. Peter Anvin6686de22019-08-10 05:33:14 -07005672 if (list_option('e') && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005673 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005674 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005675 nasm_free(buf);
5676 }
5677
H. Peter Anvin130736c2016-02-17 20:27:41 -08005678 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005679 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005680}
5681
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005682static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005683{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005684 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005685
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005686 if (defining) {
5687 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005688 nasm_nonfatal("end of file while still defining macro `%s'",
5689 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005690 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005691 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005692 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005693
5694 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005695 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005696 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005697
5698 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005699
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005700 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005701 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005702 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005703 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005704 Include *i = istk;
5705 istk = istk->next;
5706 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005707 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005708 }
5709 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005710 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005711 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005712}
5713
5714static void pp_cleanup_session(void)
5715{
5716 free_llist(predef);
5717 predef = NULL;
5718 delete_Blocks();
5719 freeTokens = NULL;
5720 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005721}
5722
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005723static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005724{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005725 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005726}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005727
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005728static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005729{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005730 Token *inc, *space, *name;
5731 Line *l;
5732
H. Peter Anvin734b1882002-04-30 21:01:08 +00005733 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5734 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5735 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005736
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005737 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005738 l->next = predef;
5739 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005740 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005741 predef = l;
5742}
5743
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005744static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005745{
5746 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005747 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005748 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005749
H. Peter Anvin130736c2016-02-17 20:27:41 -08005750 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005751
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005752 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005753 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5754 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005755 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005756 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005757 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005758 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005759 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005760
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005761 if (space->next->type != TOK_PREPROC_ID &&
5762 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005763 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005764
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005765 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005766 l->next = predef;
5767 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005768 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005769 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005770
5771 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005772}
5773
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005774static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005775{
5776 Token *def, *space;
5777 Line *l;
5778
H. Peter Anvin734b1882002-04-30 21:01:08 +00005779 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5780 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005781 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005782
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005783 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005784 l->next = predef;
5785 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005786 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005787 predef = l;
5788}
5789
H. Peter Anvin05990342018-06-11 13:32:42 -07005790/* Insert an early preprocessor command that doesn't need special handling */
5791static void pp_pre_command(const char *what, char *string)
5792{
5793 char *cmd;
5794 Token *def, *space;
5795 Line *l;
5796
5797 def = tokenize(string);
5798 if (what) {
5799 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5800 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5801 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5802 }
5803
5804 l = nasm_malloc(sizeof(Line));
5805 l->next = predef;
5806 l->first = def;
5807 l->finishes = NULL;
5808 predef = l;
5809}
5810
H. Peter Anvinf7606612016-07-13 14:23:48 -07005811static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005812{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005813 macros_t **mp;
5814
5815 /* Find the end of the list and avoid duplicates */
5816 for (mp = stdmacros; *mp; mp++) {
5817 if (*mp == macros)
5818 return; /* Nothing to do */
5819 }
5820
5821 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5822
5823 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005824}
5825
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005826static void pp_extra_stdmac(macros_t *macros)
5827{
5828 extrastdmac = macros;
5829}
5830
H. Peter Anvin8b262472019-02-26 14:00:54 -08005831static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005832{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005833 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005834 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5835 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5836}
5837
5838static Token *make_tok_qstr(const char *str)
5839{
5840 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005841 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005842 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005843}
5844
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005845static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005846{
5847 if (!m)
5848 return;
5849
5850 /* We need to print the next_active list in reverse order */
5851 pp_list_one_macro(m->next_active, severity);
5852
5853 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005854 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005855 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005856 }
5857}
5858
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005859static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005860{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005861 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005862
H. Peter Anvinddb29062018-12-11 00:06:29 -08005863 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5864 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005865
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005866 if (istk)
5867 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005868
H. Peter Anvinddb29062018-12-11 00:06:29 -08005869 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005870}
5871
H. Peter Anvine7469712016-02-18 02:20:59 -08005872const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005873 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005874 pp_reset,
5875 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005876 pp_cleanup_pass,
5877 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005878 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005879 pp_pre_define,
5880 pp_pre_undefine,
5881 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005882 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005883 pp_include_path,
5884 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005885};