blob: 0b111e2f86b9d1ac999de6c1a64670d2d6cad4eb [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 int lineno, lineinc;
289 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000290};
291
292/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700293 * File real name hash, so we don't have to re-search the include
294 * path for every pass (and potentially more than that if a file
295 * is used more than once.)
296 */
297struct hash_table FileHash;
298
299/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000300 * Conditional assembly: we maintain a separate stack of these for
301 * each level of file inclusion. (The only reason we keep the
302 * stacks separate is to ensure that a stray `%endif' in a file
303 * included from within the true branch of a `%if' won't terminate
304 * it and cause confusion: instead, rightly, it'll cause an error.)
305 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800306struct Cond {
307 Cond *next;
308 int state;
309};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000310enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000311 /*
312 * These states are for use just after %if or %elif: IF_TRUE
313 * means the condition has evaluated to truth so we are
314 * currently emitting, whereas IF_FALSE means we are not
315 * currently emitting but will start doing so if a %else comes
316 * up. In these states, all directives are admissible: %elif,
317 * %else and %endif. (And of course %if.)
318 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800319 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320 /*
321 * These states come up after a %else: ELSE_TRUE means we're
322 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
323 * any %elif or %else will cause an error.
324 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800325 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200327 * These states mean that we're not emitting now, and also that
328 * nothing until %endif will be emitted at all. COND_DONE is
329 * used when we've had our moment of emission
330 * and have now started seeing %elifs. COND_NEVER is used when
331 * the condition construct in question is contained within a
332 * non-emitting branch of a larger condition construct,
333 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800335 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800337#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338
H. Peter Anvin70653092007-10-19 14:42:29 -0700339/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000340 * These defines are used as the possible return values for do_directive
341 */
342#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300343#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000344
Keith Kanios852f1ee2009-07-12 00:19:55 -0500345/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346 * Condition codes. Note that we use c_ prefix not C_ because C_ is
347 * used in nasm.h for the "real" condition codes. At _this_ level,
348 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
349 * ones, so we need a different enum...
350 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700351static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
353 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000354 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700356enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
358 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 -0700359 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
360 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000361};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700362static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
364 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 +0000365 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366};
367
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800368/*
369 * Directive names.
370 */
371/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
372static int is_condition(enum preproc_token arg)
373{
374 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
375}
376
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000377/* For TASM compatibility we need to be able to recognise TASM compatible
378 * conditional compilation directives. Using the NASM pre-processor does
379 * not work, so we look for them specifically from the following list and
380 * then jam in the equivalent NASM directive into the input stream.
381 */
382
H. Peter Anvine2c80182005-01-15 22:15:51 +0000383enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000384 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
385 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
386};
387
H. Peter Anvin476d2862007-10-02 22:04:15 -0700388static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000389 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
390 "ifndef", "include", "local"
391};
392
393static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700394static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000395static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800396static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000397
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000398static Context *cstk;
399static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300400static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300402static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300404static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800406static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700407static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800408static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000409
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000410/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800411 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414
415/*
416 * The current set of single-line macros we have defined.
417 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700418static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419
420/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800421 * The multi-line macro we are currently defining, or the %rep
422 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000423 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800424static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425
Charles Crayned4200be2008-07-12 16:42:33 -0700426static uint64_t nested_mac_count;
427static uint64_t nested_rep_count;
428
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000429/*
430 * The number of macro parameters to allocate space for at a time.
431 */
432#define PARAM_DELTA 16
433
434/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700435 * The standard macro set: defined in macros.c in a set of arrays.
436 * This gives our position in any macro set, while we are processing it.
437 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000438 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700439static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700440static macros_t **stdmacnext;
441static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300442static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000443
444/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000445 * Tokens are allocated in blocks to improve speed
446 */
447#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800448static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000449struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000450 Blocks *next;
451 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000452};
453
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800454static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000455
456/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000457 * Forward declarations.
458 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700459static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000460static Token *expand_mmac_params(Token * tline);
461static Token *expand_smacro(Token * tline);
462static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400463static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800464static Token *make_tok_num(int64_t val);
465static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800466static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800467static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000468static void *new_Block(size_t size);
469static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700470static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700471 const char *text, size_t txtlen);
472static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000473static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000474
475/*
476 * Macros for safe checking of token pointers, avoid *(NULL)
477 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300478#define tok_type_(x,t) ((x) && (x)->type == (t))
479#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
480#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
481#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000482
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400483/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700484 * In-place reverse a list of tokens.
485 */
486static Token *reverse_tokens(Token *t)
487{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800488 Token *prev = NULL;
489 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700490
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800491 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400492 next = t->next;
493 t->next = prev;
494 prev = t;
495 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800496 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700497
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800498 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700499}
500
501/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300502 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000503 * front of them. We do it here because I could not find any other
504 * place to do it for the moment, and it is a hack (ideally it would
505 * be nice to be able to use the NASM pre-processor to do it).
506 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000507static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000508{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000509 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400510 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000511
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400512 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000513
514 /* Binary search for the directive name */
515 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400516 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400517 q = nasm_skip_word(p);
518 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000519 if (len) {
520 oldchar = p[len];
521 p[len] = 0;
522 while (j - i > 1) {
523 k = (j + i) / 2;
524 m = nasm_stricmp(p, tasm_directives[k]);
525 if (m == 0) {
526 /* We have found a directive, so jam a % in front of it
527 * so that NASM will then recognise it as one if it's own.
528 */
529 p[len] = oldchar;
530 len = strlen(p);
531 oldline = line;
532 line = nasm_malloc(len + 2);
533 line[0] = '%';
534 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700535 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300536 * NASM does not recognise IFDIFI, so we convert
537 * it to %if 0. This is not used in NASM
538 * compatible code, but does need to parse for the
539 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700541 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 } else {
543 memcpy(line + 1, p, len + 1);
544 }
545 nasm_free(oldline);
546 return line;
547 } else if (m < 0) {
548 j = k;
549 } else
550 i = k;
551 }
552 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000553 }
554 return line;
555}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000556
H. Peter Anvin76690a12002-04-30 20:52:49 +0000557/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000558 * The pre-preprocessing stage... This function translates line
559 * number indications as they emerge from GNU cpp (`# lineno "file"
560 * flags') into NASM preprocessor line number indications (`%line
561 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000562 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000563static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000564{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000565 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000566 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 if (line[0] == '#' && line[1] == ' ') {
569 oldline = line;
570 fname = oldline + 2;
571 lineno = atoi(fname);
572 fname += strspn(fname, "0123456789 ");
573 if (*fname == '"')
574 fname++;
575 fnlen = strcspn(fname, "\"");
576 line = nasm_malloc(20 + fnlen);
577 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
578 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000579 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000580 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000582 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000583}
584
585/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586 * Free a linked list of tokens.
587 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000589{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400590 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000592}
593
594/*
595 * Free a linked list of lines.
596 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000598{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400599 Line *l, *tmp;
600 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 free_tlist(l->first);
602 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000603 }
604}
605
606/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700607 * Free an array of linked lists of tokens
608 */
609static void free_tlist_array(Token **array, size_t nlists)
610{
611 Token **listp = array;
612
613 while (nlists--)
614 free_tlist(*listp++);
615
616 nasm_free(array);
617}
618
619/*
620 * Duplicate a linked list of tokens.
621 */
622static Token *dup_tlist(const Token *list, Token ***tailp)
623{
624 Token *newlist = NULL;
625 Token **tailpp = &newlist;
626 const Token *t;
627
628 list_for_each(t, list) {
629 Token *nt;
630 *tailpp = nt = dup_Token(NULL, t);
631 tailpp = &nt->next;
632 }
633
634 if (tailp)
635 *tailp = tailpp;
636
637 return newlist;
638}
639
640/*
641 * Duplicate a linked list of tokens in reverse order
642 */
643static Token *dup_tlist_reverse(const Token *list, Token *tail)
644{
645 const Token *t;
646
647 list_for_each(t, list)
648 tail = dup_Token(tail, t);
649
650 return tail;
651}
652
653/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800654 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000655 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800656static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000657{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800658 nasm_free(m->name);
659 free_tlist(m->dlist);
660 nasm_free(m->defaults);
661 free_llist(m->expansion);
662 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000663}
664
665/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700666 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800667 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700668static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800669{
670 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700671 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800672 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700673}
674
675static void clear_smacro(SMacro *s)
676{
677 free_smacro_members(s);
678 /* Wipe everything except the next pointer */
679 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
680}
681
682/*
683 * Free an SMacro
684 */
685static void free_smacro(SMacro *s)
686{
687 free_smacro_members(s);
688 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800689}
690
691/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700692 * Free all currently defined macros, and free the hash tables
693 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700694static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700695{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800696 struct hash_iterator it;
697 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700698
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800699 hash_for_each(smt, it, np) {
700 SMacro *tmp;
701 SMacro *s = np->data;
702 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800703 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700704 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700705 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700706 hash_free(smt);
707}
708
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800709static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700710{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800711 struct hash_iterator it;
712 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700713
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800714 hash_for_each(mmt, it, np) {
715 MMacro *tmp;
716 MMacro *m = np->data;
717 nasm_free((void *)np->key);
718 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800719 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700720 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800721 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700722}
723
724static void free_macros(void)
725{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700726 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800727 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700728}
729
730/*
731 * Initialize the hash tables
732 */
733static void init_macros(void)
734{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700735}
736
737/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000738 * Pop the context stack.
739 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000741{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000742 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000743
744 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700745 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000746 nasm_free(c->name);
747 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000748}
749
H. Peter Anvin072771e2008-05-22 13:17:51 -0700750/*
751 * Search for a key in the hash index; adding it if necessary
752 * (in which case we initialize the data pointer to NULL.)
753 */
754static void **
755hash_findi_add(struct hash_table *hash, const char *str)
756{
757 struct hash_insert hi;
758 void **r;
759 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800760 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700761
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800762 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700763 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300764 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700765
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800766 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
767 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700768 return hash_add(&hi, strx, NULL);
769}
770
771/*
772 * Like hash_findi, but returns the data element rather than a pointer
773 * to it. Used only when not adding a new element, hence no third
774 * argument.
775 */
776static void *
777hash_findix(struct hash_table *hash, const char *str)
778{
779 void **p;
780
781 p = hash_findi(hash, str, NULL);
782 return p ? *p : NULL;
783}
784
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400785/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800786 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400787 * if there no more left -- return NULL
788 */
789static char *line_from_stdmac(void)
790{
791 unsigned char c;
792 const unsigned char *p = stdmacpos;
793 char *line, *q;
794 size_t len = 0;
795
796 if (!stdmacpos)
797 return NULL;
798
799 while ((c = *p++)) {
800 if (c >= 0x80)
801 len += pp_directives_len[c - 0x80] + 1;
802 else
803 len++;
804 }
805
806 line = nasm_malloc(len + 1);
807 q = line;
808 while ((c = *stdmacpos++)) {
809 if (c >= 0x80) {
810 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
811 q += pp_directives_len[c - 0x80];
812 *q++ = ' ';
813 } else {
814 *q++ = c;
815 }
816 }
817 stdmacpos = p;
818 *q = '\0';
819
820 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700821 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400822 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700823 if (*stdmacnext) {
824 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400825 } else if (do_predef) {
826 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400827
828 /*
829 * Nasty hack: here we push the contents of
830 * `predef' on to the top-level expansion stack,
831 * since this is the most convenient way to
832 * implement the pre-include and pre-define
833 * features.
834 */
835 list_for_each(pd, predef) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800836 l = nasm_malloc(sizeof(Line));
837 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700838 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800839 l->finishes = NULL;
840
841 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400842 }
843 do_predef = false;
844 }
845 }
846
847 return line;
848}
849
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000850static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000851{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700852 int c;
853 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400854 const unsigned int delta = 512;
855 const unsigned int pad = 8;
856 unsigned int nr_cont = 0;
857 bool cont = false;
858 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700859 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000860
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400861 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400862 p = line_from_stdmac();
863 if (p)
864 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700865
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400866 size = delta;
867 p = buffer = nasm_malloc(size);
868
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700869 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400870 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400871
872 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700873 case EOF:
874 if (p == buffer) {
875 nasm_free(buffer);
876 return NULL;
877 }
878 c = 0;
879 break;
880
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400881 case '\r':
882 next = fgetc(istk->fp);
883 if (next != '\n')
884 ungetc(next, istk->fp);
885 if (cont) {
886 cont = false;
887 continue;
888 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700889 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400890 break;
891
892 case '\n':
893 if (cont) {
894 cont = false;
895 continue;
896 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700897 c = 0;
898 break;
899
900 case 032: /* ^Z = legacy MS-DOS end of file mark */
901 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400902 break;
903
904 case '\\':
905 next = fgetc(istk->fp);
906 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400907 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400908 cont = true;
909 nr_cont++;
910 continue;
911 }
912 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400914
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400915 if (p >= (buffer + size - pad)) {
916 buffer = nasm_realloc(buffer, size + delta);
917 p = buffer + size - pad;
918 size += delta;
919 }
920
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700921 *p++ = c;
922 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000923
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700924 lineno = src_get_linnum() + istk->lineinc +
925 (nr_cont * istk->lineinc);
926 src_set_linnum(lineno);
927 lfmt->line(LIST_READ, lineno, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000928
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 return buffer;
930}
931
932/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000933 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000934 * don't need to parse the value out of e.g. numeric tokens: we
935 * simply split one string into many.
936 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000937static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000938{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700939 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000940 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000941 Token *list = NULL;
942 Token *t, **tail = &list;
943
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700945 char *p = line;
946 char *ep = NULL; /* End of token, for trimming the end */
947
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 if (*p == '%') {
949 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300950 if (*p == '+' && !nasm_isdigit(p[1])) {
951 p++;
952 type = TOK_PASTE;
953 } else if (nasm_isdigit(*p) ||
954 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 do {
956 p++;
957 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700958 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 type = TOK_PREPROC_ID;
960 } else if (*p == '{') {
961 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800962 while (*p) {
963 if (*p == '}')
964 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 p[-1] = *p;
966 p++;
967 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800968 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800969 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700970 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 if (*p)
972 p++;
973 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300974 } else if (*p == '[') {
975 int lvl = 1;
976 line += 2; /* Skip the leading %[ */
977 p++;
978 while (lvl && (c = *p++)) {
979 switch (c) {
980 case ']':
981 lvl--;
982 break;
983 case '%':
984 if (*p == '[')
985 lvl++;
986 break;
987 case '\'':
988 case '\"':
989 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300990 p = nasm_skip_string(p - 1);
991 if (*p)
992 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300993 break;
994 default:
995 break;
996 }
997 }
998 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700999 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001000 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001001 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001002 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001003 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001004 type = TOK_INDIRECT;
1005 } else if (*p == '?') {
1006 type = TOK_PREPROC_Q; /* %? */
1007 p++;
1008 if (*p == '?') {
1009 type = TOK_PREPROC_QQ; /* %?? */
1010 p++;
1011 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001012 } else if (*p == '!') {
1013 type = TOK_PREPROC_ID;
1014 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001015 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001016 do {
1017 p++;
1018 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001019 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001020 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001021 p = nasm_skip_string(p);
1022 if (*p)
1023 p++;
1024 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001025 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001026 } else {
1027 /* %! without string or identifier */
1028 type = TOK_OTHER; /* Legacy behavior... */
1029 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001030 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001032 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 do {
1034 p++;
1035 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001036 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 type = TOK_PREPROC_ID;
1038 } else {
1039 type = TOK_OTHER;
1040 if (*p == '%')
1041 p++;
1042 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001043 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 type = TOK_ID;
1045 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001046 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001048 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 /*
1050 * A string token.
1051 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001053 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 if (*p) {
1056 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001057 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001058 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 /* Handling unterminated strings by UNV */
1060 /* type = -1; */
1061 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001062 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001063 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001064 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001065 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001066 bool is_hex = false;
1067 bool is_float = false;
1068 bool has_e = false;
1069 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001070
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001072 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001074
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001075 if (*p == '$') {
1076 p++;
1077 is_hex = true;
1078 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001079
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001080 for (;;) {
1081 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001082
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001083 if (!is_hex && (c == 'e' || c == 'E')) {
1084 has_e = true;
1085 if (*p == '+' || *p == '-') {
1086 /*
1087 * e can only be followed by +/- if it is either a
1088 * prefixed hex number or a floating-point number
1089 */
1090 p++;
1091 is_float = true;
1092 }
1093 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1094 is_hex = true;
1095 } else if (c == 'P' || c == 'p') {
1096 is_float = true;
1097 if (*p == '+' || *p == '-')
1098 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001099 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001100 ; /* just advance */
1101 else if (c == '.') {
1102 /*
1103 * we need to deal with consequences of the legacy
1104 * parser, like "1.nolist" being two tokens
1105 * (TOK_NUMBER, TOK_ID) here; at least give it
1106 * a shot for now. In the future, we probably need
1107 * a flex-based scanner with proper pattern matching
1108 * to do it as well as it can be done. Nothing in
1109 * the world is going to help the person who wants
1110 * 0x123.p16 interpreted as two tokens, though.
1111 */
1112 r = p;
1113 while (*r == '_')
1114 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001115
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001116 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1117 (!is_hex && (*r == 'e' || *r == 'E')) ||
1118 (*r == 'p' || *r == 'P')) {
1119 p = r;
1120 is_float = true;
1121 } else
1122 break; /* Terminate the token */
1123 } else
1124 break;
1125 }
1126 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001127
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001128 if (p == line+1 && *line == '$') {
1129 type = TOK_OTHER; /* TOKEN_HERE */
1130 } else {
1131 if (has_e && !is_hex) {
1132 /* 1e13 is floating-point, but 1e13h is not */
1133 is_float = true;
1134 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001135
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001136 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1137 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001138 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001140 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 /*
1142 * Whitespace just before end-of-line is discarded by
1143 * pretending it's a comment; whitespace just before a
1144 * comment gets lumped into the comment.
1145 */
1146 if (!*p || *p == ';') {
1147 type = TOK_COMMENT;
1148 while (*p)
1149 p++;
1150 }
1151 } else if (*p == ';') {
1152 type = TOK_COMMENT;
1153 while (*p)
1154 p++;
1155 } else {
1156 /*
1157 * Anything else is an operator of some kind. We check
1158 * for all the double-character operators (>>, <<, //,
1159 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001160 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 */
1162 type = TOK_OTHER;
1163 if ((p[0] == '>' && p[1] == '>') ||
1164 (p[0] == '<' && p[1] == '<') ||
1165 (p[0] == '/' && p[1] == '/') ||
1166 (p[0] == '<' && p[1] == '=') ||
1167 (p[0] == '>' && p[1] == '=') ||
1168 (p[0] == '=' && p[1] == '=') ||
1169 (p[0] == '!' && p[1] == '=') ||
1170 (p[0] == '<' && p[1] == '>') ||
1171 (p[0] == '&' && p[1] == '&') ||
1172 (p[0] == '|' && p[1] == '|') ||
1173 (p[0] == '^' && p[1] == '^')) {
1174 p++;
1175 }
1176 p++;
1177 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001178
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 /* Handling unterminated string by UNV */
1180 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001181 {
1182 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1183 t->text[p-line] = *line;
1184 tail = &t->next;
1185 }
1186 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001188 if (!ep)
1189 ep = p;
1190 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 tail = &t->next;
1192 }
1193 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001194 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001195 return list;
1196}
1197
H. Peter Anvince616072002-04-30 21:02:23 +00001198/*
1199 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001200 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001201 * deleted only all at once by the delete_Blocks function.
1202 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001203static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001204{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001205 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001207 /* first, get to the end of the linked list */
1208 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001210 /* now allocate the requested chunk */
1211 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001213 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001214 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001215 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001216}
1217
1218/*
1219 * this function deletes all managed blocks of memory
1220 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001222{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001224
H. Peter Anvin70653092007-10-19 14:42:29 -07001225 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001226 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001227 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001228 * free it.
1229 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001231 if (b->chunk)
1232 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 a = b;
1234 b = b->next;
1235 if (a != &blocks)
1236 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001237 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001238 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001240
1241/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001242 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001243 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001244 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001245static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001246 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001247{
1248 Token *t;
1249 int i;
1250
H. Peter Anvin89cee572009-07-15 09:16:54 -04001251 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1253 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1254 freeTokens[i].next = &freeTokens[i + 1];
1255 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001256 }
1257 t = freeTokens;
1258 freeTokens = t->next;
1259 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001260 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001261 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001262 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 t->text = NULL;
1264 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001265 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001267 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001268 t->text = nasm_malloc(txtlen+1);
1269 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001271 }
1272 return t;
1273}
1274
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001275static Token *dup_Token(Token *next, const Token *src)
1276{
1277 return new_Token(next, src->type, src->text, src->len);
1278}
1279
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001281{
1282 Token *next = t->next;
1283 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001284 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001285 freeTokens = t;
1286 return next;
1287}
1288
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001289/*
1290 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001291 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1292 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001293 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001294static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001295{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001296 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001297 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001298 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001300 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001301 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001302 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001303 char *v;
1304 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001305
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001306 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001307 if (nasm_isquote(*v))
1308 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001309
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001310 if (v) {
1311 char *p = getenv(v);
1312 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001313 /*!
1314 *!environment [on] nonexistent environment variable
1315 *! warns if a nonexistent environment variable
1316 *! is accessed using the \c{%!} preprocessor
1317 *! construct (see \k{getenv}.) Such environment
1318 *! variables are treated as empty (with this
1319 *! warning issued) starting in NASM 2.15;
1320 *! earlier versions of NASM would treat this as
1321 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001322 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001323 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1324 p = "";
1325 }
1326 t->text = nasm_strdup(p);
1327 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001328 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001329 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001330 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001331
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 /* Expand local macros here and not during preprocessing */
1333 if (expand_locals &&
1334 t->type == TOK_PREPROC_ID && t->text &&
1335 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001336 const char *q;
1337 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001338 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001340 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1341 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 nasm_free(t->text);
1343 t->text = p;
1344 }
1345 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001346 if (t->text) {
1347 if (debug_level(2)) {
1348 unsigned long t_len = t->len;
1349 unsigned long s_len = strlen(t->text);
1350 if (t_len != s_len) {
1351 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1352 t->text, t->type, s_len, t_len);
1353 t->len = s_len;
1354 }
1355 }
1356 len += t->len;
1357 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001359 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001360 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001361
H. Peter Anvin734b1882002-04-30 21:01:08 +00001362 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001363
1364 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001365 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001366 memcpy(p, t->text, t->len);
1367 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001368 } else if (t->type == TOK_WHITESPACE) {
1369 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001371 }
1372 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001373
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001374 return line;
1375}
1376
1377/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001378 * A scanner, suitable for use by the expression evaluator, which
1379 * operates on a line of Tokens. Expects a pointer to a pointer to
1380 * the first token in the line to be passed in as its private_data
1381 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001382 *
1383 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001384 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001385struct ppscan {
1386 Token *tptr;
1387 int ntokens;
1388};
1389
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001391{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001392 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001393 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001394 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001395
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001397 if (pps->ntokens && (tline = pps->tptr)) {
1398 pps->ntokens--;
1399 pps->tptr = tline->next;
1400 } else {
1401 pps->tptr = NULL;
1402 pps->ntokens = 0;
1403 return tokval->t_type = TOKEN_EOS;
1404 }
1405 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001406
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001407 tokval->t_charptr = tline->text;
1408
H. Peter Anvin76690a12002-04-30 20:52:49 +00001409 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001411 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001412 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001413
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001415 p = tokval->t_charptr = tline->text;
1416 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 tokval->t_charptr++;
1418 return tokval->t_type = TOKEN_ID;
1419 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001420
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001421 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001422 if (r >= p+MAX_KEYWORD)
1423 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001424 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001425 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001426 *s = '\0';
1427 /* right, so we have an identifier sitting in temp storage. now,
1428 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001429 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001430 }
1431
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001433 bool rn_error;
1434 tokval->t_integer = readnum(tline->text, &rn_error);
1435 tokval->t_charptr = tline->text;
1436 if (rn_error)
1437 return tokval->t_type = TOKEN_ERRNUM;
1438 else
1439 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001440 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001441
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001442 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001443 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001444 }
1445
H. Peter Anvine2c80182005-01-15 22:15:51 +00001446 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001447 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001448
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001449 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001450 tokval->t_charptr = tline->text;
1451 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001452
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001453 if (ep[0] != bq || ep[1] != '\0')
1454 return tokval->t_type = TOKEN_ERRSTR;
1455 else
1456 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001457 }
1458
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 if (tline->type == TOK_OTHER) {
1460 if (!strcmp(tline->text, "<<"))
1461 return tokval->t_type = TOKEN_SHL;
1462 if (!strcmp(tline->text, ">>"))
1463 return tokval->t_type = TOKEN_SHR;
1464 if (!strcmp(tline->text, "//"))
1465 return tokval->t_type = TOKEN_SDIV;
1466 if (!strcmp(tline->text, "%%"))
1467 return tokval->t_type = TOKEN_SMOD;
1468 if (!strcmp(tline->text, "=="))
1469 return tokval->t_type = TOKEN_EQ;
1470 if (!strcmp(tline->text, "<>"))
1471 return tokval->t_type = TOKEN_NE;
1472 if (!strcmp(tline->text, "!="))
1473 return tokval->t_type = TOKEN_NE;
1474 if (!strcmp(tline->text, "<="))
1475 return tokval->t_type = TOKEN_LE;
1476 if (!strcmp(tline->text, ">="))
1477 return tokval->t_type = TOKEN_GE;
1478 if (!strcmp(tline->text, "&&"))
1479 return tokval->t_type = TOKEN_DBL_AND;
1480 if (!strcmp(tline->text, "^^"))
1481 return tokval->t_type = TOKEN_DBL_XOR;
1482 if (!strcmp(tline->text, "||"))
1483 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001484 }
1485
1486 /*
1487 * We have no other options: just return the first character of
1488 * the token text.
1489 */
1490 return tokval->t_type = tline->text[0];
1491}
1492
1493/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001494 * Compare a string to the name of an existing macro; this is a
1495 * simple wrapper which calls either strcmp or nasm_stricmp
1496 * depending on the value of the `casesense' parameter.
1497 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001498static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001499{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001500 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001501}
1502
1503/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001504 * Compare a string to the name of an existing macro; this is a
1505 * simple wrapper which calls either strcmp or nasm_stricmp
1506 * depending on the value of the `casesense' parameter.
1507 */
1508static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1509{
1510 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1511}
1512
1513/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001514 * Return the Context structure associated with a %$ token. Return
1515 * NULL, having _already_ reported an error condition, if the
1516 * context stack isn't deep enough for the supplied number of $
1517 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001518 *
1519 * If "namep" is non-NULL, set it to the pointer to the macro name
1520 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001521 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001522static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001523{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001524 Context *ctx;
1525 int i;
1526
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001527 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001529
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001530 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001532
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001534 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001536 }
1537
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001538 name += 2;
1539 ctx = cstk;
1540 i = 0;
1541 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001542 name++;
1543 i++;
1544 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001545 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001546 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001547 nasm_nonfatal("`%s': context stack is only"
1548 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001550 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001551
1552 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001553 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001554
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001555 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001556}
1557
1558/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001559 * Open an include file. This routine must always return a valid
1560 * file pointer if it returns - it's responsible for throwing an
1561 * ERR_FATAL and bombing out completely if not. It should also try
1562 * the include path one by one until it finds the file or reaches
1563 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001564 *
1565 * Note: for INC_PROBE the function returns NULL at all times;
1566 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001567 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001568enum incopen_mode {
1569 INC_NEEDED, /* File must exist */
1570 INC_OPTIONAL, /* Missing is OK */
1571 INC_PROBE /* Only an existence probe */
1572};
1573
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001574/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001575static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001576 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001577{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001578 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001579 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001580 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001581 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001582 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001583
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001585 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001586 if (omode == INC_PROBE) {
1587 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001588 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001589 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001590 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001591 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001592 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001593 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001594 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001596 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001597
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001598 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001599
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001600 if (!ip) {
1601 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001602 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001603 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001604
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001605 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001606 ip = ip->next;
1607 }
1608}
1609
1610/*
1611 * Open a file, or test for the presence of one (depending on omode),
1612 * considering the include path.
1613 */
1614static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001615 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001616 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001617 enum incopen_mode omode,
1618 enum file_flags fmode)
1619{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001620 struct hash_insert hi;
1621 void **hp;
1622 char *path;
1623 FILE *fp = NULL;
1624
1625 hp = hash_find(&FileHash, file, &hi);
1626 if (hp) {
1627 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001628 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001629 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001630 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001631 } else {
1632 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001633 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001634
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001635 /* Positive or negative result */
1636 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001637
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001638 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001639 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001640 */
1641 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001642 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001643 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001644
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001645 if (!path) {
1646 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001647 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001648 } else {
1649 if (!fp && omode != INC_PROBE)
1650 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001651 }
1652
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001653 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001654 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001655
1656 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001657}
1658
1659/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001660 * Opens an include or input file. Public version, for use by modules
1661 * that get a file:lineno pair and need to look at the file again
1662 * (e.g. the CodeView debug backend). Returns NULL on failure.
1663 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001664FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001665{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001666 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001667}
1668
1669/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001670 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001671 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001672 * return true if _any_ single-line macro of that name is defined.
1673 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001674 * `nparam' or no parameters is defined.
1675 *
1676 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001677 * defined, or nparam is -1, the address of the definition structure
1678 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001679 * is NULL, no action will be taken regarding its contents, and no
1680 * error will occur.
1681 *
1682 * Note that this is also called with nparam zero to resolve
1683 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001684 *
1685 * If you already know which context macro belongs to, you can pass
1686 * the context pointer as first parameter; if you won't but name begins
1687 * with %$ the context will be automatically computed. If all_contexts
1688 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001689 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001690static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001691smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001692 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001693{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001694 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001695 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696
H. Peter Anvin97a23472007-09-16 17:57:25 -07001697 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001698 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001699 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001700 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001701 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001702 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001703 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001704 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001705 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001706 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001707 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001708 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001709
H. Peter Anvine2c80182005-01-15 22:15:51 +00001710 while (m) {
1711 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001712 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001714 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001715 *defn = m;
1716 else
1717 *defn = NULL;
1718 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001719 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 }
1721 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001722 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001723
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001724 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001725}
1726
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001727/* param should be a natural number [0; INT_MAX] */
1728static int read_param_count(const char *str)
1729{
1730 int result;
1731 bool err;
1732
1733 result = readnum(str, &err);
1734 if (result < 0 || result > INT_MAX) {
1735 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001736 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1737 str, 0, INT_MAX);
1738 } else if (err)
1739 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001740 return result;
1741}
1742
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001743/*
1744 * Count and mark off the parameters in a multi-line macro call.
1745 * This is called both from within the multi-line macro expansion
1746 * code, and also to mark off the default parameters when provided
1747 * in a %macro definition line.
1748 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001750{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001751 int paramsize, brace;
1752
1753 *nparam = paramsize = 0;
1754 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001756 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001757 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 paramsize += PARAM_DELTA;
1759 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1760 }
1761 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001762 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001764 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001766 if (brace) {
1767 while (brace && (t = t->next) != NULL) {
1768 if (tok_is_(t, "{"))
1769 brace++;
1770 else if (tok_is_(t, "}"))
1771 brace--;
1772 }
1773
1774 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 /*
1776 * Now we've found the closing brace, look further
1777 * for the comma.
1778 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001779 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 skip_white_(t);
1781 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001782 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 while (tok_isnt_(t, ","))
1784 t = t->next;
1785 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001787 } else {
1788 while (tok_isnt_(t, ","))
1789 t = t->next;
1790 }
1791 if (t) { /* got a comma/brace */
1792 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001794 }
1795}
1796
1797/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001798 * Determine whether one of the various `if' conditions is true or
1799 * not.
1800 *
1801 * We must free the tline we get passed.
1802 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001803static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001804{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001805 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001806 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001807 Token *t, *tt, *origline;
1808 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001809 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001810 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001811 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001812 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001813
1814 origline = tline;
1815
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001817 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001818 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001819 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001821 if (!tline)
1822 break;
1823 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001824 nasm_nonfatal("`%s' expects context identifiers",
1825 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 free_tlist(origline);
1827 return -1;
1828 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001829 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001830 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 tline = tline->next;
1832 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001833 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001834
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001835 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001836 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001837 while (tline) {
1838 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 if (!tline || (tline->type != TOK_ID &&
1840 (tline->type != TOK_PREPROC_ID ||
1841 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001842 nasm_nonfatal("`%s' expects macro identifiers",
1843 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001844 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001846 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001847 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001849 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001850 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001851
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001852 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001853 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001854 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001855 while (tline) {
1856 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001857 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001858 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001859 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001860 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001861 nasm_nonfatal("`%s' expects environment variable names",
1862 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001863 goto fail;
1864 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001865 p = tline->text;
1866 if (tline->type == TOK_PREPROC_ID)
1867 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001868 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001869 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001870 if (getenv(p))
1871 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001872 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001873 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001874 break;
1875
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001876 case PPC_IFIDN:
1877 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 tline = expand_smacro(tline);
1879 t = tt = tline;
1880 while (tok_isnt_(tt, ","))
1881 tt = tt->next;
1882 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001883 nasm_nonfatal("`%s' expects two comma-separated arguments",
1884 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001885 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 }
1887 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001888 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1890 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001891 nasm_nonfatal("`%s': more than one comma on line",
1892 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001893 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 }
1895 if (t->type == TOK_WHITESPACE) {
1896 t = t->next;
1897 continue;
1898 }
1899 if (tt->type == TOK_WHITESPACE) {
1900 tt = tt->next;
1901 continue;
1902 }
1903 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001904 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 break;
1906 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001907 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001909 size_t l1 = nasm_unquote(t->text, NULL);
1910 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001911
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 if (l1 != l2) {
1913 j = false;
1914 break;
1915 }
1916 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1917 j = false;
1918 break;
1919 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001920 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001921 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 break;
1923 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001924
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 t = t->next;
1926 tt = tt->next;
1927 }
1928 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001929 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001930 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001931
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001932 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001933 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001935 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001936
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 skip_white_(tline);
1938 tline = expand_id(tline);
1939 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001940 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 goto fail;
1942 }
1943 searching.name = nasm_strdup(tline->text);
1944 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001945 searching.plus = false;
1946 searching.nolist = false;
1947 searching.in_progress = 0;
1948 searching.max_depth = 0;
1949 searching.rep_nest = NULL;
1950 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001951 searching.nparam_max = INT_MAX;
1952 tline = expand_smacro(tline->next);
1953 skip_white_(tline);
1954 if (!tline) {
1955 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001956 nasm_nonfatal("`%s' expects a parameter count or nothing",
1957 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 } else {
1959 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001960 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 }
1962 if (tline && tok_is_(tline->next, "-")) {
1963 tline = tline->next->next;
1964 if (tok_is_(tline, "*"))
1965 searching.nparam_max = INT_MAX;
1966 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001967 nasm_nonfatal("`%s' expects a parameter count after `-'",
1968 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001969 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001970 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001971 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001972 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001973 searching.nparam_max = searching.nparam_min;
1974 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001975 }
1976 }
1977 if (tline && tok_is_(tline->next, "+")) {
1978 tline = tline->next;
1979 searching.plus = true;
1980 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001981 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1982 while (mmac) {
1983 if (!strcmp(mmac->name, searching.name) &&
1984 (mmac->nparam_min <= searching.nparam_max
1985 || searching.plus)
1986 && (searching.nparam_min <= mmac->nparam_max
1987 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001988 found = true;
1989 break;
1990 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001991 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001992 }
1993 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001994 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 nasm_free(searching.name);
1996 j = found;
1997 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001998 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001999
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002000 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 needtype = TOK_ID;
2002 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002003 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 needtype = TOK_NUMBER;
2005 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002006 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 needtype = TOK_STRING;
2008 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002009
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010iftype:
2011 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002012
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 while (tok_type_(t, TOK_WHITESPACE) ||
2014 (needtype == TOK_NUMBER &&
2015 tok_type_(t, TOK_OTHER) &&
2016 (t->text[0] == '-' || t->text[0] == '+') &&
2017 !t->text[1]))
2018 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002019
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002020 j = tok_type_(t, needtype);
2021 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002022
2023 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002024 t = tline = expand_smacro(tline);
2025 while (tok_type_(t, TOK_WHITESPACE))
2026 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002027
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002028 j = false;
2029 if (t) {
2030 t = t->next; /* Skip the actual token */
2031 while (tok_type_(t, TOK_WHITESPACE))
2032 t = t->next;
2033 j = !t; /* Should be nothing left */
2034 }
2035 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002036
H. Peter Anvin134b9462008-02-16 17:01:40 -08002037 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002038 t = tline = expand_smacro(tline);
2039 while (tok_type_(t, TOK_WHITESPACE))
2040 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002041
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 j = !t; /* Should be empty */
2043 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002044
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002045 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002046 pps.tptr = tline = expand_smacro(tline);
2047 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002049 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002050 if (!evalresult)
2051 return -1;
2052 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002053 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002054 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002055 nasm_nonfatal("non-constant value given to `%s'",
2056 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002058 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002059 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002060 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002061
H. Peter Anvine2c80182005-01-15 22:15:51 +00002062 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002063 nasm_fatal("preprocessor directive `%s' not yet implemented",
2064 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002065 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002066 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002067
2068 free_tlist(origline);
2069 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002070
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002071fail:
2072 free_tlist(origline);
2073 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002074}
2075
2076/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002077 * Default smacro expansion routine: just returns a copy of the
2078 * expansion list.
2079 */
2080static Token *
2081smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2082{
2083 (void)params;
2084 (void)nparams;
2085
2086 return dup_tlist(s->expansion, NULL);
2087}
2088
2089/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002090 * Emit a macro defintion or undef to the listing file, if
2091 * desired. This is similar to detoken(), but it handles the reverse
2092 * expansion list, does not expand %! or local variable tokens, and
2093 * does some special handling for macro parameters.
2094 */
2095static void
2096list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2097{
2098 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2099 const Token **param_names;
2100 Token *junk_names = NULL;
2101 Token *t;
2102 size_t namelen, size;
2103 unsigned int i;
2104 char *def, *p;
2105 unsigned int context_depth = 0;
2106
2107 nasm_newn(param_names, m->nparam);
2108
2109 namelen = strlen(m->name);
2110 size = namelen + 2; /* Include room for space after name + NUL */
2111
2112 if (ctx) {
2113 context_depth = cstk->depth - ctx->depth + 1;
2114 size += context_depth + 1; /* % + some number of $ */
2115 }
2116
2117 list_for_each(t, m->expansion) {
2118 if (!t->text) {
2119 size++; /* Whitespace, presumably */
2120 } else {
2121 size += t->len;
2122 if (is_smac_param(t->type))
2123 param_names[smac_nparam(t->type)] = t;
2124 }
2125 }
2126
2127 if (m->nparam) {
2128 /*
2129 * Space for ( and either , or ) around each
2130 * parameter, plus an optional =.
2131 */
2132 size += 1 + 2 * m->nparam;
2133 for (i = 0; i < m->nparam; i++) {
2134 if (!param_names[i])
2135 param_names[i] = &unused_arg_name;
2136 size += param_names[i]->len;
2137 }
2138 }
2139
2140 def = nasm_malloc(size);
2141 p = def+size;
2142 *--p = '\0';
2143
2144 list_for_each(t, m->expansion) {
2145 if (!t->text) {
2146 *--p = ' ';
2147 } else {
2148 p -= t->len;
2149 memcpy(p, t->text, t->len);
2150 }
2151 }
2152
2153 *--p = ' ';
2154
2155 if (m->nparam) {
2156 *--p = ')';
2157 for (i = m->nparam; i--;) {
2158 p -= param_names[i]->len;
2159 memcpy(p, param_names[i]->text, param_names[i]->len);
2160 if (m->eval_param && m->eval_param[i])
2161 *--p = '=';
2162 *--p = ',';
2163 }
2164 *p = '('; /* First parameter starts with ( not , */
2165
2166 free_tlist(junk_names);
2167 }
2168
2169 p -= namelen;
2170 memcpy(p, m->name, namelen);
2171
2172 if (context_depth) {
2173 for (i = 0; i < context_depth; i++)
2174 *--p = '$';
2175 *--p = '%';
2176 }
2177
2178 nasm_listmsg("%s %s", pp_directives[op], p);
2179
2180 nasm_free(def);
2181 }
2182
2183/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002184 * Common code for defining an smacro
2185 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002186static SMacro *define_smacro(Context *ctx, const char *mname,
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002187 bool casesense, int nparam,
2188 bool *eval_param, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002189{
2190 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002191 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002192
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002193 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002195 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002196 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002197 /*
2198 * Some instances of the old code considered this a failure,
2199 * some others didn't. What is the right thing to do here?
2200 */
2201 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002202 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002203 } else {
2204 /*
2205 * We're redefining, so we have to take over an
2206 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002207 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002208 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002209 clear_smacro(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002210 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002211 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002212 smtbl = ctx ? &ctx->localmac : &smacros;
2213 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002214 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002215 smac->next = *smhead;
2216 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002217 }
2218 smac->name = nasm_strdup(mname);
2219 smac->casesense = casesense;
2220 smac->nparam = nparam;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002221 smac->eval_param = eval_param;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002222 smac->expansion = expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002223 smac->expand = smacro_expand_default;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002224
2225 if (list_option('m'))
2226 list_smacro_def(casesense ? PP_DEFINE : PP_IDEFINE, ctx, smac);
2227
H. Peter Anvin8b262472019-02-26 14:00:54 -08002228 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002229}
2230
2231/*
2232 * Undefine an smacro
2233 */
2234static void undef_smacro(Context *ctx, const char *mname)
2235{
2236 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002237 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002238
H. Peter Anvin166c2472008-05-28 12:28:58 -07002239 smtbl = ctx ? &ctx->localmac : &smacros;
2240 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002241
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002242 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002243 /*
2244 * We now have a macro name... go hunt for it.
2245 */
2246 sp = smhead;
2247 while ((s = *sp) != NULL) {
2248 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002249 if (list_option('m'))
2250 list_smacro_def(PP_UNDEF, ctx, s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002251 *sp = s->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002252 free_smacro(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002253 } else {
2254 sp = &s->next;
2255 }
2256 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002257 }
2258}
2259
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002260/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002261 * Parse a mmacro specification.
2262 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002263static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002264{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002265 tline = tline->next;
2266 skip_white_(tline);
2267 tline = expand_id(tline);
2268 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002269 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002270 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002271 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002272
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002273 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002274 def->name = nasm_strdup(tline->text);
2275 def->plus = false;
2276 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002277 def->in_progress = 0;
2278 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002279 def->nparam_min = 0;
2280 def->nparam_max = 0;
2281
H. Peter Anvina26433d2008-07-16 14:40:01 -07002282 tline = expand_smacro(tline->next);
2283 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002284 if (!tok_type_(tline, TOK_NUMBER))
2285 nasm_nonfatal("`%s' expects a parameter count", directive);
2286 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002287 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002288 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002289 tline = tline->next->next;
2290 if (tok_is_(tline, "*")) {
2291 def->nparam_max = INT_MAX;
2292 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002293 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002294 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002295 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002296 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002297 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002298 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002299 }
2300 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002301 }
2302 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002303 tline = tline->next;
2304 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002305 }
2306 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002307 !nasm_stricmp(tline->next->text, ".nolist")) {
2308 tline = tline->next;
2309 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002310 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002311
H. Peter Anvina26433d2008-07-16 14:40:01 -07002312 /*
2313 * Handle default parameters.
2314 */
2315 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002316 def->dlist = tline->next;
2317 tline->next = NULL;
2318 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002319 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002320 def->dlist = NULL;
2321 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002322 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002323 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002324
H. Peter Anvin89cee572009-07-15 09:16:54 -04002325 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002326 !def->plus) {
2327 /*
2328 *!macro-defaults [on] macros with more default than optional parameters
2329 *! warns when a macro has more default parameters than optional parameters.
2330 *! See \k{mlmacdef} for why might want to disable this warning.
2331 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002332 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002333 "too many default macro parameters in macro `%s'", def->name);
2334 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002335
H. Peter Anvina26433d2008-07-16 14:40:01 -07002336 return true;
2337}
2338
2339
2340/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002341 * Decode a size directive
2342 */
2343static int parse_size(const char *str) {
2344 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002345 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002346 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002347 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002348 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002349}
2350
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002351/*
2352 * Process a preprocessor %pragma directive. Currently there are none.
2353 * Gets passed the token list starting with the "preproc" token from
2354 * "%pragma preproc".
2355 */
2356static void do_pragma_preproc(Token *tline)
2357{
2358 /* Skip to the real stuff */
2359 tline = tline->next;
2360 skip_white_(tline);
2361 if (!tline)
2362 return;
2363
2364 (void)tline; /* Nothing else to do at present */
2365}
2366
Ed Beroset3ab3f412002-06-11 03:31:49 +00002367/**
2368 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002369 * Find out if a line contains a preprocessor directive, and deal
2370 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002371 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002372 * If a directive _is_ found, it is the responsibility of this routine
2373 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002374 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002375 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002376 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002377 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002378 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002379 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002380static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002381{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002382 enum preproc_token i;
2383 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002384 bool err;
2385 int nparam;
2386 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002387 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002388 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002389 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002390 char *p, *pp;
2391 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002392 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002393 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002394 Include *inc;
2395 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002396 Cond *cond;
2397 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002398 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002399 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002400 struct tokenval tokval;
2401 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002402 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002403 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002404 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002405 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002406 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002407
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002408 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002409 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002410
H. Peter Anvineba20a72002-04-30 20:53:55 +00002411 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002412 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002413 (tline->text[0] && (tline->text[1] == '%' ||
2414 tline->text[1] == '$' ||
2415 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002417
H. Peter Anvin4169a472007-09-12 01:29:43 +00002418 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002419
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002420 /*
2421 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2422 * since they are known to be buggy at moment, we need to fix them
2423 * in future release (2.09-2.10)
2424 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002425 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002426 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2427 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002428 }
2429
2430 /*
2431 * If we're in a non-emitting branch of a condition construct,
2432 * or walking to the end of an already terminated %rep block,
2433 * we should ignore all directives except for condition
2434 * directives.
2435 */
2436 if (((istk->conds && !emitting(istk->conds->state)) ||
2437 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2438 return NO_DIRECTIVE_FOUND;
2439 }
2440
2441 /*
2442 * If we're defining a macro or reading a %rep block, we should
2443 * ignore all directives except for %macro/%imacro (which nest),
2444 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2445 * If we're in a %rep block, another %rep nests, so should be let through.
2446 */
2447 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2448 i != PP_RMACRO && i != PP_IRMACRO &&
2449 i != PP_ENDMACRO && i != PP_ENDM &&
2450 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2451 return NO_DIRECTIVE_FOUND;
2452 }
2453
2454 if (defining) {
2455 if (i == PP_MACRO || i == PP_IMACRO ||
2456 i == PP_RMACRO || i == PP_IRMACRO) {
2457 nested_mac_count++;
2458 return NO_DIRECTIVE_FOUND;
2459 } else if (nested_mac_count > 0) {
2460 if (i == PP_ENDMACRO) {
2461 nested_mac_count--;
2462 return NO_DIRECTIVE_FOUND;
2463 }
2464 }
2465 if (!defining->name) {
2466 if (i == PP_REP) {
2467 nested_rep_count++;
2468 return NO_DIRECTIVE_FOUND;
2469 } else if (nested_rep_count > 0) {
2470 if (i == PP_ENDREP) {
2471 nested_rep_count--;
2472 return NO_DIRECTIVE_FOUND;
2473 }
2474 }
2475 }
2476 }
2477
H. Peter Anvin8b262472019-02-26 14:00:54 -08002478 dname = pp_directives[i]; /* Directive name, for error messages */
2479 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002480 switch (i) {
2481 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002482 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002484
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002485 case PP_PRAGMA:
2486 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002487 * %pragma namespace options...
2488 *
2489 * The namespace "preproc" is reserved for the preprocessor;
2490 * all other namespaces generate a [pragma] assembly directive.
2491 *
2492 * Invalid %pragmas are ignored and may have different
2493 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002494 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002495 tline = tline->next;
2496 skip_white_(tline);
2497 tline = expand_smacro(tline);
2498 if (tok_type_(tline, TOK_ID)) {
2499 if (!nasm_stricmp(tline->text, "preproc")) {
2500 /* Preprocessor pragma */
2501 do_pragma_preproc(tline);
2502 } else {
2503 /* Build the assembler directive */
2504 t = new_Token(NULL, TOK_OTHER, "[", 1);
2505 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2506 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2507 tline = t;
2508 for (t = tline; t->next; t = t->next)
2509 ;
2510 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002511 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002512 }
2513 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002514 free_tlist(origline);
2515 return DIRECTIVE_FOUND;
2516
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 case PP_STACKSIZE:
2518 /* Directive to tell NASM what the default stack size is. The
2519 * default is for a 16-bit stack, and this can be overriden with
2520 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 */
2522 tline = tline->next;
2523 if (tline && tline->type == TOK_WHITESPACE)
2524 tline = tline->next;
2525 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002526 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 free_tlist(origline);
2528 return DIRECTIVE_FOUND;
2529 }
2530 if (nasm_stricmp(tline->text, "flat") == 0) {
2531 /* All subsequent ARG directives are for a 32-bit stack */
2532 StackSize = 4;
2533 StackPointer = "ebp";
2534 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002535 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002536 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2537 /* All subsequent ARG directives are for a 64-bit stack */
2538 StackSize = 8;
2539 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002540 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002541 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 } else if (nasm_stricmp(tline->text, "large") == 0) {
2543 /* All subsequent ARG directives are for a 16-bit stack,
2544 * far function call.
2545 */
2546 StackSize = 2;
2547 StackPointer = "bp";
2548 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002549 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 } else if (nasm_stricmp(tline->text, "small") == 0) {
2551 /* All subsequent ARG directives are for a 16-bit stack,
2552 * far function call. We don't support near functions.
2553 */
2554 StackSize = 2;
2555 StackPointer = "bp";
2556 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002557 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002559 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002560 free_tlist(origline);
2561 return DIRECTIVE_FOUND;
2562 }
2563 free_tlist(origline);
2564 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002565
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 case PP_ARG:
2567 /* TASM like ARG directive to define arguments to functions, in
2568 * the following form:
2569 *
2570 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2571 */
2572 offset = ArgOffset;
2573 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002574 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002576
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 /* Find the argument name */
2578 tline = tline->next;
2579 if (tline && tline->type == TOK_WHITESPACE)
2580 tline = tline->next;
2581 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002582 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002583 free_tlist(origline);
2584 return DIRECTIVE_FOUND;
2585 }
2586 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002587
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 /* Find the argument size type */
2589 tline = tline->next;
2590 if (!tline || tline->type != TOK_OTHER
2591 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002592 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 free_tlist(origline);
2594 return DIRECTIVE_FOUND;
2595 }
2596 tline = tline->next;
2597 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002598 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002599 free_tlist(origline);
2600 return DIRECTIVE_FOUND;
2601 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002602
H. Peter Anvine2c80182005-01-15 22:15:51 +00002603 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002604 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002606 size = parse_size(tt->text);
2607 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002608 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 free_tlist(tt);
2610 free_tlist(origline);
2611 return DIRECTIVE_FOUND;
2612 }
2613 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002614
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 /* Round up to even stack slots */
2616 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002617
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618 /* Now define the macro for the argument */
2619 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2620 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002621 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002622 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002623
H. Peter Anvine2c80182005-01-15 22:15:51 +00002624 /* Move to the next argument in the list */
2625 tline = tline->next;
2626 if (tline && tline->type == TOK_WHITESPACE)
2627 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002628 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002630 free_tlist(origline);
2631 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002632
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 case PP_LOCAL:
2634 /* TASM like LOCAL directive to define local variables for a
2635 * function, in the following form:
2636 *
2637 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2638 *
2639 * The '= LocalSize' at the end is ignored by NASM, but is
2640 * required by TASM to define the local parameter size (and used
2641 * by the TASM macro package).
2642 */
2643 offset = LocalOffset;
2644 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002645 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002647
H. Peter Anvine2c80182005-01-15 22:15:51 +00002648 /* Find the argument name */
2649 tline = tline->next;
2650 if (tline && tline->type == TOK_WHITESPACE)
2651 tline = tline->next;
2652 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002653 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 free_tlist(origline);
2655 return DIRECTIVE_FOUND;
2656 }
2657 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002658
H. Peter Anvine2c80182005-01-15 22:15:51 +00002659 /* Find the argument size type */
2660 tline = tline->next;
2661 if (!tline || tline->type != TOK_OTHER
2662 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002663 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 free_tlist(origline);
2665 return DIRECTIVE_FOUND;
2666 }
2667 tline = tline->next;
2668 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002669 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 free_tlist(origline);
2671 return DIRECTIVE_FOUND;
2672 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002673
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002675 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 size = parse_size(tt->text);
2678 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002679 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 free_tlist(tt);
2681 free_tlist(origline);
2682 return DIRECTIVE_FOUND;
2683 }
2684 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002685
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 /* Round up to even stack slots */
2687 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002688
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002690
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2693 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002694 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002695
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 /* Now define the assign to setup the enter_c macro correctly */
2697 snprintf(directive, sizeof(directive),
2698 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002699 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002700
H. Peter Anvine2c80182005-01-15 22:15:51 +00002701 /* Move to the next argument in the list */
2702 tline = tline->next;
2703 if (tline && tline->type == TOK_WHITESPACE)
2704 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002705 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002706 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 free_tlist(origline);
2708 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002709
H. Peter Anvine2c80182005-01-15 22:15:51 +00002710 case PP_CLEAR:
2711 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002712 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002713 free_macros();
2714 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 free_tlist(origline);
2716 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002717
H. Peter Anvin418ca702008-05-30 10:42:30 -07002718 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002719 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002720 skip_white_(t);
2721 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002722 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002723 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002724 free_tlist(origline);
2725 return DIRECTIVE_FOUND; /* but we did _something_ */
2726 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002727 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002728 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002729 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002730 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002731 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002732 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002734 return DIRECTIVE_FOUND;
2735
2736 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002737 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002738 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002739
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002740 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002742 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 free_tlist(origline);
2744 return DIRECTIVE_FOUND; /* but we did _something_ */
2745 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002746 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002747 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002748 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002749 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002750 nasm_unquote_cstr(p, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002751 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002753 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002754 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002755 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002756 (pp_mode == PP_DEPS)
2757 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002758 if (!inc->fp) {
2759 /* -MG given but file not found */
2760 nasm_free(inc);
2761 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002762 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002763 inc->lineno = src_set_linnum(0);
2764 inc->lineinc = 1;
2765 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002766 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002767 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002768 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002769 }
2770 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002772
H. Peter Anvind2456592008-06-19 15:04:18 -07002773 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002774 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002775 static macros_t *use_pkg;
2776 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002777
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002778 tline = tline->next;
2779 skip_white_(tline);
2780 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002781
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002782 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002783 tline->type != TOK_INTERNAL_STRING &&
2784 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002785 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002786 free_tlist(origline);
2787 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002788 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002789 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002790 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002791 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002792 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002793 use_pkg = nasm_stdmac_find_package(tline->text);
2794 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002795 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002796 else
2797 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002798 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002799 /* Not already included, go ahead and include it */
2800 stdmacpos = use_pkg;
2801 }
2802 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002803 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002804 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002807 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 tline = tline->next;
2809 skip_white_(tline);
2810 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 if (tline) {
2812 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002813 nasm_nonfatal("`%s' expects a context identifier",
2814 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002815 free_tlist(origline);
2816 return DIRECTIVE_FOUND; /* but we did _something_ */
2817 }
2818 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002819 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002820 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002821 p = nasm_strdup(tline->text);
2822 } else {
2823 p = NULL; /* Anonymous */
2824 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002825
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002826 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002827 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002828 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002830 ctx->name = p;
2831 ctx->number = unique++;
2832 cstk = ctx;
2833 } else {
2834 /* %pop or %repl */
2835 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002836 nasm_nonfatal("`%s': context stack is empty",
2837 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002838 } else if (i == PP_POP) {
2839 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002840 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002841 "expected %s",
2842 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002843 else
2844 ctx_pop();
2845 } else {
2846 /* i == PP_REPL */
2847 nasm_free(cstk->name);
2848 cstk->name = p;
2849 p = NULL;
2850 }
2851 nasm_free(p);
2852 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002854 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002855 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002856 severity = ERR_FATAL;
2857 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002858 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002859 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002860 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002861 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002862 /*!
2863 *!user [on] %warning directives
2864 *! controls output of \c{%warning} directives (see \k{pperror}).
2865 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002866 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002868
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002869issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002870 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002871 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002872 tline->next = expand_smacro(tline->next);
2873 tline = tline->next;
2874 skip_white_(tline);
2875 t = tline ? tline->next : NULL;
2876 skip_white_(t);
2877 if (tok_type_(tline, TOK_STRING) && !t) {
2878 /* The line contains only a quoted string */
2879 p = tline->text;
2880 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002881 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002882 } else {
2883 /* Not a quoted string, or more than a quoted string */
2884 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002885 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002886 nasm_free(p);
2887 }
2888 free_tlist(origline);
2889 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002890 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002891
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002892 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002893 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002894 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002895 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002896 j = if_condition(tline->next, i);
2897 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002898 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 cond = nasm_malloc(sizeof(Cond));
2901 cond->next = istk->conds;
2902 cond->state = j;
2903 istk->conds = cond;
2904 if(istk->mstk)
2905 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002906 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002908
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002909 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002910 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002911 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002912 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002913 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002914 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002915 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002916
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002917 case COND_DONE:
2918 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002919 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002920
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002921 case COND_ELSE_TRUE:
2922 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002923 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002924 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002925 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002926 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002927
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002928 case COND_IF_FALSE:
2929 /*
2930 * IMPORTANT: In the case of %if, we will already have
2931 * called expand_mmac_params(); however, if we're
2932 * processing an %elif we must have been in a
2933 * non-emitting mode, which would have inhibited
2934 * the normal invocation of expand_mmac_params().
2935 * Therefore, we have to do it explicitly here.
2936 */
2937 j = if_condition(expand_mmac_params(tline->next), i);
2938 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002939 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002940 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2941 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002943 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002945
H. Peter Anvine2c80182005-01-15 22:15:51 +00002946 case PP_ELSE:
2947 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002948 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002949 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002950 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002951 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002952 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002953 case COND_IF_TRUE:
2954 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002955 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002956 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002957
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002958 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002959 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002960
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002961 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002962 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002963 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002964
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002965 case COND_ELSE_TRUE:
2966 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002967 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002968 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002969 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002970 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002971 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 free_tlist(origline);
2973 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002974
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 case PP_ENDIF:
2976 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002977 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002978 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002979 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002980 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002981 cond = istk->conds;
2982 istk->conds = cond->next;
2983 nasm_free(cond);
2984 if(istk->mstk)
2985 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002986 free_tlist(origline);
2987 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002988
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002989 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002991 casesense = false;
2992 /* fall through */
2993 case PP_RMACRO:
2994 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002995 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002996 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002997 return DIRECTIVE_FOUND;
2998 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002999 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin322bee02019-08-10 01:38:06 -07003000 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003001 defining->casesense = casesense;
3002 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003003 nasm_free(defining);
3004 defining = NULL;
3005 return DIRECTIVE_FOUND;
3006 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003007
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003008 src_get(&defining->xline, &defining->fname);
3009
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003010 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3011 while (mmac) {
3012 if (!strcmp(mmac->name, defining->name) &&
3013 (mmac->nparam_min <= defining->nparam_max
3014 || defining->plus)
3015 && (defining->nparam_min <= mmac->nparam_max
3016 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003017 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003018 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003019 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003020 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003021 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003022 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 free_tlist(origline);
3024 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003025
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 case PP_ENDM:
3027 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003028 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003029 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003030 return DIRECTIVE_FOUND;
3031 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003032 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3033 defining->next = *mmhead;
3034 *mmhead = defining;
3035 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 free_tlist(origline);
3037 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003038
H. Peter Anvin89cee572009-07-15 09:16:54 -04003039 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003040 /*
3041 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003042 * macro-end marker for a macro with a name. Then we
3043 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003044 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003045 list_for_each(l, istk->expansion)
3046 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003047 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003048
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003049 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003050 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003051 * Remove all conditional entries relative to this
3052 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003053 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003054 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3055 cond = istk->conds;
3056 istk->conds = cond->next;
3057 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003058 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003059 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003060 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003061 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003062 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003063 free_tlist(origline);
3064 return DIRECTIVE_FOUND;
3065
H. Peter Anvina26433d2008-07-16 14:40:01 -07003066 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003067 casesense = false;
3068 /* fall through */
3069 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003070 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003071 MMacro **mmac_p;
3072 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003073
H. Peter Anvin8b262472019-02-26 14:00:54 -08003074 spec.casesense = casesense;
3075 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003076 return DIRECTIVE_FOUND;
3077 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003078 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3079 while (mmac_p && *mmac_p) {
3080 mmac = *mmac_p;
3081 if (mmac->casesense == spec.casesense &&
3082 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3083 mmac->nparam_min == spec.nparam_min &&
3084 mmac->nparam_max == spec.nparam_max &&
3085 mmac->plus == spec.plus) {
3086 *mmac_p = mmac->next;
3087 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003088 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003089 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003090 }
3091 }
3092 free_tlist(origline);
3093 free_tlist(spec.dlist);
3094 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003095 }
3096
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 case PP_ROTATE:
3098 if (tline->next && tline->next->type == TOK_WHITESPACE)
3099 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003100 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003102 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003103 return DIRECTIVE_FOUND;
3104 }
3105 t = expand_smacro(tline->next);
3106 tline->next = NULL;
3107 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003108 pps.tptr = tline = t;
3109 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003110 tokval.t_type = TOKEN_INVALID;
3111 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003112 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003113 free_tlist(tline);
3114 if (!evalresult)
3115 return DIRECTIVE_FOUND;
3116 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003117 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003119 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 return DIRECTIVE_FOUND;
3121 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003122 mmac = istk->mstk;
3123 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3124 mmac = mmac->next_active;
3125 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003126 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003127 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003128 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003129 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003130 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003131
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003132 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003133 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003134 rotate += mmac->nparam;
3135
3136 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003137 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003139
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003141 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 do {
3143 tline = tline->next;
3144 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003145
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 if (tok_type_(tline, TOK_ID) &&
3147 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003148 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003149 do {
3150 tline = tline->next;
3151 } while (tok_type_(tline, TOK_WHITESPACE));
3152 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003153
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003155 pps.tptr = expand_smacro(tline);
3156 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003158 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003160 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003161 if (!evalresult) {
3162 free_tlist(origline);
3163 return DIRECTIVE_FOUND;
3164 }
3165 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003166 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003168 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 return DIRECTIVE_FOUND;
3170 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003171 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003172 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003173 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3174 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003175 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003176 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003177 /*!
3178 *!negative-rep [on] regative %rep count
3179 *! warns about negative counts given to the \c{%rep}
3180 *! preprocessor directive.
3181 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003182 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003183 "negative `%%rep' count: %"PRId64, count);
3184 count = 0;
3185 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003186 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003187 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003189 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003190 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 }
3192 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003193
3194 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003195 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003196 defining->nolist = nolist;
3197 defining->in_progress = count;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003198 defining->next_active = istk->mstk;
3199 defining->rep_nest = tmp_defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003200 src_get(&defining->xline, &defining->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003202
H. Peter Anvine2c80182005-01-15 22:15:51 +00003203 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003204 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003205 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003206 return DIRECTIVE_FOUND;
3207 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003208
H. Peter Anvine2c80182005-01-15 22:15:51 +00003209 /*
3210 * Now we have a "macro" defined - although it has no name
3211 * and we won't be entering it in the hash tables - we must
3212 * push a macro-end marker for it on to istk->expansion.
3213 * After that, it will take care of propagating itself (a
3214 * macro-end marker line for a macro which is really a %rep
3215 * block will cause the macro to be re-expanded, complete
3216 * with another macro-end marker to ensure the process
3217 * continues) until the whole expansion is forcibly removed
3218 * from istk->expansion by a %exitrep.
3219 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003220 l = nasm_malloc(sizeof(Line));
3221 l->next = istk->expansion;
3222 l->finishes = defining;
3223 l->first = NULL;
3224 istk->expansion = l;
3225
3226 istk->mstk = defining;
3227
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003228 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003229 tmp_defining = defining;
3230 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 free_tlist(origline);
3232 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003233
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003235 /*
3236 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003237 * macro-end marker for a macro with no name. Then we set
3238 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003239 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003240 list_for_each(l, istk->expansion)
3241 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003242 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003243
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003244 if (l)
3245 l->finishes->in_progress = 1;
3246 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003247 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 free_tlist(origline);
3249 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003250
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003252 case PP_IXDEFINE:
3253 casesense = false;
3254 /* fall through */
3255 case PP_DEFINE:
3256 case PP_XDEFINE:
3257 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003258 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003259 bool *eval_params = NULL;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003260
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 tline = tline->next;
3262 skip_white_(tline);
3263 tline = expand_id(tline);
3264 if (!tline || (tline->type != TOK_ID &&
3265 (tline->type != TOK_PREPROC_ID ||
3266 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003267 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 free_tlist(origline);
3269 return DIRECTIVE_FOUND;
3270 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003271
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003272 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 last = tline;
3274 param_start = tline = tline->next;
3275 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003276
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 if (tok_is_(tline, "(")) {
3278 /*
3279 * This macro has parameters.
3280 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003281
H. Peter Anvine2c80182005-01-15 22:15:51 +00003282 tline = tline->next;
3283 while (1) {
3284 skip_white_(tline);
3285 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003286 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 free_tlist(origline);
3288 return DIRECTIVE_FOUND;
3289 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003290 if (tok_is_(tline, "=")) {
3291 have_eval_params = true;
3292 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003293 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003294 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003295
3296 /*
3297 * "*" means an unnamed, ignored argument; it can never
3298 * match anything because "*" is not TOK_ID, and so
3299 * none of the expansion entries will be converted
3300 * to parameters.
3301 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003302 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003303 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3304 /*
3305 * Empty name; duplicate the termination
3306 * token and use the first copy as a placeholder.
3307 * It will never match anything, since the
3308 * associated text doesn't correspond to a TOK_ID.
3309 */
3310 tline = dup_Token(tline, tline);
3311 } else {
3312 nasm_nonfatal("`%s': parameter identifier expected",
3313 tline->text);
3314 free_tlist(origline);
3315 return DIRECTIVE_FOUND;
3316 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003317 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003318 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 tline = tline->next;
3320 skip_white_(tline);
3321 if (tok_is_(tline, ",")) {
3322 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003323 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003324 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003325 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003326 free_tlist(origline);
3327 return DIRECTIVE_FOUND;
3328 }
3329 break;
3330 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003331 }
3332 last = tline;
3333 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003334
3335 if (have_eval_params) {
3336 /* Create evaluated parameters table */
3337 bool is_eval = false;
3338
3339 nasm_newn(eval_params, nparam);
3340 list_for_each(tt, param_start) {
3341 if (is_smac_param(tt->type))
3342 eval_params[smac_nparam(tt->type)] = is_eval;
3343 is_eval = tok_is_(tt, "=");
3344 }
3345 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003346 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003347
H. Peter Anvine2c80182005-01-15 22:15:51 +00003348 if (tok_type_(tline, TOK_WHITESPACE))
3349 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003350 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003351
3352 /* Expand the macro definition now for %xdefine and %ixdefine */
3353 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3354 tline = expand_smacro(tline);
3355
3356 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 t = tline;
3358 while (t) {
3359 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003360 list_for_each(tt, param_start)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003361 if (is_smac_param(tt->type) &&
3362 tt->len == t->len &&
3363 !memcmp(tt->text, t->text, tt->len))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003364 t->type = tt->type;
3365 }
3366 tt = t->next;
3367 t->next = macro_start;
3368 macro_start = t;
3369 t = tt;
3370 }
3371 /*
3372 * Good. We now have a macro name, a parameter count, and a
3373 * token list (in reverse order) for an expansion. We ought
3374 * to be OK just to create an SMacro, store it, and let
3375 * free_tlist have the rest of the line (which we have
3376 * carefully re-terminated after chopping off the expansion
3377 * from the end).
3378 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003379 define_smacro(ctx, mname, casesense, nparam, eval_params, macro_start);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003380
H. Peter Anvine2c80182005-01-15 22:15:51 +00003381 free_tlist(origline);
3382 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003383 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003384
H. Peter Anvine2c80182005-01-15 22:15:51 +00003385 case PP_UNDEF:
3386 tline = tline->next;
3387 skip_white_(tline);
3388 tline = expand_id(tline);
3389 if (!tline || (tline->type != TOK_ID &&
3390 (tline->type != TOK_PREPROC_ID ||
3391 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003392 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003393 free_tlist(origline);
3394 return DIRECTIVE_FOUND;
3395 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003396 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003397 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003398
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003400 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003401 undef_smacro(ctx, mname);
3402 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003404
H. Peter Anvin9e200162008-06-04 17:23:14 -07003405 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003406 casesense = false;
3407 /* fall through */
3408 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003409 tline = tline->next;
3410 skip_white_(tline);
3411 tline = expand_id(tline);
3412 if (!tline || (tline->type != TOK_ID &&
3413 (tline->type != TOK_PREPROC_ID ||
3414 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003415 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003416 free_tlist(origline);
3417 return DIRECTIVE_FOUND;
3418 }
3419
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003420 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003421 last = tline;
3422 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003423 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003424
3425 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003426 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003427
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003428 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003429 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003431
3432 /*
3433 * We now have a macro name, an implicit parameter count of
3434 * zero, and a string token to use as an expansion. Create
3435 * and store an SMacro.
3436 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003437 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003438 free_tlist(origline);
3439 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003440
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003441 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003442 casesense = false;
3443 /* fall through */
3444 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003445 tline = tline->next;
3446 skip_white_(tline);
3447 tline = expand_id(tline);
3448 if (!tline || (tline->type != TOK_ID &&
3449 (tline->type != TOK_PREPROC_ID ||
3450 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003451 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003452 free_tlist(origline);
3453 return DIRECTIVE_FOUND;
3454 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003455 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003456 last = tline;
3457 tline = expand_smacro(tline->next);
3458 last->next = NULL;
3459
3460 t = tline;
3461 while (tok_type_(t, TOK_WHITESPACE))
3462 t = t->next;
3463 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003464 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003465 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003466 free_tlist(tline);
3467 free_tlist(origline);
3468 return DIRECTIVE_FOUND;
3469 }
3470
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003471 /*
3472 * Convert the string to a token stream. Note that smacros
3473 * are stored with the token stream reversed, so we have to
3474 * reverse the output of tokenize().
3475 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003476 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003477 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003478
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003479 /*
3480 * We now have a macro name, an implicit parameter count of
3481 * zero, and a numeric token to use as an expansion. Create
3482 * and store an SMacro.
3483 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003484 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003485 free_tlist(tline);
3486 free_tlist(origline);
3487 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003488
H. Peter Anvin8b262472019-02-26 14:00:54 -08003489 case PP_IPATHSEARCH:
3490 casesense = false;
3491 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003492 case PP_PATHSEARCH:
3493 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003494 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003495
H. Peter Anvin418ca702008-05-30 10:42:30 -07003496 tline = tline->next;
3497 skip_white_(tline);
3498 tline = expand_id(tline);
3499 if (!tline || (tline->type != TOK_ID &&
3500 (tline->type != TOK_PREPROC_ID ||
3501 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003502 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003503 free_tlist(origline);
3504 return DIRECTIVE_FOUND;
3505 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003506 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003507 last = tline;
3508 tline = expand_smacro(tline->next);
3509 last->next = NULL;
3510
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003511 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003512 while (tok_type_(t, TOK_WHITESPACE))
3513 t = t->next;
3514
3515 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003516 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003517 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003518 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003519 free_tlist(origline);
3520 return DIRECTIVE_FOUND; /* but we did _something_ */
3521 }
3522 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003523 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003524 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003525 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003526 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003527
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003528 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003529 if (!found_path)
3530 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003531 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003532
3533 /*
3534 * We now have a macro name, an implicit parameter count of
3535 * zero, and a string token to use as an expansion. Create
3536 * and store an SMacro.
3537 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003538 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003539 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003540 free_tlist(origline);
3541 return DIRECTIVE_FOUND;
3542 }
3543
H. Peter Anvin8b262472019-02-26 14:00:54 -08003544 case PP_ISTRLEN:
3545 casesense = false;
3546 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003547 case PP_STRLEN:
3548 tline = tline->next;
3549 skip_white_(tline);
3550 tline = expand_id(tline);
3551 if (!tline || (tline->type != TOK_ID &&
3552 (tline->type != TOK_PREPROC_ID ||
3553 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003554 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003555 free_tlist(origline);
3556 return DIRECTIVE_FOUND;
3557 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003558 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 last = tline;
3560 tline = expand_smacro(tline->next);
3561 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003562
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 t = tline;
3564 while (tok_type_(t, TOK_WHITESPACE))
3565 t = t->next;
3566 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003567 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003568 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 free_tlist(tline);
3570 free_tlist(origline);
3571 return DIRECTIVE_FOUND;
3572 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003573
H. Peter Anvin8b262472019-02-26 14:00:54 -08003574 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003575
H. Peter Anvine2c80182005-01-15 22:15:51 +00003576 /*
3577 * We now have a macro name, an implicit parameter count of
3578 * zero, and a numeric token to use as an expansion. Create
3579 * and store an SMacro.
3580 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003581 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003582 free_tlist(tline);
3583 free_tlist(origline);
3584 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003585
H. Peter Anvin8b262472019-02-26 14:00:54 -08003586 case PP_ISTRCAT:
3587 casesense = false;
3588 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003589 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003590 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 Anvinf26e0972008-07-01 21:26:27 -07003597 free_tlist(origline);
3598 return DIRECTIVE_FOUND;
3599 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003600 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003601 last = tline;
3602 tline = expand_smacro(tline->next);
3603 last->next = NULL;
3604
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003605 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003606 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003607 switch (t->type) {
3608 case TOK_WHITESPACE:
3609 break;
3610 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003611 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003612 break;
3613 case TOK_OTHER:
3614 if (!strcmp(t->text, ",")) /* permit comma separators */
3615 break;
3616 /* else fall through */
3617 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003618 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003619 free_tlist(tline);
3620 free_tlist(origline);
3621 return DIRECTIVE_FOUND;
3622 }
3623 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003624
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003625 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003626 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003627 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003628 memcpy(p, t->text, t->len);
3629 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003630 }
3631 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003632
3633 /*
3634 * We now have a macro name, an implicit parameter count of
3635 * zero, and a numeric token to use as an expansion. Create
3636 * and store an SMacro.
3637 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003638 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003639 nasm_free(pp);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003640 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003641 free_tlist(tline);
3642 free_tlist(origline);
3643 return DIRECTIVE_FOUND;
3644
H. Peter Anvin8b262472019-02-26 14:00:54 -08003645 case PP_ISUBSTR:
3646 casesense = false;
3647 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003649 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003650 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003651 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003652
H. Peter Anvine2c80182005-01-15 22:15:51 +00003653 tline = tline->next;
3654 skip_white_(tline);
3655 tline = expand_id(tline);
3656 if (!tline || (tline->type != TOK_ID &&
3657 (tline->type != TOK_PREPROC_ID ||
3658 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003659 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003660 free_tlist(origline);
3661 return DIRECTIVE_FOUND;
3662 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003663 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 last = tline;
3665 tline = expand_smacro(tline->next);
3666 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003667
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003668 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003669 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003670 while (tok_type_(t, TOK_WHITESPACE))
3671 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003672
H. Peter Anvine2c80182005-01-15 22:15:51 +00003673 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003674 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003675 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 free_tlist(tline);
3677 free_tlist(origline);
3678 return DIRECTIVE_FOUND;
3679 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003680
H. Peter Anvin8b262472019-02-26 14:00:54 -08003681 pps.tptr = t->next;
3682 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003684 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 if (!evalresult) {
3686 free_tlist(tline);
3687 free_tlist(origline);
3688 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003689 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003690 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003691 free_tlist(tline);
3692 free_tlist(origline);
3693 return DIRECTIVE_FOUND;
3694 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003695 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003696
H. Peter Anvin8b262472019-02-26 14:00:54 -08003697 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3698 pps.tptr = pps.tptr->next;
3699 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003700 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003701 } else {
3702 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003703 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003704 if (!evalresult) {
3705 free_tlist(tline);
3706 free_tlist(origline);
3707 return DIRECTIVE_FOUND;
3708 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003709 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003710 free_tlist(tline);
3711 free_tlist(origline);
3712 return DIRECTIVE_FOUND;
3713 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003714 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003715 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003716
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003717 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003718
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003719 /* make start and count being in range */
3720 if (start < 0)
3721 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003722 if (count < 0)
3723 count = len + count + 1 - start;
3724 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003725 count = len - start;
3726 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003727 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003728
H. Peter Anvin8b262472019-02-26 14:00:54 -08003729 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003730 macro_start->len = count;
3731 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3732 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003733
H. Peter Anvine2c80182005-01-15 22:15:51 +00003734 /*
3735 * We now have a macro name, an implicit parameter count of
3736 * zero, and a numeric token to use as an expansion. Create
3737 * and store an SMacro.
3738 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003739 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740 free_tlist(tline);
3741 free_tlist(origline);
3742 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003743 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003744
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003746 casesense = false;
3747 /* fall through */
3748 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 tline = tline->next;
3750 skip_white_(tline);
3751 tline = expand_id(tline);
3752 if (!tline || (tline->type != TOK_ID &&
3753 (tline->type != TOK_PREPROC_ID ||
3754 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003755 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003756 free_tlist(origline);
3757 return DIRECTIVE_FOUND;
3758 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003759 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003760 last = tline;
3761 tline = expand_smacro(tline->next);
3762 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003763
H. Peter Anvin8b262472019-02-26 14:00:54 -08003764 pps.tptr = tline;
3765 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003767 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 free_tlist(tline);
3769 if (!evalresult) {
3770 free_tlist(origline);
3771 return DIRECTIVE_FOUND;
3772 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003773
H. Peter Anvine2c80182005-01-15 22:15:51 +00003774 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003775 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003776
H. Peter Anvine2c80182005-01-15 22:15:51 +00003777 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003778 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003779 free_tlist(origline);
3780 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003781 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003782
H. Peter Anvin8b262472019-02-26 14:00:54 -08003783 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003784
H. Peter Anvine2c80182005-01-15 22:15:51 +00003785 /*
3786 * We now have a macro name, an implicit parameter count of
3787 * zero, and a numeric token to use as an expansion. Create
3788 * and store an SMacro.
3789 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003790 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 free_tlist(origline);
3792 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003793
H. Peter Anvine2c80182005-01-15 22:15:51 +00003794 case PP_LINE:
3795 /*
3796 * Syntax is `%line nnn[+mmm] [filename]'
3797 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003798 if (unlikely(pp_noline)) {
3799 free_tlist(origline);
3800 return DIRECTIVE_FOUND;
3801 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003802 tline = tline->next;
3803 skip_white_(tline);
3804 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003805 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003806 free_tlist(origline);
3807 return DIRECTIVE_FOUND;
3808 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003809 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 m = 1;
3811 tline = tline->next;
3812 if (tok_is_(tline, "+")) {
3813 tline = tline->next;
3814 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003815 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003816 free_tlist(origline);
3817 return DIRECTIVE_FOUND;
3818 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003819 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003820 tline = tline->next;
3821 }
3822 skip_white_(tline);
3823 src_set_linnum(k);
3824 istk->lineinc = m;
3825 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003826 char *fname = detoken(tline, false);
3827 src_set_fname(fname);
3828 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003829 }
3830 free_tlist(origline);
3831 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003832
H. Peter Anvine2c80182005-01-15 22:15:51 +00003833 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003834 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003835 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003836 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003837}
3838
3839/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003840 * Ensure that a macro parameter contains a condition code and
3841 * nothing else. Return the condition code index if so, or -1
3842 * otherwise.
3843 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003845{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003846 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003847
H. Peter Anvin25a99342007-09-22 17:45:45 -07003848 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003849 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003850
H. Peter Anvineba20a72002-04-30 20:53:55 +00003851 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003852 if (!t)
3853 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003854 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003855 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003856 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003857 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003858 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003860
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003861 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003862}
3863
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003864/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003865 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003866 * pasting, if @handle_explicit passed then explicit pasting
3867 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003868 * The @m array can contain a series of token types which are
3869 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003870 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003871static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003872 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003873{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003874 Token *tok, *next, **prev_next, **prev_nonspace;
3875 bool pasted = false;
3876 char *buf, *p;
3877 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003878
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003879 /*
3880 * The last token before pasting. We need it
3881 * to be able to connect new handled tokens.
3882 * In other words if there were a tokens stream
3883 *
3884 * A -> B -> C -> D
3885 *
3886 * and we've joined tokens B and C, the resulting
3887 * stream should be
3888 *
3889 * A -> BC -> D
3890 */
3891 tok = *head;
3892 prev_next = NULL;
3893
3894 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3895 prev_nonspace = head;
3896 else
3897 prev_nonspace = NULL;
3898
3899 while (tok && (next = tok->next)) {
3900
3901 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003902 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003903 /* Zap redundant whitespaces */
3904 while (tok_type_(next, TOK_WHITESPACE))
3905 next = delete_Token(next);
3906 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003907 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003908
3909 case TOK_PASTE:
3910 /* Explicit pasting */
3911 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003912 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003913 next = delete_Token(tok);
3914
3915 while (tok_type_(next, TOK_WHITESPACE))
3916 next = delete_Token(next);
3917
3918 if (!pasted)
3919 pasted = true;
3920
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003921 /* Left pasting token is start of line */
3922 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003923 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003924
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003925 /*
3926 * No ending token, this might happen in two
3927 * cases
3928 *
3929 * 1) There indeed no right token at all
3930 * 2) There is a bare "%define ID" statement,
3931 * and @ID does expand to whitespace.
3932 *
3933 * So technically we need to do a grammar analysis
3934 * in another stage of parsing, but for now lets don't
3935 * change the behaviour people used to. Simply allow
3936 * whitespace after paste token.
3937 */
3938 if (!next) {
3939 /*
3940 * Zap ending space tokens and that's all.
3941 */
3942 tok = (*prev_nonspace)->next;
3943 while (tok_type_(tok, TOK_WHITESPACE))
3944 tok = delete_Token(tok);
3945 tok = *prev_nonspace;
3946 tok->next = NULL;
3947 break;
3948 }
3949
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003950 tok = *prev_nonspace;
3951 while (tok_type_(tok, TOK_WHITESPACE))
3952 tok = delete_Token(tok);
3953 len = strlen(tok->text);
3954 len += strlen(next->text);
3955
3956 p = buf = nasm_malloc(len + 1);
3957 strcpy(p, tok->text);
3958 p = strchr(p, '\0');
3959 strcpy(p, next->text);
3960
3961 delete_Token(tok);
3962
3963 tok = tokenize(buf);
3964 nasm_free(buf);
3965
3966 *prev_nonspace = tok;
3967 while (tok && tok->next)
3968 tok = tok->next;
3969
3970 tok->next = delete_Token(next);
3971
3972 /* Restart from pasted tokens head */
3973 tok = *prev_nonspace;
3974 break;
3975
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003976 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003977 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003978 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003979 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3980 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003981
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003982 len = 0;
3983 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3984 len += strlen(next->text);
3985 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003986 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003987
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003988 /* No match or no text to process */
3989 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003990 break;
3991
3992 len += strlen(tok->text);
3993 p = buf = nasm_malloc(len + 1);
3994
Adam Majer1a069432017-07-25 11:12:35 +02003995 strcpy(p, tok->text);
3996 p = strchr(p, '\0');
3997 tok = delete_Token(tok);
3998
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003999 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004000 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4001 strcpy(p, tok->text);
4002 p = strchr(p, '\0');
4003 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004004 tok = delete_Token(tok);
4005 }
4006
4007 tok = tokenize(buf);
4008 nasm_free(buf);
4009
4010 if (prev_next)
4011 *prev_next = tok;
4012 else
4013 *head = tok;
4014
4015 /*
4016 * Connect pasted into original stream,
4017 * ie A -> new-tokens -> B
4018 */
4019 while (tok && tok->next)
4020 tok = tok->next;
4021 tok->next = next;
4022
4023 if (!pasted)
4024 pasted = true;
4025
4026 /* Restart from pasted tokens head */
4027 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004028 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004029
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004030 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004031 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004032
4033 prev_next = &tok->next;
4034
4035 if (tok->next &&
4036 !tok_type_(tok->next, TOK_WHITESPACE) &&
4037 !tok_type_(tok->next, TOK_PASTE))
4038 prev_nonspace = prev_next;
4039
4040 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004041 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004042
4043 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004044}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004045
4046/*
4047 * expands to a list of tokens from %{x:y}
4048 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004049static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004050{
4051 Token *t = tline, **tt, *tm, *head;
4052 char *pos;
4053 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004054
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004055 pos = strchr(tline->text, ':');
4056 nasm_assert(pos);
4057
4058 lst = atoi(pos + 1);
4059 fst = atoi(tline->text + 1);
4060
4061 /*
4062 * only macros params are accounted so
4063 * if someone passes %0 -- we reject such
4064 * value(s)
4065 */
4066 if (lst == 0 || fst == 0)
4067 goto err;
4068
4069 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004070 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4071 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004072 goto err;
4073
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004074 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4075 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004076
4077 /* counted from zero */
4078 fst--, lst--;
4079
4080 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004081 * It will be at least one token. Note we
4082 * need to scan params until separator, otherwise
4083 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004084 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004085 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004086 if (!tm)
4087 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004088 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004089 tt = &head->next, tm = tm->next;
4090 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004091 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004092 *tt = t, tt = &t->next, tm = tm->next;
4093 }
4094
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004095 if (fst < lst) {
4096 for (i = fst + 1; i <= lst; i++) {
4097 t = new_Token(NULL, TOK_OTHER, ",", 0);
4098 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004099 j = (i + mac->rotate) % mac->nparam;
4100 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004101 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004102 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004103 *tt = t, tt = &t->next, tm = tm->next;
4104 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004105 }
4106 } else {
4107 for (i = fst - 1; i >= lst; i--) {
4108 t = new_Token(NULL, TOK_OTHER, ",", 0);
4109 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004110 j = (i + mac->rotate) % mac->nparam;
4111 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004112 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004113 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004114 *tt = t, tt = &t->next, tm = tm->next;
4115 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004116 }
4117 }
4118
4119 *last = tt;
4120 return head;
4121
4122err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004123 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004124 &tline->text[1]);
4125 return tline;
4126}
4127
H. Peter Anvin76690a12002-04-30 20:52:49 +00004128/*
4129 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004130 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004131 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004132 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004134{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004135 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004136 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004137 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004138
4139 tail = &thead;
4140 thead = NULL;
4141
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004143 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004144 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4145 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4146 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004147 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004148 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004149 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004150 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004151 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004152 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004153
H. Peter Anvine2c80182005-01-15 22:15:51 +00004154 t = tline;
4155 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004156
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004157 mac = istk->mstk;
4158 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4159 mac = mac->next_active;
4160 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004161 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004162 } else {
4163 pos = strchr(t->text, ':');
4164 if (!pos) {
4165 switch (t->text[1]) {
4166 /*
4167 * We have to make a substitution of one of the
4168 * forms %1, %-1, %+1, %%foo, %0.
4169 */
4170 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004171 type = TOK_NUMBER;
4172 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4173 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004174 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004175 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004176 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004177 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004178 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004179 text = nasm_strcat(tmpbuf, t->text + 2);
4180 break;
4181 case '-':
4182 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004183 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004184 tt = NULL;
4185 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004186 if (mac->nparam > 1)
4187 n = (n + mac->rotate) % mac->nparam;
4188 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004189 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004190 cc = find_cc(tt);
4191 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004192 nasm_nonfatal("macro parameter %d is not a condition code",
4193 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004194 text = NULL;
4195 } else {
4196 type = TOK_ID;
4197 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004198 nasm_nonfatal("condition code `%s' is not invertible",
4199 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004200 text = NULL;
4201 } else
4202 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4203 }
4204 break;
4205 case '+':
4206 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004207 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004208 tt = NULL;
4209 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004210 if (mac->nparam > 1)
4211 n = (n + mac->rotate) % mac->nparam;
4212 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004213 }
4214 cc = find_cc(tt);
4215 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004216 nasm_nonfatal("macro parameter %d is not a condition code",
4217 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004218 text = NULL;
4219 } else {
4220 type = TOK_ID;
4221 text = nasm_strdup(conditions[cc]);
4222 }
4223 break;
4224 default:
4225 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004226 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004227 tt = NULL;
4228 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004229 if (mac->nparam > 1)
4230 n = (n + mac->rotate) % mac->nparam;
4231 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004232 }
4233 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004234 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004235 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004236 tail = &(*tail)->next;
4237 tt = tt->next;
4238 }
4239 }
4240 text = NULL; /* we've done it here */
4241 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004242 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004243 } else {
4244 /*
4245 * seems we have a parameters range here
4246 */
4247 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004248 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004249 if (head != t) {
4250 *tail = head;
4251 *last = tline;
4252 tline = head;
4253 text = NULL;
4254 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004256 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004257 if (!text) {
4258 delete_Token(t);
4259 } else {
4260 *tail = t;
4261 tail = &t->next;
4262 t->type = type;
4263 nasm_free(t->text);
4264 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004265 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004266 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004267 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004268 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004269 } else if (tline->type == TOK_INDIRECT) {
4270 t = tline;
4271 tline = tline->next;
4272 tt = tokenize(t->text);
4273 tt = expand_mmac_params(tt);
4274 tt = expand_smacro(tt);
4275 *tail = tt;
4276 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004277 tail = &tt->next;
4278 tt = tt->next;
4279 }
4280 delete_Token(t);
4281 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004282 } else {
4283 t = *tail = tline;
4284 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004285 tail = &t->next;
4286 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004287 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004288 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004289
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004290 if (changed) {
4291 const struct tokseq_match t[] = {
4292 {
4293 PP_CONCAT_MASK(TOK_ID) |
4294 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4295 PP_CONCAT_MASK(TOK_ID) |
4296 PP_CONCAT_MASK(TOK_NUMBER) |
4297 PP_CONCAT_MASK(TOK_FLOAT) |
4298 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4299 },
4300 {
4301 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4302 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4303 }
4304 };
4305 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4306 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004307
H. Peter Anvin76690a12002-04-30 20:52:49 +00004308 return thead;
4309}
4310
H. Peter Anvin322bee02019-08-10 01:38:06 -07004311static Token *expand_smacro_noreset(Token * tline);
4312static struct {
4313 int64_t tokens;
4314 int64_t levels;
4315 bool triggered;
4316} smacro_deadman;
4317
H. Peter Anvin76690a12002-04-30 20:52:49 +00004318/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004319 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004320 * a macro at all, it is simply copied to the output and the pointer
4321 * advanced. tpp should be a pointer to a pointer (usually the next
4322 * pointer of the previous token) to the first token. **tpp is updated
4323 * to point to the last token of the expansion, and *tpp updated to
4324 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004325 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004326 * If the expansion is empty, *tpp will be unchanged but **tpp will
4327 * be advanced past the macro call.
4328 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004329 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004330 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004331static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004332{
4333 Token **params = NULL;
4334 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004335 Token *tline = **tpp;
4336 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004337 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004338 unsigned int i;
4339 Token *t, *tup, *ttail;
4340 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004341
4342 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004343 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004344
4345 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004346
H. Peter Anvin322bee02019-08-10 01:38:06 -07004347 smacro_deadman.tokens--;
4348 smacro_deadman.levels--;
4349
4350 if (unlikely(smacro_deadman.tokens < 0 || smacro_deadman.levels < 0)) {
4351 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004352 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004353 smacro_deadman.triggered = true;
4354 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004355 goto not_a_macro;
4356 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004357 head = (SMacro *)hash_findix(&smacros, mname);
4358 } else if (tline->type == TOK_PREPROC_ID) {
4359 Context *ctx = get_ctx(mname, &mname);
4360 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4361 } else {
4362 goto not_a_macro;
4363 }
4364
4365 /*
4366 * We've hit an identifier of some sort. First check whether the
4367 * identifier is a single-line macro at all, then think about
4368 * checking for parameters if necessary.
4369 */
4370 list_for_each(m, head) {
4371 if (!mstrcmp(m->name, mname, m->casesense))
4372 break;
4373 }
4374
4375 if (!m) {
4376 goto not_a_macro;
4377 }
4378
4379 /* Parse parameters, if applicable */
4380
4381 params = NULL;
4382 nparam = 0;
4383
4384 if (m->nparam == 0) {
4385 /*
4386 * Simple case: the macro is parameterless.
4387 * Nothing to parse; the expansion code will
4388 * drop the macro name token.
4389 */
4390 } else {
4391 /*
4392 * Complicated case: at least one macro with this name
4393 * exists and takes parameters. We must find the
4394 * parameters in the call, count them, find the SMacro
4395 * that corresponds to that form of the macro call, and
4396 * substitute for the parameters when we expand. What a
4397 * pain.
4398 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004399 Token **phead, **pep;
4400 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004401 int white = 0;
4402 int brackets = 0;
4403 bool bracketed = false;
4404 bool bad_bracket = false;
4405 unsigned int sparam = PARAM_DELTA;
4406
4407 tline = tline->next;
4408
4409 while (tok_type_(tline, TOK_WHITESPACE)) {
4410 tline = tline->next;
4411 }
4412 if (!tok_is_(tline, "(")) {
4413 /*
4414 * This macro wasn't called with parameters: ignore
4415 * the call. (Behaviour borrowed from gnu cpp.)
4416 */
4417 goto not_a_macro;
4418 }
4419
4420 paren = 1;
4421 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004422 nasm_newn(params, sparam);
4423 phead = pep = &params[0];
4424 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004425
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004426 while (paren) {
4427 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004428 char ch;
4429
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004430 tline = tline->next;
4431
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004432 if (!tline) {
4433 nasm_nonfatal("macro call expects terminating `)'");
4434 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004435 }
4436
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004437 ch = 0;
4438 skip = false;
4439
4440 switch (tline->type) {
4441 case TOK_OTHER:
4442 if (!tline->text[1])
4443 ch = tline->text[0];
4444 break;
4445
4446 case TOK_WHITESPACE:
4447 if (brackets || *phead)
4448 white++; /* Keep interior whitespace */
4449 skip = true;
4450 break;
4451
4452 default:
4453 break;
4454 }
4455
4456 switch (ch) {
4457 case ',':
4458 if (!brackets) {
4459 if (++nparam >= sparam) {
4460 sparam += PARAM_DELTA;
4461 params = nasm_realloc(params, sparam * sizeof *params);
4462 }
4463 pep = &params[nparam];
4464 *pep = NULL;
4465 bracketed = false;
4466 skip = true;
4467 }
4468 break;
4469
4470 case '{':
4471 if (!bracketed) {
4472 bracketed = !*phead;
4473 skip = true;
4474 }
4475 brackets++;
4476 break;
4477
4478 case '}':
4479 if (brackets > 0) {
4480 if (!--brackets)
4481 skip = bracketed;
4482 }
4483 break;
4484
4485 case '(':
4486 if (!brackets)
4487 paren++;
4488 break;
4489
4490 case ')':
4491 if (!brackets) {
4492 paren--;
4493 if (!paren) {
4494 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004495 /* Count the final argument */
4496 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004497 }
4498 }
4499 break;
4500
4501 default:
4502 break; /* Normal token */
4503 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004504
4505 if (!skip) {
4506 Token *t;
4507
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004508 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004509
4510 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004511 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004512 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004513 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004514 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004515 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004516 pep = &t->next;
4517 white = 0;
4518 }
4519 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004520
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004521 /*
4522 * Look for a macro matching in both name and parameter count.
4523 * We already know any matches cannot be anywhere before the
4524 * current position of "m", so there is no reason to
4525 * backtrack.
4526 */
4527 while (1) {
4528 if (!m) {
4529 /*!
4530 *!macro-params-single [on] single-line macro calls with wrong parameter count
4531 *! warns about \i{single-line macros} being invoked
4532 *! with the wrong number of parameters.
4533 */
4534 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4535 "single-line macro `%s' exists, "
4536 "but not taking %d parameter%s",
4537 mname, nparam, (nparam == 1) ? "" : "s");
4538 goto not_a_macro;
4539 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004540
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004541 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4542 break; /* It's good */
4543
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004544 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004545 }
4546 }
4547
4548 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004549 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004550
4551 /* Expand each parameter */
4552 m->in_progress = true;
4553
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004554 if (m->eval_param) {
4555 for (i = 0; i < nparam; i++) {
4556 if (m->eval_param[i]) {
4557 /* Evaluate this parameter as a number */
4558 struct ppscan pps;
4559 struct tokenval tokval;
4560 expr *evalresult;
4561 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004562
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004563 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4564 pps.ntokens = -1;
4565 tokval.t_type = TOKEN_INVALID;
4566 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004567
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004568 free_tlist(eval_param);
4569 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004570
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004571 if (!evalresult) {
4572 /* Nothing meaningful to do */
4573 } else if (tokval.t_type) {
4574 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4575 } else if (!is_simple(evalresult)) {
4576 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4577 } else {
4578 params[i] = make_tok_num(reloc_value(evalresult));
4579 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004580 }
4581 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004582 }
4583
4584 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004585 tline = tline->next; /* Remove the macro call from the input */
4586 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004587
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004588 /* Note: we own the expansion this returns. */
4589 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004590
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004591 tup = NULL;
4592 ttail = NULL; /* Pointer to the last token of the expansion */
4593 while (t) {
4594 enum pp_token_type type = t->type;
4595 Token *tnext = t->next;
4596 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004597
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004598 switch (type) {
4599 case TOK_PREPROC_Q:
4600 case TOK_PREPROC_QQ:
4601 t->type = TOK_ID;
4602 nasm_free(t->text);
4603 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4604 t->len = nasm_last_string_len();
4605 t->next = tline;
4606 break;
4607
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004608 case TOK_ID:
4609 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004610 /*
4611 * Chain this into the target line *before* expanding,
4612 * that way we pick up any arguments to the new macro call,
4613 * if applicable.
4614 */
4615 t->next = tline;
4616 tp = &t;
4617 expand_one_smacro(&tp);
4618 if (t == tline)
4619 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004620 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004621
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004622 default:
4623 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004624 unsigned int param = smac_nparam(t->type);
4625 nasm_assert(!tup && param < nparam);
4626 delete_Token(t);
4627 t = NULL;
4628 tup = tnext;
4629 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004630 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004631 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004632 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004633 }
4634
4635 if (t) {
4636 tline = t;
4637 if (!ttail)
4638 ttail = t;
4639 }
4640
4641 if (tnext) {
4642 t = tnext;
4643 } else {
4644 t = tup;
4645 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004646 }
4647 }
4648
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004649 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004650 if (ttail)
4651 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004652
4653 m->in_progress = false;
4654
4655 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004656 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004657 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004658
4659 /*
4660 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004661 * and then advance to the next input token. Note that this is
4662 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004663 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004664not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004665 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004666 m = NULL;
4667done:
4668 smacro_deadman.levels++;
4669 if (unlikely(params))
4670 free_tlist_array(params, nparam);
4671 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004672}
4673
4674/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004675 * Expand all single-line macro calls made in the given line.
4676 * Return the expanded version of the line. The original is deemed
4677 * to be destroyed in the process. (In reality we'll just move
4678 * Tokens from input to output a lot of the time, rather than
4679 * actually bothering to destroy and replicate.)
4680 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004681static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004682{
H. Peter Anvin322bee02019-08-10 01:38:06 -07004683 smacro_deadman.tokens = nasm_limit[LIMIT_MACRO_TOKENS];
4684 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4685 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004686 return expand_smacro_noreset(tline);
4687}
4688
4689static Token *expand_smacro_noreset(Token * tline)
4690{
4691 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004692 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004693 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004694
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004695 /*
4696 * Trick: we should avoid changing the start token pointer since it can
4697 * be contained in "next" field of other token. Because of this
4698 * we allocate a copy of first token and work with it; at the end of
4699 * routine we copy it back
4700 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004701 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004702 tline = new_Token(org_tline->next, org_tline->type,
4703 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004704 nasm_free(org_tline->text);
4705 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004706 }
4707
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004708
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004709 /*
4710 * Pretend that we always end up doing expansion on the first pass;
4711 * that way %+ get processed. However, if we process %+ before the
4712 * first pass, we end up with things like MACRO %+ TAIL trying to
4713 * look up the macro "MACROTAIL", which we don't want.
4714 */
4715 expanded = true;
4716 thead = tline;
4717 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004718 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004719 {
4720 PP_CONCAT_MASK(TOK_ID) |
4721 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4722 PP_CONCAT_MASK(TOK_ID) |
4723 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4724 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4725 }
4726 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004727
4728 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004729 while ((t = *tail)) /* main token loop */
4730 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004731
4732 if (!expanded) {
4733 tline = thead;
4734 break; /* Done! */
4735 }
4736
4737 /*
4738 * Now scan the entire line and look for successive TOK_IDs
4739 * that resulted after expansion (they can't be produced by
4740 * tokenize()). The successive TOK_IDs should be concatenated.
4741 * Also we look for %+ tokens and concatenate the tokens
4742 * before and after them (without white spaces in between).
4743 */
4744 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004745
4746 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004747 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004748
H. Peter Anvine2c80182005-01-15 22:15:51 +00004749 if (org_tline) {
4750 if (thead) {
4751 *org_tline = *thead;
4752 /* since we just gave text to org_line, don't free it */
4753 thead->text = NULL;
4754 delete_Token(thead);
4755 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004756 /*
4757 * The expression expanded to empty line;
4758 * we can't return NULL because of the "trick" above.
4759 * Just set the line to a single WHITESPACE token.
4760 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004761 memset(org_tline, 0, sizeof(*org_tline));
4762 org_tline->text = NULL;
4763 org_tline->type = TOK_WHITESPACE;
4764 }
4765 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004766 }
4767
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004768 return thead;
4769}
4770
4771/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004772 * Similar to expand_smacro but used exclusively with macro identifiers
4773 * right before they are fetched in. The reason is that there can be
4774 * identifiers consisting of several subparts. We consider that if there
4775 * are more than one element forming the name, user wants a expansion,
4776 * otherwise it will be left as-is. Example:
4777 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004778 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004779 *
4780 * the identifier %$abc will be left as-is so that the handler for %define
4781 * will suck it and define the corresponding value. Other case:
4782 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004783 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784 *
4785 * In this case user wants name to be expanded *before* %define starts
4786 * working, so we'll expand %$abc into something (if it has a value;
4787 * otherwise it will be left as-is) then concatenate all successive
4788 * PP_IDs into one.
4789 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004790static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004791{
4792 Token *cur, *oldnext = NULL;
4793
H. Peter Anvin734b1882002-04-30 21:01:08 +00004794 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004795 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004796
4797 cur = tline;
4798 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004799 (cur->next->type == TOK_ID ||
4800 cur->next->type == TOK_PREPROC_ID
4801 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004802 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004803
4804 /* If identifier consists of just one token, don't expand */
4805 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004806 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004807
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 if (cur) {
4809 oldnext = cur->next; /* Detach the tail past identifier */
4810 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004811 }
4812
H. Peter Anvin734b1882002-04-30 21:01:08 +00004813 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004814
H. Peter Anvine2c80182005-01-15 22:15:51 +00004815 if (cur) {
4816 /* expand_smacro possibly changhed tline; re-scan for EOL */
4817 cur = tline;
4818 while (cur && cur->next)
4819 cur = cur->next;
4820 if (cur)
4821 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004822 }
4823
4824 return tline;
4825}
4826
4827/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004828 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004829 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004830 * to check for an initial label - that's taken care of in
4831 * expand_mmacro - but must check numbers of parameters. Guaranteed
4832 * to be called with tline->type == TOK_ID, so the putative macro
4833 * name is easy to find.
4834 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004835static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004836{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004837 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004838 Token **params;
4839 int nparam;
4840
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004841 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004842
4843 /*
4844 * Efficiency: first we see if any macro exists with the given
4845 * name. If not, we can return NULL immediately. _Then_ we
4846 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004847 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004848 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004849 list_for_each(m, head)
4850 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004851 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004852 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004853 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854
4855 /*
4856 * OK, we have a potential macro. Count and demarcate the
4857 * parameters.
4858 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004859 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004860
4861 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004862 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004863 * structure that handles this number.
4864 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004865 while (m) {
4866 if (m->nparam_min <= nparam
4867 && (m->plus || nparam <= m->nparam_max)) {
4868 /*
4869 * This one is right. Just check if cycle removal
4870 * prohibits us using it before we actually celebrate...
4871 */
4872 if (m->in_progress > m->max_depth) {
4873 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004874 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004875 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004876 }
4877 nasm_free(params);
4878 return NULL;
4879 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 /*
4881 * It's right, and we can use it. Add its default
4882 * parameters to the end of our list if necessary.
4883 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004884 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004885 params =
4886 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004887 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004888 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004889 while (nparam < m->nparam_min + m->ndefs) {
4890 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004891 nparam++;
4892 }
4893 }
4894 /*
4895 * If we've gone over the maximum parameter count (and
4896 * we're in Plus mode), ignore parameters beyond
4897 * nparam_max.
4898 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004899 if (m->plus && nparam > m->nparam_max)
4900 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004901 /*
4902 * Then terminate the parameter list, and leave.
4903 */
4904 if (!params) { /* need this special case */
4905 params = nasm_malloc(sizeof(*params));
4906 nparam = 0;
4907 }
4908 params[nparam] = NULL;
4909 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004910 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 }
4912 /*
4913 * This one wasn't right: look for the next one with the
4914 * same name.
4915 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004916 list_for_each(m, m->next)
4917 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004918 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004919 }
4920
4921 /*
4922 * After all that, we didn't find one with the right number of
4923 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004924 *!
4925 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4926 *! warns about \i{multi-line macros} being invoked
4927 *! with the wrong number of parameters. See \k{mlmacover} for an
4928 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004929 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004930 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4931 "multi-line macro `%s' exists, but not taking %d parameter%s",
4932 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004933 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004934 return NULL;
4935}
4936
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004937
4938/*
4939 * Save MMacro invocation specific fields in
4940 * preparation for a recursive macro expansion
4941 */
4942static void push_mmacro(MMacro *m)
4943{
4944 MMacroInvocation *i;
4945
4946 i = nasm_malloc(sizeof(MMacroInvocation));
4947 i->prev = m->prev;
4948 i->params = m->params;
4949 i->iline = m->iline;
4950 i->nparam = m->nparam;
4951 i->rotate = m->rotate;
4952 i->paramlen = m->paramlen;
4953 i->unique = m->unique;
4954 i->condcnt = m->condcnt;
4955 m->prev = i;
4956}
4957
4958
4959/*
4960 * Restore MMacro invocation specific fields that were
4961 * saved during a previous recursive macro expansion
4962 */
4963static void pop_mmacro(MMacro *m)
4964{
4965 MMacroInvocation *i;
4966
4967 if (m->prev) {
4968 i = m->prev;
4969 m->prev = i->prev;
4970 m->params = i->params;
4971 m->iline = i->iline;
4972 m->nparam = i->nparam;
4973 m->rotate = i->rotate;
4974 m->paramlen = i->paramlen;
4975 m->unique = i->unique;
4976 m->condcnt = i->condcnt;
4977 nasm_free(i);
4978 }
4979}
4980
4981
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004982/*
4983 * Expand the multi-line macro call made by the given line, if
4984 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004985 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004986 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004987static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004988{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004989 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004990 Token *label = NULL;
4991 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004992 Token **params, *t, *tt;
4993 MMacro *m;
4994 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004995 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004996 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004997
4998 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004999 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005000 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005001 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005002 return 0;
5003 m = is_mmacro(t, &params);
5004 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005005 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005006 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005007 Token *last;
5008 /*
5009 * We have an id which isn't a macro call. We'll assume
5010 * it might be a label; we'll also check to see if a
5011 * colon follows it. Then, if there's another id after
5012 * that lot, we'll check it again for macro-hood.
5013 */
5014 label = last = t;
5015 t = t->next;
5016 if (tok_type_(t, TOK_WHITESPACE))
5017 last = t, t = t->next;
5018 if (tok_is_(t, ":")) {
5019 dont_prepend = 1;
5020 last = t, t = t->next;
5021 if (tok_type_(t, TOK_WHITESPACE))
5022 last = t, t = t->next;
5023 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005024 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
5025 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005026 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005027 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005028 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005029 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005030
5031 /*
5032 * Fix up the parameters: this involves stripping leading and
5033 * trailing whitespace, then stripping braces if they are
5034 * present.
5035 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005036 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005037 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005038
H. Peter Anvine2c80182005-01-15 22:15:51 +00005039 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005040 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005041 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005042
H. Peter Anvine2c80182005-01-15 22:15:51 +00005043 t = params[i];
5044 skip_white_(t);
5045 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005046 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 params[i] = t;
5048 paramlen[i] = 0;
5049 while (t) {
5050 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5051 break; /* ... because we have hit a comma */
5052 if (comma && t->type == TOK_WHITESPACE
5053 && tok_is_(t->next, ","))
5054 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005055 if (brace && t->type == TOK_OTHER) {
5056 if (t->text[0] == '{')
5057 brace++; /* ... or a nested opening brace */
5058 else if (t->text[0] == '}')
5059 if (!--brace)
5060 break; /* ... or a brace */
5061 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005062 t = t->next;
5063 paramlen[i]++;
5064 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005065 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005066 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005067 }
5068
5069 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 * OK, we have a MMacro structure together with a set of
5071 * parameters. We must now go through the expansion and push
5072 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005073 * parameter tokens and macro-local tokens doesn't get done
5074 * until the single-line macro substitution process; this is
5075 * because delaying them allows us to change the semantics
5076 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005077 *
5078 * First, push an end marker on to istk->expansion, mark this
5079 * macro as in progress, and set up its invocation-specific
5080 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005081 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005082 ll = nasm_malloc(sizeof(Line));
5083 ll->next = istk->expansion;
5084 ll->finishes = m;
5085 ll->first = NULL;
5086 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005087
5088 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005089 * Save the previous MMacro expansion in the case of
5090 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005091 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005092 if (m->max_depth && m->in_progress)
5093 push_mmacro(m);
5094
5095 m->in_progress ++;
5096 m->params = params;
5097 m->iline = tline;
5098 m->nparam = nparam;
5099 m->rotate = 0;
5100 m->paramlen = paramlen;
5101 m->unique = unique++;
5102 m->lineno = 0;
5103 m->condcnt = 0;
5104
5105 m->next_active = istk->mstk;
5106 istk->mstk = m;
5107
5108 list_for_each(l, m->expansion) {
5109 Token **tail;
5110
5111 ll = nasm_malloc(sizeof(Line));
5112 ll->finishes = NULL;
5113 ll->next = istk->expansion;
5114 istk->expansion = ll;
5115 tail = &ll->first;
5116
5117 list_for_each(t, l->first) {
5118 Token *x = t;
5119 switch (t->type) {
5120 case TOK_PREPROC_Q:
5121 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005122 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005123 case TOK_PREPROC_QQ:
5124 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
5125 break;
5126 case TOK_PREPROC_ID:
5127 if (t->text[1] == '0' && t->text[2] == '0') {
5128 dont_prepend = -1;
5129 x = label;
5130 if (!x)
5131 continue;
5132 }
5133 /* fall through */
5134 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005135 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005136 break;
5137 }
5138 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005139 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005140 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005141 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005142
5143 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005144 * If we had a label, push it on as the first line of
5145 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005146 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005147 if (label) {
5148 if (dont_prepend < 0)
5149 free_tlist(startline);
5150 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005151 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005152 ll->finishes = NULL;
5153 ll->next = istk->expansion;
5154 istk->expansion = ll;
5155 ll->first = startline;
5156 if (!dont_prepend) {
5157 while (label->next)
5158 label = label->next;
5159 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005160 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005161 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005162 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005163
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005164 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005165
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005166 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005167}
5168
H. Peter Anvin130736c2016-02-17 20:27:41 -08005169/*
5170 * This function adds macro names to error messages, and suppresses
5171 * them if necessary.
5172 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005173static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005174{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005175 /*
5176 * If we're in a dead branch of IF or something like it, ignore the error.
5177 * However, because %else etc are evaluated in the state context
5178 * of the previous branch, errors might get lost:
5179 * %if 0 ... %else trailing garbage ... %endif
5180 * So %else etc should set the ERR_PP_PRECOND flag.
5181 */
5182 if ((severity & ERR_MASK) < ERR_FATAL &&
5183 istk && istk->conds &&
5184 ((severity & ERR_PP_PRECOND) ?
5185 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005186 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005187 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005188
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005189 /* This doesn't make sense with the macro stack unwinding */
5190 if (0) {
5191 MMacro *mmac = NULL;
5192 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005193
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005194 /* get %macro name */
5195 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
5196 mmac = istk->mstk;
5197 /* but %rep blocks should be skipped */
5198 while (mmac && !mmac->name)
5199 mmac = mmac->next_active, delta++;
5200 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005201
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005202 if (mmac) {
5203 char *buf;
5204 nasm_set_verror(real_verror);
5205 buf = nasm_vasprintf(fmt, arg);
5206 nasm_error(severity, "(%s:%"PRId32") %s",
5207 mmac->name, mmac->lineno - delta, buf);
5208 nasm_set_verror(pp_verror);
5209 nasm_free(buf);
5210 return;
5211 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005212 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005213 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005214}
5215
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005216static Token *
5217stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005218{
5219 (void)s;
5220 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005221 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005222
5223 return make_tok_qstr(src_get_fname());
5224}
5225
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005226static Token *
5227stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005228{
5229 (void)s;
5230 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005231 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005232
5233 return make_tok_num(src_get_linnum());
5234}
5235
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005236static Token *
5237stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005238{
5239 (void)s;
5240 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005241 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005242
5243 return make_tok_num(globalbits);
5244}
5245
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005246static Token *
5247stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005248{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005249 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005250 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5251 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5252 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005253
5254 (void)s;
5255 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005256 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005257
5258 switch (globalbits) {
5259 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005260 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005261 break;
5262 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005263 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005264 break;
5265 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005266 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005267 break;
5268 default:
5269 panic();
5270 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005271
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005272 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005273}
5274
H. Peter Anvin8b262472019-02-26 14:00:54 -08005275/* Add magic standard macros */
5276struct magic_macros {
5277 const char *name;
5278 int nparams;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005279 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005280};
5281static const struct magic_macros magic_macros[] =
5282{
5283 { "__FILE__", 0, stdmac_file },
5284 { "__LINE__", 0, stdmac_line },
5285 { "__BITS__", 0, stdmac_bits },
5286 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005287 { NULL, 0, NULL }
5288};
5289
5290static void pp_add_magic_stdmac(void)
5291{
5292 const struct magic_macros *m;
5293 SMacro *s;
5294
5295 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005296 s = define_smacro(NULL, m->name, true, m->nparams, NULL, NULL);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005297 s->expand = m->func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005298 }
5299}
5300
H. Peter Anvin734b1882002-04-30 21:01:08 +00005301static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005302pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005303{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005304 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005305
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005306 cstk = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005307 nasm_new(istk);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07005308 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin274cda82016-05-10 02:56:29 -07005309 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005310 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005311 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03005312 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005313 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005314 nested_mac_count = 0;
5315 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005316 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005317 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005318 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005319 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005320
H. Peter Anvin8b262472019-02-26 14:00:54 -08005321 pp_add_magic_stdmac();
5322
H. Peter Anvinf7606612016-07-13 14:23:48 -07005323 if (tasm_compatible_mode)
5324 pp_add_stdmac(nasm_stdmac_tasm);
5325
5326 pp_add_stdmac(nasm_stdmac_nasm);
5327 pp_add_stdmac(nasm_stdmac_version);
5328
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005329 if (extrastdmac)
5330 pp_add_stdmac(extrastdmac);
5331
H. Peter Anvinf7606612016-07-13 14:23:48 -07005332 stdmacpos = stdmacros[0];
5333 stdmacnext = &stdmacros[1];
5334
H. Peter Anvind2456592008-06-19 15:04:18 -07005335 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005336
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03005337 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005338
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005339 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005340 * Define the __PASS__ macro. This is defined here unlike all the
5341 * other builtins, because it is special -- it varies between
5342 * passes -- but there is really no particular reason to make it
5343 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005344 *
5345 * 0 = dependencies only
5346 * 1 = preparatory passes
5347 * 2 = final pass
5348 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005349 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005350 switch (mode) {
5351 case PP_NORMAL:
5352 apass = pass_final() ? 2 : 1;
5353 break;
5354 case PP_DEPS:
5355 apass = 0;
5356 break;
5357 case PP_PREPROC:
5358 apass = 3;
5359 break;
5360 default:
5361 panic();
5362 }
5363
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005364 define_smacro(NULL, "__PASS__", true, 0, NULL, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005365}
5366
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005367static void pp_init(void)
5368{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005369}
5370
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005371/*
5372 * Get a line of tokens. If we popped the macro expansion/include stack,
5373 * we return a pointer to the dummy token tok_pop; at that point if
5374 * istk is NULL then we have reached end of input;
5375 */
5376static Token tok_pop; /* Dummy token placeholder */
5377
5378static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005379{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005380 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005381
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005382 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005383 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005384 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005385 * buffer or from the input file.
5386 */
5387 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005388 while (istk->expansion && istk->expansion->finishes) {
5389 Line *l = istk->expansion;
5390 if (!l->finishes->name && l->finishes->in_progress > 1) {
5391 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005392
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005393 /*
5394 * This is a macro-end marker for a macro with no
5395 * name, which means it's not really a macro at all
5396 * but a %rep block, and the `in_progress' field is
5397 * more than 1, meaning that we still need to
5398 * repeat. (1 means the natural last repetition; 0
5399 * means termination by %exitrep.) We have
5400 * therefore expanded up to the %endrep, and must
5401 * push the whole block on to the expansion buffer
5402 * again. We don't bother to remove the macro-end
5403 * marker: we'd only have to generate another one
5404 * if we did.
5405 */
5406 l->finishes->in_progress--;
5407 list_for_each(l, l->finishes->expansion) {
5408 Token *t, *tt, **tail;
5409
5410 ll = nasm_malloc(sizeof(Line));
5411 ll->next = istk->expansion;
5412 ll->finishes = NULL;
5413 ll->first = NULL;
5414 tail = &ll->first;
5415
5416 list_for_each(t, l->first) {
5417 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005418 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005419 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005420 }
5421 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005422 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005423 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005424 } else {
5425 /*
5426 * Check whether a `%rep' was started and not ended
5427 * within this macro expansion. This can happen and
5428 * should be detected. It's a fatal error because
5429 * I'm too confused to work out how to recover
5430 * sensibly from it.
5431 */
5432 if (defining) {
5433 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005434 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005435 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005436 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005437 " expansion of macro `%s'",
5438 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005439 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005440
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005441 /*
5442 * FIXME: investigate the relationship at this point between
5443 * istk->mstk and l->finishes
5444 */
5445 {
5446 MMacro *m = istk->mstk;
5447 istk->mstk = m->next_active;
5448 if (m->name) {
5449 /*
5450 * This was a real macro call, not a %rep, and
5451 * therefore the parameter information needs to
5452 * be freed.
5453 */
5454 if (m->prev) {
5455 pop_mmacro(m);
5456 l->finishes->in_progress --;
5457 } else {
5458 nasm_free(m->params);
5459 free_tlist(m->iline);
5460 nasm_free(m->paramlen);
5461 l->finishes->in_progress = 0;
5462 }
Adam Majer91e72402017-07-25 10:42:01 +02005463 }
5464
5465 /*
5466 * FIXME It is incorrect to always free_mmacro here.
5467 * It leads to usage-after-free.
5468 *
5469 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5470 */
5471#if 0
5472 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005473 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005474#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005475 }
5476 istk->expansion = l->next;
5477 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005478 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005479 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005480 }
5481 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005482 do { /* until we get a line we can use */
5483 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005484
5485 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005486 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005487 int32_t lineno;
5488
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005489 if (istk->mstk)
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005490 lineno = ++istk->mstk->lineno + istk->mstk->xline;
5491 else
5492 lineno = src_get_linnum();
5493
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005494 tline = l->first;
5495 istk->expansion = l->next;
5496 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005497
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005498 line = detoken(tline, false);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005499 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005500 nasm_free(line);
5501 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005502 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005503 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005504 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005505 } else {
5506 /*
5507 * The current file has ended; work down the istk
5508 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005509 Include *i = istk;
5510 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005511 if (i->conds) {
5512 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005513 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005514 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005515 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005516 if (i->next)
5517 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005518 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005519 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005520 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005521 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005522 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005523 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005524
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005525 /*
5526 * We must expand MMacro parameters and MMacro-local labels
5527 * _before_ we plunge into directive processing, to cope
5528 * with things like `%define something %1' such as STRUC
5529 * uses. Unless we're _defining_ a MMacro, in which case
5530 * those tokens should be left alone to go into the
5531 * definition; and unless we're in a non-emitting
5532 * condition, in which case we don't want to meddle with
5533 * anything.
5534 */
5535 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5536 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005537 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005538 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005539
H. Peter Anvine2c80182005-01-15 22:15:51 +00005540 /*
5541 * Check the line to see if it's a preprocessor directive.
5542 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005543 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5544 if (dtline)
5545 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005546 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005547 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005548 * We're defining a multi-line macro. We emit nothing
5549 * at all, and just
5550 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005551 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005552 Line *l = nasm_malloc(sizeof(Line));
5553 l->next = defining->expansion;
5554 l->first = tline;
5555 l->finishes = NULL;
5556 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005557 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005558 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005559 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005560 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005561 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005562 * directive so we keep our place correctly.
5563 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005564 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005565 } else if (istk->mstk && !istk->mstk->in_progress) {
5566 /*
5567 * We're in a %rep block which has been terminated, so
5568 * we're walking through to the %endrep without
5569 * emitting anything. Emit nothing at all, not even a
5570 * blank line: when we emerge from the %rep block we'll
5571 * give a line-number directive so we keep our place
5572 * correctly.
5573 */
5574 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005575 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005576 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005577 if (!expand_mmacro(tline))
5578 return tline;
5579 }
5580 }
5581}
5582
5583static char *pp_getline(void)
5584{
5585 char *line = NULL;
5586 Token *tline;
5587
5588 real_verror = nasm_set_verror(pp_verror);
5589
5590 while (true) {
5591 tline = pp_tokline();
5592 if (tline == &tok_pop) {
5593 /*
5594 * We popped the macro/include stack. If istk is empty,
5595 * we are at end of input, otherwise just loop back.
5596 */
5597 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005598 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005599 } else {
5600 /*
5601 * De-tokenize the line and emit it.
5602 */
5603 line = detoken(tline, true);
5604 free_tlist(tline);
5605 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005606 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005607 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005608
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005609 if (list_option('e') && line && line[0]) {
5610 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005611 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005612 nasm_free(buf);
5613 }
5614
H. Peter Anvin130736c2016-02-17 20:27:41 -08005615 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005616 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005617}
5618
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005619static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005620{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005621 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005622
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005623 if (defining) {
5624 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005625 nasm_nonfatal("end of file while still defining macro `%s'",
5626 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005627 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005628 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005629 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005630
5631 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005632 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005633 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005634
5635 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005636
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005637 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005638 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005639 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005640 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005641 Include *i = istk;
5642 istk = istk->next;
5643 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005644 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005645 }
5646 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005647 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005648 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005649}
5650
5651static void pp_cleanup_session(void)
5652{
5653 free_llist(predef);
5654 predef = NULL;
5655 delete_Blocks();
5656 freeTokens = NULL;
5657 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005658}
5659
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005660static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005661{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005662 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005663}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005664
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005665static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005666{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005667 Token *inc, *space, *name;
5668 Line *l;
5669
H. Peter Anvin734b1882002-04-30 21:01:08 +00005670 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5671 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5672 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005673
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005674 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005675 l->next = predef;
5676 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005677 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005678 predef = l;
5679}
5680
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005681static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005682{
5683 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005684 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005685 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005686
H. Peter Anvin130736c2016-02-17 20:27:41 -08005687 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005688
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005689 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005690 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5691 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005692 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005693 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005694 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005695 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005696 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005697
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005698 if (space->next->type != TOK_PREPROC_ID &&
5699 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005700 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005701
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005702 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005703 l->next = predef;
5704 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005705 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005706 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005707
5708 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005709}
5710
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005711static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005712{
5713 Token *def, *space;
5714 Line *l;
5715
H. Peter Anvin734b1882002-04-30 21:01:08 +00005716 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5717 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005718 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005719
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005720 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005721 l->next = predef;
5722 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005723 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005724 predef = l;
5725}
5726
H. Peter Anvin05990342018-06-11 13:32:42 -07005727/* Insert an early preprocessor command that doesn't need special handling */
5728static void pp_pre_command(const char *what, char *string)
5729{
5730 char *cmd;
5731 Token *def, *space;
5732 Line *l;
5733
5734 def = tokenize(string);
5735 if (what) {
5736 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5737 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5738 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5739 }
5740
5741 l = nasm_malloc(sizeof(Line));
5742 l->next = predef;
5743 l->first = def;
5744 l->finishes = NULL;
5745 predef = l;
5746}
5747
H. Peter Anvinf7606612016-07-13 14:23:48 -07005748static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005749{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005750 macros_t **mp;
5751
5752 /* Find the end of the list and avoid duplicates */
5753 for (mp = stdmacros; *mp; mp++) {
5754 if (*mp == macros)
5755 return; /* Nothing to do */
5756 }
5757
5758 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5759
5760 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005761}
5762
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005763static void pp_extra_stdmac(macros_t *macros)
5764{
5765 extrastdmac = macros;
5766}
5767
H. Peter Anvin8b262472019-02-26 14:00:54 -08005768static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005769{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005770 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005771 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5772 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5773}
5774
5775static Token *make_tok_qstr(const char *str)
5776{
5777 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005778 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005779 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005780}
5781
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005782static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005783{
5784 if (!m)
5785 return;
5786
5787 /* We need to print the next_active list in reverse order */
5788 pp_list_one_macro(m->next_active, severity);
5789
5790 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005791 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005792 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005793 }
5794}
5795
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005796static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005797{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005798 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005799
H. Peter Anvinddb29062018-12-11 00:06:29 -08005800 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5801 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005802
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005803 if (istk)
5804 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005805
H. Peter Anvinddb29062018-12-11 00:06:29 -08005806 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005807}
5808
H. Peter Anvine7469712016-02-18 02:20:59 -08005809const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005810 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005811 pp_reset,
5812 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005813 pp_cleanup_pass,
5814 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005815 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005816 pp_pre_define,
5817 pp_pre_undefine,
5818 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005819 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005820 pp_include_path,
5821 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005822};