blob: 251768ee49570d90a8fdeb027e10a2f23e577b4f [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000084typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000085typedef struct Line Line;
86typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080087typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088
89/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070090 * Note on the storage of both SMacro and MMacros: the hash table
91 * indexes them case-insensitively, and we then have to go through a
92 * linked list of potential case aliases (and, for MMacros, parameter
93 * ranges); this is to preserve the matching semantics of the earlier
94 * code. If the number of case aliases for a specific macro is a
95 * performance issue, you may want to reconsider your coding style.
96 */
97
98/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070099 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700100 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700101typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params,
102 unsigned int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800108 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800109 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700110 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700111 ExpandSMacro expand;
112 intorptr expandpvt;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800113 bool *eval_param;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800114 unsigned int nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800115 bool casesense;
116 bool in_progress;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -0700117 bool alias; /* This is an alias macro */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000118};
119
120/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800121 * Store the definition of a multi-line macro. This is also used to
122 * store the interiors of `%rep...%endrep' blocks, which are
123 * effectively self-re-invoking multi-line macros which simply
124 * don't have a name or bother to appear in the hash tables. %rep
125 * blocks are signified by having a NULL `name' field.
126 *
127 * In a MMacro describing a `%rep' block, the `in_progress' field
128 * isn't merely boolean, but gives the number of repeats left to
129 * run.
130 *
131 * The `next' field is used for storing MMacros in hash tables; the
132 * `next_active' field is for stacking them on istk entries.
133 *
134 * When a MMacro is being expanded, `params', `iline', `nparam',
135 * `paramlen', `rotate' and `unique' are local to the invocation.
136 */
137struct MMacro {
138 MMacro *next;
139 MMacroInvocation *prev; /* previous invocation */
140 char *name;
141 int nparam_min, nparam_max;
142 bool casesense;
143 bool plus; /* is the last parameter greedy? */
144 bool nolist; /* is this macro listing-inhibited? */
145 int64_t in_progress; /* is this macro currently being expanded? */
146 int32_t max_depth; /* maximum number of recursive expansions allowed */
147 Token *dlist; /* All defaults as one list */
148 Token **defaults; /* Parameter default pointers */
149 int ndefs; /* number of default parameters */
150 Line *expansion;
151
152 MMacro *next_active;
153 MMacro *rep_nest; /* used for nesting %rep */
154 Token **params; /* actual parameters */
155 Token *iline; /* invocation line */
156 unsigned int nparam, rotate;
157 int *paramlen;
158 uint64_t unique;
159 int lineno; /* Current line number on expansion */
160 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700161
H. Peter Anvin274cda82016-05-10 02:56:29 -0700162 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700163 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800164};
165
166
167/* Store the definition of a multi-line macro, as defined in a
168 * previous recursive macro expansion.
169 */
170struct MMacroInvocation {
171 MMacroInvocation *prev; /* previous invocation */
172 Token **params; /* actual parameters */
173 Token *iline; /* invocation line */
174 unsigned int nparam, rotate;
175 int *paramlen;
176 uint64_t unique;
177 uint64_t condcnt;
178};
179
180
181/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000182 * The context stack is composed of a linked list of these.
183 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000184struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800185 Context *next;
186 char *name;
187 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700188 uint64_t number;
189 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000190};
191
192/*
193 * This is the internal form which we break input lines up into.
194 * Typically stored in linked lists.
195 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800196 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
197 * not necessarily used as-is, but is also used to encode the number
198 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000199 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800200 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700201 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000202 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800203 * tok_smac_param(0) but the one representing `y' will be
204 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000205 *
206 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
207 * which doesn't need quotes around it. Used in the pre-include
208 * mechanism as an alternative to trying to find a sensible type of
209 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000210 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000211enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800212 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
213 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800214 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700215 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800216 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300217 TOK_PASTE, /* %+ */
218 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800219 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300220 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000221};
222
H. Peter Anvin8b262472019-02-26 14:00:54 -0800223static inline enum pp_token_type tok_smac_param(int param)
224{
225 return TOK_SMAC_START_PARAMS + param;
226}
227static int smac_nparam(enum pp_token_type toktype)
228{
229 return toktype - TOK_SMAC_START_PARAMS;
230}
231static bool is_smac_param(enum pp_token_type toktype)
232{
233 return toktype >= TOK_SMAC_START_PARAMS;
234}
235
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400236#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400237#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400238
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400239struct tokseq_match {
240 int mask_head;
241 int mask_tail;
242};
243
H. Peter Anvine2c80182005-01-15 22:15:51 +0000244struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800245 Token *next;
246 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700247 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800248 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000249};
250
251/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800252 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000253 * these, which is essentially a container to allow several linked
254 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700255 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000256 * Note that in this module, linked lists are treated as stacks
257 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800258 * `expansion' field in MMacro structures, so that the linked list,
259 * if walked, would give the macro lines in reverse order; this
260 * means that we can walk the list when expanding a macro, and thus
261 * push the lines on to the `expansion' field in _istk_ in reverse
262 * order (so that when popped back off they are in the right
263 * order). It may seem cockeyed, and it relies on my design having
264 * an even number of steps in, but it works...
265 *
266 * Some of these structures, rather than being actual lines, are
267 * markers delimiting the end of the expansion of a given macro.
268 * This is for use in the cycle-tracking and %rep-handling code.
269 * Such structures have `finishes' non-NULL, and `first' NULL. All
270 * others have `finishes' NULL, but `first' may still be NULL if
271 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000272 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000273struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800274 Line *next;
275 MMacro *finishes;
276 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500277};
278
279/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000280 * To handle an arbitrary level of file inclusion, we maintain a
281 * stack (ie linked list) of these things.
282 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000283struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800284 Include *next;
285 FILE *fp;
286 Cond *conds;
287 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700288 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800289 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvin6686de22019-08-10 05:33:14 -0700290 int lineno, lineinc;
291 bool nolist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000292};
293
294/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700295 * File real name hash, so we don't have to re-search the include
296 * path for every pass (and potentially more than that if a file
297 * is used more than once.)
298 */
299struct hash_table FileHash;
300
301/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000302 * Conditional assembly: we maintain a separate stack of these for
303 * each level of file inclusion. (The only reason we keep the
304 * stacks separate is to ensure that a stray `%endif' in a file
305 * included from within the true branch of a `%if' won't terminate
306 * it and cause confusion: instead, rightly, it'll cause an error.)
307 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700308enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000309 /*
310 * These states are for use just after %if or %elif: IF_TRUE
311 * means the condition has evaluated to truth so we are
312 * currently emitting, whereas IF_FALSE means we are not
313 * currently emitting but will start doing so if a %else comes
314 * up. In these states, all directives are admissible: %elif,
315 * %else and %endif. (And of course %if.)
316 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800317 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000318 /*
319 * These states come up after a %else: ELSE_TRUE means we're
320 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
321 * any %elif or %else will cause an error.
322 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800323 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000324 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200325 * These states mean that we're not emitting now, and also that
326 * nothing until %endif will be emitted at all. COND_DONE is
327 * used when we've had our moment of emission
328 * and have now started seeing %elifs. COND_NEVER is used when
329 * the condition construct in question is contained within a
330 * non-emitting branch of a larger condition construct,
331 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000332 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800333 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700335struct Cond {
336 Cond *next;
337 enum cond_state state;
338};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800339#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000340
H. Peter Anvin70653092007-10-19 14:42:29 -0700341/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000342 * These defines are used as the possible return values for do_directive
343 */
344#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300345#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000346
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 Anvin (Intel)1c21a532019-08-09 02:34:21 -0700609 * Free an array of linked lists of tokens
610 */
611static void free_tlist_array(Token **array, size_t nlists)
612{
613 Token **listp = array;
614
615 while (nlists--)
616 free_tlist(*listp++);
617
618 nasm_free(array);
619}
620
621/*
622 * Duplicate a linked list of tokens.
623 */
624static Token *dup_tlist(const Token *list, Token ***tailp)
625{
626 Token *newlist = NULL;
627 Token **tailpp = &newlist;
628 const Token *t;
629
630 list_for_each(t, list) {
631 Token *nt;
632 *tailpp = nt = dup_Token(NULL, t);
633 tailpp = &nt->next;
634 }
635
636 if (tailp)
637 *tailp = tailpp;
638
639 return newlist;
640}
641
642/*
643 * Duplicate a linked list of tokens in reverse order
644 */
645static Token *dup_tlist_reverse(const Token *list, Token *tail)
646{
647 const Token *t;
648
649 list_for_each(t, list)
650 tail = dup_Token(tail, t);
651
652 return tail;
653}
654
655/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800656 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000657 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800658static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000659{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800660 nasm_free(m->name);
661 free_tlist(m->dlist);
662 nasm_free(m->defaults);
663 free_llist(m->expansion);
664 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000665}
666
667/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700668 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800669 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700670static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800671{
672 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700673 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800674 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700675}
676
677static void clear_smacro(SMacro *s)
678{
679 free_smacro_members(s);
680 /* Wipe everything except the next pointer */
681 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
682}
683
684/*
685 * Free an SMacro
686 */
687static void free_smacro(SMacro *s)
688{
689 free_smacro_members(s);
690 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800691}
692
693/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700694 * Free all currently defined macros, and free the hash tables
695 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700696static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700697{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800698 struct hash_iterator it;
699 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700700
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800701 hash_for_each(smt, it, np) {
702 SMacro *tmp;
703 SMacro *s = np->data;
704 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800705 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700706 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700707 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700708 hash_free(smt);
709}
710
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800711static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700712{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800713 struct hash_iterator it;
714 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700715
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800716 hash_for_each(mmt, it, np) {
717 MMacro *tmp;
718 MMacro *m = np->data;
719 nasm_free((void *)np->key);
720 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800721 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700722 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800723 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700724}
725
726static void free_macros(void)
727{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700728 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800729 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700730}
731
732/*
733 * Initialize the hash tables
734 */
735static void init_macros(void)
736{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700737}
738
739/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000740 * Pop the context stack.
741 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000742static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000743{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000744 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000745
746 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700747 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000748 nasm_free(c->name);
749 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000750}
751
H. Peter Anvin072771e2008-05-22 13:17:51 -0700752/*
753 * Search for a key in the hash index; adding it if necessary
754 * (in which case we initialize the data pointer to NULL.)
755 */
756static void **
757hash_findi_add(struct hash_table *hash, const char *str)
758{
759 struct hash_insert hi;
760 void **r;
761 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800762 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700763
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800764 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700765 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300766 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700767
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800768 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
769 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700770 return hash_add(&hi, strx, NULL);
771}
772
773/*
774 * Like hash_findi, but returns the data element rather than a pointer
775 * to it. Used only when not adding a new element, hence no third
776 * argument.
777 */
778static void *
779hash_findix(struct hash_table *hash, const char *str)
780{
781 void **p;
782
783 p = hash_findi(hash, str, NULL);
784 return p ? *p : NULL;
785}
786
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400787/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800788 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400789 * if there no more left -- return NULL
790 */
791static char *line_from_stdmac(void)
792{
793 unsigned char c;
794 const unsigned char *p = stdmacpos;
795 char *line, *q;
796 size_t len = 0;
797
798 if (!stdmacpos)
799 return NULL;
800
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700801 /*
802 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
803 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
804 */
805 while ((c = *p++) != 127) {
806 uint8_t ndir = c - 128;
807 if (ndir < 256-96)
808 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400809 else
810 len++;
811 }
812
813 line = nasm_malloc(len + 1);
814 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700815
816 while ((c = *stdmacpos++) != 127) {
817 uint8_t ndir = c - 128;
818 if (ndir < 256-96) {
819 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
820 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400821 *q++ = ' ';
822 } else {
823 *q++ = c;
824 }
825 }
826 stdmacpos = p;
827 *q = '\0';
828
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700829 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700830 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400831 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700832 if (*stdmacnext) {
833 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400834 } else if (do_predef) {
835 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400836
837 /*
838 * Nasty hack: here we push the contents of
839 * `predef' on to the top-level expansion stack,
840 * since this is the most convenient way to
841 * implement the pre-include and pre-define
842 * features.
843 */
844 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700845 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800846 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700847 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800848 l->finishes = NULL;
849
850 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400851 }
852 do_predef = false;
853 }
854 }
855
856 return line;
857}
858
H. Peter Anvin6686de22019-08-10 05:33:14 -0700859/*
860 * Read a line from a file. Return NULL on end of file.
861 */
862static char *line_from_file(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000863{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700864 int c;
865 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400866 const unsigned int delta = 512;
867 const unsigned int pad = 8;
868 unsigned int nr_cont = 0;
869 bool cont = false;
870 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700871 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000872
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400873 size = delta;
874 p = buffer = nasm_malloc(size);
875
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700876 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400877 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400878
879 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700880 case EOF:
881 if (p == buffer) {
882 nasm_free(buffer);
883 return NULL;
884 }
885 c = 0;
886 break;
887
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400888 case '\r':
889 next = fgetc(istk->fp);
890 if (next != '\n')
891 ungetc(next, istk->fp);
892 if (cont) {
893 cont = false;
894 continue;
895 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700896 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400897 break;
898
899 case '\n':
900 if (cont) {
901 cont = false;
902 continue;
903 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700904 c = 0;
905 break;
906
907 case 032: /* ^Z = legacy MS-DOS end of file mark */
908 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400909 break;
910
911 case '\\':
912 next = fgetc(istk->fp);
913 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400914 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400915 cont = true;
916 nr_cont++;
917 continue;
918 }
919 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400921
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400922 if (p >= (buffer + size - pad)) {
923 buffer = nasm_realloc(buffer, size + delta);
924 p = buffer + size - pad;
925 size += delta;
926 }
927
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700928 *p++ = c;
929 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000930
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700931 lineno = src_get_linnum() + istk->lineinc +
932 (nr_cont * istk->lineinc);
933 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000934
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935 return buffer;
936}
937
938/*
H. Peter Anvin6686de22019-08-10 05:33:14 -0700939 * Common read routine regardless of source
940 */
941static char *read_line(void)
942{
943 char *line;
944
945 if (istk->fp)
946 line = line_from_file();
947 else
948 line = line_from_stdmac();
949
950 if (!line)
951 return NULL;
952
953 if (!istk->nolist)
954 lfmt->line(LIST_READ, src_get_linnum(), line);
955
956 return line;
957}
958
959/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000960 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000961 * don't need to parse the value out of e.g. numeric tokens: we
962 * simply split one string into many.
963 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000964static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000965{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700966 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000967 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000968 Token *list = NULL;
969 Token *t, **tail = &list;
970
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700972 char *p = line;
973 char *ep = NULL; /* End of token, for trimming the end */
974
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 if (*p == '%') {
976 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300977 if (*p == '+' && !nasm_isdigit(p[1])) {
978 p++;
979 type = TOK_PASTE;
980 } else if (nasm_isdigit(*p) ||
981 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 do {
983 p++;
984 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700985 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 type = TOK_PREPROC_ID;
987 } else if (*p == '{') {
988 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800989 while (*p) {
990 if (*p == '}')
991 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 p[-1] = *p;
993 p++;
994 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800995 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800996 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700997 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 if (*p)
999 p++;
1000 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001001 } else if (*p == '[') {
1002 int lvl = 1;
1003 line += 2; /* Skip the leading %[ */
1004 p++;
1005 while (lvl && (c = *p++)) {
1006 switch (c) {
1007 case ']':
1008 lvl--;
1009 break;
1010 case '%':
1011 if (*p == '[')
1012 lvl++;
1013 break;
1014 case '\'':
1015 case '\"':
1016 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001017 p = nasm_skip_string(p - 1);
1018 if (*p)
1019 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001020 break;
1021 default:
1022 break;
1023 }
1024 }
1025 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001026 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001027 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001028 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001029 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001030 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001031 type = TOK_INDIRECT;
1032 } else if (*p == '?') {
1033 type = TOK_PREPROC_Q; /* %? */
1034 p++;
1035 if (*p == '?') {
1036 type = TOK_PREPROC_QQ; /* %?? */
1037 p++;
1038 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001039 } else if (*p == '!') {
1040 type = TOK_PREPROC_ID;
1041 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001042 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001043 do {
1044 p++;
1045 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001046 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001047 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001048 p = nasm_skip_string(p);
1049 if (*p)
1050 p++;
1051 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001052 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001053 } else {
1054 /* %! without string or identifier */
1055 type = TOK_OTHER; /* Legacy behavior... */
1056 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001057 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001059 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 do {
1061 p++;
1062 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001063 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 type = TOK_PREPROC_ID;
1065 } else {
1066 type = TOK_OTHER;
1067 if (*p == '%')
1068 p++;
1069 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001070 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 type = TOK_ID;
1072 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001073 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001075 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 /*
1077 * A string token.
1078 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001080 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001081
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 if (*p) {
1083 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001084 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001085 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 /* Handling unterminated strings by UNV */
1087 /* type = -1; */
1088 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001089 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001090 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001091 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001092 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001093 bool is_hex = false;
1094 bool is_float = false;
1095 bool has_e = false;
1096 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001097
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001099 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001101
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001102 if (*p == '$') {
1103 p++;
1104 is_hex = true;
1105 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001106
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001107 for (;;) {
1108 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001109
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001110 if (!is_hex && (c == 'e' || c == 'E')) {
1111 has_e = true;
1112 if (*p == '+' || *p == '-') {
1113 /*
1114 * e can only be followed by +/- if it is either a
1115 * prefixed hex number or a floating-point number
1116 */
1117 p++;
1118 is_float = true;
1119 }
1120 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1121 is_hex = true;
1122 } else if (c == 'P' || c == 'p') {
1123 is_float = true;
1124 if (*p == '+' || *p == '-')
1125 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001126 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001127 ; /* just advance */
1128 else if (c == '.') {
1129 /*
1130 * we need to deal with consequences of the legacy
1131 * parser, like "1.nolist" being two tokens
1132 * (TOK_NUMBER, TOK_ID) here; at least give it
1133 * a shot for now. In the future, we probably need
1134 * a flex-based scanner with proper pattern matching
1135 * to do it as well as it can be done. Nothing in
1136 * the world is going to help the person who wants
1137 * 0x123.p16 interpreted as two tokens, though.
1138 */
1139 r = p;
1140 while (*r == '_')
1141 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001142
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001143 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1144 (!is_hex && (*r == 'e' || *r == 'E')) ||
1145 (*r == 'p' || *r == 'P')) {
1146 p = r;
1147 is_float = true;
1148 } else
1149 break; /* Terminate the token */
1150 } else
1151 break;
1152 }
1153 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001154
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001155 if (p == line+1 && *line == '$') {
1156 type = TOK_OTHER; /* TOKEN_HERE */
1157 } else {
1158 if (has_e && !is_hex) {
1159 /* 1e13 is floating-point, but 1e13h is not */
1160 is_float = true;
1161 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001162
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001163 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1164 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001165 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001167 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 /*
1169 * Whitespace just before end-of-line is discarded by
1170 * pretending it's a comment; whitespace just before a
1171 * comment gets lumped into the comment.
1172 */
1173 if (!*p || *p == ';') {
1174 type = TOK_COMMENT;
1175 while (*p)
1176 p++;
1177 }
1178 } else if (*p == ';') {
1179 type = TOK_COMMENT;
1180 while (*p)
1181 p++;
1182 } else {
1183 /*
1184 * Anything else is an operator of some kind. We check
1185 * for all the double-character operators (>>, <<, //,
1186 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001187 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 */
1189 type = TOK_OTHER;
1190 if ((p[0] == '>' && p[1] == '>') ||
1191 (p[0] == '<' && p[1] == '<') ||
1192 (p[0] == '/' && p[1] == '/') ||
1193 (p[0] == '<' && p[1] == '=') ||
1194 (p[0] == '>' && p[1] == '=') ||
1195 (p[0] == '=' && p[1] == '=') ||
1196 (p[0] == '!' && p[1] == '=') ||
1197 (p[0] == '<' && p[1] == '>') ||
1198 (p[0] == '&' && p[1] == '&') ||
1199 (p[0] == '|' && p[1] == '|') ||
1200 (p[0] == '^' && p[1] == '^')) {
1201 p++;
1202 }
1203 p++;
1204 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001205
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 /* Handling unterminated string by UNV */
1207 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001208 {
1209 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1210 t->text[p-line] = *line;
1211 tail = &t->next;
1212 }
1213 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001215 if (!ep)
1216 ep = p;
1217 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 tail = &t->next;
1219 }
1220 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001221 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001222 return list;
1223}
1224
H. Peter Anvince616072002-04-30 21:02:23 +00001225/*
1226 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001227 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001228 * deleted only all at once by the delete_Blocks function.
1229 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001231{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001232 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001234 /* first, get to the end of the linked list */
1235 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001237 /* now allocate the requested chunk */
1238 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001240 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001241 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001242 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001243}
1244
1245/*
1246 * this function deletes all managed blocks of memory
1247 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001249{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001251
H. Peter Anvin70653092007-10-19 14:42:29 -07001252 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001253 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001254 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001255 * free it.
1256 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001257 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001258 if (b->chunk)
1259 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 a = b;
1261 b = b->next;
1262 if (a != &blocks)
1263 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001264 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001265 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001267
1268/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001269 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001270 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001271 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001272static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001273 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001274{
1275 Token *t;
1276 int i;
1277
H. Peter Anvin89cee572009-07-15 09:16:54 -04001278 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001279 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1280 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1281 freeTokens[i].next = &freeTokens[i + 1];
1282 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001283 }
1284 t = freeTokens;
1285 freeTokens = t->next;
1286 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001287 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001288 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001289 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 t->text = NULL;
1291 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001292 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001294 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001295 t->text = nasm_malloc(txtlen+1);
1296 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001298 }
1299 return t;
1300}
1301
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001302static Token *dup_Token(Token *next, const Token *src)
1303{
1304 return new_Token(next, src->type, src->text, src->len);
1305}
1306
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001308{
1309 Token *next = t->next;
1310 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001311 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001312 freeTokens = t;
1313 return next;
1314}
1315
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316/*
1317 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001318 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1319 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001320 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001321static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001322{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001323 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001324 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001325 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001326
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001327 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001328 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001329 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001330 char *v;
1331 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001332
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001333 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001334 if (nasm_isquote(*v))
1335 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001336
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001337 if (v) {
1338 char *p = getenv(v);
1339 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001340 /*!
1341 *!environment [on] nonexistent environment variable
1342 *! warns if a nonexistent environment variable
1343 *! is accessed using the \c{%!} preprocessor
1344 *! construct (see \k{getenv}.) Such environment
1345 *! variables are treated as empty (with this
1346 *! warning issued) starting in NASM 2.15;
1347 *! earlier versions of NASM would treat this as
1348 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001349 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001350 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1351 p = "";
1352 }
1353 t->text = nasm_strdup(p);
1354 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001355 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001356 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001357 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001358
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 /* Expand local macros here and not during preprocessing */
1360 if (expand_locals &&
1361 t->type == TOK_PREPROC_ID && t->text &&
1362 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001363 const char *q;
1364 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001365 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001366 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001367 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1368 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 nasm_free(t->text);
1370 t->text = p;
1371 }
1372 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001373 if (t->text) {
1374 if (debug_level(2)) {
1375 unsigned long t_len = t->len;
1376 unsigned long s_len = strlen(t->text);
1377 if (t_len != s_len) {
1378 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1379 t->text, t->type, s_len, t_len);
1380 t->len = s_len;
1381 }
1382 }
1383 len += t->len;
1384 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001386 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001387 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001388
H. Peter Anvin734b1882002-04-30 21:01:08 +00001389 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001390
1391 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001392 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001393 memcpy(p, t->text, t->len);
1394 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001395 } else if (t->type == TOK_WHITESPACE) {
1396 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001398 }
1399 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001400
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001401 return line;
1402}
1403
1404/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001405 * A scanner, suitable for use by the expression evaluator, which
1406 * operates on a line of Tokens. Expects a pointer to a pointer to
1407 * the first token in the line to be passed in as its private_data
1408 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001409 *
1410 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001411 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001412struct ppscan {
1413 Token *tptr;
1414 int ntokens;
1415};
1416
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001418{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001419 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001420 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001421 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001422
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001424 if (pps->ntokens && (tline = pps->tptr)) {
1425 pps->ntokens--;
1426 pps->tptr = tline->next;
1427 } else {
1428 pps->tptr = NULL;
1429 pps->ntokens = 0;
1430 return tokval->t_type = TOKEN_EOS;
1431 }
1432 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001433
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001434 tokval->t_charptr = tline->text;
1435
H. Peter Anvin76690a12002-04-30 20:52:49 +00001436 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001438 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001440
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001442 p = tokval->t_charptr = tline->text;
1443 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 tokval->t_charptr++;
1445 return tokval->t_type = TOKEN_ID;
1446 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001447
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001448 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001449 if (r >= p+MAX_KEYWORD)
1450 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001451 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001452 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001453 *s = '\0';
1454 /* right, so we have an identifier sitting in temp storage. now,
1455 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001456 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001457 }
1458
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001460 bool rn_error;
1461 tokval->t_integer = readnum(tline->text, &rn_error);
1462 tokval->t_charptr = tline->text;
1463 if (rn_error)
1464 return tokval->t_type = TOKEN_ERRNUM;
1465 else
1466 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001467 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001468
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001469 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001470 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001471 }
1472
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001474 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001475
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001476 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001477 tokval->t_charptr = tline->text;
1478 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001479
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001480 if (ep[0] != bq || ep[1] != '\0')
1481 return tokval->t_type = TOKEN_ERRSTR;
1482 else
1483 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001484 }
1485
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 if (tline->type == TOK_OTHER) {
1487 if (!strcmp(tline->text, "<<"))
1488 return tokval->t_type = TOKEN_SHL;
1489 if (!strcmp(tline->text, ">>"))
1490 return tokval->t_type = TOKEN_SHR;
1491 if (!strcmp(tline->text, "//"))
1492 return tokval->t_type = TOKEN_SDIV;
1493 if (!strcmp(tline->text, "%%"))
1494 return tokval->t_type = TOKEN_SMOD;
1495 if (!strcmp(tline->text, "=="))
1496 return tokval->t_type = TOKEN_EQ;
1497 if (!strcmp(tline->text, "<>"))
1498 return tokval->t_type = TOKEN_NE;
1499 if (!strcmp(tline->text, "!="))
1500 return tokval->t_type = TOKEN_NE;
1501 if (!strcmp(tline->text, "<="))
1502 return tokval->t_type = TOKEN_LE;
1503 if (!strcmp(tline->text, ">="))
1504 return tokval->t_type = TOKEN_GE;
1505 if (!strcmp(tline->text, "&&"))
1506 return tokval->t_type = TOKEN_DBL_AND;
1507 if (!strcmp(tline->text, "^^"))
1508 return tokval->t_type = TOKEN_DBL_XOR;
1509 if (!strcmp(tline->text, "||"))
1510 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001511 }
1512
1513 /*
1514 * We have no other options: just return the first character of
1515 * the token text.
1516 */
1517 return tokval->t_type = tline->text[0];
1518}
1519
1520/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001521 * Compare a string to the name of an existing macro; this is a
1522 * simple wrapper which calls either strcmp or nasm_stricmp
1523 * depending on the value of the `casesense' parameter.
1524 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001525static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001526{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001527 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001528}
1529
1530/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001531 * Compare a string to the name of an existing macro; this is a
1532 * simple wrapper which calls either strcmp or nasm_stricmp
1533 * depending on the value of the `casesense' parameter.
1534 */
1535static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1536{
1537 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1538}
1539
1540/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001541 * Return the Context structure associated with a %$ token. Return
1542 * NULL, having _already_ reported an error condition, if the
1543 * context stack isn't deep enough for the supplied number of $
1544 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001545 *
1546 * If "namep" is non-NULL, set it to the pointer to the macro name
1547 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001548 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001549static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001550{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001551 Context *ctx;
1552 int i;
1553
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001554 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001555 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001556
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001557 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001559
H. Peter Anvine2c80182005-01-15 22:15:51 +00001560 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001561 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001563 }
1564
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001565 name += 2;
1566 ctx = cstk;
1567 i = 0;
1568 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001569 name++;
1570 i++;
1571 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001572 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001574 nasm_nonfatal("`%s': context stack is only"
1575 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001577 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001578
1579 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001580 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001581
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001582 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001583}
1584
1585/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001586 * Open an include file. This routine must always return a valid
1587 * file pointer if it returns - it's responsible for throwing an
1588 * ERR_FATAL and bombing out completely if not. It should also try
1589 * the include path one by one until it finds the file or reaches
1590 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001591 *
1592 * Note: for INC_PROBE the function returns NULL at all times;
1593 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001594 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001595enum incopen_mode {
1596 INC_NEEDED, /* File must exist */
1597 INC_OPTIONAL, /* Missing is OK */
1598 INC_PROBE /* Only an existence probe */
1599};
1600
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001601/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001602static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001603 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001604{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001605 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001606 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001607 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001608 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001609 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001610
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001612 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001613 if (omode == INC_PROBE) {
1614 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001615 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001616 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001617 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001618 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001619 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001620 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001621 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001623 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001624
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001625 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001626
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001627 if (!ip) {
1628 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001629 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001630 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001631
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001632 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001633 ip = ip->next;
1634 }
1635}
1636
1637/*
1638 * Open a file, or test for the presence of one (depending on omode),
1639 * considering the include path.
1640 */
1641static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001642 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001643 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001644 enum incopen_mode omode,
1645 enum file_flags fmode)
1646{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001647 struct hash_insert hi;
1648 void **hp;
1649 char *path;
1650 FILE *fp = NULL;
1651
1652 hp = hash_find(&FileHash, file, &hi);
1653 if (hp) {
1654 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001655 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001656 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001657 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001658 } else {
1659 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001660 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001661
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001662 /* Positive or negative result */
1663 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001664
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001665 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001666 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001667 */
1668 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001669 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001670 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001671
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001672 if (!path) {
1673 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001674 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001675 } else {
1676 if (!fp && omode != INC_PROBE)
1677 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001678 }
1679
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001680 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001681 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001682
1683 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001684}
1685
1686/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001687 * Opens an include or input file. Public version, for use by modules
1688 * that get a file:lineno pair and need to look at the file again
1689 * (e.g. the CodeView debug backend). Returns NULL on failure.
1690 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001691FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001692{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001693 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001694}
1695
1696/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001697 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001698 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001699 * return true if _any_ single-line macro of that name is defined.
1700 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001701 * `nparam' or no parameters is defined.
1702 *
1703 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001704 * defined, or nparam is -1, the address of the definition structure
1705 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001706 * is NULL, no action will be taken regarding its contents, and no
1707 * error will occur.
1708 *
1709 * Note that this is also called with nparam zero to resolve
1710 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001711 *
1712 * If you already know which context macro belongs to, you can pass
1713 * the context pointer as first parameter; if you won't but name begins
1714 * with %$ the context will be automatically computed. If all_contexts
1715 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001716 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001717static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001718smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001719 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001720{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001721 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001722 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001723
H. Peter Anvin97a23472007-09-16 17:57:25 -07001724 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001725 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001726 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001727 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001728 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001730 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001731 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001732 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001733 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001734 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001735 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001736
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 while (m) {
1738 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001739 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001741 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 *defn = m;
1743 else
1744 *defn = NULL;
1745 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001746 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001747 }
1748 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001749 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001750
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001751 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001752}
1753
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001754/* param should be a natural number [0; INT_MAX] */
1755static int read_param_count(const char *str)
1756{
1757 int result;
1758 bool err;
1759
1760 result = readnum(str, &err);
1761 if (result < 0 || result > INT_MAX) {
1762 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001763 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1764 str, 0, INT_MAX);
1765 } else if (err)
1766 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001767 return result;
1768}
1769
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001770/*
1771 * Count and mark off the parameters in a multi-line macro call.
1772 * This is called both from within the multi-line macro expansion
1773 * code, and also to mark off the default parameters when provided
1774 * in a %macro definition line.
1775 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001777{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001778 int paramsize, brace;
1779
1780 *nparam = paramsize = 0;
1781 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001783 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001784 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 paramsize += PARAM_DELTA;
1786 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1787 }
1788 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001789 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001791 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001793 if (brace) {
1794 while (brace && (t = t->next) != NULL) {
1795 if (tok_is_(t, "{"))
1796 brace++;
1797 else if (tok_is_(t, "}"))
1798 brace--;
1799 }
1800
1801 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 /*
1803 * Now we've found the closing brace, look further
1804 * for the comma.
1805 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001806 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 skip_white_(t);
1808 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001809 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 while (tok_isnt_(t, ","))
1811 t = t->next;
1812 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001814 } else {
1815 while (tok_isnt_(t, ","))
1816 t = t->next;
1817 }
1818 if (t) { /* got a comma/brace */
1819 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001821 }
1822}
1823
1824/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001825 * Determine whether one of the various `if' conditions is true or
1826 * not.
1827 *
1828 * We must free the tline we get passed.
1829 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001830static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001831{
H. Peter Anvin70055962007-10-11 00:05:31 -07001832 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001833 Token *t, *tt, *origline;
1834 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001835 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001836 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001837 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001838 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001839 const char *dname = pp_directives[ct];
1840 bool casesense = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001841
1842 origline = tline;
1843
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001844 switch (PP_COND(ct)) {
1845 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001846 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001847 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001849 if (!tline)
1850 break;
1851 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001852 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001853 dname);
1854 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001856 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001857 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 tline = tline->next;
1859 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001860 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001861
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001862 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001863 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001864 while (tline) {
1865 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 if (!tline || (tline->type != TOK_ID &&
1867 (tline->type != TOK_PREPROC_ID ||
1868 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001869 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001870 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001871 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001873 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001874 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001876 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001877 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001878
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001879 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001880 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001881 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001882 while (tline) {
1883 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001884 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001885 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001886 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001887 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001888 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001889 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001890 goto fail;
1891 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001892 p = tline->text;
1893 if (tline->type == TOK_PREPROC_ID)
1894 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001895 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001896 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001897 if (getenv(p))
1898 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001899 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001900 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001901 break;
1902
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001903 case PP_IFIDNI:
1904 casesense = false;
1905 /* fall through */
1906 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 tline = expand_smacro(tline);
1908 t = tt = tline;
1909 while (tok_isnt_(tt, ","))
1910 tt = tt->next;
1911 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001912 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001913 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 }
1916 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001917 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1919 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001920 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001921 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 }
1924 if (t->type == TOK_WHITESPACE) {
1925 t = t->next;
1926 continue;
1927 }
1928 if (tt->type == TOK_WHITESPACE) {
1929 tt = tt->next;
1930 continue;
1931 }
1932 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001933 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934 break;
1935 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001936 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 size_t l1 = nasm_unquote(t->text, NULL);
1939 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001940
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001941 if (l1 != l2) {
1942 j = false;
1943 break;
1944 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001945 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001946 j = false;
1947 break;
1948 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001949 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001950 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 break;
1952 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001953
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 t = t->next;
1955 tt = tt->next;
1956 }
1957 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001958 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001960
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001961 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001962 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001964 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001965
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966 skip_white_(tline);
1967 tline = expand_id(tline);
1968 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001969 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001970 goto fail;
1971 }
1972 searching.name = nasm_strdup(tline->text);
1973 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001974 searching.plus = false;
1975 searching.nolist = false;
1976 searching.in_progress = 0;
1977 searching.max_depth = 0;
1978 searching.rep_nest = NULL;
1979 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 searching.nparam_max = INT_MAX;
1981 tline = expand_smacro(tline->next);
1982 skip_white_(tline);
1983 if (!tline) {
1984 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001985 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001986 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 } else {
1988 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001989 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990 }
1991 if (tline && tok_is_(tline->next, "-")) {
1992 tline = tline->next->next;
1993 if (tok_is_(tline, "*"))
1994 searching.nparam_max = INT_MAX;
1995 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001996 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001997 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001998 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001999 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002000 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002001 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002002 searching.nparam_max = searching.nparam_min;
2003 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 }
2005 }
2006 if (tline && tok_is_(tline->next, "+")) {
2007 tline = tline->next;
2008 searching.plus = true;
2009 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002010 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2011 while (mmac) {
2012 if (!strcmp(mmac->name, searching.name) &&
2013 (mmac->nparam_min <= searching.nparam_max
2014 || searching.plus)
2015 && (searching.nparam_min <= mmac->nparam_max
2016 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 found = true;
2018 break;
2019 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002020 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 }
2022 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002023 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002024 nasm_free(searching.name);
2025 j = found;
2026 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002027 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002028
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002029 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 needtype = TOK_ID;
2031 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002032 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002033 needtype = TOK_NUMBER;
2034 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002035 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 needtype = TOK_STRING;
2037 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002038
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039iftype:
2040 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002041
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 while (tok_type_(t, TOK_WHITESPACE) ||
2043 (needtype == TOK_NUMBER &&
2044 tok_type_(t, TOK_OTHER) &&
2045 (t->text[0] == '-' || t->text[0] == '+') &&
2046 !t->text[1]))
2047 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002048
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002049 j = tok_type_(t, needtype);
2050 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002051
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002052 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002053 t = tline = expand_smacro(tline);
2054 while (tok_type_(t, TOK_WHITESPACE))
2055 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002056
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 j = false;
2058 if (t) {
2059 t = t->next; /* Skip the actual token */
2060 while (tok_type_(t, TOK_WHITESPACE))
2061 t = t->next;
2062 j = !t; /* Should be nothing left */
2063 }
2064 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002065
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002066 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002067 t = tline = expand_smacro(tline);
2068 while (tok_type_(t, TOK_WHITESPACE))
2069 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002070
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 j = !t; /* Should be empty */
2072 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002073
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002074 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002075 pps.tptr = tline = expand_smacro(tline);
2076 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002078 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002079 if (!evalresult)
2080 return -1;
2081 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002082 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002083 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002084 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002085 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002088 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002089 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002090
H. Peter Anvine2c80182005-01-15 22:15:51 +00002091 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002092 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002093 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002094 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002095
2096 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002097 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002098
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002099fail:
2100 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002101 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002102}
2103
2104/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002105 * Default smacro expansion routine: just returns a copy of the
2106 * expansion list.
2107 */
2108static Token *
2109smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2110{
2111 (void)params;
2112 (void)nparams;
2113
2114 return dup_tlist(s->expansion, NULL);
2115}
2116
2117/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002118 * Emit a macro defintion or undef to the listing file, if
2119 * desired. This is similar to detoken(), but it handles the reverse
2120 * expansion list, does not expand %! or local variable tokens, and
2121 * does some special handling for macro parameters.
2122 */
2123static void
2124list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2125{
2126 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2127 const Token **param_names;
2128 Token *junk_names = NULL;
2129 Token *t;
2130 size_t namelen, size;
2131 unsigned int i;
2132 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002133 char *context_prefix = NULL;
2134 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002135
2136 nasm_newn(param_names, m->nparam);
2137
2138 namelen = strlen(m->name);
2139 size = namelen + 2; /* Include room for space after name + NUL */
2140
2141 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002142 int context_depth = cstk->depth - ctx->depth + 1;
2143 context_prefix =
2144 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2145 ctx->name ? ctx->name : "",
2146 ctx->number, context_depth, "");
2147
2148 context_len = nasm_last_string_len();
2149 memset(context_prefix + context_len - context_depth,
2150 '$', context_depth);
2151 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002152 }
2153
2154 list_for_each(t, m->expansion) {
2155 if (!t->text) {
2156 size++; /* Whitespace, presumably */
2157 } else {
2158 size += t->len;
2159 if (is_smac_param(t->type))
2160 param_names[smac_nparam(t->type)] = t;
2161 }
2162 }
2163
2164 if (m->nparam) {
2165 /*
2166 * Space for ( and either , or ) around each
2167 * parameter, plus an optional =.
2168 */
2169 size += 1 + 2 * m->nparam;
2170 for (i = 0; i < m->nparam; i++) {
2171 if (!param_names[i])
2172 param_names[i] = &unused_arg_name;
2173 size += param_names[i]->len;
2174 }
2175 }
2176
2177 def = nasm_malloc(size);
2178 p = def+size;
2179 *--p = '\0';
2180
2181 list_for_each(t, m->expansion) {
2182 if (!t->text) {
2183 *--p = ' ';
2184 } else {
2185 p -= t->len;
2186 memcpy(p, t->text, t->len);
2187 }
2188 }
2189
2190 *--p = ' ';
2191
2192 if (m->nparam) {
2193 *--p = ')';
2194 for (i = m->nparam; i--;) {
2195 p -= param_names[i]->len;
2196 memcpy(p, param_names[i]->text, param_names[i]->len);
2197 if (m->eval_param && m->eval_param[i])
2198 *--p = '=';
2199 *--p = ',';
2200 }
2201 *p = '('; /* First parameter starts with ( not , */
2202
2203 free_tlist(junk_names);
2204 }
2205
2206 p -= namelen;
2207 memcpy(p, m->name, namelen);
2208
H. Peter Anvin6686de22019-08-10 05:33:14 -07002209 if (context_prefix) {
2210 p -= context_len;
2211 memcpy(p, context_prefix, context_len);
2212 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002213 }
2214
2215 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002216 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002217}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002218
2219/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002220 * Common code for defining an smacro. The tmpl argument, if not NULL,
2221 * contains any macro parameters that aren't explicit arguments;
2222 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002223 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002224static SMacro *define_smacro(const char *mname, bool casesense,
2225 Token *expansion, const SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002226{
2227 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002228 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002229 Context *ctx;
2230 bool defining_alias = false;
2231 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002232
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002233 if (tmpl) {
2234 defining_alias = tmpl->alias;
2235 nparam = tmpl->nparam;
2236 }
2237
2238 while (1) {
2239 ctx = get_ctx(mname, &mname);
2240
2241 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2242 /* Create a new macro */
2243 smtbl = ctx ? &ctx->localmac : &smacros;
2244 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2245 nasm_new(smac);
2246 smac->next = *smhead;
2247 *smhead = smac;
2248 break;
2249 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002250 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002251 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002252 /*
2253 * Some instances of the old code considered this a failure,
2254 * some others didn't. What is the right thing to do here?
2255 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002256 goto fail;
2257 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002258 /*
2259 * We're redefining, so we have to take over an
2260 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002261 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002262 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002263 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002264 break;
2265 } else if (smac->in_progress) {
2266 nasm_nonfatal("macro alias loop");
2267 goto fail;
2268 } else {
2269 /* It is an alias macro; follow the alias link */
2270 SMacro *s;
2271
2272 smac->in_progress = true;
2273 s = define_smacro(smac->expansion->text, casesense,
2274 expansion, tmpl);
2275 smac->in_progress = false;
2276 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002277 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002278 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002279
2280 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002281 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002282 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002283 smac->expand = smacro_expand_default;
2284 if (tmpl) {
2285 smac->nparam = tmpl->nparam;
2286 smac->eval_param = tmpl->eval_param;
2287 smac->alias = tmpl->alias;
2288 if (tmpl->expand)
2289 smac->expand = tmpl->expand;
2290 }
2291 if (list_option('m')) {
2292 static const enum preproc_token op[2][2] = {
2293 { PP_DEFINE, PP_IDEFINE },
2294 { PP_DEFALIAS, PP_IDEFALIAS }
2295 };
2296 list_smacro_def(op[!!smac->alias][casesense], ctx, smac);
2297 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002298 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002299
2300fail:
2301 free_tlist(expansion);
2302 if (tmpl && tmpl->eval_param)
2303 nasm_free(tmpl->eval_param);
2304 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002305}
2306
2307/*
2308 * Undefine an smacro
2309 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002310static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002311{
2312 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002313 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002314 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002315
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002316 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002317 smtbl = ctx ? &ctx->localmac : &smacros;
2318 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002319
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002320 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002321 /*
2322 * We now have a macro name... go hunt for it.
2323 */
2324 sp = smhead;
2325 while ((s = *sp) != NULL) {
2326 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002327 if (s->alias && !undefalias) {
2328 if (s->in_progress) {
2329 nasm_nonfatal("macro alias loop");
2330 } else {
2331 s->in_progress = true;
2332 undef_smacro(s->expansion->text, false);
2333 s->in_progress = false;
2334 }
2335 } else {
2336 if (list_option('m'))
2337 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2338 ctx, s);
2339 *sp = s->next;
2340 free_smacro(s);
2341 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002342 } else {
2343 sp = &s->next;
2344 }
2345 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002346 }
2347}
2348
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002349/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002350 * Parse a mmacro specification.
2351 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002352static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002353{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002354 tline = tline->next;
2355 skip_white_(tline);
2356 tline = expand_id(tline);
2357 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002358 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002359 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002360 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002361
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002362 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002363 def->name = nasm_strdup(tline->text);
2364 def->plus = false;
2365 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002366 def->in_progress = 0;
2367 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002368 def->nparam_min = 0;
2369 def->nparam_max = 0;
2370
H. Peter Anvina26433d2008-07-16 14:40:01 -07002371 tline = expand_smacro(tline->next);
2372 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002373 if (!tok_type_(tline, TOK_NUMBER))
2374 nasm_nonfatal("`%s' expects a parameter count", directive);
2375 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002376 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002377 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002378 tline = tline->next->next;
2379 if (tok_is_(tline, "*")) {
2380 def->nparam_max = INT_MAX;
2381 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002382 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002383 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002384 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002385 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002386 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002387 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002388 }
2389 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002390 }
2391 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002392 tline = tline->next;
2393 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002394 }
2395 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002396 !nasm_stricmp(tline->next->text, ".nolist")) {
2397 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002398 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002399 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002400
H. Peter Anvina26433d2008-07-16 14:40:01 -07002401 /*
2402 * Handle default parameters.
2403 */
2404 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002405 def->dlist = tline->next;
2406 tline->next = NULL;
2407 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002408 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002409 def->dlist = NULL;
2410 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002411 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002412 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002413
H. Peter Anvin89cee572009-07-15 09:16:54 -04002414 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002415 !def->plus) {
2416 /*
2417 *!macro-defaults [on] macros with more default than optional parameters
2418 *! warns when a macro has more default parameters than optional parameters.
2419 *! See \k{mlmacdef} for why might want to disable this warning.
2420 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002421 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002422 "too many default macro parameters in macro `%s'", def->name);
2423 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002424
H. Peter Anvina26433d2008-07-16 14:40:01 -07002425 return true;
2426}
2427
2428
2429/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002430 * Decode a size directive
2431 */
2432static int parse_size(const char *str) {
2433 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002434 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002435 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002436 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002437 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002438}
2439
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002440/*
2441 * Process a preprocessor %pragma directive. Currently there are none.
2442 * Gets passed the token list starting with the "preproc" token from
2443 * "%pragma preproc".
2444 */
2445static void do_pragma_preproc(Token *tline)
2446{
2447 /* Skip to the real stuff */
2448 tline = tline->next;
2449 skip_white_(tline);
2450 if (!tline)
2451 return;
2452
2453 (void)tline; /* Nothing else to do at present */
2454}
2455
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002456static bool is_macro_id(const Token *t)
2457{
2458 return t && (t->type == TOK_ID ||
2459 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2460}
2461
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002462static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002463{
2464 char *id;
2465 Token *t = *tp;
2466
2467 t = t->next; /* Skip directive */
2468 skip_white_(t);
2469 t = expand_id(t);
2470
2471 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002472 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002473 err ? err : "macro identifier");
2474 return NULL;
2475 }
2476
2477 id = t->text;
2478 skip_white_(t);
2479 *tp = t;
2480 return id;
2481}
2482
Ed Beroset3ab3f412002-06-11 03:31:49 +00002483/**
2484 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002485 * Find out if a line contains a preprocessor directive, and deal
2486 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002487 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002488 * If a directive _is_ found, it is the responsibility of this routine
2489 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002490 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002491 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002492 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002493 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002494 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002495 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002496static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002497{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002498 enum preproc_token i;
2499 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002500 bool err;
2501 int nparam;
2502 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002503 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002504 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002505 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002506 char *p, *pp;
2507 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002508 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002509 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002510 Include *inc;
2511 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002512 Cond *cond;
2513 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002514 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002515 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002516 struct tokenval tokval;
2517 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002518 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002519 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002520 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002521 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002522 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002523
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002524 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002525 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002526
H. Peter Anvineba20a72002-04-30 20:53:55 +00002527 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002528 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002529 (tline->text[0] && (tline->text[1] == '%' ||
2530 tline->text[1] == '$' ||
2531 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002533
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002534 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002535 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002536
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002537 casesense = true;
2538 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2539 casesense = false;
2540 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002541 }
2542
2543 /*
2544 * If we're in a non-emitting branch of a condition construct,
2545 * or walking to the end of an already terminated %rep block,
2546 * we should ignore all directives except for condition
2547 * directives.
2548 */
2549 if (((istk->conds && !emitting(istk->conds->state)) ||
2550 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2551 return NO_DIRECTIVE_FOUND;
2552 }
2553
2554 /*
2555 * If we're defining a macro or reading a %rep block, we should
2556 * ignore all directives except for %macro/%imacro (which nest),
2557 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2558 * If we're in a %rep block, another %rep nests, so should be let through.
2559 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002560 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002561 i != PP_ENDMACRO && i != PP_ENDM &&
2562 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2563 return NO_DIRECTIVE_FOUND;
2564 }
2565
2566 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002567 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002568 nested_mac_count++;
2569 return NO_DIRECTIVE_FOUND;
2570 } else if (nested_mac_count > 0) {
2571 if (i == PP_ENDMACRO) {
2572 nested_mac_count--;
2573 return NO_DIRECTIVE_FOUND;
2574 }
2575 }
2576 if (!defining->name) {
2577 if (i == PP_REP) {
2578 nested_rep_count++;
2579 return NO_DIRECTIVE_FOUND;
2580 } else if (nested_rep_count > 0) {
2581 if (i == PP_ENDREP) {
2582 nested_rep_count--;
2583 return NO_DIRECTIVE_FOUND;
2584 }
2585 }
2586 }
2587 }
2588
H. Peter Anvin4169a472007-09-12 01:29:43 +00002589 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002590 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002591 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002592 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002593
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002594 case PP_PRAGMA:
2595 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002596 * %pragma namespace options...
2597 *
2598 * The namespace "preproc" is reserved for the preprocessor;
2599 * all other namespaces generate a [pragma] assembly directive.
2600 *
2601 * Invalid %pragmas are ignored and may have different
2602 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002603 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002604 t = tline;
2605 tline = tline->next;
2606 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002607 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002608 while (tok_type_(tline, TOK_WHITESPACE)) {
2609 t = tline;
2610 tline = tline->next;
2611 delete_Token(t);
2612 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002613 if (tok_type_(tline, TOK_ID)) {
2614 if (!nasm_stricmp(tline->text, "preproc")) {
2615 /* Preprocessor pragma */
2616 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002617 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002618 } else {
2619 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002620
2621 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002622 for (t = tline; t->next; t = t->next)
2623 ;
2624 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002625
2626 /* Prepend "[pragma " */
2627 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2628 t = new_Token(t, TOK_ID, "pragma", 6);
2629 t = new_Token(t, TOK_OTHER, "[", 1);
2630 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002631 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002632 }
2633 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002634 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002635
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 case PP_STACKSIZE:
2637 /* Directive to tell NASM what the default stack size is. The
2638 * default is for a 16-bit stack, and this can be overriden with
2639 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 */
2641 tline = tline->next;
2642 if (tline && tline->type == TOK_WHITESPACE)
2643 tline = tline->next;
2644 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002645 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002646 }
2647 if (nasm_stricmp(tline->text, "flat") == 0) {
2648 /* All subsequent ARG directives are for a 32-bit stack */
2649 StackSize = 4;
2650 StackPointer = "ebp";
2651 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002652 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002653 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2654 /* All subsequent ARG directives are for a 64-bit stack */
2655 StackSize = 8;
2656 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002657 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002658 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002659 } else if (nasm_stricmp(tline->text, "large") == 0) {
2660 /* All subsequent ARG directives are for a 16-bit stack,
2661 * far function call.
2662 */
2663 StackSize = 2;
2664 StackPointer = "bp";
2665 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002666 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 } else if (nasm_stricmp(tline->text, "small") == 0) {
2668 /* All subsequent ARG directives are for a 16-bit stack,
2669 * far function call. We don't support near functions.
2670 */
2671 StackSize = 2;
2672 StackPointer = "bp";
2673 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002674 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002676 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002678 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002679
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 case PP_ARG:
2681 /* TASM like ARG directive to define arguments to functions, in
2682 * the following form:
2683 *
2684 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2685 */
2686 offset = ArgOffset;
2687 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002688 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002689 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002690
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 /* Find the argument name */
2692 tline = tline->next;
2693 if (tline && tline->type == TOK_WHITESPACE)
2694 tline = tline->next;
2695 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002696 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002697 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 }
2699 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002700
H. Peter Anvine2c80182005-01-15 22:15:51 +00002701 /* Find the argument size type */
2702 tline = tline->next;
2703 if (!tline || tline->type != TOK_OTHER
2704 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002705 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002706 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 }
2708 tline = tline->next;
2709 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002710 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002711 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002713
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002715 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 size = parse_size(tt->text);
2718 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002719 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002721 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 }
2723 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002724
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002725 /* Round up to even stack slots */
2726 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002727
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 /* Now define the macro for the argument */
2729 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2730 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002731 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002732 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002733
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 /* Move to the next argument in the list */
2735 tline = tline->next;
2736 if (tline && tline->type == TOK_WHITESPACE)
2737 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002738 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002740 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002741
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 case PP_LOCAL:
2743 /* TASM like LOCAL directive to define local variables for a
2744 * function, in the following form:
2745 *
2746 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2747 *
2748 * The '= LocalSize' at the end is ignored by NASM, but is
2749 * required by TASM to define the local parameter size (and used
2750 * by the TASM macro package).
2751 */
2752 offset = LocalOffset;
2753 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002754 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002756
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 /* Find the argument name */
2758 tline = tline->next;
2759 if (tline && tline->type == TOK_WHITESPACE)
2760 tline = tline->next;
2761 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002762 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002763 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 }
2765 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002766
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 /* Find the argument size type */
2768 tline = tline->next;
2769 if (!tline || tline->type != TOK_OTHER
2770 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002771 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002772 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 }
2774 tline = tline->next;
2775 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002776 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002777 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002779
H. Peter Anvine2c80182005-01-15 22:15:51 +00002780 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002781 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002783 size = parse_size(tt->text);
2784 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002785 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002787 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 }
2789 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002790
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002791 /* Round up to even stack slots */
2792 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002793
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002795
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002796 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2798 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002799 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002800
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 /* Now define the assign to setup the enter_c macro correctly */
2802 snprintf(directive, sizeof(directive),
2803 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002804 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002805
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 /* Move to the next argument in the list */
2807 tline = tline->next;
2808 if (tline && tline->type == TOK_WHITESPACE)
2809 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002810 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002812 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002813
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 case PP_CLEAR:
2815 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002816 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002817 free_macros();
2818 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002819 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002820
H. Peter Anvin418ca702008-05-30 10:42:30 -07002821 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002822 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002823 skip_white_(t);
2824 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002825 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002826 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002827 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002828 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002829 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002830 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002831 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002832 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002833 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002834 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002835 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002836
2837 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002838 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002839 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002840
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002841 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002842 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002843 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002844 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002845 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002846 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002847 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002848 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002849 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002850 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002851 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002853 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002854 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002855 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002856 (pp_mode == PP_DEPS)
2857 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002858 if (!inc->fp) {
2859 /* -MG given but file not found */
2860 nasm_free(inc);
2861 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002862 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002863 inc->lineno = src_set_linnum(0);
2864 inc->lineinc = 1;
2865 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002866 inc->mstk = NULL;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002867 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002868 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002869 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002870 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002871 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002872
H. Peter Anvind2456592008-06-19 15:04:18 -07002873 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002874 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002875 static macros_t *use_pkg;
2876 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002877
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002878 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002879 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002880 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002881 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002882 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002883 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002884 use_pkg = nasm_stdmac_find_package(tline->text);
2885 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002886 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002887 else
2888 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002889 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002890 /*
2891 * Not already included, go ahead and include it.
2892 * Treat it as an include file for the purpose of
2893 * producing a listing.
2894 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002895 stdmacpos = use_pkg;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002896 nasm_new(inc);
2897 inc->next = istk;
2898 inc->fname = src_set_fname(NULL);
2899 inc->lineno = src_set_linnum(0);
2900 inc->lineinc = 0;
2901 inc->expansion = NULL;
2902 inc->mstk = NULL;
2903 inc->nolist = !list_option('b') || istk->nolist;
2904 istk = inc;
2905 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002906 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002907 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002908 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002910 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002911 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 tline = tline->next;
2913 skip_white_(tline);
2914 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002915 if (tline) {
2916 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002917 nasm_nonfatal("`%s' expects a context identifier",
2918 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002919 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002920 }
2921 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002922 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002923 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002924 p = nasm_strdup(tline->text);
2925 } else {
2926 p = NULL; /* Anonymous */
2927 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002928
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002929 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002930 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002931 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002932 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002933 ctx->name = p;
2934 ctx->number = unique++;
2935 cstk = ctx;
2936 } else {
2937 /* %pop or %repl */
2938 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002939 nasm_nonfatal("`%s': context stack is empty",
2940 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002941 } else if (i == PP_POP) {
2942 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002943 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002944 "expected %s",
2945 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002946 else
2947 ctx_pop();
2948 } else {
2949 /* i == PP_REPL */
2950 nasm_free(cstk->name);
2951 cstk->name = p;
2952 p = NULL;
2953 }
2954 nasm_free(p);
2955 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002956 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002957 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002958 severity = ERR_FATAL;
2959 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002961 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002962 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002963 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002964 /*!
2965 *!user [on] %warning directives
2966 *! controls output of \c{%warning} directives (see \k{pperror}).
2967 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002968 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002969 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002970
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002971issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002972 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002973 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002974 tline->next = expand_smacro(tline->next);
2975 tline = tline->next;
2976 skip_white_(tline);
2977 t = tline ? tline->next : NULL;
2978 skip_white_(t);
2979 if (tok_type_(tline, TOK_STRING) && !t) {
2980 /* The line contains only a quoted string */
2981 p = tline->text;
2982 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002983 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002984 } else {
2985 /* Not a quoted string, or more than a quoted string */
2986 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002987 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002988 nasm_free(p);
2989 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002990 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002991 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002992
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002993 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002994 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002995 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002996 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002997 j = if_condition(tline->next, i);
2998 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002999 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003000 cond = nasm_malloc(sizeof(Cond));
3001 cond->next = istk->conds;
3002 cond->state = j;
3003 istk->conds = cond;
3004 if(istk->mstk)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003005 istk->mstk->condcnt++;
3006 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003007
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003008 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003009 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003010 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003011 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003012 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003013 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003014 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003015
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003016 case COND_DONE:
3017 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003018 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003019
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003020 case COND_ELSE_TRUE:
3021 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003022 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003023 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003024 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003025 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003026
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003027 case COND_IF_FALSE:
3028 /*
3029 * IMPORTANT: In the case of %if, we will already have
3030 * called expand_mmac_params(); however, if we're
3031 * processing an %elif we must have been in a
3032 * non-emitting mode, which would have inhibited
3033 * the normal invocation of expand_mmac_params().
3034 * Therefore, we have to do it explicitly here.
3035 */
3036 j = if_condition(expand_mmac_params(tline->next), i);
3037 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003038 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003041 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003042
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 case PP_ELSE:
3044 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003045 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003046 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003047 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003048 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003049 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003050 case COND_IF_TRUE:
3051 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003052 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003053 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003054
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003055 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003056 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003057
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003058 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003059 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003060 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003061
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003062 case COND_ELSE_TRUE:
3063 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003064 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003065 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003066 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003067 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003068 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003069 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003070
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 case PP_ENDIF:
3072 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003073 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003074 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003075 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003076 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003077 cond = istk->conds;
3078 istk->conds = cond->next;
3079 nasm_free(cond);
3080 if(istk->mstk)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003081 istk->mstk->condcnt--;
3082 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003083
H. Peter Anvin8b262472019-02-26 14:00:54 -08003084 case PP_RMACRO:
3085 case PP_MACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003086 if (defining)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003087 nasm_fatal("`%s': already defining a macro", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003088
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003089 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin322bee02019-08-10 01:38:06 -07003090 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003091 defining->casesense = casesense;
3092 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003093 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003094 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003095 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003096
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003097 src_get(&defining->xline, &defining->fname);
3098
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003099 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3100 while (mmac) {
3101 if (!strcmp(mmac->name, defining->name) &&
3102 (mmac->nparam_min <= defining->nparam_max
3103 || defining->plus)
3104 && (defining->nparam_min <= mmac->nparam_max
3105 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003106 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003107 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003108 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003109 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003110 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003111 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003112 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003113
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 case PP_ENDM:
3115 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003116 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003117 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003118 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003119 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003120 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3121 defining->next = *mmhead;
3122 *mmhead = defining;
3123 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003124 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003125
H. Peter Anvin89cee572009-07-15 09:16:54 -04003126 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003127 /*
3128 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003129 * macro-end marker for a macro with a name. Then we
3130 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003131 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003132 list_for_each(l, istk->expansion)
3133 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003134 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003135
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003136 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003137 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003138 * Remove all conditional entries relative to this
3139 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003140 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003141 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3142 cond = istk->conds;
3143 istk->conds = cond->next;
3144 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003145 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003146 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003147 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003148 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003149 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003150 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003151
H. Peter Anvina26433d2008-07-16 14:40:01 -07003152 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003153 casesense = false;
3154 /* fall through */
3155 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003156 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003157 MMacro **mmac_p;
3158 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003159
H. Peter Anvin8b262472019-02-26 14:00:54 -08003160 spec.casesense = casesense;
3161 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003162 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003163 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003164 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3165 while (mmac_p && *mmac_p) {
3166 mmac = *mmac_p;
3167 if (mmac->casesense == spec.casesense &&
3168 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3169 mmac->nparam_min == spec.nparam_min &&
3170 mmac->nparam_max == spec.nparam_max &&
3171 mmac->plus == spec.plus) {
3172 *mmac_p = mmac->next;
3173 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003174 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003175 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 }
3177 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003179 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003180 }
3181
H. Peter Anvine2c80182005-01-15 22:15:51 +00003182 case PP_ROTATE:
3183 if (tline->next && tline->next->type == TOK_WHITESPACE)
3184 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003185 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003186 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003187 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 return DIRECTIVE_FOUND;
3189 }
3190 t = expand_smacro(tline->next);
3191 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003192 pps.tptr = tline = t;
3193 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003194 tokval.t_type = TOKEN_INVALID;
3195 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003196 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 free_tlist(tline);
3198 if (!evalresult)
3199 return DIRECTIVE_FOUND;
3200 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003201 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003202 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003203 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 return DIRECTIVE_FOUND;
3205 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003206 mmac = istk->mstk;
3207 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3208 mmac = mmac->next_active;
3209 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003210 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003211 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003212 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003213 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003214 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003215
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003216 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003217 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003218 rotate += mmac->nparam;
3219
3220 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003221 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003222 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003223
H. Peter Anvine2c80182005-01-15 22:15:51 +00003224 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003225 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 do {
3227 tline = tline->next;
3228 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003229
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 if (tok_type_(tline, TOK_ID) &&
3231 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003232 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003233 do {
3234 tline = tline->next;
3235 } while (tok_type_(tline, TOK_WHITESPACE));
3236 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003237
H. Peter Anvine2c80182005-01-15 22:15:51 +00003238 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003239 pps.tptr = expand_smacro(tline);
3240 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003241 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003242 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003244 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003245 if (!evalresult)
3246 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003247 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003248 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003250 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003251 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003253 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003254 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003255 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3256 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003257 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003258 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003259 /*!
3260 *!negative-rep [on] regative %rep count
3261 *! warns about negative counts given to the \c{%rep}
3262 *! preprocessor directive.
3263 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003264 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003265 "negative `%%rep' count: %"PRId64, count);
3266 count = 0;
3267 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003268 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003269 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003270 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003271 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003272 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003273 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003274 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003275 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003276 defining->nolist = nolist;
3277 defining->in_progress = count;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003278 defining->next_active = istk->mstk;
3279 defining->rep_nest = tmp_defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003280 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003281 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003282
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003284 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003285 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003286 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003288
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289 /*
3290 * Now we have a "macro" defined - although it has no name
3291 * and we won't be entering it in the hash tables - we must
3292 * push a macro-end marker for it on to istk->expansion.
3293 * After that, it will take care of propagating itself (a
3294 * macro-end marker line for a macro which is really a %rep
3295 * block will cause the macro to be re-expanded, complete
3296 * with another macro-end marker to ensure the process
3297 * continues) until the whole expansion is forcibly removed
3298 * from istk->expansion by a %exitrep.
3299 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003300 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003301 l->next = istk->expansion;
3302 l->finishes = defining;
3303 l->first = NULL;
3304 istk->expansion = l;
3305
3306 istk->mstk = defining;
3307
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003308 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003309 tmp_defining = defining;
3310 defining = defining->rep_nest;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003311 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003312
H. Peter Anvine2c80182005-01-15 22:15:51 +00003313 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003314 /*
3315 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003316 * macro-end marker for a macro with no name. Then we set
3317 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003318 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003319 list_for_each(l, istk->expansion)
3320 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003321 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003322
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003323 if (l)
3324 l->finishes->in_progress = 1;
3325 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003326 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003327 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003328
H. Peter Anvin8b262472019-02-26 14:00:54 -08003329 case PP_DEFINE:
3330 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003331 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003332 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003333 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003334 bool *eval_params = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003335 SMacro tmpl;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003336
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003337 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003338 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003339
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003340 nasm_zero(tmpl);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 last = tline;
3342 param_start = tline = tline->next;
3343 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003344
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 if (tok_is_(tline, "(")) {
3346 /*
3347 * This macro has parameters.
3348 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003349
H. Peter Anvine2c80182005-01-15 22:15:51 +00003350 tline = tline->next;
3351 while (1) {
3352 skip_white_(tline);
3353 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003354 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003355 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003356 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003357 if (tok_is_(tline, "=")) {
3358 have_eval_params = true;
3359 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003360 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003361 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003362
3363 /*
3364 * "*" means an unnamed, ignored argument; it can never
3365 * match anything because "*" is not TOK_ID, and so
3366 * none of the expansion entries will be converted
3367 * to parameters.
3368 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003369 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003370 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3371 /*
3372 * Empty name; duplicate the termination
3373 * token and use the first copy as a placeholder.
3374 * It will never match anything, since the
3375 * associated text doesn't correspond to a TOK_ID.
3376 */
3377 tline = dup_Token(tline, tline);
3378 } else {
3379 nasm_nonfatal("`%s': parameter identifier expected",
3380 tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003381 goto done;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003382 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003383 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003384 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003385 tline = tline->next;
3386 skip_white_(tline);
3387 if (tok_is_(tline, ",")) {
3388 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003389 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003391 nasm_nonfatal("`)' expected to terminate macro template");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003392 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003393 }
3394 break;
3395 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 }
3397 last = tline;
3398 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003399
3400 if (have_eval_params) {
3401 /* Create evaluated parameters table */
3402 bool is_eval = false;
3403
3404 nasm_newn(eval_params, nparam);
3405 list_for_each(tt, param_start) {
3406 if (is_smac_param(tt->type))
3407 eval_params[smac_nparam(tt->type)] = is_eval;
3408 is_eval = tok_is_(tt, "=");
3409 }
3410 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003412
H. Peter Anvine2c80182005-01-15 22:15:51 +00003413 if (tok_type_(tline, TOK_WHITESPACE))
3414 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003416
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003417 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003418 macro_start = tline;
3419 if (!is_macro_id(macro_start)) {
3420 nasm_nonfatal("`%s' expects a macro identifier to alias",
3421 dname);
3422 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003424 tt = macro_start->next;
3425 macro_start->next = NULL;
3426 tline = tline->next;
3427 skip_white_(tline);
3428 if (tline && tline->type) {
3429 nasm_warn(WARN_OTHER,
3430 "trailing garbage after aliasing identifier ignored");
3431 }
3432 free_tlist(tt);
3433 tmpl.alias = true;
3434 } else {
3435 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003436 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003437 tline = expand_smacro(tline);
3438
3439 macro_start = NULL;
3440 t = tline;
3441 while (t) {
3442 if (t->type == TOK_ID) {
3443 list_for_each(tt, param_start)
3444 if (is_smac_param(tt->type) &&
3445 tt->len == t->len &&
3446 !memcmp(tt->text, t->text, tt->len))
3447 t->type = tt->type;
3448 }
3449 tt = t->next;
3450 t->next = macro_start;
3451 macro_start = t;
3452 t = tt;
3453 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003454 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003455
H. Peter Anvine2c80182005-01-15 22:15:51 +00003456 /*
3457 * Good. We now have a macro name, a parameter count, and a
3458 * token list (in reverse order) for an expansion. We ought
3459 * to be OK just to create an SMacro, store it, and let
3460 * free_tlist have the rest of the line (which we have
3461 * carefully re-terminated after chopping off the expansion
3462 * from the end).
3463 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003464 tmpl.nparam = nparam;
3465 tmpl.eval_param = eval_params;
3466 define_smacro(mname, casesense, macro_start, &tmpl);
3467 break;
3468 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003469
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003471 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003472 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003473 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003474 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003475 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003476
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003477 undef_smacro(mname, i == PP_UNDEFALIAS);
3478 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003479
H. Peter Anvin8b262472019-02-26 14:00:54 -08003480 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003481 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003482 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003483
H. Peter Anvin9e200162008-06-04 17:23:14 -07003484 last = tline;
3485 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003487
3488 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003489 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003490
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003491 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003492 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003493 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003494
3495 /*
3496 * We now have a macro name, an implicit parameter count of
3497 * zero, and a string token to use as an expansion. Create
3498 * and store an SMacro.
3499 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003500 define_smacro(mname, casesense, macro_start, NULL);
3501 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003502
H. Peter Anvin8b262472019-02-26 14:00:54 -08003503 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003504 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003505 goto done;
3506
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003507 last = tline;
3508 tline = expand_smacro(tline->next);
3509 last->next = NULL;
3510
3511 t = tline;
3512 while (tok_type_(t, TOK_WHITESPACE))
3513 t = t->next;
3514 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003515 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003516 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003517 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003518 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003519 }
3520
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003521 /*
3522 * Convert the string to a token stream. Note that smacros
3523 * are stored with the token stream reversed, so we have to
3524 * reverse the output of tokenize().
3525 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003526 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003527 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003528
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003529 /*
3530 * We now have a macro name, an implicit parameter count of
3531 * zero, and a numeric token to use as an expansion. Create
3532 * and store an SMacro.
3533 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003534 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003535 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003536 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003537
H. Peter Anvin418ca702008-05-30 10:42:30 -07003538 case PP_PATHSEARCH:
3539 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003540 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003541
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003542 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003543 goto done;
3544
H. Peter Anvin418ca702008-05-30 10:42:30 -07003545 last = tline;
3546 tline = expand_smacro(tline->next);
3547 last->next = NULL;
3548
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003550 while (tok_type_(t, TOK_WHITESPACE))
3551 t = t->next;
3552
3553 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003554 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003555 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003556 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003557 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003558 }
3559 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003560 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003561 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003562 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003563 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003564
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003565 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003566 if (!found_path)
3567 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003568 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003569
3570 /*
3571 * We now have a macro name, an implicit parameter count of
3572 * zero, and a string token to use as an expansion. Create
3573 * and store an SMacro.
3574 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003575 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003576 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003577 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003578 }
3579
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003581 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003582 goto done;
3583
H. Peter Anvine2c80182005-01-15 22:15:51 +00003584 last = tline;
3585 tline = expand_smacro(tline->next);
3586 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003587
H. Peter Anvine2c80182005-01-15 22:15:51 +00003588 t = tline;
3589 while (tok_type_(t, TOK_WHITESPACE))
3590 t = t->next;
3591 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003592 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003593 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 free_tlist(tline);
3595 free_tlist(origline);
3596 return DIRECTIVE_FOUND;
3597 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003598
H. Peter Anvin8b262472019-02-26 14:00:54 -08003599 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003600
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 /*
3602 * We now have a macro name, an implicit parameter count of
3603 * zero, and a numeric token to use as an expansion. Create
3604 * and store an SMacro.
3605 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003606 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607 free_tlist(tline);
3608 free_tlist(origline);
3609 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003610
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003611 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003612 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003613 goto done;
3614
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003615 last = tline;
3616 tline = expand_smacro(tline->next);
3617 last->next = NULL;
3618
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003619 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003620 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003621 switch (t->type) {
3622 case TOK_WHITESPACE:
3623 break;
3624 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003625 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003626 break;
3627 case TOK_OTHER:
3628 if (!strcmp(t->text, ",")) /* permit comma separators */
3629 break;
3630 /* else fall through */
3631 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003632 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003633 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003634 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 }
3636 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003637
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003638 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003639 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003640 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003641 memcpy(p, t->text, t->len);
3642 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643 }
3644 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003645
3646 /*
3647 * We now have a macro name, an implicit parameter count of
3648 * zero, and a numeric token to use as an expansion. Create
3649 * and store an SMacro.
3650 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003651 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003652 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003653 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003654 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003655 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003656
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003658 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003659 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003660 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003661
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003662 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003663 goto done;
3664
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 last = tline;
3666 tline = expand_smacro(tline->next);
3667 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003668
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003669 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003670 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671 while (tok_type_(t, TOK_WHITESPACE))
3672 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003673
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003675 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003676 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003677 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003678 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003679 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003680
H. Peter Anvin8b262472019-02-26 14:00:54 -08003681 pps.tptr = t->next;
3682 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003684 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 if (!evalresult) {
3686 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003687 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003688 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003689 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003690 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003691 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003692 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003693 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003694
H. Peter Anvin8b262472019-02-26 14:00:54 -08003695 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3696 pps.tptr = pps.tptr->next;
3697 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003698 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699 } else {
3700 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003701 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003702 if (!evalresult) {
3703 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003704 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003706 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003707 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003708 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003709 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003710 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003711 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003712
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003713 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003714
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003715 /* make start and count being in range */
3716 if (start < 0)
3717 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003718 if (count < 0)
3719 count = len + count + 1 - start;
3720 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003721 count = len - start;
3722 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003723 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003724
H. Peter Anvin8b262472019-02-26 14:00:54 -08003725 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003726 macro_start->len = count;
3727 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3728 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003729
H. Peter Anvine2c80182005-01-15 22:15:51 +00003730 /*
3731 * We now have a macro name, an implicit parameter count of
3732 * zero, and a numeric token to use as an expansion. Create
3733 * and store an SMacro.
3734 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003735 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003736 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003737 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003738 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003739
H. Peter Anvin8b262472019-02-26 14:00:54 -08003740 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003741 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003742 goto done;
3743
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 last = tline;
3745 tline = expand_smacro(tline->next);
3746 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003747
H. Peter Anvin8b262472019-02-26 14:00:54 -08003748 pps.tptr = tline;
3749 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003750 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003751 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003752 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003753 if (!evalresult)
3754 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003755
H. Peter Anvine2c80182005-01-15 22:15:51 +00003756 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003757 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003758
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003760 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 free_tlist(origline);
3762 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003763 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003764
H. Peter Anvin8b262472019-02-26 14:00:54 -08003765 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003766
H. Peter Anvine2c80182005-01-15 22:15:51 +00003767 /*
3768 * We now have a macro name, an implicit parameter count of
3769 * zero, and a numeric token to use as an expansion. Create
3770 * and store an SMacro.
3771 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003772 define_smacro(mname, casesense, macro_start, NULL);
3773 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003774
H. Peter Anvine2c80182005-01-15 22:15:51 +00003775 case PP_LINE:
3776 /*
3777 * Syntax is `%line nnn[+mmm] [filename]'
3778 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003779 if (unlikely(pp_noline))
3780 goto done;
3781
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 tline = tline->next;
3783 skip_white_(tline);
3784 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003785 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003786 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003787 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003788 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 m = 1;
3790 tline = tline->next;
3791 if (tok_is_(tline, "+")) {
3792 tline = tline->next;
3793 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003794 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003795 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003796 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003797 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 tline = tline->next;
3799 }
3800 skip_white_(tline);
3801 src_set_linnum(k);
3802 istk->lineinc = m;
3803 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003804 char *fname = detoken(tline, false);
3805 src_set_fname(fname);
3806 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003808 break;
3809 }
3810
3811done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003812 free_tlist(origline);
3813 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003814}
3815
3816/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003817 * Ensure that a macro parameter contains a condition code and
3818 * nothing else. Return the condition code index if so, or -1
3819 * otherwise.
3820 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003821static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003822{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003823 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003824
H. Peter Anvin25a99342007-09-22 17:45:45 -07003825 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003826 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003827
H. Peter Anvineba20a72002-04-30 20:53:55 +00003828 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003829 if (!t)
3830 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003831 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003832 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003833 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003834 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003835 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003836 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003837
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003838 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003839}
3840
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003841/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003842 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003843 * pasting, if @handle_explicit passed then explicit pasting
3844 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003845 * The @m array can contain a series of token types which are
3846 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003847 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003848static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003849 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003850{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003851 Token *tok, *next, **prev_next, **prev_nonspace;
3852 bool pasted = false;
3853 char *buf, *p;
3854 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003855
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003856 /*
3857 * The last token before pasting. We need it
3858 * to be able to connect new handled tokens.
3859 * In other words if there were a tokens stream
3860 *
3861 * A -> B -> C -> D
3862 *
3863 * and we've joined tokens B and C, the resulting
3864 * stream should be
3865 *
3866 * A -> BC -> D
3867 */
3868 tok = *head;
3869 prev_next = NULL;
3870
3871 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3872 prev_nonspace = head;
3873 else
3874 prev_nonspace = NULL;
3875
3876 while (tok && (next = tok->next)) {
3877
3878 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003879 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003880 /* Zap redundant whitespaces */
3881 while (tok_type_(next, TOK_WHITESPACE))
3882 next = delete_Token(next);
3883 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003884 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003885
3886 case TOK_PASTE:
3887 /* Explicit pasting */
3888 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003889 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003890 next = delete_Token(tok);
3891
3892 while (tok_type_(next, TOK_WHITESPACE))
3893 next = delete_Token(next);
3894
3895 if (!pasted)
3896 pasted = true;
3897
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003898 /* Left pasting token is start of line */
3899 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003900 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003901
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003902 /*
3903 * No ending token, this might happen in two
3904 * cases
3905 *
3906 * 1) There indeed no right token at all
3907 * 2) There is a bare "%define ID" statement,
3908 * and @ID does expand to whitespace.
3909 *
3910 * So technically we need to do a grammar analysis
3911 * in another stage of parsing, but for now lets don't
3912 * change the behaviour people used to. Simply allow
3913 * whitespace after paste token.
3914 */
3915 if (!next) {
3916 /*
3917 * Zap ending space tokens and that's all.
3918 */
3919 tok = (*prev_nonspace)->next;
3920 while (tok_type_(tok, TOK_WHITESPACE))
3921 tok = delete_Token(tok);
3922 tok = *prev_nonspace;
3923 tok->next = NULL;
3924 break;
3925 }
3926
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003927 tok = *prev_nonspace;
3928 while (tok_type_(tok, TOK_WHITESPACE))
3929 tok = delete_Token(tok);
3930 len = strlen(tok->text);
3931 len += strlen(next->text);
3932
3933 p = buf = nasm_malloc(len + 1);
3934 strcpy(p, tok->text);
3935 p = strchr(p, '\0');
3936 strcpy(p, next->text);
3937
3938 delete_Token(tok);
3939
3940 tok = tokenize(buf);
3941 nasm_free(buf);
3942
3943 *prev_nonspace = tok;
3944 while (tok && tok->next)
3945 tok = tok->next;
3946
3947 tok->next = delete_Token(next);
3948
3949 /* Restart from pasted tokens head */
3950 tok = *prev_nonspace;
3951 break;
3952
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003953 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003954 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003955 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003956 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3957 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003958
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003959 len = 0;
3960 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3961 len += strlen(next->text);
3962 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003963 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003964
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003965 /* No match or no text to process */
3966 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003967 break;
3968
3969 len += strlen(tok->text);
3970 p = buf = nasm_malloc(len + 1);
3971
Adam Majer1a069432017-07-25 11:12:35 +02003972 strcpy(p, tok->text);
3973 p = strchr(p, '\0');
3974 tok = delete_Token(tok);
3975
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003976 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003977 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3978 strcpy(p, tok->text);
3979 p = strchr(p, '\0');
3980 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003981 tok = delete_Token(tok);
3982 }
3983
3984 tok = tokenize(buf);
3985 nasm_free(buf);
3986
3987 if (prev_next)
3988 *prev_next = tok;
3989 else
3990 *head = tok;
3991
3992 /*
3993 * Connect pasted into original stream,
3994 * ie A -> new-tokens -> B
3995 */
3996 while (tok && tok->next)
3997 tok = tok->next;
3998 tok->next = next;
3999
4000 if (!pasted)
4001 pasted = true;
4002
4003 /* Restart from pasted tokens head */
4004 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004005 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004006
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004007 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004008 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004009
4010 prev_next = &tok->next;
4011
4012 if (tok->next &&
4013 !tok_type_(tok->next, TOK_WHITESPACE) &&
4014 !tok_type_(tok->next, TOK_PASTE))
4015 prev_nonspace = prev_next;
4016
4017 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004018 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004019
4020 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004021}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004022
4023/*
4024 * expands to a list of tokens from %{x:y}
4025 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004026static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004027{
4028 Token *t = tline, **tt, *tm, *head;
4029 char *pos;
4030 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004031
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004032 pos = strchr(tline->text, ':');
4033 nasm_assert(pos);
4034
4035 lst = atoi(pos + 1);
4036 fst = atoi(tline->text + 1);
4037
4038 /*
4039 * only macros params are accounted so
4040 * if someone passes %0 -- we reject such
4041 * value(s)
4042 */
4043 if (lst == 0 || fst == 0)
4044 goto err;
4045
4046 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004047 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4048 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004049 goto err;
4050
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004051 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4052 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004053
4054 /* counted from zero */
4055 fst--, lst--;
4056
4057 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004058 * It will be at least one token. Note we
4059 * need to scan params until separator, otherwise
4060 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004061 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004062 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004063 if (!tm)
4064 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004065 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004066 tt = &head->next, tm = tm->next;
4067 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004068 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004069 *tt = t, tt = &t->next, tm = tm->next;
4070 }
4071
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004072 if (fst < lst) {
4073 for (i = fst + 1; i <= lst; i++) {
4074 t = new_Token(NULL, TOK_OTHER, ",", 0);
4075 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004076 j = (i + mac->rotate) % mac->nparam;
4077 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004078 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004079 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004080 *tt = t, tt = &t->next, tm = tm->next;
4081 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004082 }
4083 } else {
4084 for (i = fst - 1; i >= lst; i--) {
4085 t = new_Token(NULL, TOK_OTHER, ",", 0);
4086 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004087 j = (i + mac->rotate) % mac->nparam;
4088 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004089 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004090 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004091 *tt = t, tt = &t->next, tm = tm->next;
4092 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004093 }
4094 }
4095
4096 *last = tt;
4097 return head;
4098
4099err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004100 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004101 &tline->text[1]);
4102 return tline;
4103}
4104
H. Peter Anvin76690a12002-04-30 20:52:49 +00004105/*
4106 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004107 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004108 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004109 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004110static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004111{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004112 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004113 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004114 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004115
4116 tail = &thead;
4117 thead = NULL;
4118
H. Peter Anvine2c80182005-01-15 22:15:51 +00004119 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004120 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004121 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4122 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4123 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004124 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004125 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004126 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004127 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004128 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004129 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004130
H. Peter Anvine2c80182005-01-15 22:15:51 +00004131 t = tline;
4132 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004133
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004134 mac = istk->mstk;
4135 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4136 mac = mac->next_active;
4137 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004138 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004139 } else {
4140 pos = strchr(t->text, ':');
4141 if (!pos) {
4142 switch (t->text[1]) {
4143 /*
4144 * We have to make a substitution of one of the
4145 * forms %1, %-1, %+1, %%foo, %0.
4146 */
4147 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004148 type = TOK_NUMBER;
4149 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4150 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004151 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004152 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004153 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004154 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004155 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004156 text = nasm_strcat(tmpbuf, t->text + 2);
4157 break;
4158 case '-':
4159 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004160 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004161 tt = NULL;
4162 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004163 if (mac->nparam > 1)
4164 n = (n + mac->rotate) % mac->nparam;
4165 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004166 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004167 cc = find_cc(tt);
4168 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004169 nasm_nonfatal("macro parameter %d is not a condition code",
4170 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004171 text = NULL;
4172 } else {
4173 type = TOK_ID;
4174 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004175 nasm_nonfatal("condition code `%s' is not invertible",
4176 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004177 text = NULL;
4178 } else
4179 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4180 }
4181 break;
4182 case '+':
4183 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004184 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004185 tt = NULL;
4186 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004187 if (mac->nparam > 1)
4188 n = (n + mac->rotate) % mac->nparam;
4189 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004190 }
4191 cc = find_cc(tt);
4192 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004193 nasm_nonfatal("macro parameter %d is not a condition code",
4194 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004195 text = NULL;
4196 } else {
4197 type = TOK_ID;
4198 text = nasm_strdup(conditions[cc]);
4199 }
4200 break;
4201 default:
4202 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004203 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204 tt = NULL;
4205 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004206 if (mac->nparam > 1)
4207 n = (n + mac->rotate) % mac->nparam;
4208 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004209 }
4210 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004211 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004212 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004213 tail = &(*tail)->next;
4214 tt = tt->next;
4215 }
4216 }
4217 text = NULL; /* we've done it here */
4218 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004219 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004220 } else {
4221 /*
4222 * seems we have a parameters range here
4223 */
4224 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004225 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004226 if (head != t) {
4227 *tail = head;
4228 *last = tline;
4229 tline = head;
4230 text = NULL;
4231 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004232 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004233 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004234 if (!text) {
4235 delete_Token(t);
4236 } else {
4237 *tail = t;
4238 tail = &t->next;
4239 t->type = type;
4240 nasm_free(t->text);
4241 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004242 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004243 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004244 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004245 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004246 } else if (tline->type == TOK_INDIRECT) {
4247 t = tline;
4248 tline = tline->next;
4249 tt = tokenize(t->text);
4250 tt = expand_mmac_params(tt);
4251 tt = expand_smacro(tt);
4252 *tail = tt;
4253 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004254 tail = &tt->next;
4255 tt = tt->next;
4256 }
4257 delete_Token(t);
4258 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004259 } else {
4260 t = *tail = tline;
4261 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004262 tail = &t->next;
4263 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004264 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004265 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004266
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004267 if (changed) {
4268 const struct tokseq_match t[] = {
4269 {
4270 PP_CONCAT_MASK(TOK_ID) |
4271 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4272 PP_CONCAT_MASK(TOK_ID) |
4273 PP_CONCAT_MASK(TOK_NUMBER) |
4274 PP_CONCAT_MASK(TOK_FLOAT) |
4275 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4276 },
4277 {
4278 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4279 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4280 }
4281 };
4282 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4283 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004284
H. Peter Anvin76690a12002-04-30 20:52:49 +00004285 return thead;
4286}
4287
H. Peter Anvin322bee02019-08-10 01:38:06 -07004288static Token *expand_smacro_noreset(Token * tline);
4289static struct {
4290 int64_t tokens;
4291 int64_t levels;
4292 bool triggered;
4293} smacro_deadman;
4294
H. Peter Anvin76690a12002-04-30 20:52:49 +00004295/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004296 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004297 * a macro at all, it is simply copied to the output and the pointer
4298 * advanced. tpp should be a pointer to a pointer (usually the next
4299 * pointer of the previous token) to the first token. **tpp is updated
4300 * to point to the last token of the expansion, and *tpp updated to
4301 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004302 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004303 * If the expansion is empty, *tpp will be unchanged but **tpp will
4304 * be advanced past the macro call.
4305 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004306 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004307 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004308static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004309{
4310 Token **params = NULL;
4311 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004312 Token *tline = **tpp;
4313 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004314 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004315 unsigned int i;
4316 Token *t, *tup, *ttail;
4317 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004318
4319 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004320 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004321
4322 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004323
H. Peter Anvin322bee02019-08-10 01:38:06 -07004324 smacro_deadman.tokens--;
4325 smacro_deadman.levels--;
4326
4327 if (unlikely(smacro_deadman.tokens < 0 || smacro_deadman.levels < 0)) {
4328 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004329 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004330 smacro_deadman.triggered = true;
4331 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004332 goto not_a_macro;
4333 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004334 head = (SMacro *)hash_findix(&smacros, mname);
4335 } else if (tline->type == TOK_PREPROC_ID) {
4336 Context *ctx = get_ctx(mname, &mname);
4337 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4338 } else {
4339 goto not_a_macro;
4340 }
4341
4342 /*
4343 * We've hit an identifier of some sort. First check whether the
4344 * identifier is a single-line macro at all, then think about
4345 * checking for parameters if necessary.
4346 */
4347 list_for_each(m, head) {
4348 if (!mstrcmp(m->name, mname, m->casesense))
4349 break;
4350 }
4351
4352 if (!m) {
4353 goto not_a_macro;
4354 }
4355
4356 /* Parse parameters, if applicable */
4357
4358 params = NULL;
4359 nparam = 0;
4360
4361 if (m->nparam == 0) {
4362 /*
4363 * Simple case: the macro is parameterless.
4364 * Nothing to parse; the expansion code will
4365 * drop the macro name token.
4366 */
4367 } else {
4368 /*
4369 * Complicated case: at least one macro with this name
4370 * exists and takes parameters. We must find the
4371 * parameters in the call, count them, find the SMacro
4372 * that corresponds to that form of the macro call, and
4373 * substitute for the parameters when we expand. What a
4374 * pain.
4375 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004376 Token **phead, **pep;
4377 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004378 int white = 0;
4379 int brackets = 0;
4380 bool bracketed = false;
4381 bool bad_bracket = false;
4382 unsigned int sparam = PARAM_DELTA;
4383
4384 tline = tline->next;
4385
4386 while (tok_type_(tline, TOK_WHITESPACE)) {
4387 tline = tline->next;
4388 }
4389 if (!tok_is_(tline, "(")) {
4390 /*
4391 * This macro wasn't called with parameters: ignore
4392 * the call. (Behaviour borrowed from gnu cpp.)
4393 */
4394 goto not_a_macro;
4395 }
4396
4397 paren = 1;
4398 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004399 nasm_newn(params, sparam);
4400 phead = pep = &params[0];
4401 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004402
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004403 while (paren) {
4404 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004405 char ch;
4406
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004407 tline = tline->next;
4408
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004409 if (!tline) {
4410 nasm_nonfatal("macro call expects terminating `)'");
4411 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004412 }
4413
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004414 ch = 0;
4415 skip = false;
4416
4417 switch (tline->type) {
4418 case TOK_OTHER:
4419 if (!tline->text[1])
4420 ch = tline->text[0];
4421 break;
4422
4423 case TOK_WHITESPACE:
4424 if (brackets || *phead)
4425 white++; /* Keep interior whitespace */
4426 skip = true;
4427 break;
4428
4429 default:
4430 break;
4431 }
4432
4433 switch (ch) {
4434 case ',':
4435 if (!brackets) {
4436 if (++nparam >= sparam) {
4437 sparam += PARAM_DELTA;
4438 params = nasm_realloc(params, sparam * sizeof *params);
4439 }
4440 pep = &params[nparam];
4441 *pep = NULL;
4442 bracketed = false;
4443 skip = true;
4444 }
4445 break;
4446
4447 case '{':
4448 if (!bracketed) {
4449 bracketed = !*phead;
4450 skip = true;
4451 }
4452 brackets++;
4453 break;
4454
4455 case '}':
4456 if (brackets > 0) {
4457 if (!--brackets)
4458 skip = bracketed;
4459 }
4460 break;
4461
4462 case '(':
4463 if (!brackets)
4464 paren++;
4465 break;
4466
4467 case ')':
4468 if (!brackets) {
4469 paren--;
4470 if (!paren) {
4471 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004472 /* Count the final argument */
4473 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004474 }
4475 }
4476 break;
4477
4478 default:
4479 break; /* Normal token */
4480 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004481
4482 if (!skip) {
4483 Token *t;
4484
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004485 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004486
4487 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004488 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004489 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004490 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004491 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004492 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004493 pep = &t->next;
4494 white = 0;
4495 }
4496 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004497
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004498 /*
4499 * Look for a macro matching in both name and parameter count.
4500 * We already know any matches cannot be anywhere before the
4501 * current position of "m", so there is no reason to
4502 * backtrack.
4503 */
4504 while (1) {
4505 if (!m) {
4506 /*!
4507 *!macro-params-single [on] single-line macro calls with wrong parameter count
4508 *! warns about \i{single-line macros} being invoked
4509 *! with the wrong number of parameters.
4510 */
4511 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4512 "single-line macro `%s' exists, "
4513 "but not taking %d parameter%s",
4514 mname, nparam, (nparam == 1) ? "" : "s");
4515 goto not_a_macro;
4516 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004517
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004518 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4519 break; /* It's good */
4520
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004521 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004522 }
4523 }
4524
4525 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004526 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004527
4528 /* Expand each parameter */
4529 m->in_progress = true;
4530
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004531 if (m->eval_param) {
4532 for (i = 0; i < nparam; i++) {
4533 if (m->eval_param[i]) {
4534 /* Evaluate this parameter as a number */
4535 struct ppscan pps;
4536 struct tokenval tokval;
4537 expr *evalresult;
4538 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004539
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004540 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4541 pps.ntokens = -1;
4542 tokval.t_type = TOKEN_INVALID;
4543 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004544
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004545 free_tlist(eval_param);
4546 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004547
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004548 if (!evalresult) {
4549 /* Nothing meaningful to do */
4550 } else if (tokval.t_type) {
4551 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4552 } else if (!is_simple(evalresult)) {
4553 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4554 } else {
4555 params[i] = make_tok_num(reloc_value(evalresult));
4556 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004557 }
4558 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004559 }
4560
4561 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004562 tline = tline->next; /* Remove the macro call from the input */
4563 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004564
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004565 /* Note: we own the expansion this returns. */
4566 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004567
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004568 tup = NULL;
4569 ttail = NULL; /* Pointer to the last token of the expansion */
4570 while (t) {
4571 enum pp_token_type type = t->type;
4572 Token *tnext = t->next;
4573 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004574
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004575 switch (type) {
4576 case TOK_PREPROC_Q:
4577 case TOK_PREPROC_QQ:
4578 t->type = TOK_ID;
4579 nasm_free(t->text);
4580 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4581 t->len = nasm_last_string_len();
4582 t->next = tline;
4583 break;
4584
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004585 case TOK_ID:
4586 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004587 /*
4588 * Chain this into the target line *before* expanding,
4589 * that way we pick up any arguments to the new macro call,
4590 * if applicable.
4591 */
4592 t->next = tline;
4593 tp = &t;
4594 expand_one_smacro(&tp);
4595 if (t == tline)
4596 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004597 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004598
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004599 default:
4600 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004601 unsigned int param = smac_nparam(t->type);
4602 nasm_assert(!tup && param < nparam);
4603 delete_Token(t);
4604 t = NULL;
4605 tup = tnext;
4606 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004607 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004608 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004609 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004610 }
4611
4612 if (t) {
4613 tline = t;
4614 if (!ttail)
4615 ttail = t;
4616 }
4617
4618 if (tnext) {
4619 t = tnext;
4620 } else {
4621 t = tup;
4622 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004623 }
4624 }
4625
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004626 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004627 if (ttail)
4628 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004629
4630 m->in_progress = false;
4631
4632 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004633 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004634 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004635
4636 /*
4637 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004638 * and then advance to the next input token. Note that this is
4639 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004640 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004641not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004642 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004643 m = NULL;
4644done:
4645 smacro_deadman.levels++;
4646 if (unlikely(params))
4647 free_tlist_array(params, nparam);
4648 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004649}
4650
4651/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004652 * Expand all single-line macro calls made in the given line.
4653 * Return the expanded version of the line. The original is deemed
4654 * to be destroyed in the process. (In reality we'll just move
4655 * Tokens from input to output a lot of the time, rather than
4656 * actually bothering to destroy and replicate.)
4657 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004658static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004659{
H. Peter Anvin322bee02019-08-10 01:38:06 -07004660 smacro_deadman.tokens = nasm_limit[LIMIT_MACRO_TOKENS];
4661 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4662 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004663 return expand_smacro_noreset(tline);
4664}
4665
4666static Token *expand_smacro_noreset(Token * tline)
4667{
4668 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004669 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004670 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004671
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004672 /*
4673 * Trick: we should avoid changing the start token pointer since it can
4674 * be contained in "next" field of other token. Because of this
4675 * we allocate a copy of first token and work with it; at the end of
4676 * routine we copy it back
4677 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004678 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004679 tline = new_Token(org_tline->next, org_tline->type,
4680 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004681 nasm_free(org_tline->text);
4682 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004683 }
4684
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004685
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004686 /*
4687 * Pretend that we always end up doing expansion on the first pass;
4688 * that way %+ get processed. However, if we process %+ before the
4689 * first pass, we end up with things like MACRO %+ TAIL trying to
4690 * look up the macro "MACROTAIL", which we don't want.
4691 */
4692 expanded = true;
4693 thead = tline;
4694 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004695 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004696 {
4697 PP_CONCAT_MASK(TOK_ID) |
4698 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4699 PP_CONCAT_MASK(TOK_ID) |
4700 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4701 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4702 }
4703 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004704
4705 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004706 while ((t = *tail)) /* main token loop */
4707 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004708
4709 if (!expanded) {
4710 tline = thead;
4711 break; /* Done! */
4712 }
4713
4714 /*
4715 * Now scan the entire line and look for successive TOK_IDs
4716 * that resulted after expansion (they can't be produced by
4717 * tokenize()). The successive TOK_IDs should be concatenated.
4718 * Also we look for %+ tokens and concatenate the tokens
4719 * before and after them (without white spaces in between).
4720 */
4721 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004722
4723 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004724 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004725
H. Peter Anvine2c80182005-01-15 22:15:51 +00004726 if (org_tline) {
4727 if (thead) {
4728 *org_tline = *thead;
4729 /* since we just gave text to org_line, don't free it */
4730 thead->text = NULL;
4731 delete_Token(thead);
4732 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004733 /*
4734 * The expression expanded to empty line;
4735 * we can't return NULL because of the "trick" above.
4736 * Just set the line to a single WHITESPACE token.
4737 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004738 memset(org_tline, 0, sizeof(*org_tline));
4739 org_tline->text = NULL;
4740 org_tline->type = TOK_WHITESPACE;
4741 }
4742 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004743 }
4744
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004745 return thead;
4746}
4747
4748/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004749 * Similar to expand_smacro but used exclusively with macro identifiers
4750 * right before they are fetched in. The reason is that there can be
4751 * identifiers consisting of several subparts. We consider that if there
4752 * are more than one element forming the name, user wants a expansion,
4753 * otherwise it will be left as-is. Example:
4754 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004755 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004756 *
4757 * the identifier %$abc will be left as-is so that the handler for %define
4758 * will suck it and define the corresponding value. Other case:
4759 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004760 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004761 *
4762 * In this case user wants name to be expanded *before* %define starts
4763 * working, so we'll expand %$abc into something (if it has a value;
4764 * otherwise it will be left as-is) then concatenate all successive
4765 * PP_IDs into one.
4766 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004767static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004768{
4769 Token *cur, *oldnext = NULL;
4770
H. Peter Anvin734b1882002-04-30 21:01:08 +00004771 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004772 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004773
4774 cur = tline;
4775 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004776 (cur->next->type == TOK_ID ||
4777 cur->next->type == TOK_PREPROC_ID
4778 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004779 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004780
4781 /* If identifier consists of just one token, don't expand */
4782 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004783 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784
H. Peter Anvine2c80182005-01-15 22:15:51 +00004785 if (cur) {
4786 oldnext = cur->next; /* Detach the tail past identifier */
4787 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004788 }
4789
H. Peter Anvin734b1882002-04-30 21:01:08 +00004790 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004791
H. Peter Anvine2c80182005-01-15 22:15:51 +00004792 if (cur) {
4793 /* expand_smacro possibly changhed tline; re-scan for EOL */
4794 cur = tline;
4795 while (cur && cur->next)
4796 cur = cur->next;
4797 if (cur)
4798 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004799 }
4800
4801 return tline;
4802}
4803
4804/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004805 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004806 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004807 * to check for an initial label - that's taken care of in
4808 * expand_mmacro - but must check numbers of parameters. Guaranteed
4809 * to be called with tline->type == TOK_ID, so the putative macro
4810 * name is easy to find.
4811 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004812static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004813{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004814 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004815 Token **params;
4816 int nparam;
4817
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004818 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004819
4820 /*
4821 * Efficiency: first we see if any macro exists with the given
4822 * name. If not, we can return NULL immediately. _Then_ we
4823 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004824 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004825 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004826 list_for_each(m, head)
4827 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004828 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004829 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004830 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004831
4832 /*
4833 * OK, we have a potential macro. Count and demarcate the
4834 * parameters.
4835 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004836 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004837
4838 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004839 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004840 * structure that handles this number.
4841 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004842 while (m) {
4843 if (m->nparam_min <= nparam
4844 && (m->plus || nparam <= m->nparam_max)) {
4845 /*
4846 * This one is right. Just check if cycle removal
4847 * prohibits us using it before we actually celebrate...
4848 */
4849 if (m->in_progress > m->max_depth) {
4850 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004851 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004852 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004853 }
4854 nasm_free(params);
4855 return NULL;
4856 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004857 /*
4858 * It's right, and we can use it. Add its default
4859 * parameters to the end of our list if necessary.
4860 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004861 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004862 params =
4863 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004864 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004865 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004866 while (nparam < m->nparam_min + m->ndefs) {
4867 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004868 nparam++;
4869 }
4870 }
4871 /*
4872 * If we've gone over the maximum parameter count (and
4873 * we're in Plus mode), ignore parameters beyond
4874 * nparam_max.
4875 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004876 if (m->plus && nparam > m->nparam_max)
4877 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004878 /*
4879 * Then terminate the parameter list, and leave.
4880 */
4881 if (!params) { /* need this special case */
4882 params = nasm_malloc(sizeof(*params));
4883 nparam = 0;
4884 }
4885 params[nparam] = NULL;
4886 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004887 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004888 }
4889 /*
4890 * This one wasn't right: look for the next one with the
4891 * same name.
4892 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004893 list_for_each(m, m->next)
4894 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004895 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004896 }
4897
4898 /*
4899 * After all that, we didn't find one with the right number of
4900 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004901 *!
4902 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4903 *! warns about \i{multi-line macros} being invoked
4904 *! with the wrong number of parameters. See \k{mlmacover} for an
4905 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004906 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004907 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4908 "multi-line macro `%s' exists, but not taking %d parameter%s",
4909 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004910 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004911 return NULL;
4912}
4913
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004914
4915/*
4916 * Save MMacro invocation specific fields in
4917 * preparation for a recursive macro expansion
4918 */
4919static void push_mmacro(MMacro *m)
4920{
4921 MMacroInvocation *i;
4922
4923 i = nasm_malloc(sizeof(MMacroInvocation));
4924 i->prev = m->prev;
4925 i->params = m->params;
4926 i->iline = m->iline;
4927 i->nparam = m->nparam;
4928 i->rotate = m->rotate;
4929 i->paramlen = m->paramlen;
4930 i->unique = m->unique;
4931 i->condcnt = m->condcnt;
4932 m->prev = i;
4933}
4934
4935
4936/*
4937 * Restore MMacro invocation specific fields that were
4938 * saved during a previous recursive macro expansion
4939 */
4940static void pop_mmacro(MMacro *m)
4941{
4942 MMacroInvocation *i;
4943
4944 if (m->prev) {
4945 i = m->prev;
4946 m->prev = i->prev;
4947 m->params = i->params;
4948 m->iline = i->iline;
4949 m->nparam = i->nparam;
4950 m->rotate = i->rotate;
4951 m->paramlen = i->paramlen;
4952 m->unique = i->unique;
4953 m->condcnt = i->condcnt;
4954 nasm_free(i);
4955 }
4956}
4957
4958
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004959/*
4960 * Expand the multi-line macro call made by the given line, if
4961 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004962 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004963 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004964static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004965{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004966 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004967 Token *label = NULL;
4968 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004969 Token **params, *t, *tt;
4970 MMacro *m;
4971 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004972 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004973 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004974
4975 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004976 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004977 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004978 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004979 return 0;
4980 m = is_mmacro(t, &params);
4981 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004982 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004983 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004984 Token *last;
4985 /*
4986 * We have an id which isn't a macro call. We'll assume
4987 * it might be a label; we'll also check to see if a
4988 * colon follows it. Then, if there's another id after
4989 * that lot, we'll check it again for macro-hood.
4990 */
4991 label = last = t;
4992 t = t->next;
4993 if (tok_type_(t, TOK_WHITESPACE))
4994 last = t, t = t->next;
4995 if (tok_is_(t, ":")) {
4996 dont_prepend = 1;
4997 last = t, t = t->next;
4998 if (tok_type_(t, TOK_WHITESPACE))
4999 last = t, t = t->next;
5000 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005001 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
5002 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005003 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005004 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005005 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005006 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005007
5008 /*
5009 * Fix up the parameters: this involves stripping leading and
5010 * trailing whitespace, then stripping braces if they are
5011 * present.
5012 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005013 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005014 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005015
H. Peter Anvine2c80182005-01-15 22:15:51 +00005016 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005017 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005018 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005019
H. Peter Anvine2c80182005-01-15 22:15:51 +00005020 t = params[i];
5021 skip_white_(t);
5022 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005023 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005024 params[i] = t;
5025 paramlen[i] = 0;
5026 while (t) {
5027 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5028 break; /* ... because we have hit a comma */
5029 if (comma && t->type == TOK_WHITESPACE
5030 && tok_is_(t->next, ","))
5031 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005032 if (brace && t->type == TOK_OTHER) {
5033 if (t->text[0] == '{')
5034 brace++; /* ... or a nested opening brace */
5035 else if (t->text[0] == '}')
5036 if (!--brace)
5037 break; /* ... or a brace */
5038 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005039 t = t->next;
5040 paramlen[i]++;
5041 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005042 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005043 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005044 }
5045
5046 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005047 * OK, we have a MMacro structure together with a set of
5048 * parameters. We must now go through the expansion and push
5049 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005050 * parameter tokens and macro-local tokens doesn't get done
5051 * until the single-line macro substitution process; this is
5052 * because delaying them allows us to change the semantics
5053 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005054 *
5055 * First, push an end marker on to istk->expansion, mark this
5056 * macro as in progress, and set up its invocation-specific
5057 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005058 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005059 ll = nasm_malloc(sizeof(Line));
5060 ll->next = istk->expansion;
5061 ll->finishes = m;
5062 ll->first = NULL;
5063 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005064
5065 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005066 * Save the previous MMacro expansion in the case of
5067 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005068 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005069 if (m->max_depth && m->in_progress)
5070 push_mmacro(m);
5071
5072 m->in_progress ++;
5073 m->params = params;
5074 m->iline = tline;
5075 m->nparam = nparam;
5076 m->rotate = 0;
5077 m->paramlen = paramlen;
5078 m->unique = unique++;
5079 m->lineno = 0;
5080 m->condcnt = 0;
5081
5082 m->next_active = istk->mstk;
5083 istk->mstk = m;
5084
5085 list_for_each(l, m->expansion) {
5086 Token **tail;
5087
5088 ll = nasm_malloc(sizeof(Line));
5089 ll->finishes = NULL;
5090 ll->next = istk->expansion;
5091 istk->expansion = ll;
5092 tail = &ll->first;
5093
5094 list_for_each(t, l->first) {
5095 Token *x = t;
5096 switch (t->type) {
5097 case TOK_PREPROC_Q:
5098 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005099 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005100 case TOK_PREPROC_QQ:
5101 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
5102 break;
5103 case TOK_PREPROC_ID:
5104 if (t->text[1] == '0' && t->text[2] == '0') {
5105 dont_prepend = -1;
5106 x = label;
5107 if (!x)
5108 continue;
5109 }
5110 /* fall through */
5111 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005112 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005113 break;
5114 }
5115 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005116 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005117 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005118 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005119
5120 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005121 * If we had a label, push it on as the first line of
5122 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005123 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005124 if (label) {
5125 if (dont_prepend < 0)
5126 free_tlist(startline);
5127 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005128 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005129 ll->finishes = NULL;
5130 ll->next = istk->expansion;
5131 istk->expansion = ll;
5132 ll->first = startline;
5133 if (!dont_prepend) {
5134 while (label->next)
5135 label = label->next;
5136 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005137 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005138 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005139 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005140
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005141 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005142
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005143 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005144}
5145
H. Peter Anvin130736c2016-02-17 20:27:41 -08005146/*
5147 * This function adds macro names to error messages, and suppresses
5148 * them if necessary.
5149 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005150static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005151{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005152 /*
5153 * If we're in a dead branch of IF or something like it, ignore the error.
5154 * However, because %else etc are evaluated in the state context
5155 * of the previous branch, errors might get lost:
5156 * %if 0 ... %else trailing garbage ... %endif
5157 * So %else etc should set the ERR_PP_PRECOND flag.
5158 */
5159 if ((severity & ERR_MASK) < ERR_FATAL &&
5160 istk && istk->conds &&
5161 ((severity & ERR_PP_PRECOND) ?
5162 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005163 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005164 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005165
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005166 /* This doesn't make sense with the macro stack unwinding */
5167 if (0) {
5168 MMacro *mmac = NULL;
5169 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005170
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005171 /* get %macro name */
5172 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
5173 mmac = istk->mstk;
5174 /* but %rep blocks should be skipped */
5175 while (mmac && !mmac->name)
5176 mmac = mmac->next_active, delta++;
5177 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005178
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005179 if (mmac) {
5180 char *buf;
5181 nasm_set_verror(real_verror);
5182 buf = nasm_vasprintf(fmt, arg);
5183 nasm_error(severity, "(%s:%"PRId32") %s",
5184 mmac->name, mmac->lineno - delta, buf);
5185 nasm_set_verror(pp_verror);
5186 nasm_free(buf);
5187 return;
5188 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005189 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005190 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005191}
5192
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005193static Token *
5194stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005195{
5196 (void)s;
5197 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005198 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005199
5200 return make_tok_qstr(src_get_fname());
5201}
5202
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005203static Token *
5204stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005205{
5206 (void)s;
5207 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005208 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005209
5210 return make_tok_num(src_get_linnum());
5211}
5212
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005213static Token *
5214stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005215{
5216 (void)s;
5217 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005218 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005219
5220 return make_tok_num(globalbits);
5221}
5222
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005223static Token *
5224stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005225{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005226 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005227 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5228 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5229 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005230
5231 (void)s;
5232 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005233 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005234
5235 switch (globalbits) {
5236 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005237 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005238 break;
5239 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005240 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005241 break;
5242 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005243 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005244 break;
5245 default:
5246 panic();
5247 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005248
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005249 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005250}
5251
H. Peter Anvin8b262472019-02-26 14:00:54 -08005252/* Add magic standard macros */
5253struct magic_macros {
5254 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005255 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005256 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005257};
5258static const struct magic_macros magic_macros[] =
5259{
5260 { "__FILE__", 0, stdmac_file },
5261 { "__LINE__", 0, stdmac_line },
5262 { "__BITS__", 0, stdmac_bits },
5263 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005264 { NULL, 0, NULL }
5265};
5266
5267static void pp_add_magic_stdmac(void)
5268{
5269 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005270 SMacro tmpl;
5271
5272 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005273
5274 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005275 tmpl.nparam = m->nparam;
5276 tmpl.expand = m->func;
5277 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005278 }
5279}
5280
H. Peter Anvin734b1882002-04-30 21:01:08 +00005281static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005282pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005283{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005284 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005285 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005286
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005287 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005288 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005289 nested_mac_count = 0;
5290 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005291 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005292 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005293 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005294 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005295
H. Peter Anvin6686de22019-08-10 05:33:14 -07005296 /* First set up the top level input file */
5297 nasm_new(istk);
5298 istk->fp = nasm_open_read(file, NF_TEXT);
5299 src_set(0, file);
5300 istk->lineinc = 1;
5301 if (!istk->fp)
5302 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5303
5304 strlist_add(deplist, file);
5305
5306 /*
5307 * Set up the stdmac packages as a virtual include file,
5308 * indicated by a null file pointer.
5309 */
5310 nasm_new(inc);
5311 inc->next = istk;
5312 inc->fname = src_set_fname(NULL);
5313 inc->nolist = !list_option('b');
5314 istk = inc;
5315 lfmt->uplevel(LIST_INCLUDE, 0);
5316
H. Peter Anvin8b262472019-02-26 14:00:54 -08005317 pp_add_magic_stdmac();
5318
H. Peter Anvinf7606612016-07-13 14:23:48 -07005319 if (tasm_compatible_mode)
5320 pp_add_stdmac(nasm_stdmac_tasm);
5321
5322 pp_add_stdmac(nasm_stdmac_nasm);
5323 pp_add_stdmac(nasm_stdmac_version);
5324
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005325 if (extrastdmac)
5326 pp_add_stdmac(extrastdmac);
5327
H. Peter Anvinf7606612016-07-13 14:23:48 -07005328 stdmacpos = stdmacros[0];
5329 stdmacnext = &stdmacros[1];
5330
H. Peter Anvind2456592008-06-19 15:04:18 -07005331 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005332
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005333 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005334 * Define the __PASS__ macro. This is defined here unlike all the
5335 * other builtins, because it is special -- it varies between
5336 * passes -- but there is really no particular reason to make it
5337 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005338 *
5339 * 0 = dependencies only
5340 * 1 = preparatory passes
5341 * 2 = final pass
5342 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005343 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005344 switch (mode) {
5345 case PP_NORMAL:
5346 apass = pass_final() ? 2 : 1;
5347 break;
5348 case PP_DEPS:
5349 apass = 0;
5350 break;
5351 case PP_PREPROC:
5352 apass = 3;
5353 break;
5354 default:
5355 panic();
5356 }
5357
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005358 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005359}
5360
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005361static void pp_init(void)
5362{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005363}
5364
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005365/*
5366 * Get a line of tokens. If we popped the macro expansion/include stack,
5367 * we return a pointer to the dummy token tok_pop; at that point if
5368 * istk is NULL then we have reached end of input;
5369 */
5370static Token tok_pop; /* Dummy token placeholder */
5371
5372static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005373{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005374 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005375
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005376 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005377 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005378 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005379 * buffer or from the input file.
5380 */
5381 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005382 while (istk->expansion && istk->expansion->finishes) {
5383 Line *l = istk->expansion;
5384 if (!l->finishes->name && l->finishes->in_progress > 1) {
5385 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005386
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005387 /*
5388 * This is a macro-end marker for a macro with no
5389 * name, which means it's not really a macro at all
5390 * but a %rep block, and the `in_progress' field is
5391 * more than 1, meaning that we still need to
5392 * repeat. (1 means the natural last repetition; 0
5393 * means termination by %exitrep.) We have
5394 * therefore expanded up to the %endrep, and must
5395 * push the whole block on to the expansion buffer
5396 * again. We don't bother to remove the macro-end
5397 * marker: we'd only have to generate another one
5398 * if we did.
5399 */
5400 l->finishes->in_progress--;
5401 list_for_each(l, l->finishes->expansion) {
5402 Token *t, *tt, **tail;
5403
5404 ll = nasm_malloc(sizeof(Line));
5405 ll->next = istk->expansion;
5406 ll->finishes = NULL;
5407 ll->first = NULL;
5408 tail = &ll->first;
5409
5410 list_for_each(t, l->first) {
5411 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005412 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005413 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005414 }
5415 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005416 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005417 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005418 } else {
5419 /*
5420 * Check whether a `%rep' was started and not ended
5421 * within this macro expansion. This can happen and
5422 * should be detected. It's a fatal error because
5423 * I'm too confused to work out how to recover
5424 * sensibly from it.
5425 */
5426 if (defining) {
5427 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005428 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005429 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005430 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005431 " expansion of macro `%s'",
5432 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005433 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005434
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005435 /*
5436 * FIXME: investigate the relationship at this point between
5437 * istk->mstk and l->finishes
5438 */
5439 {
5440 MMacro *m = istk->mstk;
5441 istk->mstk = m->next_active;
5442 if (m->name) {
5443 /*
5444 * This was a real macro call, not a %rep, and
5445 * therefore the parameter information needs to
5446 * be freed.
5447 */
5448 if (m->prev) {
5449 pop_mmacro(m);
5450 l->finishes->in_progress --;
5451 } else {
5452 nasm_free(m->params);
5453 free_tlist(m->iline);
5454 nasm_free(m->paramlen);
5455 l->finishes->in_progress = 0;
5456 }
Adam Majer91e72402017-07-25 10:42:01 +02005457 }
5458
5459 /*
5460 * FIXME It is incorrect to always free_mmacro here.
5461 * It leads to usage-after-free.
5462 *
5463 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5464 */
5465#if 0
5466 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005467 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005468#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005469 }
5470 istk->expansion = l->next;
5471 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005472 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005473 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005474 }
5475 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005476 do { /* until we get a line we can use */
5477 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005478
5479 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005480 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005481 int32_t lineno;
5482
H. Peter Anvin6686de22019-08-10 05:33:14 -07005483 if (istk->mstk) {
5484 istk->mstk->lineno++;
5485 if (istk->mstk->fname)
5486 lineno = istk->mstk->lineno + istk->mstk->xline;
5487 else
5488 lineno = 0; /* Defined at init time or builtin */
5489 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005490 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005491 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005492
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005493 tline = l->first;
5494 istk->expansion = l->next;
5495 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005496
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005497 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005498 if (!istk->nolist)
5499 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005500 nasm_free(line);
5501 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005502 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005503 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005504 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005505 } else {
5506 /*
5507 * The current file has ended; work down the istk
5508 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005509 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005510 if (i->fp)
5511 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005512 if (i->conds) {
5513 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005514 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005515 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005516 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005517 if (i->next)
5518 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005519 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005520 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005521 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005522 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005523 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005524 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005525
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005526 /*
5527 * We must expand MMacro parameters and MMacro-local labels
5528 * _before_ we plunge into directive processing, to cope
5529 * with things like `%define something %1' such as STRUC
5530 * uses. Unless we're _defining_ a MMacro, in which case
5531 * those tokens should be left alone to go into the
5532 * definition; and unless we're in a non-emitting
5533 * condition, in which case we don't want to meddle with
5534 * anything.
5535 */
5536 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5537 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005538 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005539 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005540
H. Peter Anvine2c80182005-01-15 22:15:51 +00005541 /*
5542 * Check the line to see if it's a preprocessor directive.
5543 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005544 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5545 if (dtline)
5546 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005547 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005548 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005549 * We're defining a multi-line macro. We emit nothing
5550 * at all, and just
5551 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005552 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005553 Line *l = nasm_malloc(sizeof(Line));
5554 l->next = defining->expansion;
5555 l->first = tline;
5556 l->finishes = NULL;
5557 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005558 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005559 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005560 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005561 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005562 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005563 * directive so we keep our place correctly.
5564 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005565 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005566 } else if (istk->mstk && !istk->mstk->in_progress) {
5567 /*
5568 * We're in a %rep block which has been terminated, so
5569 * we're walking through to the %endrep without
5570 * emitting anything. Emit nothing at all, not even a
5571 * blank line: when we emerge from the %rep block we'll
5572 * give a line-number directive so we keep our place
5573 * correctly.
5574 */
5575 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005576 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005577 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005578 if (!expand_mmacro(tline))
5579 return tline;
5580 }
5581 }
5582}
5583
5584static char *pp_getline(void)
5585{
5586 char *line = NULL;
5587 Token *tline;
5588
5589 real_verror = nasm_set_verror(pp_verror);
5590
5591 while (true) {
5592 tline = pp_tokline();
5593 if (tline == &tok_pop) {
5594 /*
5595 * We popped the macro/include stack. If istk is empty,
5596 * we are at end of input, otherwise just loop back.
5597 */
5598 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005599 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005600 } else {
5601 /*
5602 * De-tokenize the line and emit it.
5603 */
5604 line = detoken(tline, true);
5605 free_tlist(tline);
5606 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005607 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005608 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005609
H. Peter Anvin6686de22019-08-10 05:33:14 -07005610 if (list_option('e') && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005611 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005612 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005613 nasm_free(buf);
5614 }
5615
H. Peter Anvin130736c2016-02-17 20:27:41 -08005616 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005617 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005618}
5619
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005620static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005621{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005622 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005623
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005624 if (defining) {
5625 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005626 nasm_nonfatal("end of file while still defining macro `%s'",
5627 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005628 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005629 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005630 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005631
5632 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005633 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005634 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005635
5636 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005637
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005638 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005639 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005640 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005641 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005642 Include *i = istk;
5643 istk = istk->next;
5644 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005645 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005646 }
5647 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005648 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005649 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005650}
5651
5652static void pp_cleanup_session(void)
5653{
5654 free_llist(predef);
5655 predef = NULL;
5656 delete_Blocks();
5657 freeTokens = NULL;
5658 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005659}
5660
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005661static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005662{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005663 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005664}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005665
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005666static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005667{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005668 Token *inc, *space, *name;
5669 Line *l;
5670
H. Peter Anvin734b1882002-04-30 21:01:08 +00005671 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5672 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5673 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005674
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005675 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005676 l->next = predef;
5677 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005678 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005679 predef = l;
5680}
5681
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005682static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005683{
5684 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005685 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005686 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005687
H. Peter Anvin130736c2016-02-17 20:27:41 -08005688 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005689
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005690 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005691 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5692 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005693 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005694 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005695 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005696 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005697 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005698
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005699 if (space->next->type != TOK_PREPROC_ID &&
5700 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005701 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005702
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005703 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005704 l->next = predef;
5705 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005706 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005707 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005708
5709 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005710}
5711
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005712static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005713{
5714 Token *def, *space;
5715 Line *l;
5716
H. Peter Anvin734b1882002-04-30 21:01:08 +00005717 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5718 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005719 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005720
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005721 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005722 l->next = predef;
5723 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005724 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005725 predef = l;
5726}
5727
H. Peter Anvin05990342018-06-11 13:32:42 -07005728/* Insert an early preprocessor command that doesn't need special handling */
5729static void pp_pre_command(const char *what, char *string)
5730{
5731 char *cmd;
5732 Token *def, *space;
5733 Line *l;
5734
5735 def = tokenize(string);
5736 if (what) {
5737 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5738 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5739 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5740 }
5741
5742 l = nasm_malloc(sizeof(Line));
5743 l->next = predef;
5744 l->first = def;
5745 l->finishes = NULL;
5746 predef = l;
5747}
5748
H. Peter Anvinf7606612016-07-13 14:23:48 -07005749static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005750{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005751 macros_t **mp;
5752
5753 /* Find the end of the list and avoid duplicates */
5754 for (mp = stdmacros; *mp; mp++) {
5755 if (*mp == macros)
5756 return; /* Nothing to do */
5757 }
5758
5759 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5760
5761 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005762}
5763
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005764static void pp_extra_stdmac(macros_t *macros)
5765{
5766 extrastdmac = macros;
5767}
5768
H. Peter Anvin8b262472019-02-26 14:00:54 -08005769static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005770{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005771 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005772 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5773 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5774}
5775
5776static Token *make_tok_qstr(const char *str)
5777{
5778 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005779 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005780 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005781}
5782
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005783static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005784{
5785 if (!m)
5786 return;
5787
5788 /* We need to print the next_active list in reverse order */
5789 pp_list_one_macro(m->next_active, severity);
5790
5791 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005792 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005793 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005794 }
5795}
5796
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005797static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005798{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005799 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005800
H. Peter Anvinddb29062018-12-11 00:06:29 -08005801 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5802 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005803
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005804 if (istk)
5805 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005806
H. Peter Anvinddb29062018-12-11 00:06:29 -08005807 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005808}
5809
H. Peter Anvine7469712016-02-18 02:20:59 -08005810const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005811 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005812 pp_reset,
5813 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005814 pp_cleanup_pass,
5815 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005816 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005817 pp_pre_define,
5818 pp_pre_undefine,
5819 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005820 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005821 pp_include_path,
5822 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005823};