blob: 9c84e50538578affd097cb2962346daa2549d817 [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
801 while ((c = *p++)) {
802 if (c >= 0x80)
803 len += pp_directives_len[c - 0x80] + 1;
804 else
805 len++;
806 }
807
808 line = nasm_malloc(len + 1);
809 q = line;
810 while ((c = *stdmacpos++)) {
811 if (c >= 0x80) {
812 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
813 q += pp_directives_len[c - 0x80];
814 *q++ = ' ';
815 } else {
816 *q++ = c;
817 }
818 }
819 stdmacpos = p;
820 *q = '\0';
821
822 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700823 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400824 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700825 if (*stdmacnext) {
826 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400827 } else if (do_predef) {
828 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400829
830 /*
831 * Nasty hack: here we push the contents of
832 * `predef' on to the top-level expansion stack,
833 * since this is the most convenient way to
834 * implement the pre-include and pre-define
835 * features.
836 */
837 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700838 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800839 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700840 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800841 l->finishes = NULL;
842
843 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400844 }
845 do_predef = false;
846 }
847 }
848
849 return line;
850}
851
H. Peter Anvin6686de22019-08-10 05:33:14 -0700852/*
853 * Read a line from a file. Return NULL on end of file.
854 */
855static char *line_from_file(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000856{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700857 int c;
858 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400859 const unsigned int delta = 512;
860 const unsigned int pad = 8;
861 unsigned int nr_cont = 0;
862 bool cont = false;
863 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700864 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000865
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400866 size = delta;
867 p = buffer = nasm_malloc(size);
868
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700869 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400870 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400871
872 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700873 case EOF:
874 if (p == buffer) {
875 nasm_free(buffer);
876 return NULL;
877 }
878 c = 0;
879 break;
880
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400881 case '\r':
882 next = fgetc(istk->fp);
883 if (next != '\n')
884 ungetc(next, istk->fp);
885 if (cont) {
886 cont = false;
887 continue;
888 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700889 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400890 break;
891
892 case '\n':
893 if (cont) {
894 cont = false;
895 continue;
896 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700897 c = 0;
898 break;
899
900 case 032: /* ^Z = legacy MS-DOS end of file mark */
901 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400902 break;
903
904 case '\\':
905 next = fgetc(istk->fp);
906 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400907 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400908 cont = true;
909 nr_cont++;
910 continue;
911 }
912 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000913 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400914
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400915 if (p >= (buffer + size - pad)) {
916 buffer = nasm_realloc(buffer, size + delta);
917 p = buffer + size - pad;
918 size += delta;
919 }
920
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700921 *p++ = c;
922 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000923
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700924 lineno = src_get_linnum() + istk->lineinc +
925 (nr_cont * istk->lineinc);
926 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000927
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000928 return buffer;
929}
930
931/*
H. Peter Anvin6686de22019-08-10 05:33:14 -0700932 * Common read routine regardless of source
933 */
934static char *read_line(void)
935{
936 char *line;
937
938 if (istk->fp)
939 line = line_from_file();
940 else
941 line = line_from_stdmac();
942
943 if (!line)
944 return NULL;
945
946 if (!istk->nolist)
947 lfmt->line(LIST_READ, src_get_linnum(), line);
948
949 return line;
950}
951
952/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000953 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000954 * don't need to parse the value out of e.g. numeric tokens: we
955 * simply split one string into many.
956 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000957static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000958{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700959 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000960 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000961 Token *list = NULL;
962 Token *t, **tail = &list;
963
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700965 char *p = line;
966 char *ep = NULL; /* End of token, for trimming the end */
967
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 if (*p == '%') {
969 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300970 if (*p == '+' && !nasm_isdigit(p[1])) {
971 p++;
972 type = TOK_PASTE;
973 } else if (nasm_isdigit(*p) ||
974 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 do {
976 p++;
977 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700978 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 type = TOK_PREPROC_ID;
980 } else if (*p == '{') {
981 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800982 while (*p) {
983 if (*p == '}')
984 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 p[-1] = *p;
986 p++;
987 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800988 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800989 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700990 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 if (*p)
992 p++;
993 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300994 } else if (*p == '[') {
995 int lvl = 1;
996 line += 2; /* Skip the leading %[ */
997 p++;
998 while (lvl && (c = *p++)) {
999 switch (c) {
1000 case ']':
1001 lvl--;
1002 break;
1003 case '%':
1004 if (*p == '[')
1005 lvl++;
1006 break;
1007 case '\'':
1008 case '\"':
1009 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001010 p = nasm_skip_string(p - 1);
1011 if (*p)
1012 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001013 break;
1014 default:
1015 break;
1016 }
1017 }
1018 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001019 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001020 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001021 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001022 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001023 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001024 type = TOK_INDIRECT;
1025 } else if (*p == '?') {
1026 type = TOK_PREPROC_Q; /* %? */
1027 p++;
1028 if (*p == '?') {
1029 type = TOK_PREPROC_QQ; /* %?? */
1030 p++;
1031 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001032 } else if (*p == '!') {
1033 type = TOK_PREPROC_ID;
1034 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001035 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001036 do {
1037 p++;
1038 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001039 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001040 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001041 p = nasm_skip_string(p);
1042 if (*p)
1043 p++;
1044 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001045 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001046 } else {
1047 /* %! without string or identifier */
1048 type = TOK_OTHER; /* Legacy behavior... */
1049 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001050 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001052 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 do {
1054 p++;
1055 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001056 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 type = TOK_PREPROC_ID;
1058 } else {
1059 type = TOK_OTHER;
1060 if (*p == '%')
1061 p++;
1062 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001063 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 type = TOK_ID;
1065 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001066 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001068 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 /*
1070 * A string token.
1071 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001073 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001074
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 if (*p) {
1076 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001077 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001078 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 /* Handling unterminated strings by UNV */
1080 /* type = -1; */
1081 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001082 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001083 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001084 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001085 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001086 bool is_hex = false;
1087 bool is_float = false;
1088 bool has_e = false;
1089 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001092 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001094
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001095 if (*p == '$') {
1096 p++;
1097 is_hex = true;
1098 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001099
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001100 for (;;) {
1101 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001102
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001103 if (!is_hex && (c == 'e' || c == 'E')) {
1104 has_e = true;
1105 if (*p == '+' || *p == '-') {
1106 /*
1107 * e can only be followed by +/- if it is either a
1108 * prefixed hex number or a floating-point number
1109 */
1110 p++;
1111 is_float = true;
1112 }
1113 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1114 is_hex = true;
1115 } else if (c == 'P' || c == 'p') {
1116 is_float = true;
1117 if (*p == '+' || *p == '-')
1118 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001119 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001120 ; /* just advance */
1121 else if (c == '.') {
1122 /*
1123 * we need to deal with consequences of the legacy
1124 * parser, like "1.nolist" being two tokens
1125 * (TOK_NUMBER, TOK_ID) here; at least give it
1126 * a shot for now. In the future, we probably need
1127 * a flex-based scanner with proper pattern matching
1128 * to do it as well as it can be done. Nothing in
1129 * the world is going to help the person who wants
1130 * 0x123.p16 interpreted as two tokens, though.
1131 */
1132 r = p;
1133 while (*r == '_')
1134 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001135
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001136 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1137 (!is_hex && (*r == 'e' || *r == 'E')) ||
1138 (*r == 'p' || *r == 'P')) {
1139 p = r;
1140 is_float = true;
1141 } else
1142 break; /* Terminate the token */
1143 } else
1144 break;
1145 }
1146 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001147
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001148 if (p == line+1 && *line == '$') {
1149 type = TOK_OTHER; /* TOKEN_HERE */
1150 } else {
1151 if (has_e && !is_hex) {
1152 /* 1e13 is floating-point, but 1e13h is not */
1153 is_float = true;
1154 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001155
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001156 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1157 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001158 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001160 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 /*
1162 * Whitespace just before end-of-line is discarded by
1163 * pretending it's a comment; whitespace just before a
1164 * comment gets lumped into the comment.
1165 */
1166 if (!*p || *p == ';') {
1167 type = TOK_COMMENT;
1168 while (*p)
1169 p++;
1170 }
1171 } else if (*p == ';') {
1172 type = TOK_COMMENT;
1173 while (*p)
1174 p++;
1175 } else {
1176 /*
1177 * Anything else is an operator of some kind. We check
1178 * for all the double-character operators (>>, <<, //,
1179 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001180 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 */
1182 type = TOK_OTHER;
1183 if ((p[0] == '>' && p[1] == '>') ||
1184 (p[0] == '<' && p[1] == '<') ||
1185 (p[0] == '/' && p[1] == '/') ||
1186 (p[0] == '<' && p[1] == '=') ||
1187 (p[0] == '>' && p[1] == '=') ||
1188 (p[0] == '=' && p[1] == '=') ||
1189 (p[0] == '!' && p[1] == '=') ||
1190 (p[0] == '<' && p[1] == '>') ||
1191 (p[0] == '&' && p[1] == '&') ||
1192 (p[0] == '|' && p[1] == '|') ||
1193 (p[0] == '^' && p[1] == '^')) {
1194 p++;
1195 }
1196 p++;
1197 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001198
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 /* Handling unterminated string by UNV */
1200 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001201 {
1202 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1203 t->text[p-line] = *line;
1204 tail = &t->next;
1205 }
1206 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001208 if (!ep)
1209 ep = p;
1210 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 tail = &t->next;
1212 }
1213 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001214 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001215 return list;
1216}
1217
H. Peter Anvince616072002-04-30 21:02:23 +00001218/*
1219 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001220 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001221 * deleted only all at once by the delete_Blocks function.
1222 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001224{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001225 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001227 /* first, get to the end of the linked list */
1228 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001230 /* now allocate the requested chunk */
1231 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001233 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001234 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001235 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001236}
1237
1238/*
1239 * this function deletes all managed blocks of memory
1240 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001242{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001244
H. Peter Anvin70653092007-10-19 14:42:29 -07001245 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001246 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001247 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001248 * free it.
1249 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001251 if (b->chunk)
1252 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 a = b;
1254 b = b->next;
1255 if (a != &blocks)
1256 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001257 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001258 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001260
1261/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001262 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001263 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001264 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001265static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001266 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001267{
1268 Token *t;
1269 int i;
1270
H. Peter Anvin89cee572009-07-15 09:16:54 -04001271 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1273 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1274 freeTokens[i].next = &freeTokens[i + 1];
1275 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001276 }
1277 t = freeTokens;
1278 freeTokens = t->next;
1279 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001280 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001281 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001282 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 t->text = NULL;
1284 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001285 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001287 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001288 t->text = nasm_malloc(txtlen+1);
1289 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001291 }
1292 return t;
1293}
1294
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001295static Token *dup_Token(Token *next, const Token *src)
1296{
1297 return new_Token(next, src->type, src->text, src->len);
1298}
1299
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001301{
1302 Token *next = t->next;
1303 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001304 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001305 freeTokens = t;
1306 return next;
1307}
1308
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001309/*
1310 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001311 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1312 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001313 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001314static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001315{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001316 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001317 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001318 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001319
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001320 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001321 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001322 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001323 char *v;
1324 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001325
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001326 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001327 if (nasm_isquote(*v))
1328 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001329
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001330 if (v) {
1331 char *p = getenv(v);
1332 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001333 /*!
1334 *!environment [on] nonexistent environment variable
1335 *! warns if a nonexistent environment variable
1336 *! is accessed using the \c{%!} preprocessor
1337 *! construct (see \k{getenv}.) Such environment
1338 *! variables are treated as empty (with this
1339 *! warning issued) starting in NASM 2.15;
1340 *! earlier versions of NASM would treat this as
1341 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001342 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001343 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1344 p = "";
1345 }
1346 t->text = nasm_strdup(p);
1347 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001348 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001349 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001351
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 /* Expand local macros here and not during preprocessing */
1353 if (expand_locals &&
1354 t->type == TOK_PREPROC_ID && t->text &&
1355 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001356 const char *q;
1357 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001358 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001360 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1361 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001362 nasm_free(t->text);
1363 t->text = p;
1364 }
1365 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001366 if (t->text) {
1367 if (debug_level(2)) {
1368 unsigned long t_len = t->len;
1369 unsigned long s_len = strlen(t->text);
1370 if (t_len != s_len) {
1371 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1372 t->text, t->type, s_len, t_len);
1373 t->len = s_len;
1374 }
1375 }
1376 len += t->len;
1377 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001379 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001380 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001381
H. Peter Anvin734b1882002-04-30 21:01:08 +00001382 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001383
1384 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001385 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001386 memcpy(p, t->text, t->len);
1387 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001388 } else if (t->type == TOK_WHITESPACE) {
1389 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001391 }
1392 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001393
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001394 return line;
1395}
1396
1397/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001398 * A scanner, suitable for use by the expression evaluator, which
1399 * operates on a line of Tokens. Expects a pointer to a pointer to
1400 * the first token in the line to be passed in as its private_data
1401 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001402 *
1403 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001404 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001405struct ppscan {
1406 Token *tptr;
1407 int ntokens;
1408};
1409
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001411{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001412 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001413 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001414 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001415
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001417 if (pps->ntokens && (tline = pps->tptr)) {
1418 pps->ntokens--;
1419 pps->tptr = tline->next;
1420 } else {
1421 pps->tptr = NULL;
1422 pps->ntokens = 0;
1423 return tokval->t_type = TOKEN_EOS;
1424 }
1425 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001426
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001427 tokval->t_charptr = tline->text;
1428
H. Peter Anvin76690a12002-04-30 20:52:49 +00001429 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001431 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001433
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001435 p = tokval->t_charptr = tline->text;
1436 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 tokval->t_charptr++;
1438 return tokval->t_type = TOKEN_ID;
1439 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001440
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001441 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001442 if (r >= p+MAX_KEYWORD)
1443 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001444 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001445 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001446 *s = '\0';
1447 /* right, so we have an identifier sitting in temp storage. now,
1448 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001449 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001450 }
1451
H. Peter Anvine2c80182005-01-15 22:15:51 +00001452 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001453 bool rn_error;
1454 tokval->t_integer = readnum(tline->text, &rn_error);
1455 tokval->t_charptr = tline->text;
1456 if (rn_error)
1457 return tokval->t_type = TOKEN_ERRNUM;
1458 else
1459 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001460 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001461
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001462 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001463 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001464 }
1465
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001467 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001468
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001469 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001470 tokval->t_charptr = tline->text;
1471 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001472
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001473 if (ep[0] != bq || ep[1] != '\0')
1474 return tokval->t_type = TOKEN_ERRSTR;
1475 else
1476 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001477 }
1478
H. Peter Anvine2c80182005-01-15 22:15:51 +00001479 if (tline->type == TOK_OTHER) {
1480 if (!strcmp(tline->text, "<<"))
1481 return tokval->t_type = TOKEN_SHL;
1482 if (!strcmp(tline->text, ">>"))
1483 return tokval->t_type = TOKEN_SHR;
1484 if (!strcmp(tline->text, "//"))
1485 return tokval->t_type = TOKEN_SDIV;
1486 if (!strcmp(tline->text, "%%"))
1487 return tokval->t_type = TOKEN_SMOD;
1488 if (!strcmp(tline->text, "=="))
1489 return tokval->t_type = TOKEN_EQ;
1490 if (!strcmp(tline->text, "<>"))
1491 return tokval->t_type = TOKEN_NE;
1492 if (!strcmp(tline->text, "!="))
1493 return tokval->t_type = TOKEN_NE;
1494 if (!strcmp(tline->text, "<="))
1495 return tokval->t_type = TOKEN_LE;
1496 if (!strcmp(tline->text, ">="))
1497 return tokval->t_type = TOKEN_GE;
1498 if (!strcmp(tline->text, "&&"))
1499 return tokval->t_type = TOKEN_DBL_AND;
1500 if (!strcmp(tline->text, "^^"))
1501 return tokval->t_type = TOKEN_DBL_XOR;
1502 if (!strcmp(tline->text, "||"))
1503 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001504 }
1505
1506 /*
1507 * We have no other options: just return the first character of
1508 * the token text.
1509 */
1510 return tokval->t_type = tline->text[0];
1511}
1512
1513/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001514 * Compare a string to the name of an existing macro; this is a
1515 * simple wrapper which calls either strcmp or nasm_stricmp
1516 * depending on the value of the `casesense' parameter.
1517 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001518static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001519{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001520 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001521}
1522
1523/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001524 * Compare a string to the name of an existing macro; this is a
1525 * simple wrapper which calls either strcmp or nasm_stricmp
1526 * depending on the value of the `casesense' parameter.
1527 */
1528static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1529{
1530 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1531}
1532
1533/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001534 * Return the Context structure associated with a %$ token. Return
1535 * NULL, having _already_ reported an error condition, if the
1536 * context stack isn't deep enough for the supplied number of $
1537 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001538 *
1539 * If "namep" is non-NULL, set it to the pointer to the macro name
1540 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001541 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001542static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001543{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001544 Context *ctx;
1545 int i;
1546
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001547 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001548 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001549
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001550 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001552
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001554 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001556 }
1557
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001558 name += 2;
1559 ctx = cstk;
1560 i = 0;
1561 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001562 name++;
1563 i++;
1564 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001565 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001567 nasm_nonfatal("`%s': context stack is only"
1568 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001570 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001571
1572 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001573 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001574
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001575 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001576}
1577
1578/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001579 * Open an include file. This routine must always return a valid
1580 * file pointer if it returns - it's responsible for throwing an
1581 * ERR_FATAL and bombing out completely if not. It should also try
1582 * the include path one by one until it finds the file or reaches
1583 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001584 *
1585 * Note: for INC_PROBE the function returns NULL at all times;
1586 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001587 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001588enum incopen_mode {
1589 INC_NEEDED, /* File must exist */
1590 INC_OPTIONAL, /* Missing is OK */
1591 INC_PROBE /* Only an existence probe */
1592};
1593
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001594/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001595static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001596 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001597{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001598 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001599 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001600 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001601 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001602 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001603
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001605 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001606 if (omode == INC_PROBE) {
1607 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001608 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001609 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001610 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001611 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001612 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001613 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001614 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001616 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001617
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001618 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001619
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001620 if (!ip) {
1621 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001622 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001623 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001624
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001625 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001626 ip = ip->next;
1627 }
1628}
1629
1630/*
1631 * Open a file, or test for the presence of one (depending on omode),
1632 * considering the include path.
1633 */
1634static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001635 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001636 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001637 enum incopen_mode omode,
1638 enum file_flags fmode)
1639{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001640 struct hash_insert hi;
1641 void **hp;
1642 char *path;
1643 FILE *fp = NULL;
1644
1645 hp = hash_find(&FileHash, file, &hi);
1646 if (hp) {
1647 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001648 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001649 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001650 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001651 } else {
1652 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001653 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001654
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001655 /* Positive or negative result */
1656 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001657
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001658 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001659 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001660 */
1661 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001662 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001663 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001664
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001665 if (!path) {
1666 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001667 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001668 } else {
1669 if (!fp && omode != INC_PROBE)
1670 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001671 }
1672
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001673 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001674 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001675
1676 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001677}
1678
1679/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001680 * Opens an include or input file. Public version, for use by modules
1681 * that get a file:lineno pair and need to look at the file again
1682 * (e.g. the CodeView debug backend). Returns NULL on failure.
1683 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001684FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001685{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001686 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001687}
1688
1689/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001690 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001691 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001692 * return true if _any_ single-line macro of that name is defined.
1693 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001694 * `nparam' or no parameters is defined.
1695 *
1696 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001697 * defined, or nparam is -1, the address of the definition structure
1698 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001699 * is NULL, no action will be taken regarding its contents, and no
1700 * error will occur.
1701 *
1702 * Note that this is also called with nparam zero to resolve
1703 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001704 *
1705 * If you already know which context macro belongs to, you can pass
1706 * the context pointer as first parameter; if you won't but name begins
1707 * with %$ the context will be automatically computed. If all_contexts
1708 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001709 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001710static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001711smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001712 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001713{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001714 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001715 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001716
H. Peter Anvin97a23472007-09-16 17:57:25 -07001717 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001718 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001719 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001720 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001721 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001722 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001723 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001724 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001725 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001726 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001727 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001728 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001729
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 while (m) {
1731 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001732 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001733 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001734 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 *defn = m;
1736 else
1737 *defn = NULL;
1738 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001739 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 }
1741 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001742 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001743
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001744 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001745}
1746
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001747/* param should be a natural number [0; INT_MAX] */
1748static int read_param_count(const char *str)
1749{
1750 int result;
1751 bool err;
1752
1753 result = readnum(str, &err);
1754 if (result < 0 || result > INT_MAX) {
1755 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001756 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1757 str, 0, INT_MAX);
1758 } else if (err)
1759 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001760 return result;
1761}
1762
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001763/*
1764 * Count and mark off the parameters in a multi-line macro call.
1765 * This is called both from within the multi-line macro expansion
1766 * code, and also to mark off the default parameters when provided
1767 * in a %macro definition line.
1768 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001770{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001771 int paramsize, brace;
1772
1773 *nparam = paramsize = 0;
1774 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001776 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001777 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 paramsize += PARAM_DELTA;
1779 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1780 }
1781 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001782 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001784 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001786 if (brace) {
1787 while (brace && (t = t->next) != NULL) {
1788 if (tok_is_(t, "{"))
1789 brace++;
1790 else if (tok_is_(t, "}"))
1791 brace--;
1792 }
1793
1794 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 /*
1796 * Now we've found the closing brace, look further
1797 * for the comma.
1798 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001799 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 skip_white_(t);
1801 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001802 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 while (tok_isnt_(t, ","))
1804 t = t->next;
1805 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001807 } else {
1808 while (tok_isnt_(t, ","))
1809 t = t->next;
1810 }
1811 if (t) { /* got a comma/brace */
1812 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001814 }
1815}
1816
1817/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001818 * Determine whether one of the various `if' conditions is true or
1819 * not.
1820 *
1821 * We must free the tline we get passed.
1822 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001823static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001824{
H. Peter Anvin70055962007-10-11 00:05:31 -07001825 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001826 Token *t, *tt, *origline;
1827 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001828 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001829 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001830 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001831 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001832 const char *dname = pp_directives[ct];
1833 bool casesense = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001834
1835 origline = tline;
1836
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001837 switch (PP_COND(ct)) {
1838 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001839 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001840 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001842 if (!tline)
1843 break;
1844 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001845 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001846 dname);
1847 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001849 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001850 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 tline = tline->next;
1852 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001854
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001855 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001856 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001857 while (tline) {
1858 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 if (!tline || (tline->type != TOK_ID &&
1860 (tline->type != TOK_PREPROC_ID ||
1861 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001862 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001863 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001864 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001866 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001867 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001869 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001870 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001871
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001872 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001873 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001874 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001875 while (tline) {
1876 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001877 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001878 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001879 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001880 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001881 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001882 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001883 goto fail;
1884 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001885 p = tline->text;
1886 if (tline->type == TOK_PREPROC_ID)
1887 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001888 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001889 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001890 if (getenv(p))
1891 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001892 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001893 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001894 break;
1895
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001896 case PP_IFIDNI:
1897 casesense = false;
1898 /* fall through */
1899 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 tline = expand_smacro(tline);
1901 t = tt = tline;
1902 while (tok_isnt_(tt, ","))
1903 tt = tt->next;
1904 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001905 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001906 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 }
1909 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001910 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1912 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001913 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001914 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916 }
1917 if (t->type == TOK_WHITESPACE) {
1918 t = t->next;
1919 continue;
1920 }
1921 if (tt->type == TOK_WHITESPACE) {
1922 tt = tt->next;
1923 continue;
1924 }
1925 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001926 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 break;
1928 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001929 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 size_t l1 = nasm_unquote(t->text, NULL);
1932 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001933
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001934 if (l1 != l2) {
1935 j = false;
1936 break;
1937 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001938 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001939 j = false;
1940 break;
1941 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001942 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001943 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 break;
1945 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001946
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 t = t->next;
1948 tt = tt->next;
1949 }
1950 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001951 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001953
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001954 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001955 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001956 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001957 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001958
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959 skip_white_(tline);
1960 tline = expand_id(tline);
1961 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001962 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 goto fail;
1964 }
1965 searching.name = nasm_strdup(tline->text);
1966 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001967 searching.plus = false;
1968 searching.nolist = false;
1969 searching.in_progress = 0;
1970 searching.max_depth = 0;
1971 searching.rep_nest = NULL;
1972 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001973 searching.nparam_max = INT_MAX;
1974 tline = expand_smacro(tline->next);
1975 skip_white_(tline);
1976 if (!tline) {
1977 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001978 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001979 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 } else {
1981 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001982 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 }
1984 if (tline && tok_is_(tline->next, "-")) {
1985 tline = tline->next->next;
1986 if (tok_is_(tline, "*"))
1987 searching.nparam_max = INT_MAX;
1988 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001989 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001990 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001992 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001993 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001994 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001995 searching.nparam_max = searching.nparam_min;
1996 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001997 }
1998 }
1999 if (tline && tok_is_(tline->next, "+")) {
2000 tline = tline->next;
2001 searching.plus = true;
2002 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002003 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2004 while (mmac) {
2005 if (!strcmp(mmac->name, searching.name) &&
2006 (mmac->nparam_min <= searching.nparam_max
2007 || searching.plus)
2008 && (searching.nparam_min <= mmac->nparam_max
2009 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 found = true;
2011 break;
2012 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002013 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 }
2015 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002016 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002017 nasm_free(searching.name);
2018 j = found;
2019 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002020 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002021
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002022 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 needtype = TOK_ID;
2024 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002025 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 needtype = TOK_NUMBER;
2027 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002028 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 needtype = TOK_STRING;
2030 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002031
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002032iftype:
2033 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002034
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002035 while (tok_type_(t, TOK_WHITESPACE) ||
2036 (needtype == TOK_NUMBER &&
2037 tok_type_(t, TOK_OTHER) &&
2038 (t->text[0] == '-' || t->text[0] == '+') &&
2039 !t->text[1]))
2040 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002041
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 j = tok_type_(t, needtype);
2043 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002044
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002045 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 t = tline = expand_smacro(tline);
2047 while (tok_type_(t, TOK_WHITESPACE))
2048 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002049
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002050 j = false;
2051 if (t) {
2052 t = t->next; /* Skip the actual token */
2053 while (tok_type_(t, TOK_WHITESPACE))
2054 t = t->next;
2055 j = !t; /* Should be nothing left */
2056 }
2057 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002058
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002059 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002060 t = tline = expand_smacro(tline);
2061 while (tok_type_(t, TOK_WHITESPACE))
2062 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002063
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002064 j = !t; /* Should be empty */
2065 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002066
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002067 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002068 pps.tptr = tline = expand_smacro(tline);
2069 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002071 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 if (!evalresult)
2073 return -1;
2074 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002075 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002077 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002078 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002079 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002080 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002081 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002082 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002083
H. Peter Anvine2c80182005-01-15 22:15:51 +00002084 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002085 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002086 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002087 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002088
2089 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002090 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002091
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002092fail:
2093 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002094 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002095}
2096
2097/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002098 * Default smacro expansion routine: just returns a copy of the
2099 * expansion list.
2100 */
2101static Token *
2102smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2103{
2104 (void)params;
2105 (void)nparams;
2106
2107 return dup_tlist(s->expansion, NULL);
2108}
2109
2110/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002111 * Emit a macro defintion or undef to the listing file, if
2112 * desired. This is similar to detoken(), but it handles the reverse
2113 * expansion list, does not expand %! or local variable tokens, and
2114 * does some special handling for macro parameters.
2115 */
2116static void
2117list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2118{
2119 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2120 const Token **param_names;
2121 Token *junk_names = NULL;
2122 Token *t;
2123 size_t namelen, size;
2124 unsigned int i;
2125 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002126 char *context_prefix = NULL;
2127 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002128
2129 nasm_newn(param_names, m->nparam);
2130
2131 namelen = strlen(m->name);
2132 size = namelen + 2; /* Include room for space after name + NUL */
2133
2134 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002135 int context_depth = cstk->depth - ctx->depth + 1;
2136 context_prefix =
2137 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2138 ctx->name ? ctx->name : "",
2139 ctx->number, context_depth, "");
2140
2141 context_len = nasm_last_string_len();
2142 memset(context_prefix + context_len - context_depth,
2143 '$', context_depth);
2144 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002145 }
2146
2147 list_for_each(t, m->expansion) {
2148 if (!t->text) {
2149 size++; /* Whitespace, presumably */
2150 } else {
2151 size += t->len;
2152 if (is_smac_param(t->type))
2153 param_names[smac_nparam(t->type)] = t;
2154 }
2155 }
2156
2157 if (m->nparam) {
2158 /*
2159 * Space for ( and either , or ) around each
2160 * parameter, plus an optional =.
2161 */
2162 size += 1 + 2 * m->nparam;
2163 for (i = 0; i < m->nparam; i++) {
2164 if (!param_names[i])
2165 param_names[i] = &unused_arg_name;
2166 size += param_names[i]->len;
2167 }
2168 }
2169
2170 def = nasm_malloc(size);
2171 p = def+size;
2172 *--p = '\0';
2173
2174 list_for_each(t, m->expansion) {
2175 if (!t->text) {
2176 *--p = ' ';
2177 } else {
2178 p -= t->len;
2179 memcpy(p, t->text, t->len);
2180 }
2181 }
2182
2183 *--p = ' ';
2184
2185 if (m->nparam) {
2186 *--p = ')';
2187 for (i = m->nparam; i--;) {
2188 p -= param_names[i]->len;
2189 memcpy(p, param_names[i]->text, param_names[i]->len);
2190 if (m->eval_param && m->eval_param[i])
2191 *--p = '=';
2192 *--p = ',';
2193 }
2194 *p = '('; /* First parameter starts with ( not , */
2195
2196 free_tlist(junk_names);
2197 }
2198
2199 p -= namelen;
2200 memcpy(p, m->name, namelen);
2201
H. Peter Anvin6686de22019-08-10 05:33:14 -07002202 if (context_prefix) {
2203 p -= context_len;
2204 memcpy(p, context_prefix, context_len);
2205 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002206 }
2207
2208 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002209 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002210}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002211
2212/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002213 * Common code for defining an smacro. The tmpl argument, if not NULL,
2214 * contains any macro parameters that aren't explicit arguments;
2215 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002216 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002217static SMacro *define_smacro(const char *mname, bool casesense,
2218 Token *expansion, const SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002219{
2220 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002221 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002222 Context *ctx;
2223 bool defining_alias = false;
2224 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002225
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002226 if (tmpl) {
2227 defining_alias = tmpl->alias;
2228 nparam = tmpl->nparam;
2229 }
2230
2231 while (1) {
2232 ctx = get_ctx(mname, &mname);
2233
2234 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2235 /* Create a new macro */
2236 smtbl = ctx ? &ctx->localmac : &smacros;
2237 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2238 nasm_new(smac);
2239 smac->next = *smhead;
2240 *smhead = smac;
2241 break;
2242 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002243 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002244 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002245 /*
2246 * Some instances of the old code considered this a failure,
2247 * some others didn't. What is the right thing to do here?
2248 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002249 goto fail;
2250 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002251 /*
2252 * We're redefining, so we have to take over an
2253 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002254 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002255 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002256 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002257 break;
2258 } else if (smac->in_progress) {
2259 nasm_nonfatal("macro alias loop");
2260 goto fail;
2261 } else {
2262 /* It is an alias macro; follow the alias link */
2263 SMacro *s;
2264
2265 smac->in_progress = true;
2266 s = define_smacro(smac->expansion->text, casesense,
2267 expansion, tmpl);
2268 smac->in_progress = false;
2269 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002270 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002271 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002272
2273 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002274 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002275 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002276 smac->expand = smacro_expand_default;
2277 if (tmpl) {
2278 smac->nparam = tmpl->nparam;
2279 smac->eval_param = tmpl->eval_param;
2280 smac->alias = tmpl->alias;
2281 if (tmpl->expand)
2282 smac->expand = tmpl->expand;
2283 }
2284 if (list_option('m')) {
2285 static const enum preproc_token op[2][2] = {
2286 { PP_DEFINE, PP_IDEFINE },
2287 { PP_DEFALIAS, PP_IDEFALIAS }
2288 };
2289 list_smacro_def(op[!!smac->alias][casesense], ctx, smac);
2290 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002291 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002292
2293fail:
2294 free_tlist(expansion);
2295 if (tmpl && tmpl->eval_param)
2296 nasm_free(tmpl->eval_param);
2297 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002298}
2299
2300/*
2301 * Undefine an smacro
2302 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002303static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002304{
2305 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002306 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002307 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002308
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002309 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002310 smtbl = ctx ? &ctx->localmac : &smacros;
2311 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002312
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002313 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002314 /*
2315 * We now have a macro name... go hunt for it.
2316 */
2317 sp = smhead;
2318 while ((s = *sp) != NULL) {
2319 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002320 if (s->alias && !undefalias) {
2321 if (s->in_progress) {
2322 nasm_nonfatal("macro alias loop");
2323 } else {
2324 s->in_progress = true;
2325 undef_smacro(s->expansion->text, false);
2326 s->in_progress = false;
2327 }
2328 } else {
2329 if (list_option('m'))
2330 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2331 ctx, s);
2332 *sp = s->next;
2333 free_smacro(s);
2334 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002335 } else {
2336 sp = &s->next;
2337 }
2338 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002339 }
2340}
2341
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002342/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002343 * Parse a mmacro specification.
2344 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002345static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002346{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002347 tline = tline->next;
2348 skip_white_(tline);
2349 tline = expand_id(tline);
2350 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002351 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002352 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002353 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002354
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002355 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002356 def->name = nasm_strdup(tline->text);
2357 def->plus = false;
2358 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002359 def->in_progress = 0;
2360 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002361 def->nparam_min = 0;
2362 def->nparam_max = 0;
2363
H. Peter Anvina26433d2008-07-16 14:40:01 -07002364 tline = expand_smacro(tline->next);
2365 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002366 if (!tok_type_(tline, TOK_NUMBER))
2367 nasm_nonfatal("`%s' expects a parameter count", directive);
2368 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002369 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002370 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002371 tline = tline->next->next;
2372 if (tok_is_(tline, "*")) {
2373 def->nparam_max = INT_MAX;
2374 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002375 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002376 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002377 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002378 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002379 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002380 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002381 }
2382 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002383 }
2384 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002385 tline = tline->next;
2386 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002387 }
2388 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002389 !nasm_stricmp(tline->next->text, ".nolist")) {
2390 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002391 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002392 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002393
H. Peter Anvina26433d2008-07-16 14:40:01 -07002394 /*
2395 * Handle default parameters.
2396 */
2397 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002398 def->dlist = tline->next;
2399 tline->next = NULL;
2400 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002401 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002402 def->dlist = NULL;
2403 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002404 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002405 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002406
H. Peter Anvin89cee572009-07-15 09:16:54 -04002407 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002408 !def->plus) {
2409 /*
2410 *!macro-defaults [on] macros with more default than optional parameters
2411 *! warns when a macro has more default parameters than optional parameters.
2412 *! See \k{mlmacdef} for why might want to disable this warning.
2413 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002414 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002415 "too many default macro parameters in macro `%s'", def->name);
2416 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002417
H. Peter Anvina26433d2008-07-16 14:40:01 -07002418 return true;
2419}
2420
2421
2422/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002423 * Decode a size directive
2424 */
2425static int parse_size(const char *str) {
2426 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002427 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002428 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002429 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002430 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002431}
2432
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002433/*
2434 * Process a preprocessor %pragma directive. Currently there are none.
2435 * Gets passed the token list starting with the "preproc" token from
2436 * "%pragma preproc".
2437 */
2438static void do_pragma_preproc(Token *tline)
2439{
2440 /* Skip to the real stuff */
2441 tline = tline->next;
2442 skip_white_(tline);
2443 if (!tline)
2444 return;
2445
2446 (void)tline; /* Nothing else to do at present */
2447}
2448
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002449static bool is_macro_id(const Token *t)
2450{
2451 return t && (t->type == TOK_ID ||
2452 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2453}
2454
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002455static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002456{
2457 char *id;
2458 Token *t = *tp;
2459
2460 t = t->next; /* Skip directive */
2461 skip_white_(t);
2462 t = expand_id(t);
2463
2464 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002465 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002466 err ? err : "macro identifier");
2467 return NULL;
2468 }
2469
2470 id = t->text;
2471 skip_white_(t);
2472 *tp = t;
2473 return id;
2474}
2475
Ed Beroset3ab3f412002-06-11 03:31:49 +00002476/**
2477 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002478 * Find out if a line contains a preprocessor directive, and deal
2479 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002480 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002481 * If a directive _is_ found, it is the responsibility of this routine
2482 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002483 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002484 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002485 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002486 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002487 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002488 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002489static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002490{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002491 enum preproc_token i;
2492 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002493 bool err;
2494 int nparam;
2495 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002496 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002497 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002498 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002499 char *p, *pp;
2500 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002501 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002502 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002503 Include *inc;
2504 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002505 Cond *cond;
2506 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002507 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002508 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002509 struct tokenval tokval;
2510 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002511 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002512 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002513 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002514 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002515 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002516
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002517 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002518 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002519
H. Peter Anvineba20a72002-04-30 20:53:55 +00002520 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002521 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002522 (tline->text[0] && (tline->text[1] == '%' ||
2523 tline->text[1] == '$' ||
2524 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002526
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002527 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002528 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002529
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002530 casesense = true;
2531 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2532 casesense = false;
2533 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002534 }
2535
2536 /*
2537 * If we're in a non-emitting branch of a condition construct,
2538 * or walking to the end of an already terminated %rep block,
2539 * we should ignore all directives except for condition
2540 * directives.
2541 */
2542 if (((istk->conds && !emitting(istk->conds->state)) ||
2543 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2544 return NO_DIRECTIVE_FOUND;
2545 }
2546
2547 /*
2548 * If we're defining a macro or reading a %rep block, we should
2549 * ignore all directives except for %macro/%imacro (which nest),
2550 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2551 * If we're in a %rep block, another %rep nests, so should be let through.
2552 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002553 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002554 i != PP_ENDMACRO && i != PP_ENDM &&
2555 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2556 return NO_DIRECTIVE_FOUND;
2557 }
2558
2559 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002560 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002561 nested_mac_count++;
2562 return NO_DIRECTIVE_FOUND;
2563 } else if (nested_mac_count > 0) {
2564 if (i == PP_ENDMACRO) {
2565 nested_mac_count--;
2566 return NO_DIRECTIVE_FOUND;
2567 }
2568 }
2569 if (!defining->name) {
2570 if (i == PP_REP) {
2571 nested_rep_count++;
2572 return NO_DIRECTIVE_FOUND;
2573 } else if (nested_rep_count > 0) {
2574 if (i == PP_ENDREP) {
2575 nested_rep_count--;
2576 return NO_DIRECTIVE_FOUND;
2577 }
2578 }
2579 }
2580 }
2581
H. Peter Anvin4169a472007-09-12 01:29:43 +00002582 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002583 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002584 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002586
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002587 case PP_PRAGMA:
2588 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002589 * %pragma namespace options...
2590 *
2591 * The namespace "preproc" is reserved for the preprocessor;
2592 * all other namespaces generate a [pragma] assembly directive.
2593 *
2594 * Invalid %pragmas are ignored and may have different
2595 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002596 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002597 t = tline;
2598 tline = tline->next;
2599 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002600 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002601 while (tok_type_(tline, TOK_WHITESPACE)) {
2602 t = tline;
2603 tline = tline->next;
2604 delete_Token(t);
2605 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002606 if (tok_type_(tline, TOK_ID)) {
2607 if (!nasm_stricmp(tline->text, "preproc")) {
2608 /* Preprocessor pragma */
2609 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002610 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002611 } else {
2612 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002613
2614 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002615 for (t = tline; t->next; t = t->next)
2616 ;
2617 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002618
2619 /* Prepend "[pragma " */
2620 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2621 t = new_Token(t, TOK_ID, "pragma", 6);
2622 t = new_Token(t, TOK_OTHER, "[", 1);
2623 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002624 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002625 }
2626 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002627 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002628
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 case PP_STACKSIZE:
2630 /* Directive to tell NASM what the default stack size is. The
2631 * default is for a 16-bit stack, and this can be overriden with
2632 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 */
2634 tline = tline->next;
2635 if (tline && tline->type == TOK_WHITESPACE)
2636 tline = tline->next;
2637 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002638 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 }
2640 if (nasm_stricmp(tline->text, "flat") == 0) {
2641 /* All subsequent ARG directives are for a 32-bit stack */
2642 StackSize = 4;
2643 StackPointer = "ebp";
2644 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002645 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002646 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2647 /* All subsequent ARG directives are for a 64-bit stack */
2648 StackSize = 8;
2649 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002650 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002651 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 } else if (nasm_stricmp(tline->text, "large") == 0) {
2653 /* All subsequent ARG directives are for a 16-bit stack,
2654 * far function call.
2655 */
2656 StackSize = 2;
2657 StackPointer = "bp";
2658 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002659 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 } else if (nasm_stricmp(tline->text, "small") == 0) {
2661 /* All subsequent ARG directives are for a 16-bit stack,
2662 * far function call. We don't support near functions.
2663 */
2664 StackSize = 2;
2665 StackPointer = "bp";
2666 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002667 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002668 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002669 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002671 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002672
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 case PP_ARG:
2674 /* TASM like ARG directive to define arguments to functions, in
2675 * the following form:
2676 *
2677 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2678 */
2679 offset = ArgOffset;
2680 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002681 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002682 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002683
H. Peter Anvine2c80182005-01-15 22:15:51 +00002684 /* Find the argument name */
2685 tline = tline->next;
2686 if (tline && tline->type == TOK_WHITESPACE)
2687 tline = tline->next;
2688 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002689 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002690 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 }
2692 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002693
H. Peter Anvine2c80182005-01-15 22:15:51 +00002694 /* Find the argument size type */
2695 tline = tline->next;
2696 if (!tline || tline->type != TOK_OTHER
2697 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002698 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002699 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002700 }
2701 tline = tline->next;
2702 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002703 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002704 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002706
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002708 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 size = parse_size(tt->text);
2711 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002712 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002714 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 }
2716 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002717
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002718 /* Round up to even stack slots */
2719 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002720
H. Peter Anvine2c80182005-01-15 22:15:51 +00002721 /* Now define the macro for the argument */
2722 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2723 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002724 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002725 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002726
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 /* Move to the next argument in the list */
2728 tline = tline->next;
2729 if (tline && tline->type == TOK_WHITESPACE)
2730 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002731 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002733 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002734
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 case PP_LOCAL:
2736 /* TASM like LOCAL directive to define local variables for a
2737 * function, in the following form:
2738 *
2739 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2740 *
2741 * The '= LocalSize' at the end is ignored by NASM, but is
2742 * required by TASM to define the local parameter size (and used
2743 * by the TASM macro package).
2744 */
2745 offset = LocalOffset;
2746 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002747 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002749
H. Peter Anvine2c80182005-01-15 22:15:51 +00002750 /* Find the argument name */
2751 tline = tline->next;
2752 if (tline && tline->type == TOK_WHITESPACE)
2753 tline = tline->next;
2754 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002755 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002756 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 }
2758 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002759
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 /* Find the argument size type */
2761 tline = tline->next;
2762 if (!tline || tline->type != TOK_OTHER
2763 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002764 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002765 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 }
2767 tline = tline->next;
2768 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002769 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002770 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002772
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002774 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002776 size = parse_size(tt->text);
2777 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002778 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002780 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 }
2782 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002783
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002784 /* Round up to even stack slots */
2785 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002786
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002787 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002788
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002789 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002790 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2791 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002792 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002793
H. Peter Anvine2c80182005-01-15 22:15:51 +00002794 /* Now define the assign to setup the enter_c macro correctly */
2795 snprintf(directive, sizeof(directive),
2796 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002797 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002798
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 /* Move to the next argument in the list */
2800 tline = tline->next;
2801 if (tline && tline->type == TOK_WHITESPACE)
2802 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002803 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002804 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002805 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002806
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 case PP_CLEAR:
2808 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002809 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002810 free_macros();
2811 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002812 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002813
H. Peter Anvin418ca702008-05-30 10:42:30 -07002814 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002815 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002816 skip_white_(t);
2817 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002818 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002819 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002820 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002821 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002822 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002823 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002824 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002825 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002826 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002827 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002828 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002829
2830 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002831 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002832 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002833
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002834 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002835 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002836 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002837 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002838 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002839 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002840 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002841 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002842 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002843 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002844 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002845 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002846 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002847 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002848 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002849 (pp_mode == PP_DEPS)
2850 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002851 if (!inc->fp) {
2852 /* -MG given but file not found */
2853 nasm_free(inc);
2854 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002855 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002856 inc->lineno = src_set_linnum(0);
2857 inc->lineinc = 1;
2858 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002859 inc->mstk = NULL;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002860 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002861 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002862 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002863 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002864 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002865
H. Peter Anvind2456592008-06-19 15:04:18 -07002866 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002867 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002868 static macros_t *use_pkg;
2869 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002870
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002871 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002872 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002873 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002874 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002875 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002876 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002877 use_pkg = nasm_stdmac_find_package(tline->text);
2878 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002879 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002880 else
2881 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002882 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002883 /*
2884 * Not already included, go ahead and include it.
2885 * Treat it as an include file for the purpose of
2886 * producing a listing.
2887 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002888 stdmacpos = use_pkg;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002889 nasm_new(inc);
2890 inc->next = istk;
2891 inc->fname = src_set_fname(NULL);
2892 inc->lineno = src_set_linnum(0);
2893 inc->lineinc = 0;
2894 inc->expansion = NULL;
2895 inc->mstk = NULL;
2896 inc->nolist = !list_option('b') || istk->nolist;
2897 istk = inc;
2898 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002899 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002900 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002901 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002902 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002903 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002904 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002905 tline = tline->next;
2906 skip_white_(tline);
2907 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002908 if (tline) {
2909 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002910 nasm_nonfatal("`%s' expects a context identifier",
2911 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002912 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002913 }
2914 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002915 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002916 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002917 p = nasm_strdup(tline->text);
2918 } else {
2919 p = NULL; /* Anonymous */
2920 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002921
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002922 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002923 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002924 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002925 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002926 ctx->name = p;
2927 ctx->number = unique++;
2928 cstk = ctx;
2929 } else {
2930 /* %pop or %repl */
2931 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002932 nasm_nonfatal("`%s': context stack is empty",
2933 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002934 } else if (i == PP_POP) {
2935 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002936 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002937 "expected %s",
2938 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002939 else
2940 ctx_pop();
2941 } else {
2942 /* i == PP_REPL */
2943 nasm_free(cstk->name);
2944 cstk->name = p;
2945 p = NULL;
2946 }
2947 nasm_free(p);
2948 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002949 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002950 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002951 severity = ERR_FATAL;
2952 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002954 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002955 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002956 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002957 /*!
2958 *!user [on] %warning directives
2959 *! controls output of \c{%warning} directives (see \k{pperror}).
2960 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002961 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002962 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002963
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002964issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002965 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002966 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002967 tline->next = expand_smacro(tline->next);
2968 tline = tline->next;
2969 skip_white_(tline);
2970 t = tline ? tline->next : NULL;
2971 skip_white_(t);
2972 if (tok_type_(tline, TOK_STRING) && !t) {
2973 /* The line contains only a quoted string */
2974 p = tline->text;
2975 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002976 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002977 } else {
2978 /* Not a quoted string, or more than a quoted string */
2979 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002980 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002981 nasm_free(p);
2982 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002983 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002984 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002985
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002986 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002987 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002988 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002989 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002990 j = if_condition(tline->next, i);
2991 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002992 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002993 cond = nasm_malloc(sizeof(Cond));
2994 cond->next = istk->conds;
2995 cond->state = j;
2996 istk->conds = cond;
2997 if(istk->mstk)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002998 istk->mstk->condcnt++;
2999 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003000
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003001 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003002 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003003 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003004 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003005 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003006 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003007 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003008
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003009 case COND_DONE:
3010 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003011 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003012
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003013 case COND_ELSE_TRUE:
3014 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003015 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003016 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003017 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003018 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003019
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003020 case COND_IF_FALSE:
3021 /*
3022 * IMPORTANT: In the case of %if, we will already have
3023 * called expand_mmac_params(); however, if we're
3024 * processing an %elif we must have been in a
3025 * non-emitting mode, which would have inhibited
3026 * the normal invocation of expand_mmac_params().
3027 * Therefore, we have to do it explicitly here.
3028 */
3029 j = if_condition(expand_mmac_params(tline->next), i);
3030 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003031 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003032 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003033 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003034 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003035
H. Peter Anvine2c80182005-01-15 22:15:51 +00003036 case PP_ELSE:
3037 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003038 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003039 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003040 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003041 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003042 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003043 case COND_IF_TRUE:
3044 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003045 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003046 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003047
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003048 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003049 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003050
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003051 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003052 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003053 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003054
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003055 case COND_ELSE_TRUE:
3056 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003057 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003058 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003059 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003060 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003061 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003062 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003063
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 case PP_ENDIF:
3065 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003066 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003067 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003068 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003069 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003070 cond = istk->conds;
3071 istk->conds = cond->next;
3072 nasm_free(cond);
3073 if(istk->mstk)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003074 istk->mstk->condcnt--;
3075 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003076
H. Peter Anvin8b262472019-02-26 14:00:54 -08003077 case PP_RMACRO:
3078 case PP_MACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003079 if (defining)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003080 nasm_fatal("`%s': already defining a macro", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003081
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003082 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin322bee02019-08-10 01:38:06 -07003083 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003084 defining->casesense = casesense;
3085 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003086 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003087 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003088 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003089
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003090 src_get(&defining->xline, &defining->fname);
3091
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003092 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3093 while (mmac) {
3094 if (!strcmp(mmac->name, defining->name) &&
3095 (mmac->nparam_min <= defining->nparam_max
3096 || defining->plus)
3097 && (defining->nparam_min <= mmac->nparam_max
3098 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003099 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003100 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003101 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003102 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003103 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003104 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003105 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003106
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 case PP_ENDM:
3108 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003109 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003110 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003111 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003112 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003113 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3114 defining->next = *mmhead;
3115 *mmhead = defining;
3116 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003117 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003118
H. Peter Anvin89cee572009-07-15 09:16:54 -04003119 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003120 /*
3121 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003122 * macro-end marker for a macro with a name. Then we
3123 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003124 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003125 list_for_each(l, istk->expansion)
3126 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003127 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003128
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003129 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003130 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003131 * Remove all conditional entries relative to this
3132 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003133 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003134 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3135 cond = istk->conds;
3136 istk->conds = cond->next;
3137 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003138 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003139 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003140 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003141 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003142 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003143 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003144
H. Peter Anvina26433d2008-07-16 14:40:01 -07003145 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003146 casesense = false;
3147 /* fall through */
3148 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003149 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003150 MMacro **mmac_p;
3151 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003152
H. Peter Anvin8b262472019-02-26 14:00:54 -08003153 spec.casesense = casesense;
3154 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003155 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003156 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003157 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3158 while (mmac_p && *mmac_p) {
3159 mmac = *mmac_p;
3160 if (mmac->casesense == spec.casesense &&
3161 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3162 mmac->nparam_min == spec.nparam_min &&
3163 mmac->nparam_max == spec.nparam_max &&
3164 mmac->plus == spec.plus) {
3165 *mmac_p = mmac->next;
3166 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003167 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003168 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 }
3170 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003172 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003173 }
3174
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 case PP_ROTATE:
3176 if (tline->next && tline->next->type == TOK_WHITESPACE)
3177 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003178 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003180 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181 return DIRECTIVE_FOUND;
3182 }
3183 t = expand_smacro(tline->next);
3184 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003185 pps.tptr = tline = t;
3186 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003187 tokval.t_type = TOKEN_INVALID;
3188 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003189 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 free_tlist(tline);
3191 if (!evalresult)
3192 return DIRECTIVE_FOUND;
3193 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003194 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003196 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003197 return DIRECTIVE_FOUND;
3198 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003199 mmac = istk->mstk;
3200 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3201 mmac = mmac->next_active;
3202 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003203 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003204 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003205 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003206 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003207 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003208
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003209 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003210 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003211 rotate += mmac->nparam;
3212
3213 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003214 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003215 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003216
H. Peter Anvine2c80182005-01-15 22:15:51 +00003217 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003218 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 do {
3220 tline = tline->next;
3221 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003222
H. Peter Anvine2c80182005-01-15 22:15:51 +00003223 if (tok_type_(tline, TOK_ID) &&
3224 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003225 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 do {
3227 tline = tline->next;
3228 } while (tok_type_(tline, TOK_WHITESPACE));
3229 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003230
H. Peter Anvine2c80182005-01-15 22:15:51 +00003231 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003232 pps.tptr = expand_smacro(tline);
3233 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003234 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003235 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003236 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003237 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003238 if (!evalresult)
3239 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003240 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003241 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003242 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003243 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003244 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003246 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003247 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003248 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3249 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003250 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003251 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003252 /*!
3253 *!negative-rep [on] regative %rep count
3254 *! warns about negative counts given to the \c{%rep}
3255 *! preprocessor directive.
3256 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003257 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003258 "negative `%%rep' count: %"PRId64, count);
3259 count = 0;
3260 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003261 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003262 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003264 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003265 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003266 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003267 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003268 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003269 defining->nolist = nolist;
3270 defining->in_progress = count;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003271 defining->next_active = istk->mstk;
3272 defining->rep_nest = tmp_defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003273 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003274 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003275
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003277 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003278 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003279 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003280 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003281
H. Peter Anvine2c80182005-01-15 22:15:51 +00003282 /*
3283 * Now we have a "macro" defined - although it has no name
3284 * and we won't be entering it in the hash tables - we must
3285 * push a macro-end marker for it on to istk->expansion.
3286 * After that, it will take care of propagating itself (a
3287 * macro-end marker line for a macro which is really a %rep
3288 * block will cause the macro to be re-expanded, complete
3289 * with another macro-end marker to ensure the process
3290 * continues) until the whole expansion is forcibly removed
3291 * from istk->expansion by a %exitrep.
3292 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003293 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003294 l->next = istk->expansion;
3295 l->finishes = defining;
3296 l->first = NULL;
3297 istk->expansion = l;
3298
3299 istk->mstk = defining;
3300
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003301 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003302 tmp_defining = defining;
3303 defining = defining->rep_nest;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003304 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003305
H. Peter Anvine2c80182005-01-15 22:15:51 +00003306 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003307 /*
3308 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003309 * macro-end marker for a macro with no name. Then we set
3310 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003311 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003312 list_for_each(l, istk->expansion)
3313 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003314 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003315
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003316 if (l)
3317 l->finishes->in_progress = 1;
3318 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003319 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003320 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003321
H. Peter Anvin8b262472019-02-26 14:00:54 -08003322 case PP_DEFINE:
3323 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003324 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003325 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003326 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003327 bool *eval_params = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003328 SMacro tmpl;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003329
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003330 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003331 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003332
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003333 nasm_zero(tmpl);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003334 last = tline;
3335 param_start = tline = tline->next;
3336 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003337
H. Peter Anvine2c80182005-01-15 22:15:51 +00003338 if (tok_is_(tline, "(")) {
3339 /*
3340 * This macro has parameters.
3341 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003342
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 tline = tline->next;
3344 while (1) {
3345 skip_white_(tline);
3346 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003347 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003348 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003349 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003350 if (tok_is_(tline, "=")) {
3351 have_eval_params = true;
3352 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003353 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003354 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003355
3356 /*
3357 * "*" means an unnamed, ignored argument; it can never
3358 * match anything because "*" is not TOK_ID, and so
3359 * none of the expansion entries will be converted
3360 * to parameters.
3361 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003362 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003363 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3364 /*
3365 * Empty name; duplicate the termination
3366 * token and use the first copy as a placeholder.
3367 * It will never match anything, since the
3368 * associated text doesn't correspond to a TOK_ID.
3369 */
3370 tline = dup_Token(tline, tline);
3371 } else {
3372 nasm_nonfatal("`%s': parameter identifier expected",
3373 tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003374 goto done;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003375 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003377 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003378 tline = tline->next;
3379 skip_white_(tline);
3380 if (tok_is_(tline, ",")) {
3381 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003382 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003384 nasm_nonfatal("`)' expected to terminate macro template");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003385 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003386 }
3387 break;
3388 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 }
3390 last = tline;
3391 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003392
3393 if (have_eval_params) {
3394 /* Create evaluated parameters table */
3395 bool is_eval = false;
3396
3397 nasm_newn(eval_params, nparam);
3398 list_for_each(tt, param_start) {
3399 if (is_smac_param(tt->type))
3400 eval_params[smac_nparam(tt->type)] = is_eval;
3401 is_eval = tok_is_(tt, "=");
3402 }
3403 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003405
H. Peter Anvine2c80182005-01-15 22:15:51 +00003406 if (tok_type_(tline, TOK_WHITESPACE))
3407 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003409
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003410 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003411 macro_start = tline;
3412 if (!is_macro_id(macro_start)) {
3413 nasm_nonfatal("`%s' expects a macro identifier to alias",
3414 dname);
3415 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003416 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003417 tt = macro_start->next;
3418 macro_start->next = NULL;
3419 tline = tline->next;
3420 skip_white_(tline);
3421 if (tline && tline->type) {
3422 nasm_warn(WARN_OTHER,
3423 "trailing garbage after aliasing identifier ignored");
3424 }
3425 free_tlist(tt);
3426 tmpl.alias = true;
3427 } else {
3428 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003429 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003430 tline = expand_smacro(tline);
3431
3432 macro_start = NULL;
3433 t = tline;
3434 while (t) {
3435 if (t->type == TOK_ID) {
3436 list_for_each(tt, param_start)
3437 if (is_smac_param(tt->type) &&
3438 tt->len == t->len &&
3439 !memcmp(tt->text, t->text, tt->len))
3440 t->type = tt->type;
3441 }
3442 tt = t->next;
3443 t->next = macro_start;
3444 macro_start = t;
3445 t = tt;
3446 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003447 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003448
H. Peter Anvine2c80182005-01-15 22:15:51 +00003449 /*
3450 * Good. We now have a macro name, a parameter count, and a
3451 * token list (in reverse order) for an expansion. We ought
3452 * to be OK just to create an SMacro, store it, and let
3453 * free_tlist have the rest of the line (which we have
3454 * carefully re-terminated after chopping off the expansion
3455 * from the end).
3456 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003457 tmpl.nparam = nparam;
3458 tmpl.eval_param = eval_params;
3459 define_smacro(mname, casesense, macro_start, &tmpl);
3460 break;
3461 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003462
H. Peter Anvine2c80182005-01-15 22:15:51 +00003463 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003464 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003465 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003466 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003467 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003468 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003469
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003470 undef_smacro(mname, i == PP_UNDEFALIAS);
3471 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003472
H. Peter Anvin8b262472019-02-26 14:00:54 -08003473 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003474 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003475 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003476
H. Peter Anvin9e200162008-06-04 17:23:14 -07003477 last = tline;
3478 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003480
3481 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003483
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003485 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003486 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003487
3488 /*
3489 * We now have a macro name, an implicit parameter count of
3490 * zero, and a string token to use as an expansion. Create
3491 * and store an SMacro.
3492 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003493 define_smacro(mname, casesense, macro_start, NULL);
3494 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495
H. Peter Anvin8b262472019-02-26 14:00:54 -08003496 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003497 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003498 goto done;
3499
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003500 last = tline;
3501 tline = expand_smacro(tline->next);
3502 last->next = NULL;
3503
3504 t = tline;
3505 while (tok_type_(t, TOK_WHITESPACE))
3506 t = t->next;
3507 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003508 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003509 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003510 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003511 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003512 }
3513
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003514 /*
3515 * Convert the string to a token stream. Note that smacros
3516 * are stored with the token stream reversed, so we have to
3517 * reverse the output of tokenize().
3518 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003519 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003520 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003521
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003522 /*
3523 * We now have a macro name, an implicit parameter count of
3524 * zero, and a numeric token to use as an expansion. Create
3525 * and store an SMacro.
3526 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003527 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003528 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003529 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003530
H. Peter Anvin418ca702008-05-30 10:42:30 -07003531 case PP_PATHSEARCH:
3532 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003533 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003534
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003535 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003536 goto done;
3537
H. Peter Anvin418ca702008-05-30 10:42:30 -07003538 last = tline;
3539 tline = expand_smacro(tline->next);
3540 last->next = NULL;
3541
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003542 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003543 while (tok_type_(t, TOK_WHITESPACE))
3544 t = t->next;
3545
3546 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003547 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003548 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003550 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003551 }
3552 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003553 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003554 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003555 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003556 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003557
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003558 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003559 if (!found_path)
3560 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003561 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003562
3563 /*
3564 * We now have a macro name, an implicit parameter count of
3565 * zero, and a string token to use as an expansion. Create
3566 * and store an SMacro.
3567 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003568 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003569 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003570 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003571 }
3572
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003574 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003575 goto done;
3576
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 last = tline;
3578 tline = expand_smacro(tline->next);
3579 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003580
H. Peter Anvine2c80182005-01-15 22:15:51 +00003581 t = tline;
3582 while (tok_type_(t, TOK_WHITESPACE))
3583 t = t->next;
3584 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003585 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003586 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003587 free_tlist(tline);
3588 free_tlist(origline);
3589 return DIRECTIVE_FOUND;
3590 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003591
H. Peter Anvin8b262472019-02-26 14:00:54 -08003592 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003593
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 /*
3595 * We now have a macro name, an implicit parameter count of
3596 * zero, and a numeric token to use as an expansion. Create
3597 * and store an SMacro.
3598 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003599 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 free_tlist(tline);
3601 free_tlist(origline);
3602 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003603
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003604 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003605 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003606 goto done;
3607
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003608 last = tline;
3609 tline = expand_smacro(tline->next);
3610 last->next = NULL;
3611
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003612 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003613 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003614 switch (t->type) {
3615 case TOK_WHITESPACE:
3616 break;
3617 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003618 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003619 break;
3620 case TOK_OTHER:
3621 if (!strcmp(t->text, ",")) /* permit comma separators */
3622 break;
3623 /* else fall through */
3624 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003625 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003626 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003627 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003628 }
3629 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003630
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003631 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003632 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003633 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003634 memcpy(p, t->text, t->len);
3635 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003636 }
3637 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003638
3639 /*
3640 * We now have a macro name, an implicit parameter count of
3641 * zero, and a numeric token to use as an expansion. Create
3642 * and store an SMacro.
3643 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003644 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003646 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003647 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003648 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003649
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003651 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003652 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003653 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003654
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003655 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003656 goto done;
3657
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 last = tline;
3659 tline = expand_smacro(tline->next);
3660 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003661
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003662 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003663 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 while (tok_type_(t, TOK_WHITESPACE))
3665 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003666
H. Peter Anvine2c80182005-01-15 22:15:51 +00003667 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003668 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003669 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003670 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003671 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003672 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003673
H. Peter Anvin8b262472019-02-26 14:00:54 -08003674 pps.tptr = t->next;
3675 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003676 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003677 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 if (!evalresult) {
3679 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003680 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003681 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003682 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003684 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003686 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003687
H. Peter Anvin8b262472019-02-26 14:00:54 -08003688 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3689 pps.tptr = pps.tptr->next;
3690 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003691 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003692 } else {
3693 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003694 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003695 if (!evalresult) {
3696 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003697 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003698 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003699 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003700 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003701 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003702 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003703 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003704 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003705
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003706 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003707
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003708 /* make start and count being in range */
3709 if (start < 0)
3710 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003711 if (count < 0)
3712 count = len + count + 1 - start;
3713 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003714 count = len - start;
3715 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003716 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003717
H. Peter Anvin8b262472019-02-26 14:00:54 -08003718 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003719 macro_start->len = count;
3720 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3721 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003722
H. Peter Anvine2c80182005-01-15 22:15:51 +00003723 /*
3724 * We now have a macro name, an implicit parameter count of
3725 * zero, and a numeric token to use as an expansion. Create
3726 * and store an SMacro.
3727 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003728 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003730 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003731 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003732
H. Peter Anvin8b262472019-02-26 14:00:54 -08003733 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003734 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003735 goto done;
3736
H. Peter Anvine2c80182005-01-15 22:15:51 +00003737 last = tline;
3738 tline = expand_smacro(tline->next);
3739 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003740
H. Peter Anvin8b262472019-02-26 14:00:54 -08003741 pps.tptr = tline;
3742 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003743 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003744 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003745 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003746 if (!evalresult)
3747 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003748
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003750 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003751
H. Peter Anvine2c80182005-01-15 22:15:51 +00003752 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003753 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003754 free_tlist(origline);
3755 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003756 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003757
H. Peter Anvin8b262472019-02-26 14:00:54 -08003758 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003759
H. Peter Anvine2c80182005-01-15 22:15:51 +00003760 /*
3761 * We now have a macro name, an implicit parameter count of
3762 * zero, and a numeric token to use as an expansion. Create
3763 * and store an SMacro.
3764 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003765 define_smacro(mname, casesense, macro_start, NULL);
3766 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003767
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 case PP_LINE:
3769 /*
3770 * Syntax is `%line nnn[+mmm] [filename]'
3771 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003772 if (unlikely(pp_noline))
3773 goto done;
3774
H. Peter Anvine2c80182005-01-15 22:15:51 +00003775 tline = tline->next;
3776 skip_white_(tline);
3777 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003778 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003779 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003780 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003781 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 m = 1;
3783 tline = tline->next;
3784 if (tok_is_(tline, "+")) {
3785 tline = tline->next;
3786 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003787 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003788 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003790 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 tline = tline->next;
3792 }
3793 skip_white_(tline);
3794 src_set_linnum(k);
3795 istk->lineinc = m;
3796 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003797 char *fname = detoken(tline, false);
3798 src_set_fname(fname);
3799 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003800 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003801 break;
3802 }
3803
3804done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 free_tlist(origline);
3806 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003807}
3808
3809/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003810 * Ensure that a macro parameter contains a condition code and
3811 * nothing else. Return the condition code index if so, or -1
3812 * otherwise.
3813 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003815{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003816 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003817
H. Peter Anvin25a99342007-09-22 17:45:45 -07003818 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003819 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003820
H. Peter Anvineba20a72002-04-30 20:53:55 +00003821 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003822 if (!t)
3823 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003824 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003825 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003826 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003827 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003828 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003829 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003830
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003831 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003832}
3833
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003834/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003835 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003836 * pasting, if @handle_explicit passed then explicit pasting
3837 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003838 * The @m array can contain a series of token types which are
3839 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003840 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003841static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003842 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003843{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003844 Token *tok, *next, **prev_next, **prev_nonspace;
3845 bool pasted = false;
3846 char *buf, *p;
3847 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003848
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003849 /*
3850 * The last token before pasting. We need it
3851 * to be able to connect new handled tokens.
3852 * In other words if there were a tokens stream
3853 *
3854 * A -> B -> C -> D
3855 *
3856 * and we've joined tokens B and C, the resulting
3857 * stream should be
3858 *
3859 * A -> BC -> D
3860 */
3861 tok = *head;
3862 prev_next = NULL;
3863
3864 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3865 prev_nonspace = head;
3866 else
3867 prev_nonspace = NULL;
3868
3869 while (tok && (next = tok->next)) {
3870
3871 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003872 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003873 /* Zap redundant whitespaces */
3874 while (tok_type_(next, TOK_WHITESPACE))
3875 next = delete_Token(next);
3876 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003877 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003878
3879 case TOK_PASTE:
3880 /* Explicit pasting */
3881 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003882 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003883 next = delete_Token(tok);
3884
3885 while (tok_type_(next, TOK_WHITESPACE))
3886 next = delete_Token(next);
3887
3888 if (!pasted)
3889 pasted = true;
3890
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003891 /* Left pasting token is start of line */
3892 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003893 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003894
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003895 /*
3896 * No ending token, this might happen in two
3897 * cases
3898 *
3899 * 1) There indeed no right token at all
3900 * 2) There is a bare "%define ID" statement,
3901 * and @ID does expand to whitespace.
3902 *
3903 * So technically we need to do a grammar analysis
3904 * in another stage of parsing, but for now lets don't
3905 * change the behaviour people used to. Simply allow
3906 * whitespace after paste token.
3907 */
3908 if (!next) {
3909 /*
3910 * Zap ending space tokens and that's all.
3911 */
3912 tok = (*prev_nonspace)->next;
3913 while (tok_type_(tok, TOK_WHITESPACE))
3914 tok = delete_Token(tok);
3915 tok = *prev_nonspace;
3916 tok->next = NULL;
3917 break;
3918 }
3919
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003920 tok = *prev_nonspace;
3921 while (tok_type_(tok, TOK_WHITESPACE))
3922 tok = delete_Token(tok);
3923 len = strlen(tok->text);
3924 len += strlen(next->text);
3925
3926 p = buf = nasm_malloc(len + 1);
3927 strcpy(p, tok->text);
3928 p = strchr(p, '\0');
3929 strcpy(p, next->text);
3930
3931 delete_Token(tok);
3932
3933 tok = tokenize(buf);
3934 nasm_free(buf);
3935
3936 *prev_nonspace = tok;
3937 while (tok && tok->next)
3938 tok = tok->next;
3939
3940 tok->next = delete_Token(next);
3941
3942 /* Restart from pasted tokens head */
3943 tok = *prev_nonspace;
3944 break;
3945
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003946 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003947 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003948 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003949 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3950 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003951
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003952 len = 0;
3953 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3954 len += strlen(next->text);
3955 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003956 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003957
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003958 /* No match or no text to process */
3959 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003960 break;
3961
3962 len += strlen(tok->text);
3963 p = buf = nasm_malloc(len + 1);
3964
Adam Majer1a069432017-07-25 11:12:35 +02003965 strcpy(p, tok->text);
3966 p = strchr(p, '\0');
3967 tok = delete_Token(tok);
3968
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003969 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003970 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3971 strcpy(p, tok->text);
3972 p = strchr(p, '\0');
3973 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003974 tok = delete_Token(tok);
3975 }
3976
3977 tok = tokenize(buf);
3978 nasm_free(buf);
3979
3980 if (prev_next)
3981 *prev_next = tok;
3982 else
3983 *head = tok;
3984
3985 /*
3986 * Connect pasted into original stream,
3987 * ie A -> new-tokens -> B
3988 */
3989 while (tok && tok->next)
3990 tok = tok->next;
3991 tok->next = next;
3992
3993 if (!pasted)
3994 pasted = true;
3995
3996 /* Restart from pasted tokens head */
3997 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003998 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003999
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004000 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004001 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004002
4003 prev_next = &tok->next;
4004
4005 if (tok->next &&
4006 !tok_type_(tok->next, TOK_WHITESPACE) &&
4007 !tok_type_(tok->next, TOK_PASTE))
4008 prev_nonspace = prev_next;
4009
4010 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004011 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004012
4013 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004014}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004015
4016/*
4017 * expands to a list of tokens from %{x:y}
4018 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004019static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004020{
4021 Token *t = tline, **tt, *tm, *head;
4022 char *pos;
4023 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004024
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004025 pos = strchr(tline->text, ':');
4026 nasm_assert(pos);
4027
4028 lst = atoi(pos + 1);
4029 fst = atoi(tline->text + 1);
4030
4031 /*
4032 * only macros params are accounted so
4033 * if someone passes %0 -- we reject such
4034 * value(s)
4035 */
4036 if (lst == 0 || fst == 0)
4037 goto err;
4038
4039 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004040 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4041 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004042 goto err;
4043
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004044 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4045 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004046
4047 /* counted from zero */
4048 fst--, lst--;
4049
4050 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004051 * It will be at least one token. Note we
4052 * need to scan params until separator, otherwise
4053 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004054 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004055 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004056 if (!tm)
4057 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004058 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004059 tt = &head->next, tm = tm->next;
4060 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004061 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004062 *tt = t, tt = &t->next, tm = tm->next;
4063 }
4064
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004065 if (fst < lst) {
4066 for (i = fst + 1; i <= lst; i++) {
4067 t = new_Token(NULL, TOK_OTHER, ",", 0);
4068 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004069 j = (i + mac->rotate) % mac->nparam;
4070 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004071 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004072 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004073 *tt = t, tt = &t->next, tm = tm->next;
4074 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004075 }
4076 } else {
4077 for (i = fst - 1; i >= lst; i--) {
4078 t = new_Token(NULL, TOK_OTHER, ",", 0);
4079 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004080 j = (i + mac->rotate) % mac->nparam;
4081 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004082 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004083 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004084 *tt = t, tt = &t->next, tm = tm->next;
4085 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004086 }
4087 }
4088
4089 *last = tt;
4090 return head;
4091
4092err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004093 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004094 &tline->text[1]);
4095 return tline;
4096}
4097
H. Peter Anvin76690a12002-04-30 20:52:49 +00004098/*
4099 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004100 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004101 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004102 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004103static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004104{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004105 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004106 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004107 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004108
4109 tail = &thead;
4110 thead = NULL;
4111
H. Peter Anvine2c80182005-01-15 22:15:51 +00004112 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004113 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004114 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4115 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4116 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004117 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004118 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004119 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004120 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004121 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004122 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004123
H. Peter Anvine2c80182005-01-15 22:15:51 +00004124 t = tline;
4125 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004126
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004127 mac = istk->mstk;
4128 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4129 mac = mac->next_active;
4130 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004131 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004132 } else {
4133 pos = strchr(t->text, ':');
4134 if (!pos) {
4135 switch (t->text[1]) {
4136 /*
4137 * We have to make a substitution of one of the
4138 * forms %1, %-1, %+1, %%foo, %0.
4139 */
4140 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004141 type = TOK_NUMBER;
4142 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4143 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004144 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004145 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004146 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004147 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004148 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004149 text = nasm_strcat(tmpbuf, t->text + 2);
4150 break;
4151 case '-':
4152 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004153 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004154 tt = NULL;
4155 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004156 if (mac->nparam > 1)
4157 n = (n + mac->rotate) % mac->nparam;
4158 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004159 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004160 cc = find_cc(tt);
4161 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004162 nasm_nonfatal("macro parameter %d is not a condition code",
4163 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004164 text = NULL;
4165 } else {
4166 type = TOK_ID;
4167 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004168 nasm_nonfatal("condition code `%s' is not invertible",
4169 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004170 text = NULL;
4171 } else
4172 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4173 }
4174 break;
4175 case '+':
4176 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004177 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004178 tt = NULL;
4179 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004180 if (mac->nparam > 1)
4181 n = (n + mac->rotate) % mac->nparam;
4182 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004183 }
4184 cc = find_cc(tt);
4185 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004186 nasm_nonfatal("macro parameter %d is not a condition code",
4187 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004188 text = NULL;
4189 } else {
4190 type = TOK_ID;
4191 text = nasm_strdup(conditions[cc]);
4192 }
4193 break;
4194 default:
4195 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004196 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004197 tt = NULL;
4198 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004199 if (mac->nparam > 1)
4200 n = (n + mac->rotate) % mac->nparam;
4201 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004202 }
4203 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004204 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004205 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004206 tail = &(*tail)->next;
4207 tt = tt->next;
4208 }
4209 }
4210 text = NULL; /* we've done it here */
4211 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004212 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004213 } else {
4214 /*
4215 * seems we have a parameters range here
4216 */
4217 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004218 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004219 if (head != t) {
4220 *tail = head;
4221 *last = tline;
4222 tline = head;
4223 text = NULL;
4224 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004225 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004226 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004227 if (!text) {
4228 delete_Token(t);
4229 } else {
4230 *tail = t;
4231 tail = &t->next;
4232 t->type = type;
4233 nasm_free(t->text);
4234 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004235 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004236 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004237 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004238 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004239 } else if (tline->type == TOK_INDIRECT) {
4240 t = tline;
4241 tline = tline->next;
4242 tt = tokenize(t->text);
4243 tt = expand_mmac_params(tt);
4244 tt = expand_smacro(tt);
4245 *tail = tt;
4246 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004247 tail = &tt->next;
4248 tt = tt->next;
4249 }
4250 delete_Token(t);
4251 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004252 } else {
4253 t = *tail = tline;
4254 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004255 tail = &t->next;
4256 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004257 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004258 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004259
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004260 if (changed) {
4261 const struct tokseq_match t[] = {
4262 {
4263 PP_CONCAT_MASK(TOK_ID) |
4264 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4265 PP_CONCAT_MASK(TOK_ID) |
4266 PP_CONCAT_MASK(TOK_NUMBER) |
4267 PP_CONCAT_MASK(TOK_FLOAT) |
4268 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4269 },
4270 {
4271 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4272 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4273 }
4274 };
4275 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4276 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004277
H. Peter Anvin76690a12002-04-30 20:52:49 +00004278 return thead;
4279}
4280
H. Peter Anvin322bee02019-08-10 01:38:06 -07004281static Token *expand_smacro_noreset(Token * tline);
4282static struct {
4283 int64_t tokens;
4284 int64_t levels;
4285 bool triggered;
4286} smacro_deadman;
4287
H. Peter Anvin76690a12002-04-30 20:52:49 +00004288/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004289 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004290 * a macro at all, it is simply copied to the output and the pointer
4291 * advanced. tpp should be a pointer to a pointer (usually the next
4292 * pointer of the previous token) to the first token. **tpp is updated
4293 * to point to the last token of the expansion, and *tpp updated to
4294 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004295 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004296 * If the expansion is empty, *tpp will be unchanged but **tpp will
4297 * be advanced past the macro call.
4298 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004299 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004300 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004301static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004302{
4303 Token **params = NULL;
4304 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004305 Token *tline = **tpp;
4306 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004307 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004308 unsigned int i;
4309 Token *t, *tup, *ttail;
4310 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004311
4312 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004313 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004314
4315 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004316
H. Peter Anvin322bee02019-08-10 01:38:06 -07004317 smacro_deadman.tokens--;
4318 smacro_deadman.levels--;
4319
4320 if (unlikely(smacro_deadman.tokens < 0 || smacro_deadman.levels < 0)) {
4321 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004322 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004323 smacro_deadman.triggered = true;
4324 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004325 goto not_a_macro;
4326 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004327 head = (SMacro *)hash_findix(&smacros, mname);
4328 } else if (tline->type == TOK_PREPROC_ID) {
4329 Context *ctx = get_ctx(mname, &mname);
4330 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4331 } else {
4332 goto not_a_macro;
4333 }
4334
4335 /*
4336 * We've hit an identifier of some sort. First check whether the
4337 * identifier is a single-line macro at all, then think about
4338 * checking for parameters if necessary.
4339 */
4340 list_for_each(m, head) {
4341 if (!mstrcmp(m->name, mname, m->casesense))
4342 break;
4343 }
4344
4345 if (!m) {
4346 goto not_a_macro;
4347 }
4348
4349 /* Parse parameters, if applicable */
4350
4351 params = NULL;
4352 nparam = 0;
4353
4354 if (m->nparam == 0) {
4355 /*
4356 * Simple case: the macro is parameterless.
4357 * Nothing to parse; the expansion code will
4358 * drop the macro name token.
4359 */
4360 } else {
4361 /*
4362 * Complicated case: at least one macro with this name
4363 * exists and takes parameters. We must find the
4364 * parameters in the call, count them, find the SMacro
4365 * that corresponds to that form of the macro call, and
4366 * substitute for the parameters when we expand. What a
4367 * pain.
4368 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004369 Token **phead, **pep;
4370 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004371 int white = 0;
4372 int brackets = 0;
4373 bool bracketed = false;
4374 bool bad_bracket = false;
4375 unsigned int sparam = PARAM_DELTA;
4376
4377 tline = tline->next;
4378
4379 while (tok_type_(tline, TOK_WHITESPACE)) {
4380 tline = tline->next;
4381 }
4382 if (!tok_is_(tline, "(")) {
4383 /*
4384 * This macro wasn't called with parameters: ignore
4385 * the call. (Behaviour borrowed from gnu cpp.)
4386 */
4387 goto not_a_macro;
4388 }
4389
4390 paren = 1;
4391 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004392 nasm_newn(params, sparam);
4393 phead = pep = &params[0];
4394 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004395
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004396 while (paren) {
4397 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004398 char ch;
4399
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004400 tline = tline->next;
4401
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004402 if (!tline) {
4403 nasm_nonfatal("macro call expects terminating `)'");
4404 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004405 }
4406
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004407 ch = 0;
4408 skip = false;
4409
4410 switch (tline->type) {
4411 case TOK_OTHER:
4412 if (!tline->text[1])
4413 ch = tline->text[0];
4414 break;
4415
4416 case TOK_WHITESPACE:
4417 if (brackets || *phead)
4418 white++; /* Keep interior whitespace */
4419 skip = true;
4420 break;
4421
4422 default:
4423 break;
4424 }
4425
4426 switch (ch) {
4427 case ',':
4428 if (!brackets) {
4429 if (++nparam >= sparam) {
4430 sparam += PARAM_DELTA;
4431 params = nasm_realloc(params, sparam * sizeof *params);
4432 }
4433 pep = &params[nparam];
4434 *pep = NULL;
4435 bracketed = false;
4436 skip = true;
4437 }
4438 break;
4439
4440 case '{':
4441 if (!bracketed) {
4442 bracketed = !*phead;
4443 skip = true;
4444 }
4445 brackets++;
4446 break;
4447
4448 case '}':
4449 if (brackets > 0) {
4450 if (!--brackets)
4451 skip = bracketed;
4452 }
4453 break;
4454
4455 case '(':
4456 if (!brackets)
4457 paren++;
4458 break;
4459
4460 case ')':
4461 if (!brackets) {
4462 paren--;
4463 if (!paren) {
4464 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004465 /* Count the final argument */
4466 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004467 }
4468 }
4469 break;
4470
4471 default:
4472 break; /* Normal token */
4473 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004474
4475 if (!skip) {
4476 Token *t;
4477
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004478 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004479
4480 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004481 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004482 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004483 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004484 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004485 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004486 pep = &t->next;
4487 white = 0;
4488 }
4489 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004490
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004491 /*
4492 * Look for a macro matching in both name and parameter count.
4493 * We already know any matches cannot be anywhere before the
4494 * current position of "m", so there is no reason to
4495 * backtrack.
4496 */
4497 while (1) {
4498 if (!m) {
4499 /*!
4500 *!macro-params-single [on] single-line macro calls with wrong parameter count
4501 *! warns about \i{single-line macros} being invoked
4502 *! with the wrong number of parameters.
4503 */
4504 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4505 "single-line macro `%s' exists, "
4506 "but not taking %d parameter%s",
4507 mname, nparam, (nparam == 1) ? "" : "s");
4508 goto not_a_macro;
4509 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004510
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004511 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4512 break; /* It's good */
4513
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004514 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004515 }
4516 }
4517
4518 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004519 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004520
4521 /* Expand each parameter */
4522 m->in_progress = true;
4523
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004524 if (m->eval_param) {
4525 for (i = 0; i < nparam; i++) {
4526 if (m->eval_param[i]) {
4527 /* Evaluate this parameter as a number */
4528 struct ppscan pps;
4529 struct tokenval tokval;
4530 expr *evalresult;
4531 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004532
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004533 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4534 pps.ntokens = -1;
4535 tokval.t_type = TOKEN_INVALID;
4536 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004537
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004538 free_tlist(eval_param);
4539 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004540
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004541 if (!evalresult) {
4542 /* Nothing meaningful to do */
4543 } else if (tokval.t_type) {
4544 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4545 } else if (!is_simple(evalresult)) {
4546 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4547 } else {
4548 params[i] = make_tok_num(reloc_value(evalresult));
4549 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004550 }
4551 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004552 }
4553
4554 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004555 tline = tline->next; /* Remove the macro call from the input */
4556 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004557
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004558 /* Note: we own the expansion this returns. */
4559 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004560
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004561 tup = NULL;
4562 ttail = NULL; /* Pointer to the last token of the expansion */
4563 while (t) {
4564 enum pp_token_type type = t->type;
4565 Token *tnext = t->next;
4566 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004567
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004568 switch (type) {
4569 case TOK_PREPROC_Q:
4570 case TOK_PREPROC_QQ:
4571 t->type = TOK_ID;
4572 nasm_free(t->text);
4573 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4574 t->len = nasm_last_string_len();
4575 t->next = tline;
4576 break;
4577
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004578 case TOK_ID:
4579 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004580 /*
4581 * Chain this into the target line *before* expanding,
4582 * that way we pick up any arguments to the new macro call,
4583 * if applicable.
4584 */
4585 t->next = tline;
4586 tp = &t;
4587 expand_one_smacro(&tp);
4588 if (t == tline)
4589 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004590 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004591
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004592 default:
4593 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004594 unsigned int param = smac_nparam(t->type);
4595 nasm_assert(!tup && param < nparam);
4596 delete_Token(t);
4597 t = NULL;
4598 tup = tnext;
4599 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004600 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004601 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004602 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004603 }
4604
4605 if (t) {
4606 tline = t;
4607 if (!ttail)
4608 ttail = t;
4609 }
4610
4611 if (tnext) {
4612 t = tnext;
4613 } else {
4614 t = tup;
4615 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004616 }
4617 }
4618
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004619 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004620 if (ttail)
4621 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004622
4623 m->in_progress = false;
4624
4625 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004626 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004627 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004628
4629 /*
4630 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004631 * and then advance to the next input token. Note that this is
4632 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004633 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004634not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004635 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004636 m = NULL;
4637done:
4638 smacro_deadman.levels++;
4639 if (unlikely(params))
4640 free_tlist_array(params, nparam);
4641 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004642}
4643
4644/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004645 * Expand all single-line macro calls made in the given line.
4646 * Return the expanded version of the line. The original is deemed
4647 * to be destroyed in the process. (In reality we'll just move
4648 * Tokens from input to output a lot of the time, rather than
4649 * actually bothering to destroy and replicate.)
4650 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004651static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004652{
H. Peter Anvin322bee02019-08-10 01:38:06 -07004653 smacro_deadman.tokens = nasm_limit[LIMIT_MACRO_TOKENS];
4654 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4655 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004656 return expand_smacro_noreset(tline);
4657}
4658
4659static Token *expand_smacro_noreset(Token * tline)
4660{
4661 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004662 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004663 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004664
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004665 /*
4666 * Trick: we should avoid changing the start token pointer since it can
4667 * be contained in "next" field of other token. Because of this
4668 * we allocate a copy of first token and work with it; at the end of
4669 * routine we copy it back
4670 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004671 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004672 tline = new_Token(org_tline->next, org_tline->type,
4673 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 nasm_free(org_tline->text);
4675 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004676 }
4677
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004678
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004679 /*
4680 * Pretend that we always end up doing expansion on the first pass;
4681 * that way %+ get processed. However, if we process %+ before the
4682 * first pass, we end up with things like MACRO %+ TAIL trying to
4683 * look up the macro "MACROTAIL", which we don't want.
4684 */
4685 expanded = true;
4686 thead = tline;
4687 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004688 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004689 {
4690 PP_CONCAT_MASK(TOK_ID) |
4691 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4692 PP_CONCAT_MASK(TOK_ID) |
4693 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4694 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4695 }
4696 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004697
4698 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004699 while ((t = *tail)) /* main token loop */
4700 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004701
4702 if (!expanded) {
4703 tline = thead;
4704 break; /* Done! */
4705 }
4706
4707 /*
4708 * Now scan the entire line and look for successive TOK_IDs
4709 * that resulted after expansion (they can't be produced by
4710 * tokenize()). The successive TOK_IDs should be concatenated.
4711 * Also we look for %+ tokens and concatenate the tokens
4712 * before and after them (without white spaces in between).
4713 */
4714 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004715
4716 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004717 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004718
H. Peter Anvine2c80182005-01-15 22:15:51 +00004719 if (org_tline) {
4720 if (thead) {
4721 *org_tline = *thead;
4722 /* since we just gave text to org_line, don't free it */
4723 thead->text = NULL;
4724 delete_Token(thead);
4725 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004726 /*
4727 * The expression expanded to empty line;
4728 * we can't return NULL because of the "trick" above.
4729 * Just set the line to a single WHITESPACE token.
4730 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004731 memset(org_tline, 0, sizeof(*org_tline));
4732 org_tline->text = NULL;
4733 org_tline->type = TOK_WHITESPACE;
4734 }
4735 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004736 }
4737
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004738 return thead;
4739}
4740
4741/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004742 * Similar to expand_smacro but used exclusively with macro identifiers
4743 * right before they are fetched in. The reason is that there can be
4744 * identifiers consisting of several subparts. We consider that if there
4745 * are more than one element forming the name, user wants a expansion,
4746 * otherwise it will be left as-is. Example:
4747 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004748 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004749 *
4750 * the identifier %$abc will be left as-is so that the handler for %define
4751 * will suck it and define the corresponding value. Other case:
4752 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004753 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004754 *
4755 * In this case user wants name to be expanded *before* %define starts
4756 * working, so we'll expand %$abc into something (if it has a value;
4757 * otherwise it will be left as-is) then concatenate all successive
4758 * PP_IDs into one.
4759 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004760static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004761{
4762 Token *cur, *oldnext = NULL;
4763
H. Peter Anvin734b1882002-04-30 21:01:08 +00004764 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004765 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004766
4767 cur = tline;
4768 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004769 (cur->next->type == TOK_ID ||
4770 cur->next->type == TOK_PREPROC_ID
4771 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004772 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004773
4774 /* If identifier consists of just one token, don't expand */
4775 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004776 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004777
H. Peter Anvine2c80182005-01-15 22:15:51 +00004778 if (cur) {
4779 oldnext = cur->next; /* Detach the tail past identifier */
4780 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004781 }
4782
H. Peter Anvin734b1882002-04-30 21:01:08 +00004783 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784
H. Peter Anvine2c80182005-01-15 22:15:51 +00004785 if (cur) {
4786 /* expand_smacro possibly changhed tline; re-scan for EOL */
4787 cur = tline;
4788 while (cur && cur->next)
4789 cur = cur->next;
4790 if (cur)
4791 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004792 }
4793
4794 return tline;
4795}
4796
4797/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004798 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004799 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004800 * to check for an initial label - that's taken care of in
4801 * expand_mmacro - but must check numbers of parameters. Guaranteed
4802 * to be called with tline->type == TOK_ID, so the putative macro
4803 * name is easy to find.
4804 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004805static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004806{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004807 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004808 Token **params;
4809 int nparam;
4810
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004811 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004812
4813 /*
4814 * Efficiency: first we see if any macro exists with the given
4815 * name. If not, we can return NULL immediately. _Then_ we
4816 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004817 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004818 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004819 list_for_each(m, head)
4820 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004821 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004822 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004823 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004824
4825 /*
4826 * OK, we have a potential macro. Count and demarcate the
4827 * parameters.
4828 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004829 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004830
4831 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004832 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004833 * structure that handles this number.
4834 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004835 while (m) {
4836 if (m->nparam_min <= nparam
4837 && (m->plus || nparam <= m->nparam_max)) {
4838 /*
4839 * This one is right. Just check if cycle removal
4840 * prohibits us using it before we actually celebrate...
4841 */
4842 if (m->in_progress > m->max_depth) {
4843 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004844 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004845 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004846 }
4847 nasm_free(params);
4848 return NULL;
4849 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004850 /*
4851 * It's right, and we can use it. Add its default
4852 * parameters to the end of our list if necessary.
4853 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004854 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004855 params =
4856 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004857 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004858 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004859 while (nparam < m->nparam_min + m->ndefs) {
4860 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004861 nparam++;
4862 }
4863 }
4864 /*
4865 * If we've gone over the maximum parameter count (and
4866 * we're in Plus mode), ignore parameters beyond
4867 * nparam_max.
4868 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004869 if (m->plus && nparam > m->nparam_max)
4870 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004871 /*
4872 * Then terminate the parameter list, and leave.
4873 */
4874 if (!params) { /* need this special case */
4875 params = nasm_malloc(sizeof(*params));
4876 nparam = 0;
4877 }
4878 params[nparam] = NULL;
4879 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004880 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004881 }
4882 /*
4883 * This one wasn't right: look for the next one with the
4884 * same name.
4885 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004886 list_for_each(m, m->next)
4887 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004888 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004889 }
4890
4891 /*
4892 * After all that, we didn't find one with the right number of
4893 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004894 *!
4895 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4896 *! warns about \i{multi-line macros} being invoked
4897 *! with the wrong number of parameters. See \k{mlmacover} for an
4898 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004899 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004900 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4901 "multi-line macro `%s' exists, but not taking %d parameter%s",
4902 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004903 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004904 return NULL;
4905}
4906
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004907
4908/*
4909 * Save MMacro invocation specific fields in
4910 * preparation for a recursive macro expansion
4911 */
4912static void push_mmacro(MMacro *m)
4913{
4914 MMacroInvocation *i;
4915
4916 i = nasm_malloc(sizeof(MMacroInvocation));
4917 i->prev = m->prev;
4918 i->params = m->params;
4919 i->iline = m->iline;
4920 i->nparam = m->nparam;
4921 i->rotate = m->rotate;
4922 i->paramlen = m->paramlen;
4923 i->unique = m->unique;
4924 i->condcnt = m->condcnt;
4925 m->prev = i;
4926}
4927
4928
4929/*
4930 * Restore MMacro invocation specific fields that were
4931 * saved during a previous recursive macro expansion
4932 */
4933static void pop_mmacro(MMacro *m)
4934{
4935 MMacroInvocation *i;
4936
4937 if (m->prev) {
4938 i = m->prev;
4939 m->prev = i->prev;
4940 m->params = i->params;
4941 m->iline = i->iline;
4942 m->nparam = i->nparam;
4943 m->rotate = i->rotate;
4944 m->paramlen = i->paramlen;
4945 m->unique = i->unique;
4946 m->condcnt = i->condcnt;
4947 nasm_free(i);
4948 }
4949}
4950
4951
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004952/*
4953 * Expand the multi-line macro call made by the given line, if
4954 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004955 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004956 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004957static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004958{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004959 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004960 Token *label = NULL;
4961 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004962 Token **params, *t, *tt;
4963 MMacro *m;
4964 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004965 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004966 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004967
4968 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004969 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004970 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004971 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004972 return 0;
4973 m = is_mmacro(t, &params);
4974 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004975 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004976 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004977 Token *last;
4978 /*
4979 * We have an id which isn't a macro call. We'll assume
4980 * it might be a label; we'll also check to see if a
4981 * colon follows it. Then, if there's another id after
4982 * that lot, we'll check it again for macro-hood.
4983 */
4984 label = last = t;
4985 t = t->next;
4986 if (tok_type_(t, TOK_WHITESPACE))
4987 last = t, t = t->next;
4988 if (tok_is_(t, ":")) {
4989 dont_prepend = 1;
4990 last = t, t = t->next;
4991 if (tok_type_(t, TOK_WHITESPACE))
4992 last = t, t = t->next;
4993 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004994 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4995 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004996 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004997 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004998 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004999 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005000
5001 /*
5002 * Fix up the parameters: this involves stripping leading and
5003 * trailing whitespace, then stripping braces if they are
5004 * present.
5005 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005006 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005007 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005008
H. Peter Anvine2c80182005-01-15 22:15:51 +00005009 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005010 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005011 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005012
H. Peter Anvine2c80182005-01-15 22:15:51 +00005013 t = params[i];
5014 skip_white_(t);
5015 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005016 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005017 params[i] = t;
5018 paramlen[i] = 0;
5019 while (t) {
5020 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5021 break; /* ... because we have hit a comma */
5022 if (comma && t->type == TOK_WHITESPACE
5023 && tok_is_(t->next, ","))
5024 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005025 if (brace && t->type == TOK_OTHER) {
5026 if (t->text[0] == '{')
5027 brace++; /* ... or a nested opening brace */
5028 else if (t->text[0] == '}')
5029 if (!--brace)
5030 break; /* ... or a brace */
5031 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005032 t = t->next;
5033 paramlen[i]++;
5034 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005035 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005036 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005037 }
5038
5039 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005040 * OK, we have a MMacro structure together with a set of
5041 * parameters. We must now go through the expansion and push
5042 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005043 * parameter tokens and macro-local tokens doesn't get done
5044 * until the single-line macro substitution process; this is
5045 * because delaying them allows us to change the semantics
5046 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005047 *
5048 * First, push an end marker on to istk->expansion, mark this
5049 * macro as in progress, and set up its invocation-specific
5050 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005051 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005052 ll = nasm_malloc(sizeof(Line));
5053 ll->next = istk->expansion;
5054 ll->finishes = m;
5055 ll->first = NULL;
5056 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005057
5058 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005059 * Save the previous MMacro expansion in the case of
5060 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005061 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005062 if (m->max_depth && m->in_progress)
5063 push_mmacro(m);
5064
5065 m->in_progress ++;
5066 m->params = params;
5067 m->iline = tline;
5068 m->nparam = nparam;
5069 m->rotate = 0;
5070 m->paramlen = paramlen;
5071 m->unique = unique++;
5072 m->lineno = 0;
5073 m->condcnt = 0;
5074
5075 m->next_active = istk->mstk;
5076 istk->mstk = m;
5077
5078 list_for_each(l, m->expansion) {
5079 Token **tail;
5080
5081 ll = nasm_malloc(sizeof(Line));
5082 ll->finishes = NULL;
5083 ll->next = istk->expansion;
5084 istk->expansion = ll;
5085 tail = &ll->first;
5086
5087 list_for_each(t, l->first) {
5088 Token *x = t;
5089 switch (t->type) {
5090 case TOK_PREPROC_Q:
5091 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005092 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005093 case TOK_PREPROC_QQ:
5094 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
5095 break;
5096 case TOK_PREPROC_ID:
5097 if (t->text[1] == '0' && t->text[2] == '0') {
5098 dont_prepend = -1;
5099 x = label;
5100 if (!x)
5101 continue;
5102 }
5103 /* fall through */
5104 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005105 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005106 break;
5107 }
5108 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005109 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005110 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005111 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005112
5113 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005114 * If we had a label, push it on as the first line of
5115 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005116 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005117 if (label) {
5118 if (dont_prepend < 0)
5119 free_tlist(startline);
5120 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005121 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005122 ll->finishes = NULL;
5123 ll->next = istk->expansion;
5124 istk->expansion = ll;
5125 ll->first = startline;
5126 if (!dont_prepend) {
5127 while (label->next)
5128 label = label->next;
5129 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005130 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005131 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005132 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005133
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005134 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005135
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005136 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005137}
5138
H. Peter Anvin130736c2016-02-17 20:27:41 -08005139/*
5140 * This function adds macro names to error messages, and suppresses
5141 * them if necessary.
5142 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005143static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005144{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005145 /*
5146 * If we're in a dead branch of IF or something like it, ignore the error.
5147 * However, because %else etc are evaluated in the state context
5148 * of the previous branch, errors might get lost:
5149 * %if 0 ... %else trailing garbage ... %endif
5150 * So %else etc should set the ERR_PP_PRECOND flag.
5151 */
5152 if ((severity & ERR_MASK) < ERR_FATAL &&
5153 istk && istk->conds &&
5154 ((severity & ERR_PP_PRECOND) ?
5155 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005156 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005157 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005158
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005159 /* This doesn't make sense with the macro stack unwinding */
5160 if (0) {
5161 MMacro *mmac = NULL;
5162 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005163
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005164 /* get %macro name */
5165 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
5166 mmac = istk->mstk;
5167 /* but %rep blocks should be skipped */
5168 while (mmac && !mmac->name)
5169 mmac = mmac->next_active, delta++;
5170 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005171
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005172 if (mmac) {
5173 char *buf;
5174 nasm_set_verror(real_verror);
5175 buf = nasm_vasprintf(fmt, arg);
5176 nasm_error(severity, "(%s:%"PRId32") %s",
5177 mmac->name, mmac->lineno - delta, buf);
5178 nasm_set_verror(pp_verror);
5179 nasm_free(buf);
5180 return;
5181 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005182 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005183 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005184}
5185
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005186static Token *
5187stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005188{
5189 (void)s;
5190 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005191 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005192
5193 return make_tok_qstr(src_get_fname());
5194}
5195
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005196static Token *
5197stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005198{
5199 (void)s;
5200 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005201 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005202
5203 return make_tok_num(src_get_linnum());
5204}
5205
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005206static Token *
5207stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005208{
5209 (void)s;
5210 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005211 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005212
5213 return make_tok_num(globalbits);
5214}
5215
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005216static Token *
5217stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005218{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005219 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005220 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5221 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5222 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005223
5224 (void)s;
5225 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005226 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005227
5228 switch (globalbits) {
5229 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005230 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005231 break;
5232 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005233 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005234 break;
5235 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005236 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005237 break;
5238 default:
5239 panic();
5240 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005241
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005242 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005243}
5244
H. Peter Anvin8b262472019-02-26 14:00:54 -08005245/* Add magic standard macros */
5246struct magic_macros {
5247 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005248 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005249 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005250};
5251static const struct magic_macros magic_macros[] =
5252{
5253 { "__FILE__", 0, stdmac_file },
5254 { "__LINE__", 0, stdmac_line },
5255 { "__BITS__", 0, stdmac_bits },
5256 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005257 { NULL, 0, NULL }
5258};
5259
5260static void pp_add_magic_stdmac(void)
5261{
5262 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005263 SMacro tmpl;
5264
5265 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005266
5267 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005268 tmpl.nparam = m->nparam;
5269 tmpl.expand = m->func;
5270 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005271 }
5272}
5273
H. Peter Anvin734b1882002-04-30 21:01:08 +00005274static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005275pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005276{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005277 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005278 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005279
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005280 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005281 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005282 nested_mac_count = 0;
5283 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005284 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005285 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005286 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005287 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005288
H. Peter Anvin6686de22019-08-10 05:33:14 -07005289 /* First set up the top level input file */
5290 nasm_new(istk);
5291 istk->fp = nasm_open_read(file, NF_TEXT);
5292 src_set(0, file);
5293 istk->lineinc = 1;
5294 if (!istk->fp)
5295 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5296
5297 strlist_add(deplist, file);
5298
5299 /*
5300 * Set up the stdmac packages as a virtual include file,
5301 * indicated by a null file pointer.
5302 */
5303 nasm_new(inc);
5304 inc->next = istk;
5305 inc->fname = src_set_fname(NULL);
5306 inc->nolist = !list_option('b');
5307 istk = inc;
5308 lfmt->uplevel(LIST_INCLUDE, 0);
5309
H. Peter Anvin8b262472019-02-26 14:00:54 -08005310 pp_add_magic_stdmac();
5311
H. Peter Anvinf7606612016-07-13 14:23:48 -07005312 if (tasm_compatible_mode)
5313 pp_add_stdmac(nasm_stdmac_tasm);
5314
5315 pp_add_stdmac(nasm_stdmac_nasm);
5316 pp_add_stdmac(nasm_stdmac_version);
5317
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005318 if (extrastdmac)
5319 pp_add_stdmac(extrastdmac);
5320
H. Peter Anvinf7606612016-07-13 14:23:48 -07005321 stdmacpos = stdmacros[0];
5322 stdmacnext = &stdmacros[1];
5323
H. Peter Anvind2456592008-06-19 15:04:18 -07005324 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005325
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005326 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005327 * Define the __PASS__ macro. This is defined here unlike all the
5328 * other builtins, because it is special -- it varies between
5329 * passes -- but there is really no particular reason to make it
5330 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005331 *
5332 * 0 = dependencies only
5333 * 1 = preparatory passes
5334 * 2 = final pass
5335 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005336 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005337 switch (mode) {
5338 case PP_NORMAL:
5339 apass = pass_final() ? 2 : 1;
5340 break;
5341 case PP_DEPS:
5342 apass = 0;
5343 break;
5344 case PP_PREPROC:
5345 apass = 3;
5346 break;
5347 default:
5348 panic();
5349 }
5350
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005351 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005352}
5353
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005354static void pp_init(void)
5355{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005356}
5357
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005358/*
5359 * Get a line of tokens. If we popped the macro expansion/include stack,
5360 * we return a pointer to the dummy token tok_pop; at that point if
5361 * istk is NULL then we have reached end of input;
5362 */
5363static Token tok_pop; /* Dummy token placeholder */
5364
5365static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005366{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005367 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005368
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005369 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005370 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005371 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005372 * buffer or from the input file.
5373 */
5374 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005375 while (istk->expansion && istk->expansion->finishes) {
5376 Line *l = istk->expansion;
5377 if (!l->finishes->name && l->finishes->in_progress > 1) {
5378 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005379
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005380 /*
5381 * This is a macro-end marker for a macro with no
5382 * name, which means it's not really a macro at all
5383 * but a %rep block, and the `in_progress' field is
5384 * more than 1, meaning that we still need to
5385 * repeat. (1 means the natural last repetition; 0
5386 * means termination by %exitrep.) We have
5387 * therefore expanded up to the %endrep, and must
5388 * push the whole block on to the expansion buffer
5389 * again. We don't bother to remove the macro-end
5390 * marker: we'd only have to generate another one
5391 * if we did.
5392 */
5393 l->finishes->in_progress--;
5394 list_for_each(l, l->finishes->expansion) {
5395 Token *t, *tt, **tail;
5396
5397 ll = nasm_malloc(sizeof(Line));
5398 ll->next = istk->expansion;
5399 ll->finishes = NULL;
5400 ll->first = NULL;
5401 tail = &ll->first;
5402
5403 list_for_each(t, l->first) {
5404 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005405 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005406 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005407 }
5408 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005409 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005410 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005411 } else {
5412 /*
5413 * Check whether a `%rep' was started and not ended
5414 * within this macro expansion. This can happen and
5415 * should be detected. It's a fatal error because
5416 * I'm too confused to work out how to recover
5417 * sensibly from it.
5418 */
5419 if (defining) {
5420 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005421 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005422 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005423 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005424 " expansion of macro `%s'",
5425 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005426 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005427
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005428 /*
5429 * FIXME: investigate the relationship at this point between
5430 * istk->mstk and l->finishes
5431 */
5432 {
5433 MMacro *m = istk->mstk;
5434 istk->mstk = m->next_active;
5435 if (m->name) {
5436 /*
5437 * This was a real macro call, not a %rep, and
5438 * therefore the parameter information needs to
5439 * be freed.
5440 */
5441 if (m->prev) {
5442 pop_mmacro(m);
5443 l->finishes->in_progress --;
5444 } else {
5445 nasm_free(m->params);
5446 free_tlist(m->iline);
5447 nasm_free(m->paramlen);
5448 l->finishes->in_progress = 0;
5449 }
Adam Majer91e72402017-07-25 10:42:01 +02005450 }
5451
5452 /*
5453 * FIXME It is incorrect to always free_mmacro here.
5454 * It leads to usage-after-free.
5455 *
5456 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5457 */
5458#if 0
5459 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005460 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005461#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005462 }
5463 istk->expansion = l->next;
5464 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005465 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005466 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005467 }
5468 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005469 do { /* until we get a line we can use */
5470 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005471
5472 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005473 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005474 int32_t lineno;
5475
H. Peter Anvin6686de22019-08-10 05:33:14 -07005476 if (istk->mstk) {
5477 istk->mstk->lineno++;
5478 if (istk->mstk->fname)
5479 lineno = istk->mstk->lineno + istk->mstk->xline;
5480 else
5481 lineno = 0; /* Defined at init time or builtin */
5482 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005483 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005484 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005485
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005486 tline = l->first;
5487 istk->expansion = l->next;
5488 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005489
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005490 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005491 if (!istk->nolist)
5492 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005493 nasm_free(line);
5494 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005495 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005496 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005497 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005498 } else {
5499 /*
5500 * The current file has ended; work down the istk
5501 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005502 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005503 if (i->fp)
5504 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005505 if (i->conds) {
5506 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005507 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005508 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005509 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005510 if (i->next)
5511 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005512 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005513 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005514 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005515 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005516 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005517 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005518
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005519 /*
5520 * We must expand MMacro parameters and MMacro-local labels
5521 * _before_ we plunge into directive processing, to cope
5522 * with things like `%define something %1' such as STRUC
5523 * uses. Unless we're _defining_ a MMacro, in which case
5524 * those tokens should be left alone to go into the
5525 * definition; and unless we're in a non-emitting
5526 * condition, in which case we don't want to meddle with
5527 * anything.
5528 */
5529 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5530 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005531 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005532 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005533
H. Peter Anvine2c80182005-01-15 22:15:51 +00005534 /*
5535 * Check the line to see if it's a preprocessor directive.
5536 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005537 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5538 if (dtline)
5539 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005540 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005541 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005542 * We're defining a multi-line macro. We emit nothing
5543 * at all, and just
5544 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005545 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005546 Line *l = nasm_malloc(sizeof(Line));
5547 l->next = defining->expansion;
5548 l->first = tline;
5549 l->finishes = NULL;
5550 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005551 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005552 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005553 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005554 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005555 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005556 * directive so we keep our place correctly.
5557 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005558 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005559 } else if (istk->mstk && !istk->mstk->in_progress) {
5560 /*
5561 * We're in a %rep block which has been terminated, so
5562 * we're walking through to the %endrep without
5563 * emitting anything. Emit nothing at all, not even a
5564 * blank line: when we emerge from the %rep block we'll
5565 * give a line-number directive so we keep our place
5566 * correctly.
5567 */
5568 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005569 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005570 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005571 if (!expand_mmacro(tline))
5572 return tline;
5573 }
5574 }
5575}
5576
5577static char *pp_getline(void)
5578{
5579 char *line = NULL;
5580 Token *tline;
5581
5582 real_verror = nasm_set_verror(pp_verror);
5583
5584 while (true) {
5585 tline = pp_tokline();
5586 if (tline == &tok_pop) {
5587 /*
5588 * We popped the macro/include stack. If istk is empty,
5589 * we are at end of input, otherwise just loop back.
5590 */
5591 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005592 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005593 } else {
5594 /*
5595 * De-tokenize the line and emit it.
5596 */
5597 line = detoken(tline, true);
5598 free_tlist(tline);
5599 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005600 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005601 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005602
H. Peter Anvin6686de22019-08-10 05:33:14 -07005603 if (list_option('e') && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005604 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005605 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005606 nasm_free(buf);
5607 }
5608
H. Peter Anvin130736c2016-02-17 20:27:41 -08005609 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005610 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005611}
5612
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005613static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005614{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005615 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005616
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005617 if (defining) {
5618 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005619 nasm_nonfatal("end of file while still defining macro `%s'",
5620 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005621 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005622 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005623 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005624
5625 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005626 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005627 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005628
5629 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005630
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005631 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005632 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005633 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005634 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005635 Include *i = istk;
5636 istk = istk->next;
5637 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005638 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005639 }
5640 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005641 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005642 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005643}
5644
5645static void pp_cleanup_session(void)
5646{
5647 free_llist(predef);
5648 predef = NULL;
5649 delete_Blocks();
5650 freeTokens = NULL;
5651 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005652}
5653
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005654static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005655{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005656 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005657}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005658
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005659static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005660{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005661 Token *inc, *space, *name;
5662 Line *l;
5663
H. Peter Anvin734b1882002-04-30 21:01:08 +00005664 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5665 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5666 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005667
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005668 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005669 l->next = predef;
5670 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005671 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005672 predef = l;
5673}
5674
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005675static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005676{
5677 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005678 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005679 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005680
H. Peter Anvin130736c2016-02-17 20:27:41 -08005681 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005682
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005683 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005684 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5685 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005686 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005687 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005688 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005689 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005690 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005691
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005692 if (space->next->type != TOK_PREPROC_ID &&
5693 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005694 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005695
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005696 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005697 l->next = predef;
5698 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005699 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005700 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005701
5702 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005703}
5704
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005705static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005706{
5707 Token *def, *space;
5708 Line *l;
5709
H. Peter Anvin734b1882002-04-30 21:01:08 +00005710 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5711 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005712 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005713
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005714 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005715 l->next = predef;
5716 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005717 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005718 predef = l;
5719}
5720
H. Peter Anvin05990342018-06-11 13:32:42 -07005721/* Insert an early preprocessor command that doesn't need special handling */
5722static void pp_pre_command(const char *what, char *string)
5723{
5724 char *cmd;
5725 Token *def, *space;
5726 Line *l;
5727
5728 def = tokenize(string);
5729 if (what) {
5730 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5731 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5732 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5733 }
5734
5735 l = nasm_malloc(sizeof(Line));
5736 l->next = predef;
5737 l->first = def;
5738 l->finishes = NULL;
5739 predef = l;
5740}
5741
H. Peter Anvinf7606612016-07-13 14:23:48 -07005742static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005743{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005744 macros_t **mp;
5745
5746 /* Find the end of the list and avoid duplicates */
5747 for (mp = stdmacros; *mp; mp++) {
5748 if (*mp == macros)
5749 return; /* Nothing to do */
5750 }
5751
5752 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5753
5754 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005755}
5756
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005757static void pp_extra_stdmac(macros_t *macros)
5758{
5759 extrastdmac = macros;
5760}
5761
H. Peter Anvin8b262472019-02-26 14:00:54 -08005762static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005763{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005764 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005765 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5766 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5767}
5768
5769static Token *make_tok_qstr(const char *str)
5770{
5771 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005772 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005773 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005774}
5775
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005776static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005777{
5778 if (!m)
5779 return;
5780
5781 /* We need to print the next_active list in reverse order */
5782 pp_list_one_macro(m->next_active, severity);
5783
5784 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005785 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005786 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005787 }
5788}
5789
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005790static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005791{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005792 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005793
H. Peter Anvinddb29062018-12-11 00:06:29 -08005794 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5795 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005796
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005797 if (istk)
5798 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005799
H. Peter Anvinddb29062018-12-11 00:06:29 -08005800 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005801}
5802
H. Peter Anvine7469712016-02-18 02:20:59 -08005803const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005804 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005805 pp_reset,
5806 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005807 pp_cleanup_pass,
5808 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005809 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005810 pp_pre_define,
5811 pp_pre_undefine,
5812 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005813 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005814 pp_include_path,
5815 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005816};