blob: 664950fc197e61c8058c3d3660a423b705354aad [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 Anvin36206cd2012-03-03 16:14:51 -0800308struct Cond {
309 Cond *next;
310 int state;
311};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000312enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000313 /*
314 * These states are for use just after %if or %elif: IF_TRUE
315 * means the condition has evaluated to truth so we are
316 * currently emitting, whereas IF_FALSE means we are not
317 * currently emitting but will start doing so if a %else comes
318 * up. In these states, all directives are admissible: %elif,
319 * %else and %endif. (And of course %if.)
320 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800321 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000322 /*
323 * These states come up after a %else: ELSE_TRUE means we're
324 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
325 * any %elif or %else will cause an error.
326 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800327 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000328 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200329 * These states mean that we're not emitting now, and also that
330 * nothing until %endif will be emitted at all. COND_DONE is
331 * used when we've had our moment of emission
332 * and have now started seeing %elifs. COND_NEVER is used when
333 * the condition construct in question is contained within a
334 * non-emitting branch of a larger condition construct,
335 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800337 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338};
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 Anvin70055962007-10-11 00:05:31 -07001823static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001824{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001825 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001826 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001827 Token *t, *tt, *origline;
1828 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001829 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001830 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001831 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001832 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001833
1834 origline = tline;
1835
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001837 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001838 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001839 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001841 if (!tline)
1842 break;
1843 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001844 nasm_nonfatal("`%s' expects context identifiers",
1845 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 free_tlist(origline);
1847 return -1;
1848 }
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 Anvinda10e7b2007-09-12 04:18:37 +00001855 case PPC_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",
1863 pp_directives[ct]);
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 Anvin6d9b2b52010-07-13 12:00:58 -07001872 case PPC_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",
1882 pp_directives[ct]);
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 Anvinda10e7b2007-09-12 04:18:37 +00001896 case PPC_IFIDN:
1897 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 tline = expand_smacro(tline);
1899 t = tt = tline;
1900 while (tok_isnt_(tt, ","))
1901 tt = tt->next;
1902 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001903 nasm_nonfatal("`%s' expects two comma-separated arguments",
1904 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001906 }
1907 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001908 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1910 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001911 nasm_nonfatal("`%s': more than one comma on line",
1912 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001913 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 }
1915 if (t->type == TOK_WHITESPACE) {
1916 t = t->next;
1917 continue;
1918 }
1919 if (tt->type == TOK_WHITESPACE) {
1920 tt = tt->next;
1921 continue;
1922 }
1923 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001924 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 break;
1926 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001927 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001929 size_t l1 = nasm_unquote(t->text, NULL);
1930 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001931
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001932 if (l1 != l2) {
1933 j = false;
1934 break;
1935 }
1936 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1937 j = false;
1938 break;
1939 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001940 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001941 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 break;
1943 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001944
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 t = t->next;
1946 tt = tt->next;
1947 }
1948 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001949 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001950 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001951
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001952 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001953 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001955 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001956
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 skip_white_(tline);
1958 tline = expand_id(tline);
1959 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001960 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 goto fail;
1962 }
1963 searching.name = nasm_strdup(tline->text);
1964 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001965 searching.plus = false;
1966 searching.nolist = false;
1967 searching.in_progress = 0;
1968 searching.max_depth = 0;
1969 searching.rep_nest = NULL;
1970 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001971 searching.nparam_max = INT_MAX;
1972 tline = expand_smacro(tline->next);
1973 skip_white_(tline);
1974 if (!tline) {
1975 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001976 nasm_nonfatal("`%s' expects a parameter count or nothing",
1977 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 } else {
1979 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001980 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 }
1982 if (tline && tok_is_(tline->next, "-")) {
1983 tline = tline->next->next;
1984 if (tok_is_(tline, "*"))
1985 searching.nparam_max = INT_MAX;
1986 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001987 nasm_nonfatal("`%s' expects a parameter count after `-'",
1988 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001990 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001991 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001992 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001993 searching.nparam_max = searching.nparam_min;
1994 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 }
1996 }
1997 if (tline && tok_is_(tline->next, "+")) {
1998 tline = tline->next;
1999 searching.plus = true;
2000 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002001 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2002 while (mmac) {
2003 if (!strcmp(mmac->name, searching.name) &&
2004 (mmac->nparam_min <= searching.nparam_max
2005 || searching.plus)
2006 && (searching.nparam_min <= mmac->nparam_max
2007 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 found = true;
2009 break;
2010 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002011 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002012 }
2013 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002014 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002015 nasm_free(searching.name);
2016 j = found;
2017 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002018 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002019
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002020 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 needtype = TOK_ID;
2022 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002023 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002024 needtype = TOK_NUMBER;
2025 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002026 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 needtype = TOK_STRING;
2028 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002029
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030iftype:
2031 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002032
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002033 while (tok_type_(t, TOK_WHITESPACE) ||
2034 (needtype == TOK_NUMBER &&
2035 tok_type_(t, TOK_OTHER) &&
2036 (t->text[0] == '-' || t->text[0] == '+') &&
2037 !t->text[1]))
2038 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002039
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002040 j = tok_type_(t, needtype);
2041 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002042
2043 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002044 t = tline = expand_smacro(tline);
2045 while (tok_type_(t, TOK_WHITESPACE))
2046 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002047
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002048 j = false;
2049 if (t) {
2050 t = t->next; /* Skip the actual token */
2051 while (tok_type_(t, TOK_WHITESPACE))
2052 t = t->next;
2053 j = !t; /* Should be nothing left */
2054 }
2055 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002056
H. Peter Anvin134b9462008-02-16 17:01:40 -08002057 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 t = tline = expand_smacro(tline);
2059 while (tok_type_(t, TOK_WHITESPACE))
2060 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002061
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002062 j = !t; /* Should be empty */
2063 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002064
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002065 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002066 pps.tptr = tline = expand_smacro(tline);
2067 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002068 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002069 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 if (!evalresult)
2071 return -1;
2072 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002073 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002074 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002075 nasm_nonfatal("non-constant value given to `%s'",
2076 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002078 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002079 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002080 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002081
H. Peter Anvine2c80182005-01-15 22:15:51 +00002082 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002083 nasm_fatal("preprocessor directive `%s' not yet implemented",
2084 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002085 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002086 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002087
2088 free_tlist(origline);
2089 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002090
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002091fail:
2092 free_tlist(origline);
2093 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002094}
2095
2096/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002097 * Default smacro expansion routine: just returns a copy of the
2098 * expansion list.
2099 */
2100static Token *
2101smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2102{
2103 (void)params;
2104 (void)nparams;
2105
2106 return dup_tlist(s->expansion, NULL);
2107}
2108
2109/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002110 * Emit a macro defintion or undef to the listing file, if
2111 * desired. This is similar to detoken(), but it handles the reverse
2112 * expansion list, does not expand %! or local variable tokens, and
2113 * does some special handling for macro parameters.
2114 */
2115static void
2116list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2117{
2118 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2119 const Token **param_names;
2120 Token *junk_names = NULL;
2121 Token *t;
2122 size_t namelen, size;
2123 unsigned int i;
2124 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002125 char *context_prefix = NULL;
2126 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002127
2128 nasm_newn(param_names, m->nparam);
2129
2130 namelen = strlen(m->name);
2131 size = namelen + 2; /* Include room for space after name + NUL */
2132
2133 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002134 int context_depth = cstk->depth - ctx->depth + 1;
2135 context_prefix =
2136 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2137 ctx->name ? ctx->name : "",
2138 ctx->number, context_depth, "");
2139
2140 context_len = nasm_last_string_len();
2141 memset(context_prefix + context_len - context_depth,
2142 '$', context_depth);
2143 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002144 }
2145
2146 list_for_each(t, m->expansion) {
2147 if (!t->text) {
2148 size++; /* Whitespace, presumably */
2149 } else {
2150 size += t->len;
2151 if (is_smac_param(t->type))
2152 param_names[smac_nparam(t->type)] = t;
2153 }
2154 }
2155
2156 if (m->nparam) {
2157 /*
2158 * Space for ( and either , or ) around each
2159 * parameter, plus an optional =.
2160 */
2161 size += 1 + 2 * m->nparam;
2162 for (i = 0; i < m->nparam; i++) {
2163 if (!param_names[i])
2164 param_names[i] = &unused_arg_name;
2165 size += param_names[i]->len;
2166 }
2167 }
2168
2169 def = nasm_malloc(size);
2170 p = def+size;
2171 *--p = '\0';
2172
2173 list_for_each(t, m->expansion) {
2174 if (!t->text) {
2175 *--p = ' ';
2176 } else {
2177 p -= t->len;
2178 memcpy(p, t->text, t->len);
2179 }
2180 }
2181
2182 *--p = ' ';
2183
2184 if (m->nparam) {
2185 *--p = ')';
2186 for (i = m->nparam; i--;) {
2187 p -= param_names[i]->len;
2188 memcpy(p, param_names[i]->text, param_names[i]->len);
2189 if (m->eval_param && m->eval_param[i])
2190 *--p = '=';
2191 *--p = ',';
2192 }
2193 *p = '('; /* First parameter starts with ( not , */
2194
2195 free_tlist(junk_names);
2196 }
2197
2198 p -= namelen;
2199 memcpy(p, m->name, namelen);
2200
H. Peter Anvin6686de22019-08-10 05:33:14 -07002201 if (context_prefix) {
2202 p -= context_len;
2203 memcpy(p, context_prefix, context_len);
2204 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002205 }
2206
2207 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002208 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002209}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002210
2211/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002212 * Common code for defining an smacro. The tmpl argument, if not NULL,
2213 * contains any macro parameters that aren't explicit arguments;
2214 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002215 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002216static SMacro *define_smacro(const char *mname, bool casesense,
2217 Token *expansion, const SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002218{
2219 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002220 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002221 Context *ctx;
2222 bool defining_alias = false;
2223 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002224
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002225 if (tmpl) {
2226 defining_alias = tmpl->alias;
2227 nparam = tmpl->nparam;
2228 }
2229
2230 while (1) {
2231 ctx = get_ctx(mname, &mname);
2232
2233 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2234 /* Create a new macro */
2235 smtbl = ctx ? &ctx->localmac : &smacros;
2236 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2237 nasm_new(smac);
2238 smac->next = *smhead;
2239 *smhead = smac;
2240 break;
2241 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002242 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002243 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002244 /*
2245 * Some instances of the old code considered this a failure,
2246 * some others didn't. What is the right thing to do here?
2247 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002248 goto fail;
2249 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002250 /*
2251 * We're redefining, so we have to take over an
2252 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002253 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002254 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002255 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002256 break;
2257 } else if (smac->in_progress) {
2258 nasm_nonfatal("macro alias loop");
2259 goto fail;
2260 } else {
2261 /* It is an alias macro; follow the alias link */
2262 SMacro *s;
2263
2264 smac->in_progress = true;
2265 s = define_smacro(smac->expansion->text, casesense,
2266 expansion, tmpl);
2267 smac->in_progress = false;
2268 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002269 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002270 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002271
2272 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002273 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002274 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002275 smac->expand = smacro_expand_default;
2276 if (tmpl) {
2277 smac->nparam = tmpl->nparam;
2278 smac->eval_param = tmpl->eval_param;
2279 smac->alias = tmpl->alias;
2280 if (tmpl->expand)
2281 smac->expand = tmpl->expand;
2282 }
2283 if (list_option('m')) {
2284 static const enum preproc_token op[2][2] = {
2285 { PP_DEFINE, PP_IDEFINE },
2286 { PP_DEFALIAS, PP_IDEFALIAS }
2287 };
2288 list_smacro_def(op[!!smac->alias][casesense], ctx, smac);
2289 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002290 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002291
2292fail:
2293 free_tlist(expansion);
2294 if (tmpl && tmpl->eval_param)
2295 nasm_free(tmpl->eval_param);
2296 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002297}
2298
2299/*
2300 * Undefine an smacro
2301 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002302static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002303{
2304 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002305 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002306 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002307
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002308 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002309 smtbl = ctx ? &ctx->localmac : &smacros;
2310 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002311
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002312 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002313 /*
2314 * We now have a macro name... go hunt for it.
2315 */
2316 sp = smhead;
2317 while ((s = *sp) != NULL) {
2318 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002319 if (s->alias && !undefalias) {
2320 if (s->in_progress) {
2321 nasm_nonfatal("macro alias loop");
2322 } else {
2323 s->in_progress = true;
2324 undef_smacro(s->expansion->text, false);
2325 s->in_progress = false;
2326 }
2327 } else {
2328 if (list_option('m'))
2329 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2330 ctx, s);
2331 *sp = s->next;
2332 free_smacro(s);
2333 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002334 } else {
2335 sp = &s->next;
2336 }
2337 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002338 }
2339}
2340
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002341/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002342 * Parse a mmacro specification.
2343 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002344static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002345{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002346 tline = tline->next;
2347 skip_white_(tline);
2348 tline = expand_id(tline);
2349 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002350 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002351 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002352 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002353
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002354 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002355 def->name = nasm_strdup(tline->text);
2356 def->plus = false;
2357 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002358 def->in_progress = 0;
2359 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002360 def->nparam_min = 0;
2361 def->nparam_max = 0;
2362
H. Peter Anvina26433d2008-07-16 14:40:01 -07002363 tline = expand_smacro(tline->next);
2364 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002365 if (!tok_type_(tline, TOK_NUMBER))
2366 nasm_nonfatal("`%s' expects a parameter count", directive);
2367 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002368 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002369 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002370 tline = tline->next->next;
2371 if (tok_is_(tline, "*")) {
2372 def->nparam_max = INT_MAX;
2373 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002374 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002375 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002376 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002377 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002378 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002379 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002380 }
2381 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002382 }
2383 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002384 tline = tline->next;
2385 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002386 }
2387 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002388 !nasm_stricmp(tline->next->text, ".nolist")) {
2389 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002390 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002391 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002392
H. Peter Anvina26433d2008-07-16 14:40:01 -07002393 /*
2394 * Handle default parameters.
2395 */
2396 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002397 def->dlist = tline->next;
2398 tline->next = NULL;
2399 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002400 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002401 def->dlist = NULL;
2402 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002403 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002404 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002405
H. Peter Anvin89cee572009-07-15 09:16:54 -04002406 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002407 !def->plus) {
2408 /*
2409 *!macro-defaults [on] macros with more default than optional parameters
2410 *! warns when a macro has more default parameters than optional parameters.
2411 *! See \k{mlmacdef} for why might want to disable this warning.
2412 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002413 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002414 "too many default macro parameters in macro `%s'", def->name);
2415 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002416
H. Peter Anvina26433d2008-07-16 14:40:01 -07002417 return true;
2418}
2419
2420
2421/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002422 * Decode a size directive
2423 */
2424static int parse_size(const char *str) {
2425 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002426 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002427 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002428 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002429 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002430}
2431
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002432/*
2433 * Process a preprocessor %pragma directive. Currently there are none.
2434 * Gets passed the token list starting with the "preproc" token from
2435 * "%pragma preproc".
2436 */
2437static void do_pragma_preproc(Token *tline)
2438{
2439 /* Skip to the real stuff */
2440 tline = tline->next;
2441 skip_white_(tline);
2442 if (!tline)
2443 return;
2444
2445 (void)tline; /* Nothing else to do at present */
2446}
2447
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002448static bool is_macro_id(const Token *t)
2449{
2450 return t && (t->type == TOK_ID ||
2451 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2452}
2453
2454static char *get_id(Token **tp, enum preproc_token pp, const char *err)
2455{
2456 char *id;
2457 Token *t = *tp;
2458
2459 t = t->next; /* Skip directive */
2460 skip_white_(t);
2461 t = expand_id(t);
2462
2463 if (!is_macro_id(t)) {
2464 nasm_nonfatal("`%s' expects a %s", pp_directives[pp],
2465 err ? err : "macro identifier");
2466 return NULL;
2467 }
2468
2469 id = t->text;
2470 skip_white_(t);
2471 *tp = t;
2472 return id;
2473}
2474
Ed Beroset3ab3f412002-06-11 03:31:49 +00002475/**
2476 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002477 * Find out if a line contains a preprocessor directive, and deal
2478 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002479 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002480 * If a directive _is_ found, it is the responsibility of this routine
2481 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002482 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002483 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002484 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002485 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002486 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002487 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002488static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002489{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002490 enum preproc_token i;
2491 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002492 bool err;
2493 int nparam;
2494 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002495 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002496 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002497 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002498 char *p, *pp;
2499 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002500 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002501 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002502 Include *inc;
2503 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002504 Cond *cond;
2505 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002506 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002507 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002508 struct tokenval tokval;
2509 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002510 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002511 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002512 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002513 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002514 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002515
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002516 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002517 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002518
H. Peter Anvineba20a72002-04-30 20:53:55 +00002519 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002520 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002521 (tline->text[0] && (tline->text[1] == '%' ||
2522 tline->text[1] == '$' ||
2523 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002525
H. Peter Anvin4169a472007-09-12 01:29:43 +00002526 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002527
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002528 /*
2529 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2530 * since they are known to be buggy at moment, we need to fix them
2531 * in future release (2.09-2.10)
2532 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002533 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002534 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2535 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002536 }
2537
2538 /*
2539 * If we're in a non-emitting branch of a condition construct,
2540 * or walking to the end of an already terminated %rep block,
2541 * we should ignore all directives except for condition
2542 * directives.
2543 */
2544 if (((istk->conds && !emitting(istk->conds->state)) ||
2545 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2546 return NO_DIRECTIVE_FOUND;
2547 }
2548
2549 /*
2550 * If we're defining a macro or reading a %rep block, we should
2551 * ignore all directives except for %macro/%imacro (which nest),
2552 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2553 * If we're in a %rep block, another %rep nests, so should be let through.
2554 */
2555 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2556 i != PP_RMACRO && i != PP_IRMACRO &&
2557 i != PP_ENDMACRO && i != PP_ENDM &&
2558 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2559 return NO_DIRECTIVE_FOUND;
2560 }
2561
2562 if (defining) {
2563 if (i == PP_MACRO || i == PP_IMACRO ||
2564 i == PP_RMACRO || i == PP_IRMACRO) {
2565 nested_mac_count++;
2566 return NO_DIRECTIVE_FOUND;
2567 } else if (nested_mac_count > 0) {
2568 if (i == PP_ENDMACRO) {
2569 nested_mac_count--;
2570 return NO_DIRECTIVE_FOUND;
2571 }
2572 }
2573 if (!defining->name) {
2574 if (i == PP_REP) {
2575 nested_rep_count++;
2576 return NO_DIRECTIVE_FOUND;
2577 } else if (nested_rep_count > 0) {
2578 if (i == PP_ENDREP) {
2579 nested_rep_count--;
2580 return NO_DIRECTIVE_FOUND;
2581 }
2582 }
2583 }
2584 }
2585
H. Peter Anvin8b262472019-02-26 14:00:54 -08002586 dname = pp_directives[i]; /* Directive name, for error messages */
2587 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002588 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002589 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002590 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002592
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002593 case PP_PRAGMA:
2594 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002595 * %pragma namespace options...
2596 *
2597 * The namespace "preproc" is reserved for the preprocessor;
2598 * all other namespaces generate a [pragma] assembly directive.
2599 *
2600 * Invalid %pragmas are ignored and may have different
2601 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002602 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002603 t = tline;
2604 tline = tline->next;
2605 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002606 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002607 while (tok_type_(tline, TOK_WHITESPACE)) {
2608 t = tline;
2609 tline = tline->next;
2610 delete_Token(t);
2611 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002612 if (tok_type_(tline, TOK_ID)) {
2613 if (!nasm_stricmp(tline->text, "preproc")) {
2614 /* Preprocessor pragma */
2615 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002616 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002617 } else {
2618 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002619
2620 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002621 for (t = tline; t->next; t = t->next)
2622 ;
2623 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002624
2625 /* Prepend "[pragma " */
2626 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2627 t = new_Token(t, TOK_ID, "pragma", 6);
2628 t = new_Token(t, TOK_OTHER, "[", 1);
2629 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002630 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002631 }
2632 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002633 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002634
H. Peter Anvine2c80182005-01-15 22:15:51 +00002635 case PP_STACKSIZE:
2636 /* Directive to tell NASM what the default stack size is. The
2637 * default is for a 16-bit stack, and this can be overriden with
2638 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002639 */
2640 tline = tline->next;
2641 if (tline && tline->type == TOK_WHITESPACE)
2642 tline = tline->next;
2643 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002644 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 }
2646 if (nasm_stricmp(tline->text, "flat") == 0) {
2647 /* All subsequent ARG directives are for a 32-bit stack */
2648 StackSize = 4;
2649 StackPointer = "ebp";
2650 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002651 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002652 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2653 /* All subsequent ARG directives are for a 64-bit stack */
2654 StackSize = 8;
2655 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002656 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002657 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 } else if (nasm_stricmp(tline->text, "large") == 0) {
2659 /* All subsequent ARG directives are for a 16-bit stack,
2660 * far function call.
2661 */
2662 StackSize = 2;
2663 StackPointer = "bp";
2664 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002665 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 } else if (nasm_stricmp(tline->text, "small") == 0) {
2667 /* All subsequent ARG directives are for a 16-bit stack,
2668 * far function call. We don't support near functions.
2669 */
2670 StackSize = 2;
2671 StackPointer = "bp";
2672 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002673 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002675 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002677 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002678
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 case PP_ARG:
2680 /* TASM like ARG directive to define arguments to functions, in
2681 * the following form:
2682 *
2683 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2684 */
2685 offset = ArgOffset;
2686 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002687 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002688 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002689
H. Peter Anvine2c80182005-01-15 22:15:51 +00002690 /* Find the argument name */
2691 tline = tline->next;
2692 if (tline && tline->type == TOK_WHITESPACE)
2693 tline = tline->next;
2694 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002695 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002696 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 }
2698 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002699
H. Peter Anvine2c80182005-01-15 22:15:51 +00002700 /* Find the argument size type */
2701 tline = tline->next;
2702 if (!tline || tline->type != TOK_OTHER
2703 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002704 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002705 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 }
2707 tline = tline->next;
2708 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002709 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002710 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002711 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002712
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002714 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 size = parse_size(tt->text);
2717 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002718 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002720 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002721 }
2722 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002723
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002724 /* Round up to even stack slots */
2725 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002726
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 /* Now define the macro for the argument */
2728 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2729 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002730 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002732
H. Peter Anvine2c80182005-01-15 22:15:51 +00002733 /* Move to the next argument in the list */
2734 tline = tline->next;
2735 if (tline && tline->type == TOK_WHITESPACE)
2736 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002737 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002739 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002740
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 case PP_LOCAL:
2742 /* TASM like LOCAL directive to define local variables for a
2743 * function, in the following form:
2744 *
2745 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2746 *
2747 * The '= LocalSize' at the end is ignored by NASM, but is
2748 * required by TASM to define the local parameter size (and used
2749 * by the TASM macro package).
2750 */
2751 offset = LocalOffset;
2752 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002753 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002755
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 /* Find the argument name */
2757 tline = tline->next;
2758 if (tline && tline->type == TOK_WHITESPACE)
2759 tline = tline->next;
2760 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002761 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002762 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 }
2764 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002765
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 /* Find the argument size type */
2767 tline = tline->next;
2768 if (!tline || tline->type != TOK_OTHER
2769 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002770 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002771 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 }
2773 tline = tline->next;
2774 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002775 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002776 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002778
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002780 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002782 size = parse_size(tt->text);
2783 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002784 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002785 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002786 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002787 }
2788 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002789
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002790 /* Round up to even stack slots */
2791 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002792
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002793 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002794
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002795 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2797 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002798 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002799
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 /* Now define the assign to setup the enter_c macro correctly */
2801 snprintf(directive, sizeof(directive),
2802 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002803 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002804
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 /* Move to the next argument in the list */
2806 tline = tline->next;
2807 if (tline && tline->type == TOK_WHITESPACE)
2808 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002809 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002810 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002811 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002812
H. Peter Anvine2c80182005-01-15 22:15:51 +00002813 case PP_CLEAR:
2814 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002815 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002816 free_macros();
2817 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002818 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002819
H. Peter Anvin418ca702008-05-30 10:42:30 -07002820 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002821 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002822 skip_white_(t);
2823 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002824 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002825 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002826 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002827 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002828 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002829 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002830 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002831 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002832 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002833 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002834 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002835
2836 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002837 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002838 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002839
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002840 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002841 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002842 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002843 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002845 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002846 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002847 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002848 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002849 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002850 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002852 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002853 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002854 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002855 (pp_mode == PP_DEPS)
2856 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002857 if (!inc->fp) {
2858 /* -MG given but file not found */
2859 nasm_free(inc);
2860 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002861 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002862 inc->lineno = src_set_linnum(0);
2863 inc->lineinc = 1;
2864 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002865 inc->mstk = NULL;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002866 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002867 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002868 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002869 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002870 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002871
H. Peter Anvind2456592008-06-19 15:04:18 -07002872 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002873 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002874 static macros_t *use_pkg;
2875 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002876
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002877 if (!(mname = get_id(&tline, i, "package name")))
2878 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002879 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002880 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002881 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002882 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002883 use_pkg = nasm_stdmac_find_package(tline->text);
2884 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002885 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002886 else
2887 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002888 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002889 /*
2890 * Not already included, go ahead and include it.
2891 * Treat it as an include file for the purpose of
2892 * producing a listing.
2893 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002894 stdmacpos = use_pkg;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002895 nasm_new(inc);
2896 inc->next = istk;
2897 inc->fname = src_set_fname(NULL);
2898 inc->lineno = src_set_linnum(0);
2899 inc->lineinc = 0;
2900 inc->expansion = NULL;
2901 inc->mstk = NULL;
2902 inc->nolist = !list_option('b') || istk->nolist;
2903 istk = inc;
2904 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002905 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002906 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002907 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002910 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002911 tline = tline->next;
2912 skip_white_(tline);
2913 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002914 if (tline) {
2915 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002916 nasm_nonfatal("`%s' expects a context identifier",
2917 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002918 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002919 }
2920 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002921 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002922 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002923 p = nasm_strdup(tline->text);
2924 } else {
2925 p = NULL; /* Anonymous */
2926 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002927
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002928 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002929 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002930 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002931 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002932 ctx->name = p;
2933 ctx->number = unique++;
2934 cstk = ctx;
2935 } else {
2936 /* %pop or %repl */
2937 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002938 nasm_nonfatal("`%s': context stack is empty",
2939 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002940 } else if (i == PP_POP) {
2941 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002942 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002943 "expected %s",
2944 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002945 else
2946 ctx_pop();
2947 } else {
2948 /* i == PP_REPL */
2949 nasm_free(cstk->name);
2950 cstk->name = p;
2951 p = NULL;
2952 }
2953 nasm_free(p);
2954 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002955 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002956 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002957 severity = ERR_FATAL;
2958 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002960 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002961 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002962 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002963 /*!
2964 *!user [on] %warning directives
2965 *! controls output of \c{%warning} directives (see \k{pperror}).
2966 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002967 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002968 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002969
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002970issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002971 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002972 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002973 tline->next = expand_smacro(tline->next);
2974 tline = tline->next;
2975 skip_white_(tline);
2976 t = tline ? tline->next : NULL;
2977 skip_white_(t);
2978 if (tok_type_(tline, TOK_STRING) && !t) {
2979 /* The line contains only a quoted string */
2980 p = tline->text;
2981 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002982 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002983 } else {
2984 /* Not a quoted string, or more than a quoted string */
2985 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002986 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002987 nasm_free(p);
2988 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002989 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002990 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002991
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002992 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002993 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002994 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002995 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002996 j = if_condition(tline->next, i);
2997 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002998 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002999 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003000 cond = nasm_malloc(sizeof(Cond));
3001 cond->next = istk->conds;
3002 cond->state = j;
3003 istk->conds = cond;
3004 if(istk->mstk)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003005 istk->mstk->condcnt++;
3006 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003007
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003008 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003009 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003010 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003011 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003012 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003013 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003014 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003015
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003016 case COND_DONE:
3017 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003018 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003019
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003020 case COND_ELSE_TRUE:
3021 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003022 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003023 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003024 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003025 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003026
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003027 case COND_IF_FALSE:
3028 /*
3029 * IMPORTANT: In the case of %if, we will already have
3030 * called expand_mmac_params(); however, if we're
3031 * processing an %elif we must have been in a
3032 * non-emitting mode, which would have inhibited
3033 * the normal invocation of expand_mmac_params().
3034 * Therefore, we have to do it explicitly here.
3035 */
3036 j = if_condition(expand_mmac_params(tline->next), i);
3037 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003038 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003039 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
3040 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003042 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003044
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 case PP_ELSE:
3046 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003047 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003048 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003049 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003050 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003051 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003052 case COND_IF_TRUE:
3053 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003054 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003055 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003056
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003057 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003058 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003059
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003060 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003061 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003062 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003063
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003064 case COND_ELSE_TRUE:
3065 case COND_ELSE_FALSE:
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 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003068 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003069 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003070 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003071 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003072
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 case PP_ENDIF:
3074 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003075 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003076 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003077 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003078 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003079 cond = istk->conds;
3080 istk->conds = cond->next;
3081 nasm_free(cond);
3082 if(istk->mstk)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003083 istk->mstk->condcnt--;
3084 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003085
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04003086 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003087 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003088 casesense = false;
3089 /* fall through */
3090 case PP_RMACRO:
3091 case PP_MACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003092 if (defining)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003093 nasm_fatal("`%s': already defining a macro", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003094
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003095 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin322bee02019-08-10 01:38:06 -07003096 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003097 defining->casesense = casesense;
3098 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003099 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003100 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003101 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003102
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003103 src_get(&defining->xline, &defining->fname);
3104
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003105 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3106 while (mmac) {
3107 if (!strcmp(mmac->name, defining->name) &&
3108 (mmac->nparam_min <= defining->nparam_max
3109 || defining->plus)
3110 && (defining->nparam_min <= mmac->nparam_max
3111 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003112 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003113 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003114 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003115 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003116 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003117 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003118 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003119
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 case PP_ENDM:
3121 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003122 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003123 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003124 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003125 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003126 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3127 defining->next = *mmhead;
3128 *mmhead = defining;
3129 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003130 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003131
H. Peter Anvin89cee572009-07-15 09:16:54 -04003132 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003133 /*
3134 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003135 * macro-end marker for a macro with a name. Then we
3136 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003137 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003138 list_for_each(l, istk->expansion)
3139 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003140 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003141
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003142 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003143 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003144 * Remove all conditional entries relative to this
3145 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003146 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003147 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3148 cond = istk->conds;
3149 istk->conds = cond->next;
3150 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003151 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003152 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003153 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003154 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003155 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003156 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003157
H. Peter Anvina26433d2008-07-16 14:40:01 -07003158 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003159 casesense = false;
3160 /* fall through */
3161 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003162 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003163 MMacro **mmac_p;
3164 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003165
H. Peter Anvin8b262472019-02-26 14:00:54 -08003166 spec.casesense = casesense;
3167 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003168 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003170 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3171 while (mmac_p && *mmac_p) {
3172 mmac = *mmac_p;
3173 if (mmac->casesense == spec.casesense &&
3174 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3175 mmac->nparam_min == spec.nparam_min &&
3176 mmac->nparam_max == spec.nparam_max &&
3177 mmac->plus == spec.plus) {
3178 *mmac_p = mmac->next;
3179 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003180 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003181 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003182 }
3183 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003184 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003185 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003186 }
3187
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 case PP_ROTATE:
3189 if (tline->next && tline->next->type == TOK_WHITESPACE)
3190 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003191 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003192 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003193 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003194 return DIRECTIVE_FOUND;
3195 }
3196 t = expand_smacro(tline->next);
3197 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003198 pps.tptr = tline = t;
3199 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003200 tokval.t_type = TOKEN_INVALID;
3201 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003202 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003203 free_tlist(tline);
3204 if (!evalresult)
3205 return DIRECTIVE_FOUND;
3206 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003207 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003208 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003209 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 return DIRECTIVE_FOUND;
3211 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003212 mmac = istk->mstk;
3213 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3214 mmac = mmac->next_active;
3215 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003216 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003217 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003218 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003219 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003220 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003221
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003222 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003223 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003224 rotate += mmac->nparam;
3225
3226 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003227 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003228 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003229
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003231 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003232 do {
3233 tline = tline->next;
3234 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003235
H. Peter Anvine2c80182005-01-15 22:15:51 +00003236 if (tok_type_(tline, TOK_ID) &&
3237 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003238 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003239 do {
3240 tline = tline->next;
3241 } while (tok_type_(tline, TOK_WHITESPACE));
3242 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003243
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003245 pps.tptr = expand_smacro(tline);
3246 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003247 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003248 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003250 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003251 if (!evalresult)
3252 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003253 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003254 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003256 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003257 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003259 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003260 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003261 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3262 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003263 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003264 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003265 /*!
3266 *!negative-rep [on] regative %rep count
3267 *! warns about negative counts given to the \c{%rep}
3268 *! preprocessor directive.
3269 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003270 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003271 "negative `%%rep' count: %"PRId64, count);
3272 count = 0;
3273 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003274 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003275 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003276 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003277 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003278 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003279 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003280 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003281 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003282 defining->nolist = nolist;
3283 defining->in_progress = count;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003284 defining->next_active = istk->mstk;
3285 defining->rep_nest = tmp_defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003286 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003287 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003288
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003290 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003291 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003292 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003293 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003294
H. Peter Anvine2c80182005-01-15 22:15:51 +00003295 /*
3296 * Now we have a "macro" defined - although it has no name
3297 * and we won't be entering it in the hash tables - we must
3298 * push a macro-end marker for it on to istk->expansion.
3299 * After that, it will take care of propagating itself (a
3300 * macro-end marker line for a macro which is really a %rep
3301 * block will cause the macro to be re-expanded, complete
3302 * with another macro-end marker to ensure the process
3303 * continues) until the whole expansion is forcibly removed
3304 * from istk->expansion by a %exitrep.
3305 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003306 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003307 l->next = istk->expansion;
3308 l->finishes = defining;
3309 l->first = NULL;
3310 istk->expansion = l;
3311
3312 istk->mstk = defining;
3313
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003314 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003315 tmp_defining = defining;
3316 defining = defining->rep_nest;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003317 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003318
H. Peter Anvine2c80182005-01-15 22:15:51 +00003319 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003320 /*
3321 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003322 * macro-end marker for a macro with no name. Then we set
3323 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003324 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003325 list_for_each(l, istk->expansion)
3326 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003327 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003328
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003329 if (l)
3330 l->finishes->in_progress = 1;
3331 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003332 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003333 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003334
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003336 case PP_IXDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003337 case PP_IDEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003338 casesense = false;
3339 /* fall through */
3340 case PP_DEFINE:
3341 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003342 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003343 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003344 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003345 bool *eval_params = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003346 SMacro tmpl;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003347
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003348 if (!(mname = get_id(&tline, i, NULL)))
3349 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003350
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003351 nasm_zero(tmpl);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 last = tline;
3353 param_start = tline = tline->next;
3354 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003355
H. Peter Anvine2c80182005-01-15 22:15:51 +00003356 if (tok_is_(tline, "(")) {
3357 /*
3358 * This macro has parameters.
3359 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003360
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 tline = tline->next;
3362 while (1) {
3363 skip_white_(tline);
3364 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003365 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003366 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003367 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003368 if (tok_is_(tline, "=")) {
3369 have_eval_params = true;
3370 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003371 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003372 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003373
3374 /*
3375 * "*" means an unnamed, ignored argument; it can never
3376 * match anything because "*" is not TOK_ID, and so
3377 * none of the expansion entries will be converted
3378 * to parameters.
3379 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003381 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3382 /*
3383 * Empty name; duplicate the termination
3384 * token and use the first copy as a placeholder.
3385 * It will never match anything, since the
3386 * associated text doesn't correspond to a TOK_ID.
3387 */
3388 tline = dup_Token(tline, tline);
3389 } else {
3390 nasm_nonfatal("`%s': parameter identifier expected",
3391 tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003392 goto done;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003393 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003395 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003396 tline = tline->next;
3397 skip_white_(tline);
3398 if (tok_is_(tline, ",")) {
3399 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003400 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003401 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003402 nasm_nonfatal("`)' expected to terminate macro template");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003403 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003404 }
3405 break;
3406 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 }
3408 last = tline;
3409 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003410
3411 if (have_eval_params) {
3412 /* Create evaluated parameters table */
3413 bool is_eval = false;
3414
3415 nasm_newn(eval_params, nparam);
3416 list_for_each(tt, param_start) {
3417 if (is_smac_param(tt->type))
3418 eval_params[smac_nparam(tt->type)] = is_eval;
3419 is_eval = tok_is_(tt, "=");
3420 }
3421 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003422 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003423
H. Peter Anvine2c80182005-01-15 22:15:51 +00003424 if (tok_type_(tline, TOK_WHITESPACE))
3425 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003426 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003427
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003428 if (unlikely(i == PP_DEFALIAS || i == PP_IDEFALIAS)) {
3429 macro_start = tline;
3430 if (!is_macro_id(macro_start)) {
3431 nasm_nonfatal("`%s' expects a macro identifier to alias",
3432 dname);
3433 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003435 tt = macro_start->next;
3436 macro_start->next = NULL;
3437 tline = tline->next;
3438 skip_white_(tline);
3439 if (tline && tline->type) {
3440 nasm_warn(WARN_OTHER,
3441 "trailing garbage after aliasing identifier ignored");
3442 }
3443 free_tlist(tt);
3444 tmpl.alias = true;
3445 } else {
3446 /* Expand the macro definition now for %xdefine and %ixdefine */
3447 if (i == PP_XDEFINE || i == PP_IXDEFINE)
3448 tline = expand_smacro(tline);
3449
3450 macro_start = NULL;
3451 t = tline;
3452 while (t) {
3453 if (t->type == TOK_ID) {
3454 list_for_each(tt, param_start)
3455 if (is_smac_param(tt->type) &&
3456 tt->len == t->len &&
3457 !memcmp(tt->text, t->text, tt->len))
3458 t->type = tt->type;
3459 }
3460 tt = t->next;
3461 t->next = macro_start;
3462 macro_start = t;
3463 t = tt;
3464 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003465 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003466
H. Peter Anvine2c80182005-01-15 22:15:51 +00003467 /*
3468 * Good. We now have a macro name, a parameter count, and a
3469 * token list (in reverse order) for an expansion. We ought
3470 * to be OK just to create an SMacro, store it, and let
3471 * free_tlist have the rest of the line (which we have
3472 * carefully re-terminated after chopping off the expansion
3473 * from the end).
3474 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003475 tmpl.nparam = nparam;
3476 tmpl.eval_param = eval_params;
3477 define_smacro(mname, casesense, macro_start, &tmpl);
3478 break;
3479 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003480
H. Peter Anvine2c80182005-01-15 22:15:51 +00003481 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003482 case PP_UNDEFALIAS:
3483 if (!(mname = get_id(&tline, i, NULL)))
3484 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003485 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003486 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003487
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003488 undef_smacro(mname, i == PP_UNDEFALIAS);
3489 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003490
H. Peter Anvin9e200162008-06-04 17:23:14 -07003491 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003492 casesense = false;
3493 /* fall through */
3494 case PP_DEFSTR:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003495 if (!(mname = get_id(&tline, i, NULL)))
3496 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003497
H. Peter Anvin9e200162008-06-04 17:23:14 -07003498 last = tline;
3499 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003500 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003501
3502 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003503 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003504
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003505 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003506 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003507 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003508
3509 /*
3510 * We now have a macro name, an implicit parameter count of
3511 * zero, and a string token to use as an expansion. Create
3512 * and store an SMacro.
3513 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003514 define_smacro(mname, casesense, macro_start, NULL);
3515 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003516
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003517 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003518 casesense = false;
3519 /* fall through */
3520 case PP_DEFTOK:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003521 if (!(mname = get_id(&tline, i, NULL)))
3522 goto done;
3523
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003524 last = tline;
3525 tline = expand_smacro(tline->next);
3526 last->next = NULL;
3527
3528 t = tline;
3529 while (tok_type_(t, TOK_WHITESPACE))
3530 t = t->next;
3531 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003532 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003533 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003534 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003535 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003536 }
3537
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003538 /*
3539 * Convert the string to a token stream. Note that smacros
3540 * are stored with the token stream reversed, so we have to
3541 * reverse the output of tokenize().
3542 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003543 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003544 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003545
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003546 /*
3547 * We now have a macro name, an implicit parameter count of
3548 * zero, and a numeric token to use as an expansion. Create
3549 * and store an SMacro.
3550 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003551 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003552 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003553 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003554
H. Peter Anvin8b262472019-02-26 14:00:54 -08003555 case PP_IPATHSEARCH:
3556 casesense = false;
3557 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003558 case PP_PATHSEARCH:
3559 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003560 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003561
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003562 if (!(mname = get_id(&tline, i, NULL)))
3563 goto done;
3564
H. Peter Anvin418ca702008-05-30 10:42:30 -07003565 last = tline;
3566 tline = expand_smacro(tline->next);
3567 last->next = NULL;
3568
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003569 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003570 while (tok_type_(t, TOK_WHITESPACE))
3571 t = t->next;
3572
3573 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003574 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003575 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003576 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003577 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003578 }
3579 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003580 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003581 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003582 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003583 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003584
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003585 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003586 if (!found_path)
3587 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003588 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003589
3590 /*
3591 * We now have a macro name, an implicit parameter count of
3592 * zero, and a string token to use as an expansion. Create
3593 * and store an SMacro.
3594 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003595 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003596 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003597 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003598 }
3599
H. Peter Anvin8b262472019-02-26 14:00:54 -08003600 case PP_ISTRLEN:
3601 casesense = false;
3602 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003603 case PP_STRLEN:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003604 if (!(mname = get_id(&tline, i, NULL)))
3605 goto done;
3606
H. Peter Anvine2c80182005-01-15 22:15:51 +00003607 last = tline;
3608 tline = expand_smacro(tline->next);
3609 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003610
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 t = tline;
3612 while (tok_type_(t, TOK_WHITESPACE))
3613 t = t->next;
3614 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003615 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003616 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 free_tlist(tline);
3618 free_tlist(origline);
3619 return DIRECTIVE_FOUND;
3620 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003621
H. Peter Anvin8b262472019-02-26 14:00:54 -08003622 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003623
H. Peter Anvine2c80182005-01-15 22:15:51 +00003624 /*
3625 * We now have a macro name, an implicit parameter count of
3626 * zero, and a numeric token to use as an expansion. Create
3627 * and store an SMacro.
3628 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003629 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 free_tlist(tline);
3631 free_tlist(origline);
3632 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003633
H. Peter Anvin8b262472019-02-26 14:00:54 -08003634 case PP_ISTRCAT:
3635 casesense = false;
3636 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003637 case PP_STRCAT:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003638 if (!(mname = get_id(&tline, i, NULL)))
3639 goto done;
3640
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003641 last = tline;
3642 tline = expand_smacro(tline->next);
3643 last->next = NULL;
3644
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003645 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003646 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003647 switch (t->type) {
3648 case TOK_WHITESPACE:
3649 break;
3650 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003651 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003652 break;
3653 case TOK_OTHER:
3654 if (!strcmp(t->text, ",")) /* permit comma separators */
3655 break;
3656 /* else fall through */
3657 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003658 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003659 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003660 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003661 }
3662 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003663
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003664 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003665 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003666 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003667 memcpy(p, t->text, t->len);
3668 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003669 }
3670 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003671
3672 /*
3673 * We now have a macro name, an implicit parameter count of
3674 * zero, and a numeric token to use as an expansion. Create
3675 * and store an SMacro.
3676 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003677 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003678 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003679 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003680 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003681 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003682
H. Peter Anvin8b262472019-02-26 14:00:54 -08003683 case PP_ISUBSTR:
3684 casesense = false;
3685 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003687 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003688 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003689 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003690
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003691 if (!(mname = get_id(&tline, i, NULL)))
3692 goto done;
3693
H. Peter Anvine2c80182005-01-15 22:15:51 +00003694 last = tline;
3695 tline = expand_smacro(tline->next);
3696 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003697
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003698 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003699 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 while (tok_type_(t, TOK_WHITESPACE))
3701 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003702
H. Peter Anvine2c80182005-01-15 22:15:51 +00003703 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003704 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003705 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003706 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003707 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003708 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003709
H. Peter Anvin8b262472019-02-26 14:00:54 -08003710 pps.tptr = t->next;
3711 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003712 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003713 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003714 if (!evalresult) {
3715 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003716 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003717 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003718 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003719 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003720 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003721 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003722 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003723
H. Peter Anvin8b262472019-02-26 14:00:54 -08003724 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3725 pps.tptr = pps.tptr->next;
3726 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003727 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003728 } else {
3729 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003730 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003731 if (!evalresult) {
3732 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003733 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003734 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003735 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003736 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003737 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003738 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003739 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003740 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003741
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003742 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003743
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003744 /* make start and count being in range */
3745 if (start < 0)
3746 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003747 if (count < 0)
3748 count = len + count + 1 - start;
3749 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003750 count = len - start;
3751 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003752 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003753
H. Peter Anvin8b262472019-02-26 14:00:54 -08003754 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003755 macro_start->len = count;
3756 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3757 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003758
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 /*
3760 * We now have a macro name, an implicit parameter count of
3761 * zero, and a numeric token to use as an expansion. Create
3762 * and store an SMacro.
3763 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003764 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003765 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003766 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003767 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003768
H. Peter Anvine2c80182005-01-15 22:15:51 +00003769 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003770 casesense = false;
3771 /* fall through */
3772 case PP_ASSIGN:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003773 if (!(mname = get_id(&tline, i, NULL)))
3774 goto done;
3775
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 last = tline;
3777 tline = expand_smacro(tline->next);
3778 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003779
H. Peter Anvin8b262472019-02-26 14:00:54 -08003780 pps.tptr = tline;
3781 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003782 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003783 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003785 if (!evalresult)
3786 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003787
H. Peter Anvine2c80182005-01-15 22:15:51 +00003788 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003789 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003790
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003792 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003793 free_tlist(origline);
3794 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003795 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003796
H. Peter Anvin8b262472019-02-26 14:00:54 -08003797 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003798
H. Peter Anvine2c80182005-01-15 22:15:51 +00003799 /*
3800 * We now have a macro name, an implicit parameter count of
3801 * zero, and a numeric token to use as an expansion. Create
3802 * and store an SMacro.
3803 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003804 define_smacro(mname, casesense, macro_start, NULL);
3805 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003806
H. Peter Anvine2c80182005-01-15 22:15:51 +00003807 case PP_LINE:
3808 /*
3809 * Syntax is `%line nnn[+mmm] [filename]'
3810 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003811 if (unlikely(pp_noline))
3812 goto done;
3813
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 tline = tline->next;
3815 skip_white_(tline);
3816 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003817 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003818 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003819 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003820 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003821 m = 1;
3822 tline = tline->next;
3823 if (tok_is_(tline, "+")) {
3824 tline = tline->next;
3825 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003826 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003827 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003828 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003829 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003830 tline = tline->next;
3831 }
3832 skip_white_(tline);
3833 src_set_linnum(k);
3834 istk->lineinc = m;
3835 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003836 char *fname = detoken(tline, false);
3837 src_set_fname(fname);
3838 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003839 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003840 break;
3841 }
3842
3843done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003844 free_tlist(origline);
3845 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003846}
3847
3848/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003849 * Ensure that a macro parameter contains a condition code and
3850 * nothing else. Return the condition code index if so, or -1
3851 * otherwise.
3852 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003853static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003854{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003855 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003856
H. Peter Anvin25a99342007-09-22 17:45:45 -07003857 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003858 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003859
H. Peter Anvineba20a72002-04-30 20:53:55 +00003860 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003861 if (!t)
3862 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003863 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003864 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003865 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003866 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003867 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003868 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003869
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003870 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003871}
3872
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003873/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003874 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003875 * pasting, if @handle_explicit passed then explicit pasting
3876 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003877 * The @m array can contain a series of token types which are
3878 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003879 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003880static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003881 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003882{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003883 Token *tok, *next, **prev_next, **prev_nonspace;
3884 bool pasted = false;
3885 char *buf, *p;
3886 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003887
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003888 /*
3889 * The last token before pasting. We need it
3890 * to be able to connect new handled tokens.
3891 * In other words if there were a tokens stream
3892 *
3893 * A -> B -> C -> D
3894 *
3895 * and we've joined tokens B and C, the resulting
3896 * stream should be
3897 *
3898 * A -> BC -> D
3899 */
3900 tok = *head;
3901 prev_next = NULL;
3902
3903 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3904 prev_nonspace = head;
3905 else
3906 prev_nonspace = NULL;
3907
3908 while (tok && (next = tok->next)) {
3909
3910 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003911 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003912 /* Zap redundant whitespaces */
3913 while (tok_type_(next, TOK_WHITESPACE))
3914 next = delete_Token(next);
3915 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003916 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003917
3918 case TOK_PASTE:
3919 /* Explicit pasting */
3920 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003921 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003922 next = delete_Token(tok);
3923
3924 while (tok_type_(next, TOK_WHITESPACE))
3925 next = delete_Token(next);
3926
3927 if (!pasted)
3928 pasted = true;
3929
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003930 /* Left pasting token is start of line */
3931 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003932 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003933
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003934 /*
3935 * No ending token, this might happen in two
3936 * cases
3937 *
3938 * 1) There indeed no right token at all
3939 * 2) There is a bare "%define ID" statement,
3940 * and @ID does expand to whitespace.
3941 *
3942 * So technically we need to do a grammar analysis
3943 * in another stage of parsing, but for now lets don't
3944 * change the behaviour people used to. Simply allow
3945 * whitespace after paste token.
3946 */
3947 if (!next) {
3948 /*
3949 * Zap ending space tokens and that's all.
3950 */
3951 tok = (*prev_nonspace)->next;
3952 while (tok_type_(tok, TOK_WHITESPACE))
3953 tok = delete_Token(tok);
3954 tok = *prev_nonspace;
3955 tok->next = NULL;
3956 break;
3957 }
3958
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003959 tok = *prev_nonspace;
3960 while (tok_type_(tok, TOK_WHITESPACE))
3961 tok = delete_Token(tok);
3962 len = strlen(tok->text);
3963 len += strlen(next->text);
3964
3965 p = buf = nasm_malloc(len + 1);
3966 strcpy(p, tok->text);
3967 p = strchr(p, '\0');
3968 strcpy(p, next->text);
3969
3970 delete_Token(tok);
3971
3972 tok = tokenize(buf);
3973 nasm_free(buf);
3974
3975 *prev_nonspace = tok;
3976 while (tok && tok->next)
3977 tok = tok->next;
3978
3979 tok->next = delete_Token(next);
3980
3981 /* Restart from pasted tokens head */
3982 tok = *prev_nonspace;
3983 break;
3984
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003985 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003986 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003987 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003988 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3989 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003990
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003991 len = 0;
3992 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3993 len += strlen(next->text);
3994 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003995 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003996
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003997 /* No match or no text to process */
3998 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003999 break;
4000
4001 len += strlen(tok->text);
4002 p = buf = nasm_malloc(len + 1);
4003
Adam Majer1a069432017-07-25 11:12:35 +02004004 strcpy(p, tok->text);
4005 p = strchr(p, '\0');
4006 tok = delete_Token(tok);
4007
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004008 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004009 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4010 strcpy(p, tok->text);
4011 p = strchr(p, '\0');
4012 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004013 tok = delete_Token(tok);
4014 }
4015
4016 tok = tokenize(buf);
4017 nasm_free(buf);
4018
4019 if (prev_next)
4020 *prev_next = tok;
4021 else
4022 *head = tok;
4023
4024 /*
4025 * Connect pasted into original stream,
4026 * ie A -> new-tokens -> B
4027 */
4028 while (tok && tok->next)
4029 tok = tok->next;
4030 tok->next = next;
4031
4032 if (!pasted)
4033 pasted = true;
4034
4035 /* Restart from pasted tokens head */
4036 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004037 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004038
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004039 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004040 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004041
4042 prev_next = &tok->next;
4043
4044 if (tok->next &&
4045 !tok_type_(tok->next, TOK_WHITESPACE) &&
4046 !tok_type_(tok->next, TOK_PASTE))
4047 prev_nonspace = prev_next;
4048
4049 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004050 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004051
4052 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004053}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004054
4055/*
4056 * expands to a list of tokens from %{x:y}
4057 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004058static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004059{
4060 Token *t = tline, **tt, *tm, *head;
4061 char *pos;
4062 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004063
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004064 pos = strchr(tline->text, ':');
4065 nasm_assert(pos);
4066
4067 lst = atoi(pos + 1);
4068 fst = atoi(tline->text + 1);
4069
4070 /*
4071 * only macros params are accounted so
4072 * if someone passes %0 -- we reject such
4073 * value(s)
4074 */
4075 if (lst == 0 || fst == 0)
4076 goto err;
4077
4078 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004079 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4080 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004081 goto err;
4082
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004083 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4084 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004085
4086 /* counted from zero */
4087 fst--, lst--;
4088
4089 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004090 * It will be at least one token. Note we
4091 * need to scan params until separator, otherwise
4092 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004093 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004094 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004095 if (!tm)
4096 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004097 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004098 tt = &head->next, tm = tm->next;
4099 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004100 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004101 *tt = t, tt = &t->next, tm = tm->next;
4102 }
4103
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004104 if (fst < lst) {
4105 for (i = fst + 1; i <= lst; i++) {
4106 t = new_Token(NULL, TOK_OTHER, ",", 0);
4107 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004108 j = (i + mac->rotate) % mac->nparam;
4109 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004110 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004111 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004112 *tt = t, tt = &t->next, tm = tm->next;
4113 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004114 }
4115 } else {
4116 for (i = fst - 1; i >= lst; i--) {
4117 t = new_Token(NULL, TOK_OTHER, ",", 0);
4118 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004119 j = (i + mac->rotate) % mac->nparam;
4120 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004121 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004122 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004123 *tt = t, tt = &t->next, tm = tm->next;
4124 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004125 }
4126 }
4127
4128 *last = tt;
4129 return head;
4130
4131err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004132 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004133 &tline->text[1]);
4134 return tline;
4135}
4136
H. Peter Anvin76690a12002-04-30 20:52:49 +00004137/*
4138 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004139 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004140 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004141 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004143{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004144 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004145 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004146 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004147
4148 tail = &thead;
4149 thead = NULL;
4150
H. Peter Anvine2c80182005-01-15 22:15:51 +00004151 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004152 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004153 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4154 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4155 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004156 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004157 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004158 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004159 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004160 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004161 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004162
H. Peter Anvine2c80182005-01-15 22:15:51 +00004163 t = tline;
4164 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004165
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004166 mac = istk->mstk;
4167 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4168 mac = mac->next_active;
4169 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004170 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004171 } else {
4172 pos = strchr(t->text, ':');
4173 if (!pos) {
4174 switch (t->text[1]) {
4175 /*
4176 * We have to make a substitution of one of the
4177 * forms %1, %-1, %+1, %%foo, %0.
4178 */
4179 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004180 type = TOK_NUMBER;
4181 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4182 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004183 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004184 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004185 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004186 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004187 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004188 text = nasm_strcat(tmpbuf, t->text + 2);
4189 break;
4190 case '-':
4191 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004192 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004193 tt = NULL;
4194 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004195 if (mac->nparam > 1)
4196 n = (n + mac->rotate) % mac->nparam;
4197 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004198 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004199 cc = find_cc(tt);
4200 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004201 nasm_nonfatal("macro parameter %d is not a condition code",
4202 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004203 text = NULL;
4204 } else {
4205 type = TOK_ID;
4206 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004207 nasm_nonfatal("condition code `%s' is not invertible",
4208 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004209 text = NULL;
4210 } else
4211 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4212 }
4213 break;
4214 case '+':
4215 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004216 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004217 tt = NULL;
4218 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004219 if (mac->nparam > 1)
4220 n = (n + mac->rotate) % mac->nparam;
4221 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004222 }
4223 cc = find_cc(tt);
4224 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004225 nasm_nonfatal("macro parameter %d is not a condition code",
4226 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004227 text = NULL;
4228 } else {
4229 type = TOK_ID;
4230 text = nasm_strdup(conditions[cc]);
4231 }
4232 break;
4233 default:
4234 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004235 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004236 tt = NULL;
4237 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004238 if (mac->nparam > 1)
4239 n = (n + mac->rotate) % mac->nparam;
4240 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004241 }
4242 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004243 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004244 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004245 tail = &(*tail)->next;
4246 tt = tt->next;
4247 }
4248 }
4249 text = NULL; /* we've done it here */
4250 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004251 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004252 } else {
4253 /*
4254 * seems we have a parameters range here
4255 */
4256 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004257 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004258 if (head != t) {
4259 *tail = head;
4260 *last = tline;
4261 tline = head;
4262 text = NULL;
4263 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004264 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004265 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004266 if (!text) {
4267 delete_Token(t);
4268 } else {
4269 *tail = t;
4270 tail = &t->next;
4271 t->type = type;
4272 nasm_free(t->text);
4273 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004274 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004275 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004276 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004278 } else if (tline->type == TOK_INDIRECT) {
4279 t = tline;
4280 tline = tline->next;
4281 tt = tokenize(t->text);
4282 tt = expand_mmac_params(tt);
4283 tt = expand_smacro(tt);
4284 *tail = tt;
4285 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004286 tail = &tt->next;
4287 tt = tt->next;
4288 }
4289 delete_Token(t);
4290 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004291 } else {
4292 t = *tail = tline;
4293 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004294 tail = &t->next;
4295 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004296 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004297 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004298
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004299 if (changed) {
4300 const struct tokseq_match t[] = {
4301 {
4302 PP_CONCAT_MASK(TOK_ID) |
4303 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4304 PP_CONCAT_MASK(TOK_ID) |
4305 PP_CONCAT_MASK(TOK_NUMBER) |
4306 PP_CONCAT_MASK(TOK_FLOAT) |
4307 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4308 },
4309 {
4310 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4311 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4312 }
4313 };
4314 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4315 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004316
H. Peter Anvin76690a12002-04-30 20:52:49 +00004317 return thead;
4318}
4319
H. Peter Anvin322bee02019-08-10 01:38:06 -07004320static Token *expand_smacro_noreset(Token * tline);
4321static struct {
4322 int64_t tokens;
4323 int64_t levels;
4324 bool triggered;
4325} smacro_deadman;
4326
H. Peter Anvin76690a12002-04-30 20:52:49 +00004327/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004328 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004329 * a macro at all, it is simply copied to the output and the pointer
4330 * advanced. tpp should be a pointer to a pointer (usually the next
4331 * pointer of the previous token) to the first token. **tpp is updated
4332 * to point to the last token of the expansion, and *tpp updated to
4333 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004334 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004335 * If the expansion is empty, *tpp will be unchanged but **tpp will
4336 * be advanced past the macro call.
4337 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004338 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004339 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004340static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004341{
4342 Token **params = NULL;
4343 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004344 Token *tline = **tpp;
4345 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004346 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004347 unsigned int i;
4348 Token *t, *tup, *ttail;
4349 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004350
4351 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004352 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004353
4354 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004355
H. Peter Anvin322bee02019-08-10 01:38:06 -07004356 smacro_deadman.tokens--;
4357 smacro_deadman.levels--;
4358
4359 if (unlikely(smacro_deadman.tokens < 0 || smacro_deadman.levels < 0)) {
4360 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004361 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004362 smacro_deadman.triggered = true;
4363 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004364 goto not_a_macro;
4365 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004366 head = (SMacro *)hash_findix(&smacros, mname);
4367 } else if (tline->type == TOK_PREPROC_ID) {
4368 Context *ctx = get_ctx(mname, &mname);
4369 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4370 } else {
4371 goto not_a_macro;
4372 }
4373
4374 /*
4375 * We've hit an identifier of some sort. First check whether the
4376 * identifier is a single-line macro at all, then think about
4377 * checking for parameters if necessary.
4378 */
4379 list_for_each(m, head) {
4380 if (!mstrcmp(m->name, mname, m->casesense))
4381 break;
4382 }
4383
4384 if (!m) {
4385 goto not_a_macro;
4386 }
4387
4388 /* Parse parameters, if applicable */
4389
4390 params = NULL;
4391 nparam = 0;
4392
4393 if (m->nparam == 0) {
4394 /*
4395 * Simple case: the macro is parameterless.
4396 * Nothing to parse; the expansion code will
4397 * drop the macro name token.
4398 */
4399 } else {
4400 /*
4401 * Complicated case: at least one macro with this name
4402 * exists and takes parameters. We must find the
4403 * parameters in the call, count them, find the SMacro
4404 * that corresponds to that form of the macro call, and
4405 * substitute for the parameters when we expand. What a
4406 * pain.
4407 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004408 Token **phead, **pep;
4409 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004410 int white = 0;
4411 int brackets = 0;
4412 bool bracketed = false;
4413 bool bad_bracket = false;
4414 unsigned int sparam = PARAM_DELTA;
4415
4416 tline = tline->next;
4417
4418 while (tok_type_(tline, TOK_WHITESPACE)) {
4419 tline = tline->next;
4420 }
4421 if (!tok_is_(tline, "(")) {
4422 /*
4423 * This macro wasn't called with parameters: ignore
4424 * the call. (Behaviour borrowed from gnu cpp.)
4425 */
4426 goto not_a_macro;
4427 }
4428
4429 paren = 1;
4430 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004431 nasm_newn(params, sparam);
4432 phead = pep = &params[0];
4433 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004434
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004435 while (paren) {
4436 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004437 char ch;
4438
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004439 tline = tline->next;
4440
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004441 if (!tline) {
4442 nasm_nonfatal("macro call expects terminating `)'");
4443 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004444 }
4445
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004446 ch = 0;
4447 skip = false;
4448
4449 switch (tline->type) {
4450 case TOK_OTHER:
4451 if (!tline->text[1])
4452 ch = tline->text[0];
4453 break;
4454
4455 case TOK_WHITESPACE:
4456 if (brackets || *phead)
4457 white++; /* Keep interior whitespace */
4458 skip = true;
4459 break;
4460
4461 default:
4462 break;
4463 }
4464
4465 switch (ch) {
4466 case ',':
4467 if (!brackets) {
4468 if (++nparam >= sparam) {
4469 sparam += PARAM_DELTA;
4470 params = nasm_realloc(params, sparam * sizeof *params);
4471 }
4472 pep = &params[nparam];
4473 *pep = NULL;
4474 bracketed = false;
4475 skip = true;
4476 }
4477 break;
4478
4479 case '{':
4480 if (!bracketed) {
4481 bracketed = !*phead;
4482 skip = true;
4483 }
4484 brackets++;
4485 break;
4486
4487 case '}':
4488 if (brackets > 0) {
4489 if (!--brackets)
4490 skip = bracketed;
4491 }
4492 break;
4493
4494 case '(':
4495 if (!brackets)
4496 paren++;
4497 break;
4498
4499 case ')':
4500 if (!brackets) {
4501 paren--;
4502 if (!paren) {
4503 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004504 /* Count the final argument */
4505 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004506 }
4507 }
4508 break;
4509
4510 default:
4511 break; /* Normal token */
4512 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004513
4514 if (!skip) {
4515 Token *t;
4516
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004517 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004518
4519 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004520 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004521 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004522 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004523 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004524 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004525 pep = &t->next;
4526 white = 0;
4527 }
4528 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004529
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004530 /*
4531 * Look for a macro matching in both name and parameter count.
4532 * We already know any matches cannot be anywhere before the
4533 * current position of "m", so there is no reason to
4534 * backtrack.
4535 */
4536 while (1) {
4537 if (!m) {
4538 /*!
4539 *!macro-params-single [on] single-line macro calls with wrong parameter count
4540 *! warns about \i{single-line macros} being invoked
4541 *! with the wrong number of parameters.
4542 */
4543 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4544 "single-line macro `%s' exists, "
4545 "but not taking %d parameter%s",
4546 mname, nparam, (nparam == 1) ? "" : "s");
4547 goto not_a_macro;
4548 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004549
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004550 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4551 break; /* It's good */
4552
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004553 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004554 }
4555 }
4556
4557 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004558 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004559
4560 /* Expand each parameter */
4561 m->in_progress = true;
4562
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004563 if (m->eval_param) {
4564 for (i = 0; i < nparam; i++) {
4565 if (m->eval_param[i]) {
4566 /* Evaluate this parameter as a number */
4567 struct ppscan pps;
4568 struct tokenval tokval;
4569 expr *evalresult;
4570 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004571
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004572 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4573 pps.ntokens = -1;
4574 tokval.t_type = TOKEN_INVALID;
4575 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004576
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004577 free_tlist(eval_param);
4578 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004579
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004580 if (!evalresult) {
4581 /* Nothing meaningful to do */
4582 } else if (tokval.t_type) {
4583 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4584 } else if (!is_simple(evalresult)) {
4585 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4586 } else {
4587 params[i] = make_tok_num(reloc_value(evalresult));
4588 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004589 }
4590 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004591 }
4592
4593 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004594 tline = tline->next; /* Remove the macro call from the input */
4595 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004596
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004597 /* Note: we own the expansion this returns. */
4598 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004599
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004600 tup = NULL;
4601 ttail = NULL; /* Pointer to the last token of the expansion */
4602 while (t) {
4603 enum pp_token_type type = t->type;
4604 Token *tnext = t->next;
4605 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004606
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004607 switch (type) {
4608 case TOK_PREPROC_Q:
4609 case TOK_PREPROC_QQ:
4610 t->type = TOK_ID;
4611 nasm_free(t->text);
4612 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4613 t->len = nasm_last_string_len();
4614 t->next = tline;
4615 break;
4616
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004617 case TOK_ID:
4618 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004619 /*
4620 * Chain this into the target line *before* expanding,
4621 * that way we pick up any arguments to the new macro call,
4622 * if applicable.
4623 */
4624 t->next = tline;
4625 tp = &t;
4626 expand_one_smacro(&tp);
4627 if (t == tline)
4628 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004629 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004630
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004631 default:
4632 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004633 unsigned int param = smac_nparam(t->type);
4634 nasm_assert(!tup && param < nparam);
4635 delete_Token(t);
4636 t = NULL;
4637 tup = tnext;
4638 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004639 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004640 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004641 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004642 }
4643
4644 if (t) {
4645 tline = t;
4646 if (!ttail)
4647 ttail = t;
4648 }
4649
4650 if (tnext) {
4651 t = tnext;
4652 } else {
4653 t = tup;
4654 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004655 }
4656 }
4657
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004658 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004659 if (ttail)
4660 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004661
4662 m->in_progress = false;
4663
4664 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004665 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004666 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004667
4668 /*
4669 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004670 * and then advance to the next input token. Note that this is
4671 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004672 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004673not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004674 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004675 m = NULL;
4676done:
4677 smacro_deadman.levels++;
4678 if (unlikely(params))
4679 free_tlist_array(params, nparam);
4680 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004681}
4682
4683/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004684 * Expand all single-line macro calls made in the given line.
4685 * Return the expanded version of the line. The original is deemed
4686 * to be destroyed in the process. (In reality we'll just move
4687 * Tokens from input to output a lot of the time, rather than
4688 * actually bothering to destroy and replicate.)
4689 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004690static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004691{
H. Peter Anvin322bee02019-08-10 01:38:06 -07004692 smacro_deadman.tokens = nasm_limit[LIMIT_MACRO_TOKENS];
4693 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4694 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004695 return expand_smacro_noreset(tline);
4696}
4697
4698static Token *expand_smacro_noreset(Token * tline)
4699{
4700 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004701 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004702 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004703
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004704 /*
4705 * Trick: we should avoid changing the start token pointer since it can
4706 * be contained in "next" field of other token. Because of this
4707 * we allocate a copy of first token and work with it; at the end of
4708 * routine we copy it back
4709 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004710 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004711 tline = new_Token(org_tline->next, org_tline->type,
4712 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004713 nasm_free(org_tline->text);
4714 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004715 }
4716
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004717
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004718 /*
4719 * Pretend that we always end up doing expansion on the first pass;
4720 * that way %+ get processed. However, if we process %+ before the
4721 * first pass, we end up with things like MACRO %+ TAIL trying to
4722 * look up the macro "MACROTAIL", which we don't want.
4723 */
4724 expanded = true;
4725 thead = tline;
4726 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004727 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004728 {
4729 PP_CONCAT_MASK(TOK_ID) |
4730 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4731 PP_CONCAT_MASK(TOK_ID) |
4732 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4733 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4734 }
4735 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004736
4737 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004738 while ((t = *tail)) /* main token loop */
4739 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004740
4741 if (!expanded) {
4742 tline = thead;
4743 break; /* Done! */
4744 }
4745
4746 /*
4747 * Now scan the entire line and look for successive TOK_IDs
4748 * that resulted after expansion (they can't be produced by
4749 * tokenize()). The successive TOK_IDs should be concatenated.
4750 * Also we look for %+ tokens and concatenate the tokens
4751 * before and after them (without white spaces in between).
4752 */
4753 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004754
4755 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004756 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004757
H. Peter Anvine2c80182005-01-15 22:15:51 +00004758 if (org_tline) {
4759 if (thead) {
4760 *org_tline = *thead;
4761 /* since we just gave text to org_line, don't free it */
4762 thead->text = NULL;
4763 delete_Token(thead);
4764 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004765 /*
4766 * The expression expanded to empty line;
4767 * we can't return NULL because of the "trick" above.
4768 * Just set the line to a single WHITESPACE token.
4769 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004770 memset(org_tline, 0, sizeof(*org_tline));
4771 org_tline->text = NULL;
4772 org_tline->type = TOK_WHITESPACE;
4773 }
4774 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004775 }
4776
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004777 return thead;
4778}
4779
4780/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004781 * Similar to expand_smacro but used exclusively with macro identifiers
4782 * right before they are fetched in. The reason is that there can be
4783 * identifiers consisting of several subparts. We consider that if there
4784 * are more than one element forming the name, user wants a expansion,
4785 * otherwise it will be left as-is. Example:
4786 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004787 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004788 *
4789 * the identifier %$abc will be left as-is so that the handler for %define
4790 * will suck it and define the corresponding value. Other case:
4791 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004792 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004793 *
4794 * In this case user wants name to be expanded *before* %define starts
4795 * working, so we'll expand %$abc into something (if it has a value;
4796 * otherwise it will be left as-is) then concatenate all successive
4797 * PP_IDs into one.
4798 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004799static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004800{
4801 Token *cur, *oldnext = NULL;
4802
H. Peter Anvin734b1882002-04-30 21:01:08 +00004803 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004804 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004805
4806 cur = tline;
4807 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004808 (cur->next->type == TOK_ID ||
4809 cur->next->type == TOK_PREPROC_ID
4810 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004811 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004812
4813 /* If identifier consists of just one token, don't expand */
4814 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004815 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004816
H. Peter Anvine2c80182005-01-15 22:15:51 +00004817 if (cur) {
4818 oldnext = cur->next; /* Detach the tail past identifier */
4819 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004820 }
4821
H. Peter Anvin734b1882002-04-30 21:01:08 +00004822 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004823
H. Peter Anvine2c80182005-01-15 22:15:51 +00004824 if (cur) {
4825 /* expand_smacro possibly changhed tline; re-scan for EOL */
4826 cur = tline;
4827 while (cur && cur->next)
4828 cur = cur->next;
4829 if (cur)
4830 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004831 }
4832
4833 return tline;
4834}
4835
4836/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004837 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004838 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004839 * to check for an initial label - that's taken care of in
4840 * expand_mmacro - but must check numbers of parameters. Guaranteed
4841 * to be called with tline->type == TOK_ID, so the putative macro
4842 * name is easy to find.
4843 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004844static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004845{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004846 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004847 Token **params;
4848 int nparam;
4849
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004850 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004851
4852 /*
4853 * Efficiency: first we see if any macro exists with the given
4854 * name. If not, we can return NULL immediately. _Then_ we
4855 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004856 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004857 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004858 list_for_each(m, head)
4859 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004860 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004861 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004862 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004863
4864 /*
4865 * OK, we have a potential macro. Count and demarcate the
4866 * parameters.
4867 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004868 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004869
4870 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004872 * structure that handles this number.
4873 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004874 while (m) {
4875 if (m->nparam_min <= nparam
4876 && (m->plus || nparam <= m->nparam_max)) {
4877 /*
4878 * This one is right. Just check if cycle removal
4879 * prohibits us using it before we actually celebrate...
4880 */
4881 if (m->in_progress > m->max_depth) {
4882 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004883 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004884 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004885 }
4886 nasm_free(params);
4887 return NULL;
4888 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004889 /*
4890 * It's right, and we can use it. Add its default
4891 * parameters to the end of our list if necessary.
4892 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004893 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004894 params =
4895 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004896 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004897 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004898 while (nparam < m->nparam_min + m->ndefs) {
4899 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004900 nparam++;
4901 }
4902 }
4903 /*
4904 * If we've gone over the maximum parameter count (and
4905 * we're in Plus mode), ignore parameters beyond
4906 * nparam_max.
4907 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004908 if (m->plus && nparam > m->nparam_max)
4909 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004910 /*
4911 * Then terminate the parameter list, and leave.
4912 */
4913 if (!params) { /* need this special case */
4914 params = nasm_malloc(sizeof(*params));
4915 nparam = 0;
4916 }
4917 params[nparam] = NULL;
4918 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004919 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004920 }
4921 /*
4922 * This one wasn't right: look for the next one with the
4923 * same name.
4924 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004925 list_for_each(m, m->next)
4926 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004927 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004928 }
4929
4930 /*
4931 * After all that, we didn't find one with the right number of
4932 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004933 *!
4934 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4935 *! warns about \i{multi-line macros} being invoked
4936 *! with the wrong number of parameters. See \k{mlmacover} for an
4937 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004938 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004939 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4940 "multi-line macro `%s' exists, but not taking %d parameter%s",
4941 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004942 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004943 return NULL;
4944}
4945
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004946
4947/*
4948 * Save MMacro invocation specific fields in
4949 * preparation for a recursive macro expansion
4950 */
4951static void push_mmacro(MMacro *m)
4952{
4953 MMacroInvocation *i;
4954
4955 i = nasm_malloc(sizeof(MMacroInvocation));
4956 i->prev = m->prev;
4957 i->params = m->params;
4958 i->iline = m->iline;
4959 i->nparam = m->nparam;
4960 i->rotate = m->rotate;
4961 i->paramlen = m->paramlen;
4962 i->unique = m->unique;
4963 i->condcnt = m->condcnt;
4964 m->prev = i;
4965}
4966
4967
4968/*
4969 * Restore MMacro invocation specific fields that were
4970 * saved during a previous recursive macro expansion
4971 */
4972static void pop_mmacro(MMacro *m)
4973{
4974 MMacroInvocation *i;
4975
4976 if (m->prev) {
4977 i = m->prev;
4978 m->prev = i->prev;
4979 m->params = i->params;
4980 m->iline = i->iline;
4981 m->nparam = i->nparam;
4982 m->rotate = i->rotate;
4983 m->paramlen = i->paramlen;
4984 m->unique = i->unique;
4985 m->condcnt = i->condcnt;
4986 nasm_free(i);
4987 }
4988}
4989
4990
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004991/*
4992 * Expand the multi-line macro call made by the given line, if
4993 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004994 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004995 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004996static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004997{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004998 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004999 Token *label = NULL;
5000 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005001 Token **params, *t, *tt;
5002 MMacro *m;
5003 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005004 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005005 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005006
5007 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005008 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005009 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005010 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005011 return 0;
5012 m = is_mmacro(t, &params);
5013 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005014 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005015 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005016 Token *last;
5017 /*
5018 * We have an id which isn't a macro call. We'll assume
5019 * it might be a label; we'll also check to see if a
5020 * colon follows it. Then, if there's another id after
5021 * that lot, we'll check it again for macro-hood.
5022 */
5023 label = last = t;
5024 t = t->next;
5025 if (tok_type_(t, TOK_WHITESPACE))
5026 last = t, t = t->next;
5027 if (tok_is_(t, ":")) {
5028 dont_prepend = 1;
5029 last = t, t = t->next;
5030 if (tok_type_(t, TOK_WHITESPACE))
5031 last = t, t = t->next;
5032 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005033 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
5034 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005035 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005036 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005037 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005038 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005039
5040 /*
5041 * Fix up the parameters: this involves stripping leading and
5042 * trailing whitespace, then stripping braces if they are
5043 * present.
5044 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005045 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005046 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005047
H. Peter Anvine2c80182005-01-15 22:15:51 +00005048 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005049 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005050 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005051
H. Peter Anvine2c80182005-01-15 22:15:51 +00005052 t = params[i];
5053 skip_white_(t);
5054 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005055 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005056 params[i] = t;
5057 paramlen[i] = 0;
5058 while (t) {
5059 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5060 break; /* ... because we have hit a comma */
5061 if (comma && t->type == TOK_WHITESPACE
5062 && tok_is_(t->next, ","))
5063 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005064 if (brace && t->type == TOK_OTHER) {
5065 if (t->text[0] == '{')
5066 brace++; /* ... or a nested opening brace */
5067 else if (t->text[0] == '}')
5068 if (!--brace)
5069 break; /* ... or a brace */
5070 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005071 t = t->next;
5072 paramlen[i]++;
5073 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005074 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005075 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005076 }
5077
5078 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005079 * OK, we have a MMacro structure together with a set of
5080 * parameters. We must now go through the expansion and push
5081 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005082 * parameter tokens and macro-local tokens doesn't get done
5083 * until the single-line macro substitution process; this is
5084 * because delaying them allows us to change the semantics
5085 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005086 *
5087 * First, push an end marker on to istk->expansion, mark this
5088 * macro as in progress, and set up its invocation-specific
5089 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005090 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005091 ll = nasm_malloc(sizeof(Line));
5092 ll->next = istk->expansion;
5093 ll->finishes = m;
5094 ll->first = NULL;
5095 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005096
5097 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005098 * Save the previous MMacro expansion in the case of
5099 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005100 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005101 if (m->max_depth && m->in_progress)
5102 push_mmacro(m);
5103
5104 m->in_progress ++;
5105 m->params = params;
5106 m->iline = tline;
5107 m->nparam = nparam;
5108 m->rotate = 0;
5109 m->paramlen = paramlen;
5110 m->unique = unique++;
5111 m->lineno = 0;
5112 m->condcnt = 0;
5113
5114 m->next_active = istk->mstk;
5115 istk->mstk = m;
5116
5117 list_for_each(l, m->expansion) {
5118 Token **tail;
5119
5120 ll = nasm_malloc(sizeof(Line));
5121 ll->finishes = NULL;
5122 ll->next = istk->expansion;
5123 istk->expansion = ll;
5124 tail = &ll->first;
5125
5126 list_for_each(t, l->first) {
5127 Token *x = t;
5128 switch (t->type) {
5129 case TOK_PREPROC_Q:
5130 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005131 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 case TOK_PREPROC_QQ:
5133 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
5134 break;
5135 case TOK_PREPROC_ID:
5136 if (t->text[1] == '0' && t->text[2] == '0') {
5137 dont_prepend = -1;
5138 x = label;
5139 if (!x)
5140 continue;
5141 }
5142 /* fall through */
5143 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005144 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005145 break;
5146 }
5147 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005148 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005149 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005150 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005151
5152 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005153 * If we had a label, push it on as the first line of
5154 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005155 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005156 if (label) {
5157 if (dont_prepend < 0)
5158 free_tlist(startline);
5159 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005160 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005161 ll->finishes = NULL;
5162 ll->next = istk->expansion;
5163 istk->expansion = ll;
5164 ll->first = startline;
5165 if (!dont_prepend) {
5166 while (label->next)
5167 label = label->next;
5168 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005169 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005170 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005171 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005172
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005173 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005174
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005175 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005176}
5177
H. Peter Anvin130736c2016-02-17 20:27:41 -08005178/*
5179 * This function adds macro names to error messages, and suppresses
5180 * them if necessary.
5181 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005182static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005183{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005184 /*
5185 * If we're in a dead branch of IF or something like it, ignore the error.
5186 * However, because %else etc are evaluated in the state context
5187 * of the previous branch, errors might get lost:
5188 * %if 0 ... %else trailing garbage ... %endif
5189 * So %else etc should set the ERR_PP_PRECOND flag.
5190 */
5191 if ((severity & ERR_MASK) < ERR_FATAL &&
5192 istk && istk->conds &&
5193 ((severity & ERR_PP_PRECOND) ?
5194 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005195 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005196 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005197
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005198 /* This doesn't make sense with the macro stack unwinding */
5199 if (0) {
5200 MMacro *mmac = NULL;
5201 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005202
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005203 /* get %macro name */
5204 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
5205 mmac = istk->mstk;
5206 /* but %rep blocks should be skipped */
5207 while (mmac && !mmac->name)
5208 mmac = mmac->next_active, delta++;
5209 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005210
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005211 if (mmac) {
5212 char *buf;
5213 nasm_set_verror(real_verror);
5214 buf = nasm_vasprintf(fmt, arg);
5215 nasm_error(severity, "(%s:%"PRId32") %s",
5216 mmac->name, mmac->lineno - delta, buf);
5217 nasm_set_verror(pp_verror);
5218 nasm_free(buf);
5219 return;
5220 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005221 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005222 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005223}
5224
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005225static Token *
5226stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005227{
5228 (void)s;
5229 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005230 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005231
5232 return make_tok_qstr(src_get_fname());
5233}
5234
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005235static Token *
5236stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005237{
5238 (void)s;
5239 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005240 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005241
5242 return make_tok_num(src_get_linnum());
5243}
5244
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005245static Token *
5246stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005247{
5248 (void)s;
5249 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005250 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005251
5252 return make_tok_num(globalbits);
5253}
5254
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005255static Token *
5256stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005257{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005258 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005259 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5260 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5261 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005262
5263 (void)s;
5264 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005265 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005266
5267 switch (globalbits) {
5268 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005269 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005270 break;
5271 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005272 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005273 break;
5274 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005275 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005276 break;
5277 default:
5278 panic();
5279 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005280
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005281 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005282}
5283
H. Peter Anvin8b262472019-02-26 14:00:54 -08005284/* Add magic standard macros */
5285struct magic_macros {
5286 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005287 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005288 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005289};
5290static const struct magic_macros magic_macros[] =
5291{
5292 { "__FILE__", 0, stdmac_file },
5293 { "__LINE__", 0, stdmac_line },
5294 { "__BITS__", 0, stdmac_bits },
5295 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005296 { NULL, 0, NULL }
5297};
5298
5299static void pp_add_magic_stdmac(void)
5300{
5301 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005302 SMacro tmpl;
5303
5304 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005305
5306 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005307 tmpl.nparam = m->nparam;
5308 tmpl.expand = m->func;
5309 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005310 }
5311}
5312
H. Peter Anvin734b1882002-04-30 21:01:08 +00005313static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005314pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005315{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005316 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005317 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005318
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005319 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005320 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005321 nested_mac_count = 0;
5322 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005323 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005324 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005325 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005326 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005327
H. Peter Anvin6686de22019-08-10 05:33:14 -07005328 /* First set up the top level input file */
5329 nasm_new(istk);
5330 istk->fp = nasm_open_read(file, NF_TEXT);
5331 src_set(0, file);
5332 istk->lineinc = 1;
5333 if (!istk->fp)
5334 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5335
5336 strlist_add(deplist, file);
5337
5338 /*
5339 * Set up the stdmac packages as a virtual include file,
5340 * indicated by a null file pointer.
5341 */
5342 nasm_new(inc);
5343 inc->next = istk;
5344 inc->fname = src_set_fname(NULL);
5345 inc->nolist = !list_option('b');
5346 istk = inc;
5347 lfmt->uplevel(LIST_INCLUDE, 0);
5348
H. Peter Anvin8b262472019-02-26 14:00:54 -08005349 pp_add_magic_stdmac();
5350
H. Peter Anvinf7606612016-07-13 14:23:48 -07005351 if (tasm_compatible_mode)
5352 pp_add_stdmac(nasm_stdmac_tasm);
5353
5354 pp_add_stdmac(nasm_stdmac_nasm);
5355 pp_add_stdmac(nasm_stdmac_version);
5356
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005357 if (extrastdmac)
5358 pp_add_stdmac(extrastdmac);
5359
H. Peter Anvinf7606612016-07-13 14:23:48 -07005360 stdmacpos = stdmacros[0];
5361 stdmacnext = &stdmacros[1];
5362
H. Peter Anvind2456592008-06-19 15:04:18 -07005363 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005364
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005365 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005366 * Define the __PASS__ macro. This is defined here unlike all the
5367 * other builtins, because it is special -- it varies between
5368 * passes -- but there is really no particular reason to make it
5369 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005370 *
5371 * 0 = dependencies only
5372 * 1 = preparatory passes
5373 * 2 = final pass
5374 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005375 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005376 switch (mode) {
5377 case PP_NORMAL:
5378 apass = pass_final() ? 2 : 1;
5379 break;
5380 case PP_DEPS:
5381 apass = 0;
5382 break;
5383 case PP_PREPROC:
5384 apass = 3;
5385 break;
5386 default:
5387 panic();
5388 }
5389
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005390 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005391}
5392
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005393static void pp_init(void)
5394{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005395}
5396
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005397/*
5398 * Get a line of tokens. If we popped the macro expansion/include stack,
5399 * we return a pointer to the dummy token tok_pop; at that point if
5400 * istk is NULL then we have reached end of input;
5401 */
5402static Token tok_pop; /* Dummy token placeholder */
5403
5404static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005405{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005406 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005407
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005408 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005409 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005410 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005411 * buffer or from the input file.
5412 */
5413 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005414 while (istk->expansion && istk->expansion->finishes) {
5415 Line *l = istk->expansion;
5416 if (!l->finishes->name && l->finishes->in_progress > 1) {
5417 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005418
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005419 /*
5420 * This is a macro-end marker for a macro with no
5421 * name, which means it's not really a macro at all
5422 * but a %rep block, and the `in_progress' field is
5423 * more than 1, meaning that we still need to
5424 * repeat. (1 means the natural last repetition; 0
5425 * means termination by %exitrep.) We have
5426 * therefore expanded up to the %endrep, and must
5427 * push the whole block on to the expansion buffer
5428 * again. We don't bother to remove the macro-end
5429 * marker: we'd only have to generate another one
5430 * if we did.
5431 */
5432 l->finishes->in_progress--;
5433 list_for_each(l, l->finishes->expansion) {
5434 Token *t, *tt, **tail;
5435
5436 ll = nasm_malloc(sizeof(Line));
5437 ll->next = istk->expansion;
5438 ll->finishes = NULL;
5439 ll->first = NULL;
5440 tail = &ll->first;
5441
5442 list_for_each(t, l->first) {
5443 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005444 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005445 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005446 }
5447 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005448 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005449 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005450 } else {
5451 /*
5452 * Check whether a `%rep' was started and not ended
5453 * within this macro expansion. This can happen and
5454 * should be detected. It's a fatal error because
5455 * I'm too confused to work out how to recover
5456 * sensibly from it.
5457 */
5458 if (defining) {
5459 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005460 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005461 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005462 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005463 " expansion of macro `%s'",
5464 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005465 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005466
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005467 /*
5468 * FIXME: investigate the relationship at this point between
5469 * istk->mstk and l->finishes
5470 */
5471 {
5472 MMacro *m = istk->mstk;
5473 istk->mstk = m->next_active;
5474 if (m->name) {
5475 /*
5476 * This was a real macro call, not a %rep, and
5477 * therefore the parameter information needs to
5478 * be freed.
5479 */
5480 if (m->prev) {
5481 pop_mmacro(m);
5482 l->finishes->in_progress --;
5483 } else {
5484 nasm_free(m->params);
5485 free_tlist(m->iline);
5486 nasm_free(m->paramlen);
5487 l->finishes->in_progress = 0;
5488 }
Adam Majer91e72402017-07-25 10:42:01 +02005489 }
5490
5491 /*
5492 * FIXME It is incorrect to always free_mmacro here.
5493 * It leads to usage-after-free.
5494 *
5495 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5496 */
5497#if 0
5498 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005499 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005500#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005501 }
5502 istk->expansion = l->next;
5503 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005504 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005505 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005506 }
5507 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005508 do { /* until we get a line we can use */
5509 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005510
5511 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005512 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005513 int32_t lineno;
5514
H. Peter Anvin6686de22019-08-10 05:33:14 -07005515 if (istk->mstk) {
5516 istk->mstk->lineno++;
5517 if (istk->mstk->fname)
5518 lineno = istk->mstk->lineno + istk->mstk->xline;
5519 else
5520 lineno = 0; /* Defined at init time or builtin */
5521 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005522 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005523 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005524
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005525 tline = l->first;
5526 istk->expansion = l->next;
5527 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005528
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005529 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005530 if (!istk->nolist)
5531 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005532 nasm_free(line);
5533 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005534 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005535 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005536 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005537 } else {
5538 /*
5539 * The current file has ended; work down the istk
5540 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005541 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005542 if (i->fp)
5543 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005544 if (i->conds) {
5545 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005546 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005547 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005548 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005549 if (i->next)
5550 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005551 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005552 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005553 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005554 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005555 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005556 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005557
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005558 /*
5559 * We must expand MMacro parameters and MMacro-local labels
5560 * _before_ we plunge into directive processing, to cope
5561 * with things like `%define something %1' such as STRUC
5562 * uses. Unless we're _defining_ a MMacro, in which case
5563 * those tokens should be left alone to go into the
5564 * definition; and unless we're in a non-emitting
5565 * condition, in which case we don't want to meddle with
5566 * anything.
5567 */
5568 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5569 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005570 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005571 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005572
H. Peter Anvine2c80182005-01-15 22:15:51 +00005573 /*
5574 * Check the line to see if it's a preprocessor directive.
5575 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005576 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5577 if (dtline)
5578 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005579 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005580 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005581 * We're defining a multi-line macro. We emit nothing
5582 * at all, and just
5583 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005584 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005585 Line *l = nasm_malloc(sizeof(Line));
5586 l->next = defining->expansion;
5587 l->first = tline;
5588 l->finishes = NULL;
5589 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005590 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005591 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005592 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005593 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005594 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005595 * directive so we keep our place correctly.
5596 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005597 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005598 } else if (istk->mstk && !istk->mstk->in_progress) {
5599 /*
5600 * We're in a %rep block which has been terminated, so
5601 * we're walking through to the %endrep without
5602 * emitting anything. Emit nothing at all, not even a
5603 * blank line: when we emerge from the %rep block we'll
5604 * give a line-number directive so we keep our place
5605 * correctly.
5606 */
5607 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005608 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005609 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005610 if (!expand_mmacro(tline))
5611 return tline;
5612 }
5613 }
5614}
5615
5616static char *pp_getline(void)
5617{
5618 char *line = NULL;
5619 Token *tline;
5620
5621 real_verror = nasm_set_verror(pp_verror);
5622
5623 while (true) {
5624 tline = pp_tokline();
5625 if (tline == &tok_pop) {
5626 /*
5627 * We popped the macro/include stack. If istk is empty,
5628 * we are at end of input, otherwise just loop back.
5629 */
5630 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005631 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005632 } else {
5633 /*
5634 * De-tokenize the line and emit it.
5635 */
5636 line = detoken(tline, true);
5637 free_tlist(tline);
5638 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005639 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005640 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005641
H. Peter Anvin6686de22019-08-10 05:33:14 -07005642 if (list_option('e') && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005643 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005644 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005645 nasm_free(buf);
5646 }
5647
H. Peter Anvin130736c2016-02-17 20:27:41 -08005648 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005649 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005650}
5651
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005652static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005653{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005654 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005655
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005656 if (defining) {
5657 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005658 nasm_nonfatal("end of file while still defining macro `%s'",
5659 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005660 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005661 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005662 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005663
5664 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005665 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005666 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005667
5668 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005669
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005670 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005671 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005672 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005673 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005674 Include *i = istk;
5675 istk = istk->next;
5676 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005677 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005678 }
5679 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005680 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005681 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005682}
5683
5684static void pp_cleanup_session(void)
5685{
5686 free_llist(predef);
5687 predef = NULL;
5688 delete_Blocks();
5689 freeTokens = NULL;
5690 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005691}
5692
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005693static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005694{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005695 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005696}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005697
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005698static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005699{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005700 Token *inc, *space, *name;
5701 Line *l;
5702
H. Peter Anvin734b1882002-04-30 21:01:08 +00005703 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5704 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5705 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005706
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005707 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005708 l->next = predef;
5709 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005710 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005711 predef = l;
5712}
5713
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005714static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005715{
5716 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005717 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005718 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005719
H. Peter Anvin130736c2016-02-17 20:27:41 -08005720 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005721
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005722 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005723 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5724 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005725 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005726 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005727 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005728 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005729 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005730
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005731 if (space->next->type != TOK_PREPROC_ID &&
5732 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005733 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005734
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005735 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005736 l->next = predef;
5737 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005738 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005739 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005740
5741 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005742}
5743
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005744static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005745{
5746 Token *def, *space;
5747 Line *l;
5748
H. Peter Anvin734b1882002-04-30 21:01:08 +00005749 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5750 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005751 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005752
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005753 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005754 l->next = predef;
5755 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005756 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005757 predef = l;
5758}
5759
H. Peter Anvin05990342018-06-11 13:32:42 -07005760/* Insert an early preprocessor command that doesn't need special handling */
5761static void pp_pre_command(const char *what, char *string)
5762{
5763 char *cmd;
5764 Token *def, *space;
5765 Line *l;
5766
5767 def = tokenize(string);
5768 if (what) {
5769 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5770 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5771 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5772 }
5773
5774 l = nasm_malloc(sizeof(Line));
5775 l->next = predef;
5776 l->first = def;
5777 l->finishes = NULL;
5778 predef = l;
5779}
5780
H. Peter Anvinf7606612016-07-13 14:23:48 -07005781static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005782{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005783 macros_t **mp;
5784
5785 /* Find the end of the list and avoid duplicates */
5786 for (mp = stdmacros; *mp; mp++) {
5787 if (*mp == macros)
5788 return; /* Nothing to do */
5789 }
5790
5791 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5792
5793 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005794}
5795
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005796static void pp_extra_stdmac(macros_t *macros)
5797{
5798 extrastdmac = macros;
5799}
5800
H. Peter Anvin8b262472019-02-26 14:00:54 -08005801static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005802{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005803 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005804 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5805 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5806}
5807
5808static Token *make_tok_qstr(const char *str)
5809{
5810 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005811 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005812 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005813}
5814
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005815static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005816{
5817 if (!m)
5818 return;
5819
5820 /* We need to print the next_active list in reverse order */
5821 pp_list_one_macro(m->next_active, severity);
5822
5823 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005824 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005825 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005826 }
5827}
5828
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005829static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005830{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005831 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005832
H. Peter Anvinddb29062018-12-11 00:06:29 -08005833 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5834 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005835
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005836 if (istk)
5837 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005838
H. Peter Anvinddb29062018-12-11 00:06:29 -08005839 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005840}
5841
H. Peter Anvine7469712016-02-18 02:20:59 -08005842const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005843 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005844 pp_reset,
5845 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005846 pp_cleanup_pass,
5847 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005848 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005849 pp_pre_define,
5850 pp_pre_undefine,
5851 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005852 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005853 pp_include_path,
5854 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005855};