blob: 82fe60ab2be7db2c3a944a962fb3315dd33e1abf [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)41e96822019-04-25 18:00:32 -070099 * Function call for a magic smacro
100 */
101typedef Token *(*MagicSMacro)(const SMacro *s, Token **params,
102 unsigned int nparams, bool *free_expansion);
103
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;
111 MagicSMacro magic;
112 intorptr magicpvt;
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;
187 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188};
189
190/*
191 * This is the internal form which we break input lines up into.
192 * Typically stored in linked lists.
193 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800194 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
195 * not necessarily used as-is, but is also used to encode the number
196 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800198 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700199 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000200 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800201 * tok_smac_param(0) but the one representing `y' will be
202 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000203 *
204 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
205 * which doesn't need quotes around it. Used in the pre-include
206 * mechanism as an alternative to trying to find a sensible type of
207 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000208 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000209enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800210 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
211 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800212 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700213 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800214 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300215 TOK_PASTE, /* %+ */
216 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800217 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300218 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000219};
220
H. Peter Anvin8b262472019-02-26 14:00:54 -0800221static inline enum pp_token_type tok_smac_param(int param)
222{
223 return TOK_SMAC_START_PARAMS + param;
224}
225static int smac_nparam(enum pp_token_type toktype)
226{
227 return toktype - TOK_SMAC_START_PARAMS;
228}
229static bool is_smac_param(enum pp_token_type toktype)
230{
231 return toktype >= TOK_SMAC_START_PARAMS;
232}
233
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400234#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400235#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400236
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400237struct tokseq_match {
238 int mask_head;
239 int mask_tail;
240};
241
H. Peter Anvine2c80182005-01-15 22:15:51 +0000242struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 Token *next;
244 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700245 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800246 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000247};
248
249/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800250 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000251 * these, which is essentially a container to allow several linked
252 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700253 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000254 * Note that in this module, linked lists are treated as stacks
255 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800256 * `expansion' field in MMacro structures, so that the linked list,
257 * if walked, would give the macro lines in reverse order; this
258 * means that we can walk the list when expanding a macro, and thus
259 * push the lines on to the `expansion' field in _istk_ in reverse
260 * order (so that when popped back off they are in the right
261 * order). It may seem cockeyed, and it relies on my design having
262 * an even number of steps in, but it works...
263 *
264 * Some of these structures, rather than being actual lines, are
265 * markers delimiting the end of the expansion of a given macro.
266 * This is for use in the cycle-tracking and %rep-handling code.
267 * Such structures have `finishes' non-NULL, and `first' NULL. All
268 * others have `finishes' NULL, but `first' may still be NULL if
269 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800272 Line *next;
273 MMacro *finishes;
274 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * To handle an arbitrary level of file inclusion, we maintain a
279 * stack (ie linked list) of these things.
280 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800282 Include *next;
283 FILE *fp;
284 Cond *conds;
285 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700286 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800287 int lineno, lineinc;
288 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289};
290
291/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700292 * File real name hash, so we don't have to re-search the include
293 * path for every pass (and potentially more than that if a file
294 * is used more than once.)
295 */
296struct hash_table FileHash;
297
298/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000299 * Conditional assembly: we maintain a separate stack of these for
300 * each level of file inclusion. (The only reason we keep the
301 * stacks separate is to ensure that a stray `%endif' in a file
302 * included from within the true branch of a `%if' won't terminate
303 * it and cause confusion: instead, rightly, it'll cause an error.)
304 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305struct Cond {
306 Cond *next;
307 int state;
308};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000309enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310 /*
311 * These states are for use just after %if or %elif: IF_TRUE
312 * means the condition has evaluated to truth so we are
313 * currently emitting, whereas IF_FALSE means we are not
314 * currently emitting but will start doing so if a %else comes
315 * up. In these states, all directives are admissible: %elif,
316 * %else and %endif. (And of course %if.)
317 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800318 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000319 /*
320 * These states come up after a %else: ELSE_TRUE means we're
321 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
322 * any %elif or %else will cause an error.
323 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800324 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200326 * These states mean that we're not emitting now, and also that
327 * nothing until %endif will be emitted at all. COND_DONE is
328 * used when we've had our moment of emission
329 * and have now started seeing %elifs. COND_NEVER is used when
330 * the condition construct in question is contained within a
331 * non-emitting branch of a larger condition construct,
332 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800334 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800336#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
H. Peter Anvin70653092007-10-19 14:42:29 -0700338/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000339 * These defines are used as the possible return values for do_directive
340 */
341#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300342#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000343
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400344/* max reps */
345#define REP_LIMIT ((INT64_C(1) << 62))
346
Keith Kanios852f1ee2009-07-12 00:19:55 -0500347/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 * Condition codes. Note that we use c_ prefix not C_ because C_ is
349 * used in nasm.h for the "real" condition codes. At _this_ level,
350 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
351 * ones, so we need a different enum...
352 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700353static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
355 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000356 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700358enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
360 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 -0700361 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
362 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700364static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
366 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 +0000367 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368};
369
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800370/*
371 * Directive names.
372 */
373/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
374static int is_condition(enum preproc_token arg)
375{
376 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
377}
378
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379/* For TASM compatibility we need to be able to recognise TASM compatible
380 * conditional compilation directives. Using the NASM pre-processor does
381 * not work, so we look for them specifically from the following list and
382 * then jam in the equivalent NASM directive into the input stream.
383 */
384
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000386 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
387 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
388};
389
H. Peter Anvin476d2862007-10-02 22:04:15 -0700390static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000391 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
392 "ifndef", "include", "local"
393};
394
395static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700396static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000397static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800398static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000399
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400static Context *cstk;
401static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300402static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300404static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300406static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800408static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700409static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800410static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000411
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800415static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000416
417/*
418 * The current set of single-line macros we have defined.
419 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700420static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421
422/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800423 * The multi-line macro we are currently defining, or the %rep
424 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427
Charles Crayned4200be2008-07-12 16:42:33 -0700428static uint64_t nested_mac_count;
429static uint64_t nested_rep_count;
430
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000431/*
432 * The number of macro parameters to allocate space for at a time.
433 */
434#define PARAM_DELTA 16
435
436/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700437 * The standard macro set: defined in macros.c in a set of arrays.
438 * This gives our position in any macro set, while we are processing it.
439 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000440 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700441static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700442static macros_t **stdmacnext;
443static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300444static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000445
446/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447 * Tokens are allocated in blocks to improve speed
448 */
449#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800450static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000451struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 Blocks *next;
453 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000454};
455
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800456static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000457
458/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459 * Forward declarations.
460 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700461static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000462static Token *expand_mmac_params(Token * tline);
463static Token *expand_smacro(Token * tline);
464static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400465static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800466static Token *make_tok_num(int64_t val);
467static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800468static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800469static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000470static void *new_Block(size_t size);
471static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700472static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700473 const char *text, size_t txtlen);
474static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000475static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476
477/*
478 * Macros for safe checking of token pointers, avoid *(NULL)
479 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480#define tok_type_(x,t) ((x) && (x)->type == (t))
481#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
482#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
483#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400485/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700486 * In-place reverse a list of tokens.
487 */
488static Token *reverse_tokens(Token *t)
489{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800490 Token *prev = NULL;
491 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700492
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800493 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400494 next = t->next;
495 t->next = prev;
496 prev = t;
497 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800498 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700499
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700501}
502
503/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300504 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000505 * front of them. We do it here because I could not find any other
506 * place to do it for the moment, and it is a hack (ideally it would
507 * be nice to be able to use the NASM pre-processor to do it).
508 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000509static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000510{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000511 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400512 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000513
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400514 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000515
516 /* Binary search for the directive name */
517 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400518 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400519 q = nasm_skip_word(p);
520 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 if (len) {
522 oldchar = p[len];
523 p[len] = 0;
524 while (j - i > 1) {
525 k = (j + i) / 2;
526 m = nasm_stricmp(p, tasm_directives[k]);
527 if (m == 0) {
528 /* We have found a directive, so jam a % in front of it
529 * so that NASM will then recognise it as one if it's own.
530 */
531 p[len] = oldchar;
532 len = strlen(p);
533 oldline = line;
534 line = nasm_malloc(len + 2);
535 line[0] = '%';
536 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700537 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300538 * NASM does not recognise IFDIFI, so we convert
539 * it to %if 0. This is not used in NASM
540 * compatible code, but does need to parse for the
541 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700543 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 } else {
545 memcpy(line + 1, p, len + 1);
546 }
547 nasm_free(oldline);
548 return line;
549 } else if (m < 0) {
550 j = k;
551 } else
552 i = k;
553 }
554 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000555 }
556 return line;
557}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000558
H. Peter Anvin76690a12002-04-30 20:52:49 +0000559/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000560 * The pre-preprocessing stage... This function translates line
561 * number indications as they emerge from GNU cpp (`# lineno "file"
562 * flags') into NASM preprocessor line number indications (`%line
563 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000564 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000565static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000566{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000568 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 if (line[0] == '#' && line[1] == ' ') {
571 oldline = line;
572 fname = oldline + 2;
573 lineno = atoi(fname);
574 fname += strspn(fname, "0123456789 ");
575 if (*fname == '"')
576 fname++;
577 fnlen = strcspn(fname, "\"");
578 line = nasm_malloc(20 + fnlen);
579 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
580 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000581 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000582 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000584 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585}
586
587/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588 * Free a linked list of tokens.
589 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000591{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400592 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000594}
595
596/*
597 * Free a linked list of lines.
598 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000600{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400601 Line *l, *tmp;
602 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 free_tlist(l->first);
604 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605 }
606}
607
608/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800609 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000610 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800611static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000612{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800613 nasm_free(m->name);
614 free_tlist(m->dlist);
615 nasm_free(m->defaults);
616 free_llist(m->expansion);
617 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000618}
619
620/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700621 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800622 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700623static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800624{
625 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700626 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800627 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700628}
629
630static void clear_smacro(SMacro *s)
631{
632 free_smacro_members(s);
633 /* Wipe everything except the next pointer */
634 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
635}
636
637/*
638 * Free an SMacro
639 */
640static void free_smacro(SMacro *s)
641{
642 free_smacro_members(s);
643 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800644}
645
646/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700647 * Free all currently defined macros, and free the hash tables
648 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700649static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700650{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800651 struct hash_iterator it;
652 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700653
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800654 hash_for_each(smt, it, np) {
655 SMacro *tmp;
656 SMacro *s = np->data;
657 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800658 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700659 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700660 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700661 hash_free(smt);
662}
663
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800664static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700665{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800666 struct hash_iterator it;
667 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700668
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800669 hash_for_each(mmt, it, np) {
670 MMacro *tmp;
671 MMacro *m = np->data;
672 nasm_free((void *)np->key);
673 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800674 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700675 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800676 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700677}
678
679static void free_macros(void)
680{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700681 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800682 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700683}
684
685/*
686 * Initialize the hash tables
687 */
688static void init_macros(void)
689{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700690}
691
692/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000693 * Pop the context stack.
694 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000695static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000696{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000697 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000698
699 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700700 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000701 nasm_free(c->name);
702 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000703}
704
H. Peter Anvin072771e2008-05-22 13:17:51 -0700705/*
706 * Search for a key in the hash index; adding it if necessary
707 * (in which case we initialize the data pointer to NULL.)
708 */
709static void **
710hash_findi_add(struct hash_table *hash, const char *str)
711{
712 struct hash_insert hi;
713 void **r;
714 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800715 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700716
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800717 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700718 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300719 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700720
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800721 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
722 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700723 return hash_add(&hi, strx, NULL);
724}
725
726/*
727 * Like hash_findi, but returns the data element rather than a pointer
728 * to it. Used only when not adding a new element, hence no third
729 * argument.
730 */
731static void *
732hash_findix(struct hash_table *hash, const char *str)
733{
734 void **p;
735
736 p = hash_findi(hash, str, NULL);
737 return p ? *p : NULL;
738}
739
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400740/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800741 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400742 * if there no more left -- return NULL
743 */
744static char *line_from_stdmac(void)
745{
746 unsigned char c;
747 const unsigned char *p = stdmacpos;
748 char *line, *q;
749 size_t len = 0;
750
751 if (!stdmacpos)
752 return NULL;
753
754 while ((c = *p++)) {
755 if (c >= 0x80)
756 len += pp_directives_len[c - 0x80] + 1;
757 else
758 len++;
759 }
760
761 line = nasm_malloc(len + 1);
762 q = line;
763 while ((c = *stdmacpos++)) {
764 if (c >= 0x80) {
765 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
766 q += pp_directives_len[c - 0x80];
767 *q++ = ' ';
768 } else {
769 *q++ = c;
770 }
771 }
772 stdmacpos = p;
773 *q = '\0';
774
775 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700776 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400777 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700778 if (*stdmacnext) {
779 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400780 } else if (do_predef) {
781 Line *pd, *l;
782 Token *head, **tail, *t;
783
784 /*
785 * Nasty hack: here we push the contents of
786 * `predef' on to the top-level expansion stack,
787 * since this is the most convenient way to
788 * implement the pre-include and pre-define
789 * features.
790 */
791 list_for_each(pd, predef) {
792 head = NULL;
793 tail = &head;
794 list_for_each(t, pd->first) {
795 *tail = new_Token(NULL, t->type, t->text, 0);
796 tail = &(*tail)->next;
797 }
798
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800799 l = nasm_malloc(sizeof(Line));
800 l->next = istk->expansion;
801 l->first = head;
802 l->finishes = NULL;
803
804 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400805 }
806 do_predef = false;
807 }
808 }
809
810 return line;
811}
812
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000813static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000814{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700815 int c;
816 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400817 const unsigned int delta = 512;
818 const unsigned int pad = 8;
819 unsigned int nr_cont = 0;
820 bool cont = false;
821 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000822
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400823 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400824 p = line_from_stdmac();
825 if (p)
826 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700827
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400828 size = delta;
829 p = buffer = nasm_malloc(size);
830
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700831 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400832 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400833
834 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700835 case EOF:
836 if (p == buffer) {
837 nasm_free(buffer);
838 return NULL;
839 }
840 c = 0;
841 break;
842
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400843 case '\r':
844 next = fgetc(istk->fp);
845 if (next != '\n')
846 ungetc(next, istk->fp);
847 if (cont) {
848 cont = false;
849 continue;
850 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700851 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400852 break;
853
854 case '\n':
855 if (cont) {
856 cont = false;
857 continue;
858 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700859 c = 0;
860 break;
861
862 case 032: /* ^Z = legacy MS-DOS end of file mark */
863 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400864 break;
865
866 case '\\':
867 next = fgetc(istk->fp);
868 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400869 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400870 cont = true;
871 nr_cont++;
872 continue;
873 }
874 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000875 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400876
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400877 if (p >= (buffer + size - pad)) {
878 buffer = nasm_realloc(buffer, size + delta);
879 p = buffer + size - pad;
880 size += delta;
881 }
882
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700883 *p++ = c;
884 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000885
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300886 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400887 (nr_cont * istk->lineinc));
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800888 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000889
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000890 return buffer;
891}
892
893/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000894 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000895 * don't need to parse the value out of e.g. numeric tokens: we
896 * simply split one string into many.
897 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000898static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000899{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700900 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000901 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000902 Token *list = NULL;
903 Token *t, **tail = &list;
904
H. Peter Anvine2c80182005-01-15 22:15:51 +0000905 while (*line) {
906 p = line;
907 if (*p == '%') {
908 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300909 if (*p == '+' && !nasm_isdigit(p[1])) {
910 p++;
911 type = TOK_PASTE;
912 } else if (nasm_isdigit(*p) ||
913 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 do {
915 p++;
916 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700917 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 type = TOK_PREPROC_ID;
919 } else if (*p == '{') {
920 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800921 while (*p) {
922 if (*p == '}')
923 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 p[-1] = *p;
925 p++;
926 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800927 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800928 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000929 p[-1] = '\0';
930 if (*p)
931 p++;
932 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300933 } else if (*p == '[') {
934 int lvl = 1;
935 line += 2; /* Skip the leading %[ */
936 p++;
937 while (lvl && (c = *p++)) {
938 switch (c) {
939 case ']':
940 lvl--;
941 break;
942 case '%':
943 if (*p == '[')
944 lvl++;
945 break;
946 case '\'':
947 case '\"':
948 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300949 p = nasm_skip_string(p - 1);
950 if (*p)
951 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300952 break;
953 default:
954 break;
955 }
956 }
957 p--;
958 if (*p)
959 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800960 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300961 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300962 type = TOK_INDIRECT;
963 } else if (*p == '?') {
964 type = TOK_PREPROC_Q; /* %? */
965 p++;
966 if (*p == '?') {
967 type = TOK_PREPROC_QQ; /* %?? */
968 p++;
969 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400970 } else if (*p == '!') {
971 type = TOK_PREPROC_ID;
972 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800973 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400974 do {
975 p++;
976 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800977 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -0800978 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400979 p = nasm_skip_string(p);
980 if (*p)
981 p++;
982 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300983 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400984 } else {
985 /* %! without string or identifier */
986 type = TOK_OTHER; /* Legacy behavior... */
987 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800988 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -0800990 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 do {
992 p++;
993 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800994 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 type = TOK_PREPROC_ID;
996 } else {
997 type = TOK_OTHER;
998 if (*p == '%')
999 p++;
1000 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001001 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 type = TOK_ID;
1003 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001004 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001006 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 /*
1008 * A string token.
1009 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001011 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 if (*p) {
1014 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001015 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001016 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 /* Handling unterminated strings by UNV */
1018 /* type = -1; */
1019 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001020 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001021 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001022 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001023 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001024 bool is_hex = false;
1025 bool is_float = false;
1026 bool has_e = false;
1027 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001028
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001030 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001032
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001033 if (*p == '$') {
1034 p++;
1035 is_hex = true;
1036 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001037
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001038 for (;;) {
1039 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001040
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001041 if (!is_hex && (c == 'e' || c == 'E')) {
1042 has_e = true;
1043 if (*p == '+' || *p == '-') {
1044 /*
1045 * e can only be followed by +/- if it is either a
1046 * prefixed hex number or a floating-point number
1047 */
1048 p++;
1049 is_float = true;
1050 }
1051 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1052 is_hex = true;
1053 } else if (c == 'P' || c == 'p') {
1054 is_float = true;
1055 if (*p == '+' || *p == '-')
1056 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001057 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001058 ; /* just advance */
1059 else if (c == '.') {
1060 /*
1061 * we need to deal with consequences of the legacy
1062 * parser, like "1.nolist" being two tokens
1063 * (TOK_NUMBER, TOK_ID) here; at least give it
1064 * a shot for now. In the future, we probably need
1065 * a flex-based scanner with proper pattern matching
1066 * to do it as well as it can be done. Nothing in
1067 * the world is going to help the person who wants
1068 * 0x123.p16 interpreted as two tokens, though.
1069 */
1070 r = p;
1071 while (*r == '_')
1072 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001073
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001074 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1075 (!is_hex && (*r == 'e' || *r == 'E')) ||
1076 (*r == 'p' || *r == 'P')) {
1077 p = r;
1078 is_float = true;
1079 } else
1080 break; /* Terminate the token */
1081 } else
1082 break;
1083 }
1084 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001085
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001086 if (p == line+1 && *line == '$') {
1087 type = TOK_OTHER; /* TOKEN_HERE */
1088 } else {
1089 if (has_e && !is_hex) {
1090 /* 1e13 is floating-point, but 1e13h is not */
1091 is_float = true;
1092 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001093
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001094 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1095 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001096 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001098 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 /*
1100 * Whitespace just before end-of-line is discarded by
1101 * pretending it's a comment; whitespace just before a
1102 * comment gets lumped into the comment.
1103 */
1104 if (!*p || *p == ';') {
1105 type = TOK_COMMENT;
1106 while (*p)
1107 p++;
1108 }
1109 } else if (*p == ';') {
1110 type = TOK_COMMENT;
1111 while (*p)
1112 p++;
1113 } else {
1114 /*
1115 * Anything else is an operator of some kind. We check
1116 * for all the double-character operators (>>, <<, //,
1117 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001118 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 */
1120 type = TOK_OTHER;
1121 if ((p[0] == '>' && p[1] == '>') ||
1122 (p[0] == '<' && p[1] == '<') ||
1123 (p[0] == '/' && p[1] == '/') ||
1124 (p[0] == '<' && p[1] == '=') ||
1125 (p[0] == '>' && p[1] == '=') ||
1126 (p[0] == '=' && p[1] == '=') ||
1127 (p[0] == '!' && p[1] == '=') ||
1128 (p[0] == '<' && p[1] == '>') ||
1129 (p[0] == '&' && p[1] == '&') ||
1130 (p[0] == '|' && p[1] == '|') ||
1131 (p[0] == '^' && p[1] == '^')) {
1132 p++;
1133 }
1134 p++;
1135 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 /* Handling unterminated string by UNV */
1138 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 {
1140 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1141 t->text[p-line] = *line;
1142 tail = &t->next;
1143 }
1144 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 if (type != TOK_COMMENT) {
1146 *tail = t = new_Token(NULL, type, line, p - line);
1147 tail = &t->next;
1148 }
1149 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001150 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001151 return list;
1152}
1153
H. Peter Anvince616072002-04-30 21:02:23 +00001154/*
1155 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001156 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001157 * deleted only all at once by the delete_Blocks function.
1158 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001160{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001161 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001163 /* first, get to the end of the linked list */
1164 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001166 /* now allocate the requested chunk */
1167 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001169 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001170 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001171 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001172}
1173
1174/*
1175 * this function deletes all managed blocks of memory
1176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001178{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001180
H. Peter Anvin70653092007-10-19 14:42:29 -07001181 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001182 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001183 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001184 * free it.
1185 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001187 if (b->chunk)
1188 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 a = b;
1190 b = b->next;
1191 if (a != &blocks)
1192 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001193 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001194 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001196
1197/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001198 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001199 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001200 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001201static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001202 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001203{
1204 Token *t;
1205 int i;
1206
H. Peter Anvin89cee572009-07-15 09:16:54 -04001207 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1209 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1210 freeTokens[i].next = &freeTokens[i + 1];
1211 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001212 }
1213 t = freeTokens;
1214 freeTokens = t->next;
1215 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001216 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001217 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001218 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 t->text = NULL;
1220 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001221 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001223 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001224 t->text = nasm_malloc(txtlen+1);
1225 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001227 }
1228 return t;
1229}
1230
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001231static Token *dup_Token(Token *next, const Token *src)
1232{
1233 return new_Token(next, src->type, src->text, src->len);
1234}
1235
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001237{
1238 Token *next = t->next;
1239 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001240 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001241 freeTokens = t;
1242 return next;
1243}
1244
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001245/*
1246 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001247 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1248 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001249 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001250static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001251{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001252 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001253 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001254 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001255 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001256
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001257 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001258 if (t->type == TOK_PREPROC_ID && t->text &&
1259 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001260 char *v;
1261 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001262
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001263 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001264 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001265 size_t len = nasm_unquote(v, NULL);
1266 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001267
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001268 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001269 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001270 v = NULL;
1271 }
1272 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001273
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001274 if (v) {
1275 char *p = getenv(v);
1276 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001277 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001278 /*
1279 * FIXME We better should investigate if accessing
1280 * ->text[1] without ->text[0] is safe enough.
1281 */
1282 t->text = nasm_zalloc(2);
1283 } else
1284 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001285 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001286 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001288
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 /* Expand local macros here and not during preprocessing */
1290 if (expand_locals &&
1291 t->type == TOK_PREPROC_ID && t->text &&
1292 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001293 const char *q;
1294 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001295 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001297 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001298 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 p = nasm_strcat(buffer, q);
1300 nasm_free(t->text);
1301 t->text = p;
1302 }
1303 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001304 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001306 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001308 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001309
H. Peter Anvin734b1882002-04-30 21:01:08 +00001310 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001311
1312 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001314 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001316 q = t->text;
1317 while (*q)
1318 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001320 }
1321 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001322
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001323 return line;
1324}
1325
1326/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001327 * A scanner, suitable for use by the expression evaluator, which
1328 * operates on a line of Tokens. Expects a pointer to a pointer to
1329 * the first token in the line to be passed in as its private_data
1330 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001331 *
1332 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001333 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001334struct ppscan {
1335 Token *tptr;
1336 int ntokens;
1337};
1338
H. Peter Anvine2c80182005-01-15 22:15:51 +00001339static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001340{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001341 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001342 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001343 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001344
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001346 if (pps->ntokens && (tline = pps->tptr)) {
1347 pps->ntokens--;
1348 pps->tptr = tline->next;
1349 } else {
1350 pps->tptr = NULL;
1351 pps->ntokens = 0;
1352 return tokval->t_type = TOKEN_EOS;
1353 }
1354 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001355
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001356 tokval->t_charptr = tline->text;
1357
H. Peter Anvin76690a12002-04-30 20:52:49 +00001358 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001360 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001362
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001364 p = tokval->t_charptr = tline->text;
1365 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 tokval->t_charptr++;
1367 return tokval->t_type = TOKEN_ID;
1368 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001369
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001370 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001371 if (r >= p+MAX_KEYWORD)
1372 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001373 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001374 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001375 *s = '\0';
1376 /* right, so we have an identifier sitting in temp storage. now,
1377 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001378 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001379 }
1380
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001382 bool rn_error;
1383 tokval->t_integer = readnum(tline->text, &rn_error);
1384 tokval->t_charptr = tline->text;
1385 if (rn_error)
1386 return tokval->t_type = TOKEN_ERRNUM;
1387 else
1388 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001389 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001390
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001391 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001392 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001393 }
1394
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001396 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001397
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001398 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001399 tokval->t_charptr = tline->text;
1400 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001401
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001402 if (ep[0] != bq || ep[1] != '\0')
1403 return tokval->t_type = TOKEN_ERRSTR;
1404 else
1405 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001406 }
1407
H. Peter Anvine2c80182005-01-15 22:15:51 +00001408 if (tline->type == TOK_OTHER) {
1409 if (!strcmp(tline->text, "<<"))
1410 return tokval->t_type = TOKEN_SHL;
1411 if (!strcmp(tline->text, ">>"))
1412 return tokval->t_type = TOKEN_SHR;
1413 if (!strcmp(tline->text, "//"))
1414 return tokval->t_type = TOKEN_SDIV;
1415 if (!strcmp(tline->text, "%%"))
1416 return tokval->t_type = TOKEN_SMOD;
1417 if (!strcmp(tline->text, "=="))
1418 return tokval->t_type = TOKEN_EQ;
1419 if (!strcmp(tline->text, "<>"))
1420 return tokval->t_type = TOKEN_NE;
1421 if (!strcmp(tline->text, "!="))
1422 return tokval->t_type = TOKEN_NE;
1423 if (!strcmp(tline->text, "<="))
1424 return tokval->t_type = TOKEN_LE;
1425 if (!strcmp(tline->text, ">="))
1426 return tokval->t_type = TOKEN_GE;
1427 if (!strcmp(tline->text, "&&"))
1428 return tokval->t_type = TOKEN_DBL_AND;
1429 if (!strcmp(tline->text, "^^"))
1430 return tokval->t_type = TOKEN_DBL_XOR;
1431 if (!strcmp(tline->text, "||"))
1432 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001433 }
1434
1435 /*
1436 * We have no other options: just return the first character of
1437 * the token text.
1438 */
1439 return tokval->t_type = tline->text[0];
1440}
1441
1442/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001443 * Compare a string to the name of an existing macro; this is a
1444 * simple wrapper which calls either strcmp or nasm_stricmp
1445 * depending on the value of the `casesense' parameter.
1446 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001447static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001448{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001449 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001450}
1451
1452/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001453 * Compare a string to the name of an existing macro; this is a
1454 * simple wrapper which calls either strcmp or nasm_stricmp
1455 * depending on the value of the `casesense' parameter.
1456 */
1457static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1458{
1459 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1460}
1461
1462/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001463 * Return the Context structure associated with a %$ token. Return
1464 * NULL, having _already_ reported an error condition, if the
1465 * context stack isn't deep enough for the supplied number of $
1466 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001467 *
1468 * If "namep" is non-NULL, set it to the pointer to the macro name
1469 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001470 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001471static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001472{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001473 Context *ctx;
1474 int i;
1475
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001476 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001477 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001478
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001479 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001480 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001481
H. Peter Anvine2c80182005-01-15 22:15:51 +00001482 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001483 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001484 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001485 }
1486
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001487 name += 2;
1488 ctx = cstk;
1489 i = 0;
1490 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 name++;
1492 i++;
1493 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001494 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001495 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001496 nasm_nonfatal("`%s': context stack is only"
1497 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001499 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001500
1501 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001502 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001503
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001504 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001505}
1506
1507/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001508 * Open an include file. This routine must always return a valid
1509 * file pointer if it returns - it's responsible for throwing an
1510 * ERR_FATAL and bombing out completely if not. It should also try
1511 * the include path one by one until it finds the file or reaches
1512 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001513 *
1514 * Note: for INC_PROBE the function returns NULL at all times;
1515 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001516 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001517enum incopen_mode {
1518 INC_NEEDED, /* File must exist */
1519 INC_OPTIONAL, /* Missing is OK */
1520 INC_PROBE /* Only an existence probe */
1521};
1522
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001523/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001524static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001525 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001526{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001527 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001528 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001529 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001530 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001531 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001532
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001534 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001535 if (omode == INC_PROBE) {
1536 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001537 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001538 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001539 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001540 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001541 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001542 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001543 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001545 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001546
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001547 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001548
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001549 if (!ip) {
1550 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001551 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001552 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001553
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001554 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001555 ip = ip->next;
1556 }
1557}
1558
1559/*
1560 * Open a file, or test for the presence of one (depending on omode),
1561 * considering the include path.
1562 */
1563static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001564 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001565 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001566 enum incopen_mode omode,
1567 enum file_flags fmode)
1568{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001569 struct hash_insert hi;
1570 void **hp;
1571 char *path;
1572 FILE *fp = NULL;
1573
1574 hp = hash_find(&FileHash, file, &hi);
1575 if (hp) {
1576 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001577 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001578 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001579 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001580 } else {
1581 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001582 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001583
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001584 /* Positive or negative result */
1585 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001586
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001587 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001588 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001589 */
1590 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001591 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001592 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001593
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001594 if (!path) {
1595 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001596 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001597 } else {
1598 if (!fp && omode != INC_PROBE)
1599 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001600 }
1601
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001602 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001603 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001604
1605 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001606}
1607
1608/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001609 * Opens an include or input file. Public version, for use by modules
1610 * that get a file:lineno pair and need to look at the file again
1611 * (e.g. the CodeView debug backend). Returns NULL on failure.
1612 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001613FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001614{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001615 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001616}
1617
1618/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001619 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001620 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001621 * return true if _any_ single-line macro of that name is defined.
1622 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001623 * `nparam' or no parameters is defined.
1624 *
1625 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001626 * defined, or nparam is -1, the address of the definition structure
1627 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001628 * is NULL, no action will be taken regarding its contents, and no
1629 * error will occur.
1630 *
1631 * Note that this is also called with nparam zero to resolve
1632 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001633 *
1634 * If you already know which context macro belongs to, you can pass
1635 * the context pointer as first parameter; if you won't but name begins
1636 * with %$ the context will be automatically computed. If all_contexts
1637 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001638 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001639static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001640smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001641 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001642{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001643 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001644 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645
H. Peter Anvin97a23472007-09-16 17:57:25 -07001646 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001647 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001648 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001649 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001650 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001652 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001653 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001654 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001655 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001656 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001657 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001658
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 while (m) {
1660 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001661 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001662 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001663 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 *defn = m;
1665 else
1666 *defn = NULL;
1667 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001668 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 }
1670 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001671 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001672
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001673 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001674}
1675
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001676/* param should be a natural number [0; INT_MAX] */
1677static int read_param_count(const char *str)
1678{
1679 int result;
1680 bool err;
1681
1682 result = readnum(str, &err);
1683 if (result < 0 || result > INT_MAX) {
1684 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001685 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1686 str, 0, INT_MAX);
1687 } else if (err)
1688 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001689 return result;
1690}
1691
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001692/*
1693 * Count and mark off the parameters in a multi-line macro call.
1694 * This is called both from within the multi-line macro expansion
1695 * code, and also to mark off the default parameters when provided
1696 * in a %macro definition line.
1697 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001699{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001700 int paramsize, brace;
1701
1702 *nparam = paramsize = 0;
1703 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001704 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001705 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001706 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001707 paramsize += PARAM_DELTA;
1708 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1709 }
1710 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001711 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001712 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001713 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001715 if (brace) {
1716 while (brace && (t = t->next) != NULL) {
1717 if (tok_is_(t, "{"))
1718 brace++;
1719 else if (tok_is_(t, "}"))
1720 brace--;
1721 }
1722
1723 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001724 /*
1725 * Now we've found the closing brace, look further
1726 * for the comma.
1727 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001728 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 skip_white_(t);
1730 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001731 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 while (tok_isnt_(t, ","))
1733 t = t->next;
1734 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001736 } else {
1737 while (tok_isnt_(t, ","))
1738 t = t->next;
1739 }
1740 if (t) { /* got a comma/brace */
1741 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001743 }
1744}
1745
1746/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001747 * Determine whether one of the various `if' conditions is true or
1748 * not.
1749 *
1750 * We must free the tline we get passed.
1751 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001752static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001753{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001754 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001755 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001756 Token *t, *tt, *origline;
1757 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001758 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001759 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001760 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001761 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001762
1763 origline = tline;
1764
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001766 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001767 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001768 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001770 if (!tline)
1771 break;
1772 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001773 nasm_nonfatal("`%s' expects context identifiers",
1774 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 free_tlist(origline);
1776 return -1;
1777 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001778 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001779 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 tline = tline->next;
1781 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001782 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001783
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001784 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001785 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001786 while (tline) {
1787 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 if (!tline || (tline->type != TOK_ID &&
1789 (tline->type != TOK_PREPROC_ID ||
1790 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001791 nasm_nonfatal("`%s' expects macro identifiers",
1792 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001793 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001795 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001796 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001798 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001799 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001800
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001801 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001802 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001803 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001804 while (tline) {
1805 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001806 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001807 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001808 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001809 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001810 nasm_nonfatal("`%s' expects environment variable names",
1811 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001812 goto fail;
1813 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001814 p = tline->text;
1815 if (tline->type == TOK_PREPROC_ID)
1816 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001817 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001818 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001819 if (getenv(p))
1820 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001821 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001822 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001823 break;
1824
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001825 case PPC_IFIDN:
1826 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 tline = expand_smacro(tline);
1828 t = tt = tline;
1829 while (tok_isnt_(tt, ","))
1830 tt = tt->next;
1831 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001832 nasm_nonfatal("`%s' expects two comma-separated arguments",
1833 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001834 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 }
1836 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001837 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1839 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001840 nasm_nonfatal("`%s': more than one comma on line",
1841 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001842 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 }
1844 if (t->type == TOK_WHITESPACE) {
1845 t = t->next;
1846 continue;
1847 }
1848 if (tt->type == TOK_WHITESPACE) {
1849 tt = tt->next;
1850 continue;
1851 }
1852 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001853 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 break;
1855 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001856 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001858 size_t l1 = nasm_unquote(t->text, NULL);
1859 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001860
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001861 if (l1 != l2) {
1862 j = false;
1863 break;
1864 }
1865 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1866 j = false;
1867 break;
1868 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001869 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001870 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 break;
1872 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001873
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 t = t->next;
1875 tt = tt->next;
1876 }
1877 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001878 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001880
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001881 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001882 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001883 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001884 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001885
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886 skip_white_(tline);
1887 tline = expand_id(tline);
1888 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001889 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001890 goto fail;
1891 }
1892 searching.name = nasm_strdup(tline->text);
1893 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001894 searching.plus = false;
1895 searching.nolist = false;
1896 searching.in_progress = 0;
1897 searching.max_depth = 0;
1898 searching.rep_nest = NULL;
1899 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001900 searching.nparam_max = INT_MAX;
1901 tline = expand_smacro(tline->next);
1902 skip_white_(tline);
1903 if (!tline) {
1904 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001905 nasm_nonfatal("`%s' expects a parameter count or nothing",
1906 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 } else {
1908 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001909 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 }
1911 if (tline && tok_is_(tline->next, "-")) {
1912 tline = tline->next->next;
1913 if (tok_is_(tline, "*"))
1914 searching.nparam_max = INT_MAX;
1915 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001916 nasm_nonfatal("`%s' expects a parameter count after `-'",
1917 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001919 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001920 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001921 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001922 searching.nparam_max = searching.nparam_min;
1923 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001924 }
1925 }
1926 if (tline && tok_is_(tline->next, "+")) {
1927 tline = tline->next;
1928 searching.plus = true;
1929 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001930 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1931 while (mmac) {
1932 if (!strcmp(mmac->name, searching.name) &&
1933 (mmac->nparam_min <= searching.nparam_max
1934 || searching.plus)
1935 && (searching.nparam_min <= mmac->nparam_max
1936 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 found = true;
1938 break;
1939 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001940 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 }
1942 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001943 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 nasm_free(searching.name);
1945 j = found;
1946 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001947 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001948
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001949 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 needtype = TOK_ID;
1951 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001952 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001953 needtype = TOK_NUMBER;
1954 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001955 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001956 needtype = TOK_STRING;
1957 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001958
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959iftype:
1960 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001961
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 while (tok_type_(t, TOK_WHITESPACE) ||
1963 (needtype == TOK_NUMBER &&
1964 tok_type_(t, TOK_OTHER) &&
1965 (t->text[0] == '-' || t->text[0] == '+') &&
1966 !t->text[1]))
1967 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001968
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001969 j = tok_type_(t, needtype);
1970 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001971
1972 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 t = tline = expand_smacro(tline);
1974 while (tok_type_(t, TOK_WHITESPACE))
1975 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001976
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001977 j = false;
1978 if (t) {
1979 t = t->next; /* Skip the actual token */
1980 while (tok_type_(t, TOK_WHITESPACE))
1981 t = t->next;
1982 j = !t; /* Should be nothing left */
1983 }
1984 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001985
H. Peter Anvin134b9462008-02-16 17:01:40 -08001986 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 t = tline = expand_smacro(tline);
1988 while (tok_type_(t, TOK_WHITESPACE))
1989 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001990
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 j = !t; /* Should be empty */
1992 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08001993
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001994 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08001995 pps.tptr = tline = expand_smacro(tline);
1996 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001997 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07001998 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 if (!evalresult)
2000 return -1;
2001 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002002 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002004 nasm_nonfatal("non-constant value given to `%s'",
2005 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002006 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002008 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002009 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002010
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002012 nasm_fatal("preprocessor directive `%s' not yet implemented",
2013 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002015 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002016
2017 free_tlist(origline);
2018 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002019
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002020fail:
2021 free_tlist(origline);
2022 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002023}
2024
2025/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002026 * Common code for defining an smacro
2027 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002028static SMacro *define_smacro(Context *ctx, const char *mname,
2029 bool casesense, int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002030{
2031 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002032 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002033
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002034 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002035 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002036 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002037 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002038 /*
2039 * Some instances of the old code considered this a failure,
2040 * some others didn't. What is the right thing to do here?
2041 */
2042 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002043 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002044 } else {
2045 /*
2046 * We're redefining, so we have to take over an
2047 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002048 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002049 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002050 clear_smacro(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002051 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002052 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002053 smtbl = ctx ? &ctx->localmac : &smacros;
2054 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002055 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056 smac->next = *smhead;
2057 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002058 }
2059 smac->name = nasm_strdup(mname);
2060 smac->casesense = casesense;
2061 smac->nparam = nparam;
2062 smac->expansion = expansion;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002063 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002064}
2065
2066/*
2067 * Undefine an smacro
2068 */
2069static void undef_smacro(Context *ctx, const char *mname)
2070{
2071 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002072 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002073
H. Peter Anvin166c2472008-05-28 12:28:58 -07002074 smtbl = ctx ? &ctx->localmac : &smacros;
2075 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002076
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002077 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002078 /*
2079 * We now have a macro name... go hunt for it.
2080 */
2081 sp = smhead;
2082 while ((s = *sp) != NULL) {
2083 if (!mstrcmp(s->name, mname, s->casesense)) {
2084 *sp = s->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002085 free_smacro(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 } else {
2087 sp = &s->next;
2088 }
2089 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002090 }
2091}
2092
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002093/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002094 * Parse a mmacro specification.
2095 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002096static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002097{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002098 tline = tline->next;
2099 skip_white_(tline);
2100 tline = expand_id(tline);
2101 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002102 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002104 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002105
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002106 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002107 def->name = nasm_strdup(tline->text);
2108 def->plus = false;
2109 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002110 def->in_progress = 0;
2111 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002112 def->nparam_min = 0;
2113 def->nparam_max = 0;
2114
H. Peter Anvina26433d2008-07-16 14:40:01 -07002115 tline = expand_smacro(tline->next);
2116 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002117 if (!tok_type_(tline, TOK_NUMBER))
2118 nasm_nonfatal("`%s' expects a parameter count", directive);
2119 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002120 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002121 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002122 tline = tline->next->next;
2123 if (tok_is_(tline, "*")) {
2124 def->nparam_max = INT_MAX;
2125 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002126 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002127 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002128 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002129 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002130 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002131 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002132 }
2133 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002134 }
2135 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002136 tline = tline->next;
2137 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002138 }
2139 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002140 !nasm_stricmp(tline->next->text, ".nolist")) {
2141 tline = tline->next;
2142 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002143 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144
H. Peter Anvina26433d2008-07-16 14:40:01 -07002145 /*
2146 * Handle default parameters.
2147 */
2148 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 def->dlist = tline->next;
2150 tline->next = NULL;
2151 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002152 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002153 def->dlist = NULL;
2154 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002155 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002156 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002157
H. Peter Anvin89cee572009-07-15 09:16:54 -04002158 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002159 !def->plus) {
2160 /*
2161 *!macro-defaults [on] macros with more default than optional parameters
2162 *! warns when a macro has more default parameters than optional parameters.
2163 *! See \k{mlmacdef} for why might want to disable this warning.
2164 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002165 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002166 "too many default macro parameters in macro `%s'", def->name);
2167 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002168
H. Peter Anvina26433d2008-07-16 14:40:01 -07002169 return true;
2170}
2171
2172
2173/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002174 * Decode a size directive
2175 */
2176static int parse_size(const char *str) {
2177 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002178 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002179 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002181 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002182}
2183
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002184/*
2185 * Process a preprocessor %pragma directive. Currently there are none.
2186 * Gets passed the token list starting with the "preproc" token from
2187 * "%pragma preproc".
2188 */
2189static void do_pragma_preproc(Token *tline)
2190{
2191 /* Skip to the real stuff */
2192 tline = tline->next;
2193 skip_white_(tline);
2194 if (!tline)
2195 return;
2196
2197 (void)tline; /* Nothing else to do at present */
2198}
2199
Ed Beroset3ab3f412002-06-11 03:31:49 +00002200/**
2201 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002202 * Find out if a line contains a preprocessor directive, and deal
2203 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002204 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002205 * If a directive _is_ found, it is the responsibility of this routine
2206 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002207 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002208 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002209 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002210 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002211 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002212 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002213static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002214{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002215 enum preproc_token i;
2216 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002217 bool err;
2218 int nparam;
2219 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002220 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002221 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002222 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002223 char *p, *pp;
2224 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002225 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002226 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002227 Include *inc;
2228 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002229 Cond *cond;
2230 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002231 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002232 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002233 struct tokenval tokval;
2234 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002235 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002236 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002237 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002238 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002239 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002240
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002241 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002242 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002243
H. Peter Anvineba20a72002-04-30 20:53:55 +00002244 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002245 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002246 (tline->text[0] && (tline->text[1] == '%' ||
2247 tline->text[1] == '$' ||
2248 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002249 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002250
H. Peter Anvin4169a472007-09-12 01:29:43 +00002251 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002252
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002253 /*
2254 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2255 * since they are known to be buggy at moment, we need to fix them
2256 * in future release (2.09-2.10)
2257 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002258 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002259 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2260 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002261 }
2262
2263 /*
2264 * If we're in a non-emitting branch of a condition construct,
2265 * or walking to the end of an already terminated %rep block,
2266 * we should ignore all directives except for condition
2267 * directives.
2268 */
2269 if (((istk->conds && !emitting(istk->conds->state)) ||
2270 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2271 return NO_DIRECTIVE_FOUND;
2272 }
2273
2274 /*
2275 * If we're defining a macro or reading a %rep block, we should
2276 * ignore all directives except for %macro/%imacro (which nest),
2277 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2278 * If we're in a %rep block, another %rep nests, so should be let through.
2279 */
2280 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2281 i != PP_RMACRO && i != PP_IRMACRO &&
2282 i != PP_ENDMACRO && i != PP_ENDM &&
2283 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2284 return NO_DIRECTIVE_FOUND;
2285 }
2286
2287 if (defining) {
2288 if (i == PP_MACRO || i == PP_IMACRO ||
2289 i == PP_RMACRO || i == PP_IRMACRO) {
2290 nested_mac_count++;
2291 return NO_DIRECTIVE_FOUND;
2292 } else if (nested_mac_count > 0) {
2293 if (i == PP_ENDMACRO) {
2294 nested_mac_count--;
2295 return NO_DIRECTIVE_FOUND;
2296 }
2297 }
2298 if (!defining->name) {
2299 if (i == PP_REP) {
2300 nested_rep_count++;
2301 return NO_DIRECTIVE_FOUND;
2302 } else if (nested_rep_count > 0) {
2303 if (i == PP_ENDREP) {
2304 nested_rep_count--;
2305 return NO_DIRECTIVE_FOUND;
2306 }
2307 }
2308 }
2309 }
2310
H. Peter Anvin8b262472019-02-26 14:00:54 -08002311 dname = pp_directives[i]; /* Directive name, for error messages */
2312 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002313 switch (i) {
2314 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002315 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002316 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002317
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002318 case PP_PRAGMA:
2319 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002320 * %pragma namespace options...
2321 *
2322 * The namespace "preproc" is reserved for the preprocessor;
2323 * all other namespaces generate a [pragma] assembly directive.
2324 *
2325 * Invalid %pragmas are ignored and may have different
2326 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002327 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002328 tline = tline->next;
2329 skip_white_(tline);
2330 tline = expand_smacro(tline);
2331 if (tok_type_(tline, TOK_ID)) {
2332 if (!nasm_stricmp(tline->text, "preproc")) {
2333 /* Preprocessor pragma */
2334 do_pragma_preproc(tline);
2335 } else {
2336 /* Build the assembler directive */
2337 t = new_Token(NULL, TOK_OTHER, "[", 1);
2338 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2339 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2340 tline = t;
2341 for (t = tline; t->next; t = t->next)
2342 ;
2343 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002344 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002345 }
2346 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002347 free_tlist(origline);
2348 return DIRECTIVE_FOUND;
2349
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 case PP_STACKSIZE:
2351 /* Directive to tell NASM what the default stack size is. The
2352 * default is for a 16-bit stack, and this can be overriden with
2353 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 */
2355 tline = tline->next;
2356 if (tline && tline->type == TOK_WHITESPACE)
2357 tline = tline->next;
2358 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002359 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 free_tlist(origline);
2361 return DIRECTIVE_FOUND;
2362 }
2363 if (nasm_stricmp(tline->text, "flat") == 0) {
2364 /* All subsequent ARG directives are for a 32-bit stack */
2365 StackSize = 4;
2366 StackPointer = "ebp";
2367 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002368 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002369 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2370 /* All subsequent ARG directives are for a 64-bit stack */
2371 StackSize = 8;
2372 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002373 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002374 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 } else if (nasm_stricmp(tline->text, "large") == 0) {
2376 /* All subsequent ARG directives are for a 16-bit stack,
2377 * far function call.
2378 */
2379 StackSize = 2;
2380 StackPointer = "bp";
2381 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002382 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 } else if (nasm_stricmp(tline->text, "small") == 0) {
2384 /* All subsequent ARG directives are for a 16-bit stack,
2385 * far function call. We don't support near functions.
2386 */
2387 StackSize = 2;
2388 StackPointer = "bp";
2389 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002390 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002392 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393 free_tlist(origline);
2394 return DIRECTIVE_FOUND;
2395 }
2396 free_tlist(origline);
2397 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002398
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 case PP_ARG:
2400 /* TASM like ARG directive to define arguments to functions, in
2401 * the following form:
2402 *
2403 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2404 */
2405 offset = ArgOffset;
2406 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002407 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002408 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002409
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410 /* Find the argument name */
2411 tline = tline->next;
2412 if (tline && tline->type == TOK_WHITESPACE)
2413 tline = tline->next;
2414 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002415 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002416 free_tlist(origline);
2417 return DIRECTIVE_FOUND;
2418 }
2419 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 /* Find the argument size type */
2422 tline = tline->next;
2423 if (!tline || tline->type != TOK_OTHER
2424 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002425 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002426 free_tlist(origline);
2427 return DIRECTIVE_FOUND;
2428 }
2429 tline = tline->next;
2430 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002431 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 free_tlist(origline);
2433 return DIRECTIVE_FOUND;
2434 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002435
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002437 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002439 size = parse_size(tt->text);
2440 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002441 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002442 free_tlist(tt);
2443 free_tlist(origline);
2444 return DIRECTIVE_FOUND;
2445 }
2446 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002447
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002448 /* Round up to even stack slots */
2449 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002450
H. Peter Anvine2c80182005-01-15 22:15:51 +00002451 /* Now define the macro for the argument */
2452 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2453 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002454 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002456
H. Peter Anvine2c80182005-01-15 22:15:51 +00002457 /* Move to the next argument in the list */
2458 tline = tline->next;
2459 if (tline && tline->type == TOK_WHITESPACE)
2460 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002461 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002462 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 free_tlist(origline);
2464 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002465
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 case PP_LOCAL:
2467 /* TASM like LOCAL directive to define local variables for a
2468 * function, in the following form:
2469 *
2470 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2471 *
2472 * The '= LocalSize' at the end is ignored by NASM, but is
2473 * required by TASM to define the local parameter size (and used
2474 * by the TASM macro package).
2475 */
2476 offset = LocalOffset;
2477 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002478 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002480
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 /* Find the argument name */
2482 tline = tline->next;
2483 if (tline && tline->type == TOK_WHITESPACE)
2484 tline = tline->next;
2485 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002486 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002487 free_tlist(origline);
2488 return DIRECTIVE_FOUND;
2489 }
2490 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002491
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 /* Find the argument size type */
2493 tline = tline->next;
2494 if (!tline || tline->type != TOK_OTHER
2495 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002496 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002497 free_tlist(origline);
2498 return DIRECTIVE_FOUND;
2499 }
2500 tline = tline->next;
2501 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002502 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 free_tlist(origline);
2504 return DIRECTIVE_FOUND;
2505 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002506
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002508 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002510 size = parse_size(tt->text);
2511 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002512 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 free_tlist(tt);
2514 free_tlist(origline);
2515 return DIRECTIVE_FOUND;
2516 }
2517 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002518
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002519 /* Round up to even stack slots */
2520 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002521
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002522 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002523
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002524 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2526 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002527 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002528
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 /* Now define the assign to setup the enter_c macro correctly */
2530 snprintf(directive, sizeof(directive),
2531 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002532 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 /* Move to the next argument in the list */
2535 tline = tline->next;
2536 if (tline && tline->type == TOK_WHITESPACE)
2537 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002538 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002539 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 free_tlist(origline);
2541 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002542
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 case PP_CLEAR:
2544 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002545 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 free_macros();
2547 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002548 free_tlist(origline);
2549 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002550
H. Peter Anvin418ca702008-05-30 10:42:30 -07002551 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002552 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002553 skip_white_(t);
2554 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002555 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002556 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002557 free_tlist(origline);
2558 return DIRECTIVE_FOUND; /* but we did _something_ */
2559 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002560 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002561 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002562 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002563 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002564 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002565 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002566 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002567 return DIRECTIVE_FOUND;
2568
2569 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002570 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002571 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002572
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002573 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002575 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 free_tlist(origline);
2577 return DIRECTIVE_FOUND; /* but we did _something_ */
2578 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002579 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002580 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002581 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002582 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002583 nasm_unquote_cstr(p, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002584 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002586 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002587 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002588 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002589 (pp_mode == PP_DEPS)
2590 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 if (!inc->fp) {
2592 /* -MG given but file not found */
2593 nasm_free(inc);
2594 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002595 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 inc->lineno = src_set_linnum(0);
2597 inc->lineinc = 1;
2598 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002599 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002600 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002601 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002602 }
2603 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002605
H. Peter Anvind2456592008-06-19 15:04:18 -07002606 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002607 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 static macros_t *use_pkg;
2609 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002610
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002611 tline = tline->next;
2612 skip_white_(tline);
2613 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002614
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002615 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 tline->type != TOK_INTERNAL_STRING &&
2617 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002618 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002619 free_tlist(origline);
2620 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002621 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002622 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002623 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002625 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002626 use_pkg = nasm_stdmac_find_package(tline->text);
2627 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002628 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 else
2630 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002631 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 /* Not already included, go ahead and include it */
2633 stdmacpos = use_pkg;
2634 }
2635 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002636 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002637 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002640 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002641 tline = tline->next;
2642 skip_white_(tline);
2643 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 if (tline) {
2645 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002646 nasm_nonfatal("`%s' expects a context identifier",
2647 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002648 free_tlist(origline);
2649 return DIRECTIVE_FOUND; /* but we did _something_ */
2650 }
2651 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002652 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002653 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 p = nasm_strdup(tline->text);
2655 } else {
2656 p = NULL; /* Anonymous */
2657 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002658
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002659 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002660 nasm_new(ctx);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002662 ctx->name = p;
2663 ctx->number = unique++;
2664 cstk = ctx;
2665 } else {
2666 /* %pop or %repl */
2667 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002668 nasm_nonfatal("`%s': context stack is empty",
2669 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 } else if (i == PP_POP) {
2671 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002672 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002673 "expected %s",
2674 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 else
2676 ctx_pop();
2677 } else {
2678 /* i == PP_REPL */
2679 nasm_free(cstk->name);
2680 cstk->name = p;
2681 p = NULL;
2682 }
2683 nasm_free(p);
2684 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002685 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002686 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002687 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 severity = ERR_FATAL;
2689 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002690 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002691 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002693 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002694 /*!
2695 *!user [on] %warning directives
2696 *! controls output of \c{%warning} directives (see \k{pperror}).
2697 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002698 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002700
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002702 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002703 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 tline->next = expand_smacro(tline->next);
2705 tline = tline->next;
2706 skip_white_(tline);
2707 t = tline ? tline->next : NULL;
2708 skip_white_(t);
2709 if (tok_type_(tline, TOK_STRING) && !t) {
2710 /* The line contains only a quoted string */
2711 p = tline->text;
2712 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002713 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 } else {
2715 /* Not a quoted string, or more than a quoted string */
2716 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002717 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 nasm_free(p);
2719 }
2720 free_tlist(origline);
2721 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002722 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002723
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002724 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002725 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002726 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002727 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002728 j = if_condition(tline->next, i);
2729 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002730 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002731 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002732 cond = nasm_malloc(sizeof(Cond));
2733 cond->next = istk->conds;
2734 cond->state = j;
2735 istk->conds = cond;
2736 if(istk->mstk)
2737 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002740
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002741 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002742 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002743 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002744 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002745 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002746 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002747 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002748
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002749 case COND_DONE:
2750 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002751 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002752
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002753 case COND_ELSE_TRUE:
2754 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002755 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002756 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002757 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002758 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002759
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 case COND_IF_FALSE:
2761 /*
2762 * IMPORTANT: In the case of %if, we will already have
2763 * called expand_mmac_params(); however, if we're
2764 * processing an %elif we must have been in a
2765 * non-emitting mode, which would have inhibited
2766 * the normal invocation of expand_mmac_params().
2767 * Therefore, we have to do it explicitly here.
2768 */
2769 j = if_condition(expand_mmac_params(tline->next), i);
2770 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002771 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002772 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2773 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002775 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002776 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002777
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 case PP_ELSE:
2779 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002780 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002781 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002782 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002783 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002784 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002785 case COND_IF_TRUE:
2786 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002787 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002788 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002789
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002790 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002791 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002792
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002793 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002794 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002795 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002796
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 case COND_ELSE_TRUE:
2798 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002799 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002800 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002801 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002802 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002803 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002804 free_tlist(origline);
2805 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002806
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 case PP_ENDIF:
2808 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002809 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002810 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002811 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002812 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002813 cond = istk->conds;
2814 istk->conds = cond->next;
2815 nasm_free(cond);
2816 if(istk->mstk)
2817 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 free_tlist(origline);
2819 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002820
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002821 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002822 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002823 casesense = false;
2824 /* fall through */
2825 case PP_RMACRO:
2826 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002827 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002828 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 return DIRECTIVE_FOUND;
2830 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002831 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002832 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2833 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002834 defining->casesense = casesense;
2835 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002836 nasm_free(defining);
2837 defining = NULL;
2838 return DIRECTIVE_FOUND;
2839 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002840
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002841 src_get(&defining->xline, &defining->fname);
2842
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002843 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2844 while (mmac) {
2845 if (!strcmp(mmac->name, defining->name) &&
2846 (mmac->nparam_min <= defining->nparam_max
2847 || defining->plus)
2848 && (defining->nparam_min <= mmac->nparam_max
2849 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002850 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002851 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002852 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002853 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002854 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002855 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 free_tlist(origline);
2857 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002858
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 case PP_ENDM:
2860 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002861 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002862 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002863 return DIRECTIVE_FOUND;
2864 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002865 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2866 defining->next = *mmhead;
2867 *mmhead = defining;
2868 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 free_tlist(origline);
2870 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002871
H. Peter Anvin89cee572009-07-15 09:16:54 -04002872 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002873 /*
2874 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002875 * macro-end marker for a macro with a name. Then we
2876 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002877 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002878 list_for_each(l, istk->expansion)
2879 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002880 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002881
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002882 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002883 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002884 * Remove all conditional entries relative to this
2885 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002886 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002887 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2888 cond = istk->conds;
2889 istk->conds = cond->next;
2890 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002891 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002892 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002893 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002894 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002896 free_tlist(origline);
2897 return DIRECTIVE_FOUND;
2898
H. Peter Anvina26433d2008-07-16 14:40:01 -07002899 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002900 casesense = false;
2901 /* fall through */
2902 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002903 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002904 MMacro **mmac_p;
2905 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002906
H. Peter Anvin8b262472019-02-26 14:00:54 -08002907 spec.casesense = casesense;
2908 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002909 return DIRECTIVE_FOUND;
2910 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002911 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2912 while (mmac_p && *mmac_p) {
2913 mmac = *mmac_p;
2914 if (mmac->casesense == spec.casesense &&
2915 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2916 mmac->nparam_min == spec.nparam_min &&
2917 mmac->nparam_max == spec.nparam_max &&
2918 mmac->plus == spec.plus) {
2919 *mmac_p = mmac->next;
2920 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002921 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002922 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002923 }
2924 }
2925 free_tlist(origline);
2926 free_tlist(spec.dlist);
2927 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002928 }
2929
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 case PP_ROTATE:
2931 if (tline->next && tline->next->type == TOK_WHITESPACE)
2932 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002933 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002934 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002935 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002936 return DIRECTIVE_FOUND;
2937 }
2938 t = expand_smacro(tline->next);
2939 tline->next = NULL;
2940 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002941 pps.tptr = tline = t;
2942 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002943 tokval.t_type = TOKEN_INVALID;
2944 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002945 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002946 free_tlist(tline);
2947 if (!evalresult)
2948 return DIRECTIVE_FOUND;
2949 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002950 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002952 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 return DIRECTIVE_FOUND;
2954 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002955 mmac = istk->mstk;
2956 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2957 mmac = mmac->next_active;
2958 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002959 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002960 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002961 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002962 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002963 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002964
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002965 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002966 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002967 rotate += mmac->nparam;
2968
2969 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002970 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002971 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002972
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002974 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 do {
2976 tline = tline->next;
2977 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002978
H. Peter Anvine2c80182005-01-15 22:15:51 +00002979 if (tok_type_(tline, TOK_ID) &&
2980 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002981 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 do {
2983 tline = tline->next;
2984 } while (tok_type_(tline, TOK_WHITESPACE));
2985 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002986
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08002988 pps.tptr = expand_smacro(tline);
2989 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002991 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002992 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002993 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002994 if (!evalresult) {
2995 free_tlist(origline);
2996 return DIRECTIVE_FOUND;
2997 }
2998 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002999 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003001 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 return DIRECTIVE_FOUND;
3003 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003004 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003005 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003006 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3007 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003008 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003009 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003010 /*!
3011 *!negative-rep [on] regative %rep count
3012 *! warns about negative counts given to the \c{%rep}
3013 *! preprocessor directive.
3014 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003015 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003016 "negative `%%rep' count: %"PRId64, count);
3017 count = 0;
3018 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003019 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003020 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003022 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003023 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 }
3025 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003026
3027 tmp_defining = defining;
3028 defining = nasm_malloc(sizeof(MMacro));
3029 defining->prev = NULL;
3030 defining->name = NULL; /* flags this macro as a %rep block */
3031 defining->casesense = false;
3032 defining->plus = false;
3033 defining->nolist = nolist;
3034 defining->in_progress = count;
3035 defining->max_depth = 0;
3036 defining->nparam_min = defining->nparam_max = 0;
3037 defining->defaults = NULL;
3038 defining->dlist = NULL;
3039 defining->expansion = NULL;
3040 defining->next_active = istk->mstk;
3041 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003042 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003043
H. Peter Anvine2c80182005-01-15 22:15:51 +00003044 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003045 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003046 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 return DIRECTIVE_FOUND;
3048 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003049
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 /*
3051 * Now we have a "macro" defined - although it has no name
3052 * and we won't be entering it in the hash tables - we must
3053 * push a macro-end marker for it on to istk->expansion.
3054 * After that, it will take care of propagating itself (a
3055 * macro-end marker line for a macro which is really a %rep
3056 * block will cause the macro to be re-expanded, complete
3057 * with another macro-end marker to ensure the process
3058 * continues) until the whole expansion is forcibly removed
3059 * from istk->expansion by a %exitrep.
3060 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003061 l = nasm_malloc(sizeof(Line));
3062 l->next = istk->expansion;
3063 l->finishes = defining;
3064 l->first = NULL;
3065 istk->expansion = l;
3066
3067 istk->mstk = defining;
3068
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003069 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003070 tmp_defining = defining;
3071 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 free_tlist(origline);
3073 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003074
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003076 /*
3077 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003078 * macro-end marker for a macro with no name. Then we set
3079 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003080 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003081 list_for_each(l, istk->expansion)
3082 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003083 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003084
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003085 if (l)
3086 l->finishes->in_progress = 1;
3087 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003088 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003089 free_tlist(origline);
3090 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003091
H. Peter Anvine2c80182005-01-15 22:15:51 +00003092 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003093 case PP_IXDEFINE:
3094 casesense = false;
3095 /* fall through */
3096 case PP_DEFINE:
3097 case PP_XDEFINE:
3098 {
3099 SMacro *s;
3100 bool have_eval_params = false;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003101
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 tline = tline->next;
3103 skip_white_(tline);
3104 tline = expand_id(tline);
3105 if (!tline || (tline->type != TOK_ID &&
3106 (tline->type != TOK_PREPROC_ID ||
3107 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003108 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003109 free_tlist(origline);
3110 return DIRECTIVE_FOUND;
3111 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003112
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003113 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 last = tline;
3115 param_start = tline = tline->next;
3116 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003117
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 if (tok_is_(tline, "(")) {
3119 /*
3120 * This macro has parameters.
3121 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003122
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123 tline = tline->next;
3124 while (1) {
3125 skip_white_(tline);
3126 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003127 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 free_tlist(origline);
3129 return DIRECTIVE_FOUND;
3130 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003131 if (tok_is_(tline, "=")) {
3132 have_eval_params = true;
3133 tline = tline->next;
3134 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003135 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003136 nasm_nonfatal("`%s': parameter identifier expected",
3137 tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 free_tlist(origline);
3139 return DIRECTIVE_FOUND;
3140 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003141 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 tline = tline->next;
3143 skip_white_(tline);
3144 if (tok_is_(tline, ",")) {
3145 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003146 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003147 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003148 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003149 free_tlist(origline);
3150 return DIRECTIVE_FOUND;
3151 }
3152 break;
3153 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 }
3155 last = tline;
3156 tline = tline->next;
3157 }
3158 if (tok_type_(tline, TOK_WHITESPACE))
3159 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003161
3162 /* Expand the macro definition now for %xdefine and %ixdefine */
3163 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3164 tline = expand_smacro(tline);
3165
3166 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 t = tline;
3168 while (t) {
3169 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003170 list_for_each(tt, param_start)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003171 if (is_smac_param(tt->type) &&
3172 tt->len == t->len &&
3173 !memcmp(tt->text, t->text, tt->len))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003174 t->type = tt->type;
3175 }
3176 tt = t->next;
3177 t->next = macro_start;
3178 macro_start = t;
3179 t = tt;
3180 }
3181 /*
3182 * Good. We now have a macro name, a parameter count, and a
3183 * token list (in reverse order) for an expansion. We ought
3184 * to be OK just to create an SMacro, store it, and let
3185 * free_tlist have the rest of the line (which we have
3186 * carefully re-terminated after chopping off the expansion
3187 * from the end).
3188 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003189 s = define_smacro(ctx, mname, casesense, nparam, macro_start);
3190
3191 if (have_eval_params) {
3192 /* Create evaluated parameters table */
3193 bool is_eval = false;
3194
3195 nasm_newn(s->eval_param, nparam);
3196 list_for_each(tt, param_start) {
3197 if (is_smac_param(tt->type))
3198 s->eval_param[smac_nparam(tt->type)] = is_eval;
3199 is_eval = tok_is_(tt, "=");
3200 }
3201 }
3202
3203
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 free_tlist(origline);
3205 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003206 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003207
H. Peter Anvine2c80182005-01-15 22:15:51 +00003208 case PP_UNDEF:
3209 tline = tline->next;
3210 skip_white_(tline);
3211 tline = expand_id(tline);
3212 if (!tline || (tline->type != TOK_ID &&
3213 (tline->type != TOK_PREPROC_ID ||
3214 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003215 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003216 free_tlist(origline);
3217 return DIRECTIVE_FOUND;
3218 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003219 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003220 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003221
H. Peter Anvine2c80182005-01-15 22:15:51 +00003222 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003223 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003224 undef_smacro(ctx, mname);
3225 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003227
H. Peter Anvin9e200162008-06-04 17:23:14 -07003228 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003229 casesense = false;
3230 /* fall through */
3231 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003232 tline = tline->next;
3233 skip_white_(tline);
3234 tline = expand_id(tline);
3235 if (!tline || (tline->type != TOK_ID &&
3236 (tline->type != TOK_PREPROC_ID ||
3237 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003238 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003239 free_tlist(origline);
3240 return DIRECTIVE_FOUND;
3241 }
3242
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003243 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003244 last = tline;
3245 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003246 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003247
3248 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003249 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003250
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003251 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003252 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003253 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003254
3255 /*
3256 * We now have a macro name, an implicit parameter count of
3257 * zero, and a string token to use as an expansion. Create
3258 * and store an SMacro.
3259 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003260 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003261 free_tlist(origline);
3262 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003263
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003264 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003265 casesense = false;
3266 /* fall through */
3267 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003268 tline = tline->next;
3269 skip_white_(tline);
3270 tline = expand_id(tline);
3271 if (!tline || (tline->type != TOK_ID &&
3272 (tline->type != TOK_PREPROC_ID ||
3273 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003274 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003275 free_tlist(origline);
3276 return DIRECTIVE_FOUND;
3277 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003278 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003279 last = tline;
3280 tline = expand_smacro(tline->next);
3281 last->next = NULL;
3282
3283 t = tline;
3284 while (tok_type_(t, TOK_WHITESPACE))
3285 t = t->next;
3286 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003287 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003288 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003289 free_tlist(tline);
3290 free_tlist(origline);
3291 return DIRECTIVE_FOUND;
3292 }
3293
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003294 /*
3295 * Convert the string to a token stream. Note that smacros
3296 * are stored with the token stream reversed, so we have to
3297 * reverse the output of tokenize().
3298 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003299 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003300 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003301
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003302 /*
3303 * We now have a macro name, an implicit parameter count of
3304 * zero, and a numeric token to use as an expansion. Create
3305 * and store an SMacro.
3306 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003307 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003308 free_tlist(tline);
3309 free_tlist(origline);
3310 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003311
H. Peter Anvin8b262472019-02-26 14:00:54 -08003312 case PP_IPATHSEARCH:
3313 casesense = false;
3314 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003315 case PP_PATHSEARCH:
3316 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003317 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003318
H. Peter Anvin418ca702008-05-30 10:42:30 -07003319 tline = tline->next;
3320 skip_white_(tline);
3321 tline = expand_id(tline);
3322 if (!tline || (tline->type != TOK_ID &&
3323 (tline->type != TOK_PREPROC_ID ||
3324 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003325 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003326 free_tlist(origline);
3327 return DIRECTIVE_FOUND;
3328 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003329 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003330 last = tline;
3331 tline = expand_smacro(tline->next);
3332 last->next = NULL;
3333
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003334 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003335 while (tok_type_(t, TOK_WHITESPACE))
3336 t = t->next;
3337
3338 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003339 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003340 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003341 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003342 free_tlist(origline);
3343 return DIRECTIVE_FOUND; /* but we did _something_ */
3344 }
3345 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003346 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003347 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003348 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003349 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003350
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003351 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003352 if (!found_path)
3353 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003354 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003355
3356 /*
3357 * We now have a macro name, an implicit parameter count of
3358 * zero, and a string token to use as an expansion. Create
3359 * and store an SMacro.
3360 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003361 define_smacro(ctx, mname, casesense, 0, macro_start);
3362 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003363 free_tlist(origline);
3364 return DIRECTIVE_FOUND;
3365 }
3366
H. Peter Anvin8b262472019-02-26 14:00:54 -08003367 case PP_ISTRLEN:
3368 casesense = false;
3369 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003370 case PP_STRLEN:
3371 tline = tline->next;
3372 skip_white_(tline);
3373 tline = expand_id(tline);
3374 if (!tline || (tline->type != TOK_ID &&
3375 (tline->type != TOK_PREPROC_ID ||
3376 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003377 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003378 free_tlist(origline);
3379 return DIRECTIVE_FOUND;
3380 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003381 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003382 last = tline;
3383 tline = expand_smacro(tline->next);
3384 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003385
H. Peter Anvine2c80182005-01-15 22:15:51 +00003386 t = tline;
3387 while (tok_type_(t, TOK_WHITESPACE))
3388 t = t->next;
3389 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003390 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003391 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 free_tlist(tline);
3393 free_tlist(origline);
3394 return DIRECTIVE_FOUND;
3395 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003396
H. Peter Anvin8b262472019-02-26 14:00:54 -08003397 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003398
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 /*
3400 * We now have a macro name, an implicit parameter count of
3401 * zero, and a numeric token to use as an expansion. Create
3402 * and store an SMacro.
3403 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003405 free_tlist(tline);
3406 free_tlist(origline);
3407 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003408
H. Peter Anvin8b262472019-02-26 14:00:54 -08003409 case PP_ISTRCAT:
3410 casesense = false;
3411 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003412 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003413 tline = tline->next;
3414 skip_white_(tline);
3415 tline = expand_id(tline);
3416 if (!tline || (tline->type != TOK_ID &&
3417 (tline->type != TOK_PREPROC_ID ||
3418 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003419 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003420 free_tlist(origline);
3421 return DIRECTIVE_FOUND;
3422 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003423 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003424 last = tline;
3425 tline = expand_smacro(tline->next);
3426 last->next = NULL;
3427
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003428 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003429 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 switch (t->type) {
3431 case TOK_WHITESPACE:
3432 break;
3433 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003434 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003435 break;
3436 case TOK_OTHER:
3437 if (!strcmp(t->text, ",")) /* permit comma separators */
3438 break;
3439 /* else fall through */
3440 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003441 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003442 free_tlist(tline);
3443 free_tlist(origline);
3444 return DIRECTIVE_FOUND;
3445 }
3446 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003447
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003448 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003449 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003450 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003451 memcpy(p, t->text, t->len);
3452 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003453 }
3454 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003455
3456 /*
3457 * We now have a macro name, an implicit parameter count of
3458 * zero, and a numeric token to use as an expansion. Create
3459 * and store an SMacro.
3460 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003461 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003462 nasm_free(pp);
3463 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003464 free_tlist(tline);
3465 free_tlist(origline);
3466 return DIRECTIVE_FOUND;
3467
H. Peter Anvin8b262472019-02-26 14:00:54 -08003468 case PP_ISUBSTR:
3469 casesense = false;
3470 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003471 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003472 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003473 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003474 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003475
H. Peter Anvine2c80182005-01-15 22:15:51 +00003476 tline = tline->next;
3477 skip_white_(tline);
3478 tline = expand_id(tline);
3479 if (!tline || (tline->type != TOK_ID &&
3480 (tline->type != TOK_PREPROC_ID ||
3481 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003482 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003483 free_tlist(origline);
3484 return DIRECTIVE_FOUND;
3485 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003486 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003487 last = tline;
3488 tline = expand_smacro(tline->next);
3489 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003490
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003491 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003492 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 while (tok_type_(t, TOK_WHITESPACE))
3494 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003495
H. Peter Anvine2c80182005-01-15 22:15:51 +00003496 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003497 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003498 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003499 free_tlist(tline);
3500 free_tlist(origline);
3501 return DIRECTIVE_FOUND;
3502 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003503
H. Peter Anvin8b262472019-02-26 14:00:54 -08003504 pps.tptr = t->next;
3505 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003506 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003507 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003508 if (!evalresult) {
3509 free_tlist(tline);
3510 free_tlist(origline);
3511 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003512 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003513 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003514 free_tlist(tline);
3515 free_tlist(origline);
3516 return DIRECTIVE_FOUND;
3517 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003518 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003519
H. Peter Anvin8b262472019-02-26 14:00:54 -08003520 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3521 pps.tptr = pps.tptr->next;
3522 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003523 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003524 } else {
3525 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003526 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 if (!evalresult) {
3528 free_tlist(tline);
3529 free_tlist(origline);
3530 return DIRECTIVE_FOUND;
3531 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003532 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003533 free_tlist(tline);
3534 free_tlist(origline);
3535 return DIRECTIVE_FOUND;
3536 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003537 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003538 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003539
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003540 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003541
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003542 /* make start and count being in range */
3543 if (start < 0)
3544 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003545 if (count < 0)
3546 count = len + count + 1 - start;
3547 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003548 count = len - start;
3549 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003550 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003551
H. Peter Anvin8b262472019-02-26 14:00:54 -08003552 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003553 macro_start->len = count;
3554 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3555 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003556
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 /*
3558 * We now have a macro name, an implicit parameter count of
3559 * zero, and a numeric token to use as an expansion. Create
3560 * and store an SMacro.
3561 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003562 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 free_tlist(tline);
3564 free_tlist(origline);
3565 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003566 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003567
H. Peter Anvine2c80182005-01-15 22:15:51 +00003568 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003569 casesense = false;
3570 /* fall through */
3571 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003572 tline = tline->next;
3573 skip_white_(tline);
3574 tline = expand_id(tline);
3575 if (!tline || (tline->type != TOK_ID &&
3576 (tline->type != TOK_PREPROC_ID ||
3577 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003578 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 free_tlist(origline);
3580 return DIRECTIVE_FOUND;
3581 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003582 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003583 last = tline;
3584 tline = expand_smacro(tline->next);
3585 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003586
H. Peter Anvin8b262472019-02-26 14:00:54 -08003587 pps.tptr = tline;
3588 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003589 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003590 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003591 free_tlist(tline);
3592 if (!evalresult) {
3593 free_tlist(origline);
3594 return DIRECTIVE_FOUND;
3595 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003596
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003598 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003599
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003601 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003602 free_tlist(origline);
3603 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003604 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003605
H. Peter Anvin8b262472019-02-26 14:00:54 -08003606 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003607
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 /*
3609 * We now have a macro name, an implicit parameter count of
3610 * zero, and a numeric token to use as an expansion. Create
3611 * and store an SMacro.
3612 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003613 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003614 free_tlist(origline);
3615 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 case PP_LINE:
3618 /*
3619 * Syntax is `%line nnn[+mmm] [filename]'
3620 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003621 if (unlikely(pp_noline)) {
3622 free_tlist(origline);
3623 return DIRECTIVE_FOUND;
3624 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 tline = tline->next;
3626 skip_white_(tline);
3627 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003628 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 free_tlist(origline);
3630 return DIRECTIVE_FOUND;
3631 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003632 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003633 m = 1;
3634 tline = tline->next;
3635 if (tok_is_(tline, "+")) {
3636 tline = tline->next;
3637 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003638 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 free_tlist(origline);
3640 return DIRECTIVE_FOUND;
3641 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003642 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 tline = tline->next;
3644 }
3645 skip_white_(tline);
3646 src_set_linnum(k);
3647 istk->lineinc = m;
3648 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003649 char *fname = detoken(tline, false);
3650 src_set_fname(fname);
3651 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003652 }
3653 free_tlist(origline);
3654 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003655
H. Peter Anvine2c80182005-01-15 22:15:51 +00003656 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003657 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003658 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003659 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003660}
3661
3662/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003663 * Ensure that a macro parameter contains a condition code and
3664 * nothing else. Return the condition code index if so, or -1
3665 * otherwise.
3666 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003667static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003668{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003669 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003670
H. Peter Anvin25a99342007-09-22 17:45:45 -07003671 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003672 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003673
H. Peter Anvineba20a72002-04-30 20:53:55 +00003674 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003675 if (!t)
3676 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003677 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003679 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003680 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003681 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003682 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003683
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003684 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003685}
3686
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003687/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003688 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003689 * pasting, if @handle_explicit passed then explicit pasting
3690 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003691 * The @m array can contain a series of token types which are
3692 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003693 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003694static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003695 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003696{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003697 Token *tok, *next, **prev_next, **prev_nonspace;
3698 bool pasted = false;
3699 char *buf, *p;
3700 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003701
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003702 /*
3703 * The last token before pasting. We need it
3704 * to be able to connect new handled tokens.
3705 * In other words if there were a tokens stream
3706 *
3707 * A -> B -> C -> D
3708 *
3709 * and we've joined tokens B and C, the resulting
3710 * stream should be
3711 *
3712 * A -> BC -> D
3713 */
3714 tok = *head;
3715 prev_next = NULL;
3716
3717 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3718 prev_nonspace = head;
3719 else
3720 prev_nonspace = NULL;
3721
3722 while (tok && (next = tok->next)) {
3723
3724 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003725 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003726 /* Zap redundant whitespaces */
3727 while (tok_type_(next, TOK_WHITESPACE))
3728 next = delete_Token(next);
3729 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003730 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003731
3732 case TOK_PASTE:
3733 /* Explicit pasting */
3734 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003735 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003736 next = delete_Token(tok);
3737
3738 while (tok_type_(next, TOK_WHITESPACE))
3739 next = delete_Token(next);
3740
3741 if (!pasted)
3742 pasted = true;
3743
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003744 /* Left pasting token is start of line */
3745 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003746 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003747
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003748 /*
3749 * No ending token, this might happen in two
3750 * cases
3751 *
3752 * 1) There indeed no right token at all
3753 * 2) There is a bare "%define ID" statement,
3754 * and @ID does expand to whitespace.
3755 *
3756 * So technically we need to do a grammar analysis
3757 * in another stage of parsing, but for now lets don't
3758 * change the behaviour people used to. Simply allow
3759 * whitespace after paste token.
3760 */
3761 if (!next) {
3762 /*
3763 * Zap ending space tokens and that's all.
3764 */
3765 tok = (*prev_nonspace)->next;
3766 while (tok_type_(tok, TOK_WHITESPACE))
3767 tok = delete_Token(tok);
3768 tok = *prev_nonspace;
3769 tok->next = NULL;
3770 break;
3771 }
3772
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003773 tok = *prev_nonspace;
3774 while (tok_type_(tok, TOK_WHITESPACE))
3775 tok = delete_Token(tok);
3776 len = strlen(tok->text);
3777 len += strlen(next->text);
3778
3779 p = buf = nasm_malloc(len + 1);
3780 strcpy(p, tok->text);
3781 p = strchr(p, '\0');
3782 strcpy(p, next->text);
3783
3784 delete_Token(tok);
3785
3786 tok = tokenize(buf);
3787 nasm_free(buf);
3788
3789 *prev_nonspace = tok;
3790 while (tok && tok->next)
3791 tok = tok->next;
3792
3793 tok->next = delete_Token(next);
3794
3795 /* Restart from pasted tokens head */
3796 tok = *prev_nonspace;
3797 break;
3798
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003799 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003800 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003801 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003802 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3803 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003804
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003805 len = 0;
3806 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3807 len += strlen(next->text);
3808 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003809 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003810
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003811 /* No match or no text to process */
3812 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003813 break;
3814
3815 len += strlen(tok->text);
3816 p = buf = nasm_malloc(len + 1);
3817
Adam Majer1a069432017-07-25 11:12:35 +02003818 strcpy(p, tok->text);
3819 p = strchr(p, '\0');
3820 tok = delete_Token(tok);
3821
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003822 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003823 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3824 strcpy(p, tok->text);
3825 p = strchr(p, '\0');
3826 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003827 tok = delete_Token(tok);
3828 }
3829
3830 tok = tokenize(buf);
3831 nasm_free(buf);
3832
3833 if (prev_next)
3834 *prev_next = tok;
3835 else
3836 *head = tok;
3837
3838 /*
3839 * Connect pasted into original stream,
3840 * ie A -> new-tokens -> B
3841 */
3842 while (tok && tok->next)
3843 tok = tok->next;
3844 tok->next = next;
3845
3846 if (!pasted)
3847 pasted = true;
3848
3849 /* Restart from pasted tokens head */
3850 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003851 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003852
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003853 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003854 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003855
3856 prev_next = &tok->next;
3857
3858 if (tok->next &&
3859 !tok_type_(tok->next, TOK_WHITESPACE) &&
3860 !tok_type_(tok->next, TOK_PASTE))
3861 prev_nonspace = prev_next;
3862
3863 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003864 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003865
3866 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003867}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003868
3869/*
3870 * expands to a list of tokens from %{x:y}
3871 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003872static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003873{
3874 Token *t = tline, **tt, *tm, *head;
3875 char *pos;
3876 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003877
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003878 pos = strchr(tline->text, ':');
3879 nasm_assert(pos);
3880
3881 lst = atoi(pos + 1);
3882 fst = atoi(tline->text + 1);
3883
3884 /*
3885 * only macros params are accounted so
3886 * if someone passes %0 -- we reject such
3887 * value(s)
3888 */
3889 if (lst == 0 || fst == 0)
3890 goto err;
3891
3892 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003893 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3894 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003895 goto err;
3896
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003897 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3898 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003899
3900 /* counted from zero */
3901 fst--, lst--;
3902
3903 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003904 * It will be at least one token. Note we
3905 * need to scan params until separator, otherwise
3906 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003907 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003908 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003909 if (!tm)
3910 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003911 head = new_Token(NULL, tm->type, tm->text, 0);
3912 tt = &head->next, tm = tm->next;
3913 while (tok_isnt_(tm, ",")) {
3914 t = new_Token(NULL, tm->type, tm->text, 0);
3915 *tt = t, tt = &t->next, tm = tm->next;
3916 }
3917
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003918 if (fst < lst) {
3919 for (i = fst + 1; i <= lst; i++) {
3920 t = new_Token(NULL, TOK_OTHER, ",", 0);
3921 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003922 j = (i + mac->rotate) % mac->nparam;
3923 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003924 while (tok_isnt_(tm, ",")) {
3925 t = new_Token(NULL, tm->type, tm->text, 0);
3926 *tt = t, tt = &t->next, tm = tm->next;
3927 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003928 }
3929 } else {
3930 for (i = fst - 1; i >= lst; i--) {
3931 t = new_Token(NULL, TOK_OTHER, ",", 0);
3932 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003933 j = (i + mac->rotate) % mac->nparam;
3934 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003935 while (tok_isnt_(tm, ",")) {
3936 t = new_Token(NULL, tm->type, tm->text, 0);
3937 *tt = t, tt = &t->next, tm = tm->next;
3938 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003939 }
3940 }
3941
3942 *last = tt;
3943 return head;
3944
3945err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003946 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003947 &tline->text[1]);
3948 return tline;
3949}
3950
H. Peter Anvin76690a12002-04-30 20:52:49 +00003951/*
3952 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003953 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003954 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003955 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003956static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003957{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003958 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003959 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003960 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003961
3962 tail = &thead;
3963 thead = NULL;
3964
H. Peter Anvine2c80182005-01-15 22:15:51 +00003965 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003966 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003967 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3968 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3969 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003970 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003971 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003972 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003973 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003974 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003975 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003976
H. Peter Anvine2c80182005-01-15 22:15:51 +00003977 t = tline;
3978 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003979
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003980 mac = istk->mstk;
3981 while (mac && !mac->name) /* avoid mistaking %reps for macros */
3982 mac = mac->next_active;
3983 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003984 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003985 } else {
3986 pos = strchr(t->text, ':');
3987 if (!pos) {
3988 switch (t->text[1]) {
3989 /*
3990 * We have to make a substitution of one of the
3991 * forms %1, %-1, %+1, %%foo, %0.
3992 */
3993 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003994 type = TOK_NUMBER;
3995 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
3996 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003997 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003998 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004000 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004001 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004002 text = nasm_strcat(tmpbuf, t->text + 2);
4003 break;
4004 case '-':
4005 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004006 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004007 tt = NULL;
4008 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004009 if (mac->nparam > 1)
4010 n = (n + mac->rotate) % mac->nparam;
4011 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004012 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004013 cc = find_cc(tt);
4014 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004015 nasm_nonfatal("macro parameter %d is not a condition code",
4016 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004017 text = NULL;
4018 } else {
4019 type = TOK_ID;
4020 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004021 nasm_nonfatal("condition code `%s' is not invertible",
4022 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004023 text = NULL;
4024 } else
4025 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4026 }
4027 break;
4028 case '+':
4029 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004030 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004031 tt = NULL;
4032 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004033 if (mac->nparam > 1)
4034 n = (n + mac->rotate) % mac->nparam;
4035 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004036 }
4037 cc = find_cc(tt);
4038 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004039 nasm_nonfatal("macro parameter %d is not a condition code",
4040 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004041 text = NULL;
4042 } else {
4043 type = TOK_ID;
4044 text = nasm_strdup(conditions[cc]);
4045 }
4046 break;
4047 default:
4048 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004049 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004050 tt = NULL;
4051 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004052 if (mac->nparam > 1)
4053 n = (n + mac->rotate) % mac->nparam;
4054 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004055 }
4056 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004057 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004058 *tail = new_Token(NULL, tt->type, tt->text, 0);
4059 tail = &(*tail)->next;
4060 tt = tt->next;
4061 }
4062 }
4063 text = NULL; /* we've done it here */
4064 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004066 } else {
4067 /*
4068 * seems we have a parameters range here
4069 */
4070 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004071 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004072 if (head != t) {
4073 *tail = head;
4074 *last = tline;
4075 tline = head;
4076 text = NULL;
4077 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004078 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004079 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004080 if (!text) {
4081 delete_Token(t);
4082 } else {
4083 *tail = t;
4084 tail = &t->next;
4085 t->type = type;
4086 nasm_free(t->text);
4087 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004088 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004089 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004090 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004091 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004092 } else if (tline->type == TOK_INDIRECT) {
4093 t = tline;
4094 tline = tline->next;
4095 tt = tokenize(t->text);
4096 tt = expand_mmac_params(tt);
4097 tt = expand_smacro(tt);
4098 *tail = tt;
4099 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004100 tail = &tt->next;
4101 tt = tt->next;
4102 }
4103 delete_Token(t);
4104 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004105 } else {
4106 t = *tail = tline;
4107 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004108 tail = &t->next;
4109 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004110 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004111 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004112
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004113 if (changed) {
4114 const struct tokseq_match t[] = {
4115 {
4116 PP_CONCAT_MASK(TOK_ID) |
4117 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4118 PP_CONCAT_MASK(TOK_ID) |
4119 PP_CONCAT_MASK(TOK_NUMBER) |
4120 PP_CONCAT_MASK(TOK_FLOAT) |
4121 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4122 },
4123 {
4124 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4125 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4126 }
4127 };
4128 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4129 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004130
H. Peter Anvin76690a12002-04-30 20:52:49 +00004131 return thead;
4132}
4133
4134/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004135 * Expand *one* single-line macro instance. If the first token is not
4136 * a macro at all, it is simply copied to the output. tpp should be
4137 * a pointer to a pointer (usually the next pointer of the previous token)
4138 * to the first token. **tpp is updated to point to the pointer to first token
4139 * of the expansion, which is also the return value, and then *tpp will point to
4140 * the pointer to the first input token after the expansion.
4141 *
4142 * If the expansion is empty, then these two values point to the same token.
4143 *
4144 */
4145static Token *expand_smacro_noreset(Token * tline);
4146
4147static Token *expand_one_smacro(Token ***tpp)
4148{
4149 Token **params = NULL;
4150 const char *mname;
4151 Token *tline = **tpp;
4152 Token **tnextp;
4153 SMacro *head, *m;
4154 unsigned int nparam, i;
4155 Token *expansion;
4156 Token *t, *mstart, **ttail;
4157 bool free_expansion;
4158
4159 if (!tline)
4160 return NULL;
4161
4162 mname = tline->text;
4163 tnextp = &tline->next; /* By default we shift out one token */
4164
4165 if (tline->type == TOK_ID) {
4166 head = (SMacro *)hash_findix(&smacros, mname);
4167 } else if (tline->type == TOK_PREPROC_ID) {
4168 Context *ctx = get_ctx(mname, &mname);
4169 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4170 } else {
4171 goto not_a_macro;
4172 }
4173
4174 /*
4175 * We've hit an identifier of some sort. First check whether the
4176 * identifier is a single-line macro at all, then think about
4177 * checking for parameters if necessary.
4178 */
4179 list_for_each(m, head) {
4180 if (!mstrcmp(m->name, mname, m->casesense))
4181 break;
4182 }
4183
4184 if (!m) {
4185 goto not_a_macro;
4186 }
4187
4188 /* Parse parameters, if applicable */
4189
4190 params = NULL;
4191 nparam = 0;
4192
4193 if (m->nparam == 0) {
4194 /*
4195 * Simple case: the macro is parameterless.
4196 * Nothing to parse; the expansion code will
4197 * drop the macro name token.
4198 */
4199 } else {
4200 /*
4201 * Complicated case: at least one macro with this name
4202 * exists and takes parameters. We must find the
4203 * parameters in the call, count them, find the SMacro
4204 * that corresponds to that form of the macro call, and
4205 * substitute for the parameters when we expand. What a
4206 * pain.
4207 */
4208 Token **pep;
4209 int paren;
4210 int white = 0;
4211 int brackets = 0;
4212 bool bracketed = false;
4213 bool bad_bracket = false;
4214 unsigned int sparam = PARAM_DELTA;
4215
4216 tline = tline->next;
4217
4218 while (tok_type_(tline, TOK_WHITESPACE)) {
4219 tline = tline->next;
4220 }
4221 if (!tok_is_(tline, "(")) {
4222 /*
4223 * This macro wasn't called with parameters: ignore
4224 * the call. (Behaviour borrowed from gnu cpp.)
4225 */
4226 goto not_a_macro;
4227 }
4228
4229 paren = 1;
4230 nparam = 0;
4231 params = nasm_malloc(sparam * sizeof *params);
4232 params[0] = NULL;
4233 pep = &params[0];
4234
4235 while (true) {
4236 bool skip = false;
4237 char ch;
4238
4239 if (!tline->next) {
4240 nasm_nonfatal("macro call expects terminating `)'");
4241 break;
4242 }
4243 tline = tline->next;
4244
4245 ch = (tline->type == TOK_OTHER && !tline->text[1])
4246 ? tline->text[0] : 0;
4247
4248 if (!brackets) {
4249 if (tline->type == TOK_WHITESPACE) {
4250 if (params[nparam]) {
4251 /* Keep interior whitespace */
4252 white++;
4253 }
4254 skip = true;
4255 }
4256
4257 switch (ch) {
4258 case ',':
4259 if (!paren) {
4260 if (++nparam >= sparam) {
4261 sparam += PARAM_DELTA;
4262 params = nasm_realloc(params, sparam * sizeof *params);
4263 }
4264 params[nparam] = NULL;
4265 pep = &params[nparam];
4266 white = 0;
4267 bracketed = false;
4268 }
4269 break;
4270
4271 case '{':
4272 brackets++;
4273 bracketed = skip = !params[nparam];
4274 break;
4275
4276 case '(':
4277 paren++;
4278 break;
4279
4280 case ')':
4281 paren--;
4282 break;
4283
4284 default:
4285 break; /* Just advance to the next token */
4286 }
4287 } else {
4288 if (ch == '}')
4289 brackets--;
4290
4291 if (brackets == 0)
4292 skip = bracketed;
4293 }
4294
4295 if (!paren)
4296 break; /* We are done */
4297
4298 if (!skip) {
4299 Token *t;
4300
4301 if (bracketed && !brackets)
4302 bad_bracket = true;
4303
4304 if (white) {
4305 t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4306 *pep = t;
4307 pep = &t->next;
4308 }
4309
4310 t = new_Token(NULL, tline->type, tline->text, 0);
4311 *pep = t;
4312 pep = &t->next;
4313 white = 0;
4314 }
4315 }
4316 nparam++; /* Count the last parameter */
4317
4318 if (bad_bracket)
4319 nasm_nonfatal("braces do not enclose all of macro parameter");
4320
4321 /* Look for a macro matching in both name and parameter count */
4322 while (m && (m->nparam != nparam ||
4323 mstrcmp(m->name, mname, m->casesense)))
4324 m = m->next;
4325
4326 if (!m) {
4327 /*!
4328 *!macro-params [on] macro calls with wrong parameter count
4329 *! covers warnings about \i{multi-line macros} being invoked
4330 *! with the wrong number of parameters. See \k{mlmacover} for an
4331 *! example of why you might want to disable this warning.
4332 */
4333 nasm_warn(WARN_MACRO_PARAMS,
4334 "macro `%s' exists, "
4335 "but not taking %d parameters",
4336 mname, nparam);
4337 goto not_a_macro_cleanup;
4338 }
4339 }
4340
4341 if (m->in_progress)
4342 goto not_a_macro_cleanup;
4343
4344 /* Expand each parameter */
4345 m->in_progress = true;
4346
4347 for (i = 0; i < nparam; i++) {
4348 params[i] = expand_smacro_noreset(params[i]);
4349
4350 if (m->eval_param && m->eval_param[i]) {
4351 /* Evaluate this parameter as a number */
4352 struct ppscan pps;
4353 struct tokenval tokval;
4354 expr *evalresult;
4355
4356 pps.tptr = params[i];
4357 pps.ntokens = -1;
4358 tokval.t_type = TOKEN_INVALID;
4359 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
4360
4361 if (!evalresult) {
4362 /* Nothing meaningful to do */
4363 } else if (tokval.t_type) {
4364 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4365 } else if (!is_simple(evalresult)) {
4366 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4367 } else {
4368 free_tlist(params[i]);
4369 params[i] = make_tok_num(reloc_value(evalresult));
4370 }
4371 }
4372
4373 /* Put tokens in reverse order like the expansion list */
4374 params[i] = reverse_tokens(params[i]);
4375 }
4376
4377 t = tline;
4378 tline = tline->next;
4379 t->next = NULL; /* Remove the macro call from the input */
4380
4381 if (unlikely(m->magic)) {
4382 expansion = m->magic(m, params, nparam, &free_expansion);
4383 } else {
4384 expansion = m->expansion;
4385 free_expansion = false;
4386 }
4387
4388 ttail = NULL;
4389
4390 list_for_each(t, expansion) {
4391 if (is_smac_param(t->type)) {
4392 Token *ttt;
4393 list_for_each(ttt, params[smac_nparam(t->type)]) {
4394 tline = dup_Token(tline, ttt);
4395 if (!ttail)
4396 ttail = &tline->next;
4397 }
4398 } else {
4399 if (t->type == TOK_PREPROC_Q) {
4400 tline = new_Token(tline, TOK_ID, mname, 0);
4401 } else if (t->type == TOK_PREPROC_QQ) {
4402 tline = new_Token(tline, TOK_ID, m->name, 0);
4403 } else {
4404 tline = dup_Token(tline, t);
4405 }
4406 if (!ttail)
4407 ttail = &tline->next;
4408 }
4409 }
4410
4411 if (free_expansion)
4412 free_tlist(expansion);
4413
4414 m->in_progress = false;
4415
4416 /* Don't do this until after expansion or we will clobber mname */
4417 mstart = **tpp;
4418 tnextp = ttail ? ttail : *tpp;
4419 **tpp = tline;
4420 free_tlist(mstart);
4421
4422 /*
4423 * No macro expansion needed; roll back to mstart (if necessary)
4424 * and then advance to the next input token.
4425 */
4426not_a_macro_cleanup:
4427 nasm_free(params);
4428
4429not_a_macro:
4430 t = **tpp;
4431 *tpp = tnextp;
4432 return t;
4433}
4434
4435/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004436 * Expand all single-line macro calls made in the given line.
4437 * Return the expanded version of the line. The original is deemed
4438 * to be destroyed in the process. (In reality we'll just move
4439 * Tokens from input to output a lot of the time, rather than
4440 * actually bothering to destroy and replicate.)
4441 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004442static int64_t smacro_deadman;
4443static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004444{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004445 smacro_deadman = nasm_limit[LIMIT_MACROS];
4446 return expand_smacro_noreset(tline);
4447}
4448
4449static Token *expand_smacro_noreset(Token * tline)
4450{
4451 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004452 Token *org_tline = tline;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004453 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004454
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004455 /*
4456 * Trick: we should avoid changing the start token pointer since it can
4457 * be contained in "next" field of other token. Because of this
4458 * we allocate a copy of first token and work with it; at the end of
4459 * routine we copy it back
4460 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004461 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004462 tline = new_Token(org_tline->next, org_tline->type,
4463 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004464 nasm_free(org_tline->text);
4465 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004466 }
4467
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004468 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004469
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004470again:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004471 thead = tline;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004472 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004473
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004474 while ((t = *tail)) { /* main token loop */
4475 if (!--smacro_deadman) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004476 nasm_nonfatal("interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004477 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004478 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004479
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004480 /*
4481 * An expansion happened if and only if the first token of the
4482 * expansion was the first token of the original simply moved over.
4483 */
4484 expanded |= (expand_one_smacro(&tail) != t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004485 }
4486
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004487 /*
4488 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004489 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004490 * TOK_IDs should be concatenated.
4491 * Also we look for %+ tokens and concatenate the tokens before and after
4492 * them (without white spaces in between).
4493 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004494 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004495 const struct tokseq_match t[] = {
4496 {
4497 PP_CONCAT_MASK(TOK_ID) |
4498 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4499 PP_CONCAT_MASK(TOK_ID) |
4500 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4501 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4502 }
4503 };
4504 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004505 /*
4506 * If we concatenated something, *and* we had previously expanded
4507 * an actual macro, scan the lines again for macros...
4508 */
4509 tline = thead;
4510 expanded = false;
4511 goto again;
4512 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004513 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004514
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004515err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004516 if (org_tline) {
4517 if (thead) {
4518 *org_tline = *thead;
4519 /* since we just gave text to org_line, don't free it */
4520 thead->text = NULL;
4521 delete_Token(thead);
4522 } else {
4523 /* the expression expanded to empty line;
4524 we can't return NULL for some reasons
4525 we just set the line to a single WHITESPACE token. */
4526 memset(org_tline, 0, sizeof(*org_tline));
4527 org_tline->text = NULL;
4528 org_tline->type = TOK_WHITESPACE;
4529 }
4530 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004531 }
4532
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004533 return thead;
4534}
4535
4536/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004537 * Similar to expand_smacro but used exclusively with macro identifiers
4538 * right before they are fetched in. The reason is that there can be
4539 * identifiers consisting of several subparts. We consider that if there
4540 * are more than one element forming the name, user wants a expansion,
4541 * otherwise it will be left as-is. Example:
4542 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004543 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004544 *
4545 * the identifier %$abc will be left as-is so that the handler for %define
4546 * will suck it and define the corresponding value. Other case:
4547 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004548 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004549 *
4550 * In this case user wants name to be expanded *before* %define starts
4551 * working, so we'll expand %$abc into something (if it has a value;
4552 * otherwise it will be left as-is) then concatenate all successive
4553 * PP_IDs into one.
4554 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004555static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004556{
4557 Token *cur, *oldnext = NULL;
4558
H. Peter Anvin734b1882002-04-30 21:01:08 +00004559 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004560 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004561
4562 cur = tline;
4563 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004564 (cur->next->type == TOK_ID ||
4565 cur->next->type == TOK_PREPROC_ID
4566 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004567 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004568
4569 /* If identifier consists of just one token, don't expand */
4570 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004571 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004572
H. Peter Anvine2c80182005-01-15 22:15:51 +00004573 if (cur) {
4574 oldnext = cur->next; /* Detach the tail past identifier */
4575 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004576 }
4577
H. Peter Anvin734b1882002-04-30 21:01:08 +00004578 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004579
H. Peter Anvine2c80182005-01-15 22:15:51 +00004580 if (cur) {
4581 /* expand_smacro possibly changhed tline; re-scan for EOL */
4582 cur = tline;
4583 while (cur && cur->next)
4584 cur = cur->next;
4585 if (cur)
4586 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004587 }
4588
4589 return tline;
4590}
4591
4592/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004593 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004594 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004595 * to check for an initial label - that's taken care of in
4596 * expand_mmacro - but must check numbers of parameters. Guaranteed
4597 * to be called with tline->type == TOK_ID, so the putative macro
4598 * name is easy to find.
4599 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004600static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004601{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004602 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004603 Token **params;
4604 int nparam;
4605
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004606 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004607
4608 /*
4609 * Efficiency: first we see if any macro exists with the given
4610 * name. If not, we can return NULL immediately. _Then_ we
4611 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004612 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004613 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004614 list_for_each(m, head)
4615 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004616 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004617 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004618 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004619
4620 /*
4621 * OK, we have a potential macro. Count and demarcate the
4622 * parameters.
4623 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004624 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004625
4626 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004627 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004628 * structure that handles this number.
4629 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004630 while (m) {
4631 if (m->nparam_min <= nparam
4632 && (m->plus || nparam <= m->nparam_max)) {
4633 /*
4634 * This one is right. Just check if cycle removal
4635 * prohibits us using it before we actually celebrate...
4636 */
4637 if (m->in_progress > m->max_depth) {
4638 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004639 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004640 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004641 }
4642 nasm_free(params);
4643 return NULL;
4644 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004645 /*
4646 * It's right, and we can use it. Add its default
4647 * parameters to the end of our list if necessary.
4648 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004649 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004650 params =
4651 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004652 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004653 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004654 while (nparam < m->nparam_min + m->ndefs) {
4655 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004656 nparam++;
4657 }
4658 }
4659 /*
4660 * If we've gone over the maximum parameter count (and
4661 * we're in Plus mode), ignore parameters beyond
4662 * nparam_max.
4663 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004664 if (m->plus && nparam > m->nparam_max)
4665 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004666 /*
4667 * Then terminate the parameter list, and leave.
4668 */
4669 if (!params) { /* need this special case */
4670 params = nasm_malloc(sizeof(*params));
4671 nparam = 0;
4672 }
4673 params[nparam] = NULL;
4674 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004675 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004676 }
4677 /*
4678 * This one wasn't right: look for the next one with the
4679 * same name.
4680 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004681 list_for_each(m, m->next)
4682 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004683 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004684 }
4685
4686 /*
4687 * After all that, we didn't find one with the right number of
4688 * parameters. Issue a warning, and fail to expand the macro.
4689 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004690 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004691 "macro `%s' exists, but not taking %d parameters",
4692 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004693 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004694 return NULL;
4695}
4696
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004697
4698/*
4699 * Save MMacro invocation specific fields in
4700 * preparation for a recursive macro expansion
4701 */
4702static void push_mmacro(MMacro *m)
4703{
4704 MMacroInvocation *i;
4705
4706 i = nasm_malloc(sizeof(MMacroInvocation));
4707 i->prev = m->prev;
4708 i->params = m->params;
4709 i->iline = m->iline;
4710 i->nparam = m->nparam;
4711 i->rotate = m->rotate;
4712 i->paramlen = m->paramlen;
4713 i->unique = m->unique;
4714 i->condcnt = m->condcnt;
4715 m->prev = i;
4716}
4717
4718
4719/*
4720 * Restore MMacro invocation specific fields that were
4721 * saved during a previous recursive macro expansion
4722 */
4723static void pop_mmacro(MMacro *m)
4724{
4725 MMacroInvocation *i;
4726
4727 if (m->prev) {
4728 i = m->prev;
4729 m->prev = i->prev;
4730 m->params = i->params;
4731 m->iline = i->iline;
4732 m->nparam = i->nparam;
4733 m->rotate = i->rotate;
4734 m->paramlen = i->paramlen;
4735 m->unique = i->unique;
4736 m->condcnt = i->condcnt;
4737 nasm_free(i);
4738 }
4739}
4740
4741
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004742/*
4743 * Expand the multi-line macro call made by the given line, if
4744 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004745 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004746 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004747static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004748{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004749 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004750 Token *label = NULL;
4751 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004752 Token **params, *t, *tt;
4753 MMacro *m;
4754 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004755 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004756 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004757
4758 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004759 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004760 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004761 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004762 return 0;
4763 m = is_mmacro(t, &params);
4764 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004765 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004766 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004767 Token *last;
4768 /*
4769 * We have an id which isn't a macro call. We'll assume
4770 * it might be a label; we'll also check to see if a
4771 * colon follows it. Then, if there's another id after
4772 * that lot, we'll check it again for macro-hood.
4773 */
4774 label = last = t;
4775 t = t->next;
4776 if (tok_type_(t, TOK_WHITESPACE))
4777 last = t, t = t->next;
4778 if (tok_is_(t, ":")) {
4779 dont_prepend = 1;
4780 last = t, t = t->next;
4781 if (tok_type_(t, TOK_WHITESPACE))
4782 last = t, t = t->next;
4783 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004784 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4785 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004786 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004787 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004788 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004789 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004790
4791 /*
4792 * Fix up the parameters: this involves stripping leading and
4793 * trailing whitespace, then stripping braces if they are
4794 * present.
4795 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004796 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004797 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004798
H. Peter Anvine2c80182005-01-15 22:15:51 +00004799 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004800 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004801 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004802
H. Peter Anvine2c80182005-01-15 22:15:51 +00004803 t = params[i];
4804 skip_white_(t);
4805 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004806 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004807 params[i] = t;
4808 paramlen[i] = 0;
4809 while (t) {
4810 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4811 break; /* ... because we have hit a comma */
4812 if (comma && t->type == TOK_WHITESPACE
4813 && tok_is_(t->next, ","))
4814 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004815 if (brace && t->type == TOK_OTHER) {
4816 if (t->text[0] == '{')
4817 brace++; /* ... or a nested opening brace */
4818 else if (t->text[0] == '}')
4819 if (!--brace)
4820 break; /* ... or a brace */
4821 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004822 t = t->next;
4823 paramlen[i]++;
4824 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004825 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004826 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004827 }
4828
4829 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004830 * OK, we have a MMacro structure together with a set of
4831 * parameters. We must now go through the expansion and push
4832 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004833 * parameter tokens and macro-local tokens doesn't get done
4834 * until the single-line macro substitution process; this is
4835 * because delaying them allows us to change the semantics
4836 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004837 *
4838 * First, push an end marker on to istk->expansion, mark this
4839 * macro as in progress, and set up its invocation-specific
4840 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004841 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004842 ll = nasm_malloc(sizeof(Line));
4843 ll->next = istk->expansion;
4844 ll->finishes = m;
4845 ll->first = NULL;
4846 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004847
4848 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004849 * Save the previous MMacro expansion in the case of
4850 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004851 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004852 if (m->max_depth && m->in_progress)
4853 push_mmacro(m);
4854
4855 m->in_progress ++;
4856 m->params = params;
4857 m->iline = tline;
4858 m->nparam = nparam;
4859 m->rotate = 0;
4860 m->paramlen = paramlen;
4861 m->unique = unique++;
4862 m->lineno = 0;
4863 m->condcnt = 0;
4864
4865 m->next_active = istk->mstk;
4866 istk->mstk = m;
4867
4868 list_for_each(l, m->expansion) {
4869 Token **tail;
4870
4871 ll = nasm_malloc(sizeof(Line));
4872 ll->finishes = NULL;
4873 ll->next = istk->expansion;
4874 istk->expansion = ll;
4875 tail = &ll->first;
4876
4877 list_for_each(t, l->first) {
4878 Token *x = t;
4879 switch (t->type) {
4880 case TOK_PREPROC_Q:
4881 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004882 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004883 case TOK_PREPROC_QQ:
4884 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4885 break;
4886 case TOK_PREPROC_ID:
4887 if (t->text[1] == '0' && t->text[2] == '0') {
4888 dont_prepend = -1;
4889 x = label;
4890 if (!x)
4891 continue;
4892 }
4893 /* fall through */
4894 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07004895 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004896 break;
4897 }
4898 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004899 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004900 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004901 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004902
4903 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004904 * If we had a label, push it on as the first line of
4905 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004906 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004907 if (label) {
4908 if (dont_prepend < 0)
4909 free_tlist(startline);
4910 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07004911 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004912 ll->finishes = NULL;
4913 ll->next = istk->expansion;
4914 istk->expansion = ll;
4915 ll->first = startline;
4916 if (!dont_prepend) {
4917 while (label->next)
4918 label = label->next;
4919 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004920 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004921 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004922 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004923
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07004924 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004925
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004926 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004927}
4928
H. Peter Anvin130736c2016-02-17 20:27:41 -08004929/*
4930 * This function adds macro names to error messages, and suppresses
4931 * them if necessary.
4932 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08004933static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004934{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004935 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004936 MMacro *mmac = NULL;
4937 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004938
H. Peter Anvin130736c2016-02-17 20:27:41 -08004939 /*
4940 * If we're in a dead branch of IF or something like it, ignore the error.
4941 * However, because %else etc are evaluated in the state context
4942 * of the previous branch, errors might get lost:
4943 * %if 0 ... %else trailing garbage ... %endif
4944 * So %else etc should set the ERR_PP_PRECOND flag.
4945 */
4946 if ((severity & ERR_MASK) < ERR_FATAL &&
4947 istk && istk->conds &&
4948 ((severity & ERR_PP_PRECOND) ?
4949 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004950 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004951 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004952
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004953 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004954 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004955 mmac = istk->mstk;
4956 /* but %rep blocks should be skipped */
4957 while (mmac && !mmac->name)
4958 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004959 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004960
H. Peter Anvin130736c2016-02-17 20:27:41 -08004961 if (mmac) {
4962 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004963
H. Peter Anvin130736c2016-02-17 20:27:41 -08004964 nasm_set_verror(real_verror);
4965 nasm_error(severity, "(%s:%d) %s",
4966 mmac->name, mmac->lineno - delta, buff);
4967 nasm_set_verror(pp_verror);
4968 } else {
4969 real_verror(severity, fmt, arg);
4970 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004971}
4972
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004973static Token *stdmac_file(const SMacro *s, Token **params,
4974 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08004975{
4976 (void)s;
4977 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004978 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004979
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004980 *free_expansion = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004981 return make_tok_qstr(src_get_fname());
4982}
4983
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004984static Token *stdmac_line(const SMacro *s, Token **params,
4985 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08004986{
4987 (void)s;
4988 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004989 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004990
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004991 *free_expansion = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08004992 return make_tok_num(src_get_linnum());
4993}
4994
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004995static Token *stdmac_bits(const SMacro *s, Token **params,
4996 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08004997{
4998 (void)s;
4999 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005000 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005001
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005002 *free_expansion = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005003 return make_tok_num(globalbits);
5004}
5005
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005006static Token *stdmac_ptr(const SMacro *s, Token **params,
5007 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005008{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005009 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005010 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5011 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5012 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005013
5014 (void)s;
5015 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005016 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005017
5018 switch (globalbits) {
5019 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005020 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005021 break;
5022 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005023 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005024 break;
5025 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005026 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005027 break;
5028 default:
5029 panic();
5030 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005031
5032 *free_expansion = false;
5033 return (Token *)t;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005034}
5035
H. Peter Anvin8b262472019-02-26 14:00:54 -08005036/* Add magic standard macros */
5037struct magic_macros {
5038 const char *name;
5039 int nparams;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005040 MagicSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005041};
5042static const struct magic_macros magic_macros[] =
5043{
5044 { "__FILE__", 0, stdmac_file },
5045 { "__LINE__", 0, stdmac_line },
5046 { "__BITS__", 0, stdmac_bits },
5047 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005048 { NULL, 0, NULL }
5049};
5050
5051static void pp_add_magic_stdmac(void)
5052{
5053 const struct magic_macros *m;
5054 SMacro *s;
5055
5056 for (m = magic_macros; m->name; m++) {
5057 s = define_smacro(NULL, m->name, true, m->nparams, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005058 s->magic = m->func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005059 }
5060}
5061
H. Peter Anvin734b1882002-04-30 21:01:08 +00005062static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005063pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005064{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005065 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005066
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005067 cstk = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005068 nasm_new(istk);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07005069 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin274cda82016-05-10 02:56:29 -07005070 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005071 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005072 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03005073 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005074 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005075 nested_mac_count = 0;
5076 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005077 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005078 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005079 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005080 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005081
H. Peter Anvin8b262472019-02-26 14:00:54 -08005082 pp_add_magic_stdmac();
5083
H. Peter Anvinf7606612016-07-13 14:23:48 -07005084 if (tasm_compatible_mode)
5085 pp_add_stdmac(nasm_stdmac_tasm);
5086
5087 pp_add_stdmac(nasm_stdmac_nasm);
5088 pp_add_stdmac(nasm_stdmac_version);
5089
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005090 if (extrastdmac)
5091 pp_add_stdmac(extrastdmac);
5092
H. Peter Anvinf7606612016-07-13 14:23:48 -07005093 stdmacpos = stdmacros[0];
5094 stdmacnext = &stdmacros[1];
5095
H. Peter Anvind2456592008-06-19 15:04:18 -07005096 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005097
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03005098 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005099
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005100 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005101 * Define the __PASS__ macro. This is defined here unlike all the
5102 * other builtins, because it is special -- it varies between
5103 * passes -- but there is really no particular reason to make it
5104 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005105 *
5106 * 0 = dependencies only
5107 * 1 = preparatory passes
5108 * 2 = final pass
5109 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005110 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005111 switch (mode) {
5112 case PP_NORMAL:
5113 apass = pass_final() ? 2 : 1;
5114 break;
5115 case PP_DEPS:
5116 apass = 0;
5117 break;
5118 case PP_PREPROC:
5119 apass = 3;
5120 break;
5121 default:
5122 panic();
5123 }
5124
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005125 define_smacro(NULL, "__PASS__", true, 0, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005126}
5127
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005128static void pp_init(void)
5129{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005130}
5131
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005132/*
5133 * Get a line of tokens. If we popped the macro expansion/include stack,
5134 * we return a pointer to the dummy token tok_pop; at that point if
5135 * istk is NULL then we have reached end of input;
5136 */
5137static Token tok_pop; /* Dummy token placeholder */
5138
5139static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005140{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005141 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005142
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005143 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005144 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005145 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005146 * buffer or from the input file.
5147 */
5148 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005149 while (istk->expansion && istk->expansion->finishes) {
5150 Line *l = istk->expansion;
5151 if (!l->finishes->name && l->finishes->in_progress > 1) {
5152 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005153
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005154 /*
5155 * This is a macro-end marker for a macro with no
5156 * name, which means it's not really a macro at all
5157 * but a %rep block, and the `in_progress' field is
5158 * more than 1, meaning that we still need to
5159 * repeat. (1 means the natural last repetition; 0
5160 * means termination by %exitrep.) We have
5161 * therefore expanded up to the %endrep, and must
5162 * push the whole block on to the expansion buffer
5163 * again. We don't bother to remove the macro-end
5164 * marker: we'd only have to generate another one
5165 * if we did.
5166 */
5167 l->finishes->in_progress--;
5168 list_for_each(l, l->finishes->expansion) {
5169 Token *t, *tt, **tail;
5170
5171 ll = nasm_malloc(sizeof(Line));
5172 ll->next = istk->expansion;
5173 ll->finishes = NULL;
5174 ll->first = NULL;
5175 tail = &ll->first;
5176
5177 list_for_each(t, l->first) {
5178 if (t->text || t->type == TOK_WHITESPACE) {
5179 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5180 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005181 }
5182 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005183 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005184 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005185 } else {
5186 /*
5187 * Check whether a `%rep' was started and not ended
5188 * within this macro expansion. This can happen and
5189 * should be detected. It's a fatal error because
5190 * I'm too confused to work out how to recover
5191 * sensibly from it.
5192 */
5193 if (defining) {
5194 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005195 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005196 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005197 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005198 " expansion of macro `%s'",
5199 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005200 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005201
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005202 /*
5203 * FIXME: investigate the relationship at this point between
5204 * istk->mstk and l->finishes
5205 */
5206 {
5207 MMacro *m = istk->mstk;
5208 istk->mstk = m->next_active;
5209 if (m->name) {
5210 /*
5211 * This was a real macro call, not a %rep, and
5212 * therefore the parameter information needs to
5213 * be freed.
5214 */
5215 if (m->prev) {
5216 pop_mmacro(m);
5217 l->finishes->in_progress --;
5218 } else {
5219 nasm_free(m->params);
5220 free_tlist(m->iline);
5221 nasm_free(m->paramlen);
5222 l->finishes->in_progress = 0;
5223 }
Adam Majer91e72402017-07-25 10:42:01 +02005224 }
5225
5226 /*
5227 * FIXME It is incorrect to always free_mmacro here.
5228 * It leads to usage-after-free.
5229 *
5230 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5231 */
5232#if 0
5233 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005234 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005235#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005236 }
5237 istk->expansion = l->next;
5238 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005239 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005240 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005241 }
5242 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005243 do { /* until we get a line we can use */
5244 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005245
5246 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005247 Line *l = istk->expansion;
5248 if (istk->mstk)
5249 istk->mstk->lineno++;
5250 tline = l->first;
5251 istk->expansion = l->next;
5252 nasm_free(l);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005253 line = detoken(tline, false);
5254 lfmt->line(LIST_MACRO, line);
5255 nasm_free(line);
5256 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005257 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005258 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005259 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005260 } else {
5261 /*
5262 * The current file has ended; work down the istk
5263 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005264 Include *i = istk;
5265 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005266 if (i->conds) {
5267 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005268 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005269 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005270 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005271 if (i->next)
5272 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005273 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005274 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005275 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005276 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005277 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005278 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005279
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005280 /*
5281 * We must expand MMacro parameters and MMacro-local labels
5282 * _before_ we plunge into directive processing, to cope
5283 * with things like `%define something %1' such as STRUC
5284 * uses. Unless we're _defining_ a MMacro, in which case
5285 * those tokens should be left alone to go into the
5286 * definition; and unless we're in a non-emitting
5287 * condition, in which case we don't want to meddle with
5288 * anything.
5289 */
5290 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5291 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005292 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005293 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005294
H. Peter Anvine2c80182005-01-15 22:15:51 +00005295 /*
5296 * Check the line to see if it's a preprocessor directive.
5297 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005298 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5299 if (dtline)
5300 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005301 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005302 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005303 * We're defining a multi-line macro. We emit nothing
5304 * at all, and just
5305 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005306 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005307 Line *l = nasm_malloc(sizeof(Line));
5308 l->next = defining->expansion;
5309 l->first = tline;
5310 l->finishes = NULL;
5311 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005312 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005313 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005314 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005315 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005316 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005317 * directive so we keep our place correctly.
5318 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005319 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005320 } else if (istk->mstk && !istk->mstk->in_progress) {
5321 /*
5322 * We're in a %rep block which has been terminated, so
5323 * we're walking through to the %endrep without
5324 * emitting anything. Emit nothing at all, not even a
5325 * blank line: when we emerge from the %rep block we'll
5326 * give a line-number directive so we keep our place
5327 * correctly.
5328 */
5329 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005330 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005331 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005332 if (!expand_mmacro(tline))
5333 return tline;
5334 }
5335 }
5336}
5337
5338static char *pp_getline(void)
5339{
5340 char *line = NULL;
5341 Token *tline;
5342
5343 real_verror = nasm_set_verror(pp_verror);
5344
5345 while (true) {
5346 tline = pp_tokline();
5347 if (tline == &tok_pop) {
5348 /*
5349 * We popped the macro/include stack. If istk is empty,
5350 * we are at end of input, otherwise just loop back.
5351 */
5352 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005353 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005354 } else {
5355 /*
5356 * De-tokenize the line and emit it.
5357 */
5358 line = detoken(tline, true);
5359 free_tlist(tline);
5360 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005361 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005362 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005363
H. Peter Anvin130736c2016-02-17 20:27:41 -08005364 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005365 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005366}
5367
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005368static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005369{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005370 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005371
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005372 if (defining) {
5373 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005374 nasm_nonfatal("end of file while still defining macro `%s'",
5375 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005376 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005377 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005378 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005379
5380 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005381 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005382 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005383
5384 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005385
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005386 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005387 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005388 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005389 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005390 Include *i = istk;
5391 istk = istk->next;
5392 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005393 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005394 }
5395 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005396 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005397 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005398}
5399
5400static void pp_cleanup_session(void)
5401{
5402 free_llist(predef);
5403 predef = NULL;
5404 delete_Blocks();
5405 freeTokens = NULL;
5406 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005407}
5408
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005409static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005410{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005411 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005412}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005413
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005414static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005415{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005416 Token *inc, *space, *name;
5417 Line *l;
5418
H. Peter Anvin734b1882002-04-30 21:01:08 +00005419 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5420 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5421 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005422
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005423 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005424 l->next = predef;
5425 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005426 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005427 predef = l;
5428}
5429
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005430static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005431{
5432 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005433 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005434 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005435
H. Peter Anvin130736c2016-02-17 20:27:41 -08005436 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005437
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005438 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005439 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5440 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005441 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005442 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005443 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005444 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005445 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005446
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005447 if (space->next->type != TOK_PREPROC_ID &&
5448 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005449 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005450
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005451 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005452 l->next = predef;
5453 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005454 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005455 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005456
5457 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005458}
5459
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005460static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005461{
5462 Token *def, *space;
5463 Line *l;
5464
H. Peter Anvin734b1882002-04-30 21:01:08 +00005465 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5466 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005467 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005468
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005469 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005470 l->next = predef;
5471 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005472 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005473 predef = l;
5474}
5475
H. Peter Anvin05990342018-06-11 13:32:42 -07005476/* Insert an early preprocessor command that doesn't need special handling */
5477static void pp_pre_command(const char *what, char *string)
5478{
5479 char *cmd;
5480 Token *def, *space;
5481 Line *l;
5482
5483 def = tokenize(string);
5484 if (what) {
5485 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5486 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5487 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5488 }
5489
5490 l = nasm_malloc(sizeof(Line));
5491 l->next = predef;
5492 l->first = def;
5493 l->finishes = NULL;
5494 predef = l;
5495}
5496
H. Peter Anvinf7606612016-07-13 14:23:48 -07005497static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005498{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005499 macros_t **mp;
5500
5501 /* Find the end of the list and avoid duplicates */
5502 for (mp = stdmacros; *mp; mp++) {
5503 if (*mp == macros)
5504 return; /* Nothing to do */
5505 }
5506
5507 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5508
5509 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005510}
5511
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005512static void pp_extra_stdmac(macros_t *macros)
5513{
5514 extrastdmac = macros;
5515}
5516
H. Peter Anvin8b262472019-02-26 14:00:54 -08005517static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005518{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005519 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005520 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5521 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5522}
5523
5524static Token *make_tok_qstr(const char *str)
5525{
5526 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005527 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005528 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005529}
5530
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005531static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005532{
5533 if (!m)
5534 return;
5535
5536 /* We need to print the next_active list in reverse order */
5537 pp_list_one_macro(m->next_active, severity);
5538
5539 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005540 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005541 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005542 }
5543}
5544
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005545static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005546{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005547 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005548
H. Peter Anvinddb29062018-12-11 00:06:29 -08005549 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5550 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005551
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005552 if (istk)
5553 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005554
H. Peter Anvinddb29062018-12-11 00:06:29 -08005555 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005556}
5557
H. Peter Anvine7469712016-02-18 02:20:59 -08005558const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005559 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005560 pp_reset,
5561 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005562 pp_cleanup_pass,
5563 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005564 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005565 pp_pre_define,
5566 pp_pre_undefine,
5567 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005568 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005569 pp_include_path,
5570 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005571};