blob: b40417ac88ee5c9eb446b0557e248e6344ba55f1 [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 Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -0700302 * Counters to trap on insane macro recursion or processing.
303 * Note: for smacros these count *down*, for mmacros they count *up*.
304 */
305struct deadman {
306 int64_t total; /* Total number of macros/tokens */
307 int64_t levels; /* Descent depth across all macros */
308 bool triggered; /* Already triggered, no need for error msg */
309};
310
311static struct deadman smacro_deadman, mmacro_deadman;
312
313/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000314 * Conditional assembly: we maintain a separate stack of these for
315 * each level of file inclusion. (The only reason we keep the
316 * stacks separate is to ensure that a stray `%endif' in a file
317 * included from within the true branch of a `%if' won't terminate
318 * it and cause confusion: instead, rightly, it'll cause an error.)
319 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700320enum cond_state {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000321 /*
322 * These states are for use just after %if or %elif: IF_TRUE
323 * means the condition has evaluated to truth so we are
324 * currently emitting, whereas IF_FALSE means we are not
325 * currently emitting but will start doing so if a %else comes
326 * up. In these states, all directives are admissible: %elif,
327 * %else and %endif. (And of course %if.)
328 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800329 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000330 /*
331 * These states come up after a %else: ELSE_TRUE means we're
332 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
333 * any %elif or %else will cause an error.
334 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800335 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200337 * These states mean that we're not emitting now, and also that
338 * nothing until %endif will be emitted at all. COND_DONE is
339 * used when we've had our moment of emission
340 * and have now started seeing %elifs. COND_NEVER is used when
341 * the condition construct in question is contained within a
342 * non-emitting branch of a larger condition construct,
343 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000344 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800345 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000346};
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -0700347struct Cond {
348 Cond *next;
349 enum cond_state state;
350};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800351#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000352
H. Peter Anvin70653092007-10-19 14:42:29 -0700353/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000354 * These defines are used as the possible return values for do_directive
355 */
356#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300357#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000358
Keith Kanios852f1ee2009-07-12 00:19:55 -0500359/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360 * Condition codes. Note that we use c_ prefix not C_ because C_ is
361 * used in nasm.h for the "real" condition codes. At _this_ level,
362 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
363 * ones, so we need a different enum...
364 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700365static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
367 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000368 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000369};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700370enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000371 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
372 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 -0700373 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
374 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000375};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700376static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000377 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
378 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 +0000379 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000380};
381
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800382/*
383 * Directive names.
384 */
385/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
386static int is_condition(enum preproc_token arg)
387{
388 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
389}
390
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000391/* For TASM compatibility we need to be able to recognise TASM compatible
392 * conditional compilation directives. Using the NASM pre-processor does
393 * not work, so we look for them specifically from the following list and
394 * then jam in the equivalent NASM directive into the input stream.
395 */
396
H. Peter Anvine2c80182005-01-15 22:15:51 +0000397enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000398 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
399 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
400};
401
H. Peter Anvin476d2862007-10-02 22:04:15 -0700402static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000403 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
404 "ifndef", "include", "local"
405};
406
407static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700408static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000409static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800410static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000411
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412static Context *cstk;
413static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300414static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300416static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300418static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000419
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800420static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700421static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800422static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000423
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000424/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800425 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800427static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428
429/*
430 * The current set of single-line macros we have defined.
431 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700432static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000433
434/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800435 * The multi-line macro we are currently defining, or the %rep
436 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000437 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800438static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000439
Charles Crayned4200be2008-07-12 16:42:33 -0700440static uint64_t nested_mac_count;
441static uint64_t nested_rep_count;
442
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000443/*
444 * The number of macro parameters to allocate space for at a time.
445 */
446#define PARAM_DELTA 16
447
448/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700449 * The standard macro set: defined in macros.c in a set of arrays.
450 * This gives our position in any macro set, while we are processing it.
451 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000452 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700453static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700454static macros_t **stdmacnext;
455static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300456static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000457
458/*
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -0700459 * Map of which %use packages have been loaded
460 */
461static bool *use_loaded;
462
463/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000464 * Tokens are allocated in blocks to improve speed
465 */
466#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800467static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000468struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000469 Blocks *next;
470 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000471};
472
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800473static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000474
475/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000476 * Forward declarations.
477 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700478static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000479static Token *expand_mmac_params(Token * tline);
480static Token *expand_smacro(Token * tline);
481static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400482static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800483static Token *make_tok_num(int64_t val);
484static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800485static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800486static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000487static void *new_Block(size_t size);
488static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700489static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700490 const char *text, size_t txtlen);
491static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000492static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000493
494/*
495 * Macros for safe checking of token pointers, avoid *(NULL)
496 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300497#define tok_type_(x,t) ((x) && (x)->type == (t))
498#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
499#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
500#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000501
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400502/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700503 * In-place reverse a list of tokens.
504 */
505static Token *reverse_tokens(Token *t)
506{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800507 Token *prev = NULL;
508 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700509
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800510 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400511 next = t->next;
512 t->next = prev;
513 prev = t;
514 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800515 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700516
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800517 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700518}
519
520/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300521 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000522 * front of them. We do it here because I could not find any other
523 * place to do it for the moment, and it is a hack (ideally it would
524 * be nice to be able to use the NASM pre-processor to do it).
525 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000526static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000528 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400529 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000530
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400531 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000532
533 /* Binary search for the directive name */
534 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400535 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400536 q = nasm_skip_word(p);
537 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000538 if (len) {
539 oldchar = p[len];
540 p[len] = 0;
541 while (j - i > 1) {
542 k = (j + i) / 2;
543 m = nasm_stricmp(p, tasm_directives[k]);
544 if (m == 0) {
545 /* We have found a directive, so jam a % in front of it
546 * so that NASM will then recognise it as one if it's own.
547 */
548 p[len] = oldchar;
549 len = strlen(p);
550 oldline = line;
551 line = nasm_malloc(len + 2);
552 line[0] = '%';
553 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700554 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300555 * NASM does not recognise IFDIFI, so we convert
556 * it to %if 0. This is not used in NASM
557 * compatible code, but does need to parse for the
558 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700560 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 } else {
562 memcpy(line + 1, p, len + 1);
563 }
564 nasm_free(oldline);
565 return line;
566 } else if (m < 0) {
567 j = k;
568 } else
569 i = k;
570 }
571 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000572 }
573 return line;
574}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000575
H. Peter Anvin76690a12002-04-30 20:52:49 +0000576/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000577 * The pre-preprocessing stage... This function translates line
578 * number indications as they emerge from GNU cpp (`# lineno "file"
579 * flags') into NASM preprocessor line number indications (`%line
580 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000581 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000582static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000583{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000585 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 if (line[0] == '#' && line[1] == ' ') {
588 oldline = line;
589 fname = oldline + 2;
590 lineno = atoi(fname);
591 fname += strspn(fname, "0123456789 ");
592 if (*fname == '"')
593 fname++;
594 fnlen = strcspn(fname, "\"");
595 line = nasm_malloc(20 + fnlen);
596 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
597 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000598 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000599 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000601 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000602}
603
604/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605 * Free a linked list of tokens.
606 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000608{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400609 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000610 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611}
612
613/*
614 * Free a linked list of lines.
615 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400618 Line *l, *tmp;
619 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 free_tlist(l->first);
621 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000622 }
623}
624
625/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700626 * Free an array of linked lists of tokens
627 */
628static void free_tlist_array(Token **array, size_t nlists)
629{
630 Token **listp = array;
631
632 while (nlists--)
633 free_tlist(*listp++);
634
635 nasm_free(array);
636}
637
638/*
639 * Duplicate a linked list of tokens.
640 */
641static Token *dup_tlist(const Token *list, Token ***tailp)
642{
643 Token *newlist = NULL;
644 Token **tailpp = &newlist;
645 const Token *t;
646
647 list_for_each(t, list) {
648 Token *nt;
649 *tailpp = nt = dup_Token(NULL, t);
650 tailpp = &nt->next;
651 }
652
653 if (tailp)
654 *tailp = tailpp;
655
656 return newlist;
657}
658
659/*
660 * Duplicate a linked list of tokens in reverse order
661 */
662static Token *dup_tlist_reverse(const Token *list, Token *tail)
663{
664 const Token *t;
665
666 list_for_each(t, list)
667 tail = dup_Token(tail, t);
668
669 return tail;
670}
671
672/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800673 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000674 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800675static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000676{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800677 nasm_free(m->name);
678 free_tlist(m->dlist);
679 nasm_free(m->defaults);
680 free_llist(m->expansion);
681 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000682}
683
684/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700685 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800686 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700687static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800688{
689 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700690 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800691 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700692}
693
694static void clear_smacro(SMacro *s)
695{
696 free_smacro_members(s);
697 /* Wipe everything except the next pointer */
698 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
699}
700
701/*
702 * Free an SMacro
703 */
704static void free_smacro(SMacro *s)
705{
706 free_smacro_members(s);
707 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800708}
709
710/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700711 * Free all currently defined macros, and free the hash tables
712 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700713static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700714{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800715 struct hash_iterator it;
716 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700717
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800718 hash_for_each(smt, it, np) {
719 SMacro *tmp;
720 SMacro *s = np->data;
721 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800722 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700723 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700724 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700725 hash_free(smt);
726}
727
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800728static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700729{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800730 struct hash_iterator it;
731 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700732
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800733 hash_for_each(mmt, it, np) {
734 MMacro *tmp;
735 MMacro *m = np->data;
736 nasm_free((void *)np->key);
737 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800738 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700739 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800740 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700741}
742
743static void free_macros(void)
744{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700745 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800746 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700747}
748
749/*
750 * Initialize the hash tables
751 */
752static void init_macros(void)
753{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700754}
755
756/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000757 * Pop the context stack.
758 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000759static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000760{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000761 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000762
763 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700764 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000765 nasm_free(c->name);
766 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000767}
768
H. Peter Anvin072771e2008-05-22 13:17:51 -0700769/*
770 * Search for a key in the hash index; adding it if necessary
771 * (in which case we initialize the data pointer to NULL.)
772 */
773static void **
774hash_findi_add(struct hash_table *hash, const char *str)
775{
776 struct hash_insert hi;
777 void **r;
778 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800779 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700780
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800781 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700782 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300783 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700784
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800785 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
786 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700787 return hash_add(&hi, strx, NULL);
788}
789
790/*
791 * Like hash_findi, but returns the data element rather than a pointer
792 * to it. Used only when not adding a new element, hence no third
793 * argument.
794 */
795static void *
796hash_findix(struct hash_table *hash, const char *str)
797{
798 void **p;
799
800 p = hash_findi(hash, str, NULL);
801 return p ? *p : NULL;
802}
803
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400804/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800805 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400806 * if there no more left -- return NULL
807 */
808static char *line_from_stdmac(void)
809{
810 unsigned char c;
811 const unsigned char *p = stdmacpos;
812 char *line, *q;
813 size_t len = 0;
814
815 if (!stdmacpos)
816 return NULL;
817
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700818 /*
819 * 32-126 is ASCII, 127 is end of line, 128-31 are directives
820 * (allowed to wrap around) corresponding to PP_* tokens 0-159.
821 */
822 while ((c = *p++) != 127) {
823 uint8_t ndir = c - 128;
824 if (ndir < 256-96)
825 len += pp_directives_len[ndir] + 1;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400826 else
827 len++;
828 }
829
830 line = nasm_malloc(len + 1);
831 q = line;
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700832
833 while ((c = *stdmacpos++) != 127) {
834 uint8_t ndir = c - 128;
835 if (ndir < 256-96) {
836 memcpy(q, pp_directives[ndir], pp_directives_len[ndir]);
837 q += pp_directives_len[ndir];
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400838 *q++ = ' ';
839 } else {
840 *q++ = c;
841 }
842 }
843 stdmacpos = p;
844 *q = '\0';
845
H. Peter Anvin (Intel)6d5c77c2019-08-15 02:29:40 -0700846 if (*stdmacpos == 127) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700847 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400848 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700849 if (*stdmacnext) {
850 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400851 } else if (do_predef) {
852 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400853
854 /*
855 * Nasty hack: here we push the contents of
856 * `predef' on to the top-level expansion stack,
857 * since this is the most convenient way to
858 * implement the pre-include and pre-define
859 * features.
860 */
861 list_for_each(pd, predef) {
H. Peter Anvin6686de22019-08-10 05:33:14 -0700862 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800863 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700864 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800865 l->finishes = NULL;
866
867 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400868 }
869 do_predef = false;
870 }
871 }
872
873 return line;
874}
875
H. Peter Anvin6686de22019-08-10 05:33:14 -0700876/*
877 * Read a line from a file. Return NULL on end of file.
878 */
879static char *line_from_file(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000880{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700881 int c;
882 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400883 const unsigned int delta = 512;
884 const unsigned int pad = 8;
885 unsigned int nr_cont = 0;
886 bool cont = false;
887 char *buffer, *p;
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700888 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000889
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400890 size = delta;
891 p = buffer = nasm_malloc(size);
892
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700893 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400894 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400895
896 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700897 case EOF:
898 if (p == buffer) {
899 nasm_free(buffer);
900 return NULL;
901 }
902 c = 0;
903 break;
904
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400905 case '\r':
906 next = fgetc(istk->fp);
907 if (next != '\n')
908 ungetc(next, istk->fp);
909 if (cont) {
910 cont = false;
911 continue;
912 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700913 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400914 break;
915
916 case '\n':
917 if (cont) {
918 cont = false;
919 continue;
920 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700921 c = 0;
922 break;
923
924 case 032: /* ^Z = legacy MS-DOS end of file mark */
925 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400926 break;
927
928 case '\\':
929 next = fgetc(istk->fp);
930 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400931 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400932 cont = true;
933 nr_cont++;
934 continue;
935 }
936 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000937 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400938
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400939 if (p >= (buffer + size - pad)) {
940 buffer = nasm_realloc(buffer, size + delta);
941 p = buffer + size - pad;
942 size += delta;
943 }
944
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700945 *p++ = c;
946 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000947
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700948 lineno = src_get_linnum() + istk->lineinc +
949 (nr_cont * istk->lineinc);
950 src_set_linnum(lineno);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000951
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000952 return buffer;
953}
954
955/*
H. Peter Anvin6686de22019-08-10 05:33:14 -0700956 * Common read routine regardless of source
957 */
958static char *read_line(void)
959{
960 char *line;
961
962 if (istk->fp)
963 line = line_from_file();
964 else
965 line = line_from_stdmac();
966
967 if (!line)
968 return NULL;
969
970 if (!istk->nolist)
971 lfmt->line(LIST_READ, src_get_linnum(), line);
972
973 return line;
974}
975
976/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000977 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000978 * don't need to parse the value out of e.g. numeric tokens: we
979 * simply split one string into many.
980 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000981static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000982{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700983 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000984 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000985 Token *list = NULL;
986 Token *t, **tail = &list;
987
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700989 char *p = line;
990 char *ep = NULL; /* End of token, for trimming the end */
991
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 if (*p == '%') {
993 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300994 if (*p == '+' && !nasm_isdigit(p[1])) {
995 p++;
996 type = TOK_PASTE;
997 } else if (nasm_isdigit(*p) ||
998 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 do {
1000 p++;
1001 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001002 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 type = TOK_PREPROC_ID;
1004 } else if (*p == '{') {
1005 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001006 while (*p) {
1007 if (*p == '}')
1008 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 p[-1] = *p;
1010 p++;
1011 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001012 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001013 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001014 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 if (*p)
1016 p++;
1017 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001018 } else if (*p == '[') {
1019 int lvl = 1;
1020 line += 2; /* Skip the leading %[ */
1021 p++;
1022 while (lvl && (c = *p++)) {
1023 switch (c) {
1024 case ']':
1025 lvl--;
1026 break;
1027 case '%':
1028 if (*p == '[')
1029 lvl++;
1030 break;
1031 case '\'':
1032 case '\"':
1033 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +03001034 p = nasm_skip_string(p - 1);
1035 if (*p)
1036 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001037 break;
1038 default:
1039 break;
1040 }
1041 }
1042 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001043 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001044 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001045 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001046 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001047 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001048 type = TOK_INDIRECT;
1049 } else if (*p == '?') {
1050 type = TOK_PREPROC_Q; /* %? */
1051 p++;
1052 if (*p == '?') {
1053 type = TOK_PREPROC_QQ; /* %?? */
1054 p++;
1055 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001056 } else if (*p == '!') {
1057 type = TOK_PREPROC_ID;
1058 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001059 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001060 do {
1061 p++;
1062 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001063 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001064 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001065 p = nasm_skip_string(p);
1066 if (*p)
1067 p++;
1068 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001069 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001070 } else {
1071 /* %! without string or identifier */
1072 type = TOK_OTHER; /* Legacy behavior... */
1073 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001074 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001076 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 do {
1078 p++;
1079 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001080 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 type = TOK_PREPROC_ID;
1082 } else {
1083 type = TOK_OTHER;
1084 if (*p == '%')
1085 p++;
1086 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001087 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 type = TOK_ID;
1089 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001090 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001092 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 /*
1094 * A string token.
1095 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001097 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001098
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 if (*p) {
1100 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001101 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001102 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 /* Handling unterminated strings by UNV */
1104 /* type = -1; */
1105 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001106 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001107 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001108 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001109 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001110 bool is_hex = false;
1111 bool is_float = false;
1112 bool has_e = false;
1113 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001114
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001116 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001118
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001119 if (*p == '$') {
1120 p++;
1121 is_hex = true;
1122 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001123
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001124 for (;;) {
1125 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001126
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001127 if (!is_hex && (c == 'e' || c == 'E')) {
1128 has_e = true;
1129 if (*p == '+' || *p == '-') {
1130 /*
1131 * e can only be followed by +/- if it is either a
1132 * prefixed hex number or a floating-point number
1133 */
1134 p++;
1135 is_float = true;
1136 }
1137 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1138 is_hex = true;
1139 } else if (c == 'P' || c == 'p') {
1140 is_float = true;
1141 if (*p == '+' || *p == '-')
1142 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001143 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001144 ; /* just advance */
1145 else if (c == '.') {
1146 /*
1147 * we need to deal with consequences of the legacy
1148 * parser, like "1.nolist" being two tokens
1149 * (TOK_NUMBER, TOK_ID) here; at least give it
1150 * a shot for now. In the future, we probably need
1151 * a flex-based scanner with proper pattern matching
1152 * to do it as well as it can be done. Nothing in
1153 * the world is going to help the person who wants
1154 * 0x123.p16 interpreted as two tokens, though.
1155 */
1156 r = p;
1157 while (*r == '_')
1158 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001159
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001160 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1161 (!is_hex && (*r == 'e' || *r == 'E')) ||
1162 (*r == 'p' || *r == 'P')) {
1163 p = r;
1164 is_float = true;
1165 } else
1166 break; /* Terminate the token */
1167 } else
1168 break;
1169 }
1170 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001171
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001172 if (p == line+1 && *line == '$') {
1173 type = TOK_OTHER; /* TOKEN_HERE */
1174 } else {
1175 if (has_e && !is_hex) {
1176 /* 1e13 is floating-point, but 1e13h is not */
1177 is_float = true;
1178 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001179
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001180 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1181 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001182 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001184 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 /*
1186 * Whitespace just before end-of-line is discarded by
1187 * pretending it's a comment; whitespace just before a
1188 * comment gets lumped into the comment.
1189 */
1190 if (!*p || *p == ';') {
1191 type = TOK_COMMENT;
1192 while (*p)
1193 p++;
1194 }
1195 } else if (*p == ';') {
1196 type = TOK_COMMENT;
1197 while (*p)
1198 p++;
1199 } else {
1200 /*
1201 * Anything else is an operator of some kind. We check
1202 * for all the double-character operators (>>, <<, //,
1203 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001204 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 */
1206 type = TOK_OTHER;
1207 if ((p[0] == '>' && p[1] == '>') ||
1208 (p[0] == '<' && p[1] == '<') ||
1209 (p[0] == '/' && p[1] == '/') ||
1210 (p[0] == '<' && p[1] == '=') ||
1211 (p[0] == '>' && p[1] == '=') ||
1212 (p[0] == '=' && p[1] == '=') ||
1213 (p[0] == '!' && p[1] == '=') ||
1214 (p[0] == '<' && p[1] == '>') ||
1215 (p[0] == '&' && p[1] == '&') ||
1216 (p[0] == '|' && p[1] == '|') ||
1217 (p[0] == '^' && p[1] == '^')) {
1218 p++;
1219 }
1220 p++;
1221 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001222
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 /* Handling unterminated string by UNV */
1224 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001225 {
1226 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1227 t->text[p-line] = *line;
1228 tail = &t->next;
1229 }
1230 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001232 if (!ep)
1233 ep = p;
1234 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235 tail = &t->next;
1236 }
1237 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001238 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001239 return list;
1240}
1241
H. Peter Anvince616072002-04-30 21:02:23 +00001242/*
1243 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001244 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001245 * deleted only all at once by the delete_Blocks function.
1246 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001248{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001249 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001251 /* first, get to the end of the linked list */
1252 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001254 /* now allocate the requested chunk */
1255 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001257 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001258 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001259 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001260}
1261
1262/*
1263 * this function deletes all managed blocks of memory
1264 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001266{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001268
H. Peter Anvin70653092007-10-19 14:42:29 -07001269 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001270 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001271 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001272 * free it.
1273 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001275 if (b->chunk)
1276 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 a = b;
1278 b = b->next;
1279 if (a != &blocks)
1280 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001281 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001282 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001284
1285/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001286 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001287 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001288 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001289static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001290 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001291{
1292 Token *t;
1293 int i;
1294
H. Peter Anvin89cee572009-07-15 09:16:54 -04001295 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1297 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1298 freeTokens[i].next = &freeTokens[i + 1];
1299 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001300 }
1301 t = freeTokens;
1302 freeTokens = t->next;
1303 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001304 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001305 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001306 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 t->text = NULL;
1308 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001309 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001311 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001312 t->text = nasm_malloc(txtlen+1);
1313 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001314 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001315 }
1316 return t;
1317}
1318
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001319static Token *dup_Token(Token *next, const Token *src)
1320{
1321 return new_Token(next, src->type, src->text, src->len);
1322}
1323
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001325{
1326 Token *next = t->next;
1327 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001328 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001329 freeTokens = t;
1330 return next;
1331}
1332
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001333/*
1334 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001335 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1336 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001337 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001338static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001339{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001340 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001341 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001342 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001343
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001344 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001345 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001346 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001347 char *v;
1348 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001349
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001350 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001351 if (nasm_isquote(*v))
1352 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001353
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001354 if (v) {
1355 char *p = getenv(v);
1356 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001357 /*!
1358 *!environment [on] nonexistent environment variable
1359 *! warns if a nonexistent environment variable
1360 *! is accessed using the \c{%!} preprocessor
1361 *! construct (see \k{getenv}.) Such environment
1362 *! variables are treated as empty (with this
1363 *! warning issued) starting in NASM 2.15;
1364 *! earlier versions of NASM would treat this as
1365 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001366 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001367 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1368 p = "";
1369 }
1370 t->text = nasm_strdup(p);
1371 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001372 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001373 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001375
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 /* Expand local macros here and not during preprocessing */
1377 if (expand_locals &&
1378 t->type == TOK_PREPROC_ID && t->text &&
1379 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001380 const char *q;
1381 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001382 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001384 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1385 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 nasm_free(t->text);
1387 t->text = p;
1388 }
1389 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001390 if (t->text) {
1391 if (debug_level(2)) {
1392 unsigned long t_len = t->len;
1393 unsigned long s_len = strlen(t->text);
1394 if (t_len != s_len) {
1395 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1396 t->text, t->type, s_len, t_len);
1397 t->len = s_len;
1398 }
1399 }
1400 len += t->len;
1401 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001402 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001403 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001404 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001405
H. Peter Anvin734b1882002-04-30 21:01:08 +00001406 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001407
1408 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001409 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001410 memcpy(p, t->text, t->len);
1411 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001412 } else if (t->type == TOK_WHITESPACE) {
1413 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001415 }
1416 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001417
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001418 return line;
1419}
1420
1421/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001422 * A scanner, suitable for use by the expression evaluator, which
1423 * operates on a line of Tokens. Expects a pointer to a pointer to
1424 * the first token in the line to be passed in as its private_data
1425 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001426 *
1427 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001428 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001429struct ppscan {
1430 Token *tptr;
1431 int ntokens;
1432};
1433
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001435{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001436 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001437 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001438 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001439
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001441 if (pps->ntokens && (tline = pps->tptr)) {
1442 pps->ntokens--;
1443 pps->tptr = tline->next;
1444 } else {
1445 pps->tptr = NULL;
1446 pps->ntokens = 0;
1447 return tokval->t_type = TOKEN_EOS;
1448 }
1449 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001450
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001451 tokval->t_charptr = tline->text;
1452
H. Peter Anvin76690a12002-04-30 20:52:49 +00001453 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001454 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001455 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001456 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001457
H. Peter Anvine2c80182005-01-15 22:15:51 +00001458 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001459 p = tokval->t_charptr = tline->text;
1460 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 tokval->t_charptr++;
1462 return tokval->t_type = TOKEN_ID;
1463 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001464
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001465 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001466 if (r >= p+MAX_KEYWORD)
1467 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001468 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001469 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001470 *s = '\0';
1471 /* right, so we have an identifier sitting in temp storage. now,
1472 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001473 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001474 }
1475
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001477 bool rn_error;
1478 tokval->t_integer = readnum(tline->text, &rn_error);
1479 tokval->t_charptr = tline->text;
1480 if (rn_error)
1481 return tokval->t_type = TOKEN_ERRNUM;
1482 else
1483 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001484 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001485
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001486 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001487 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001488 }
1489
H. Peter Anvine2c80182005-01-15 22:15:51 +00001490 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001491 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001492
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001493 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001494 tokval->t_charptr = tline->text;
1495 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001496
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001497 if (ep[0] != bq || ep[1] != '\0')
1498 return tokval->t_type = TOKEN_ERRSTR;
1499 else
1500 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001501 }
1502
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 if (tline->type == TOK_OTHER) {
1504 if (!strcmp(tline->text, "<<"))
1505 return tokval->t_type = TOKEN_SHL;
1506 if (!strcmp(tline->text, ">>"))
1507 return tokval->t_type = TOKEN_SHR;
1508 if (!strcmp(tline->text, "//"))
1509 return tokval->t_type = TOKEN_SDIV;
1510 if (!strcmp(tline->text, "%%"))
1511 return tokval->t_type = TOKEN_SMOD;
1512 if (!strcmp(tline->text, "=="))
1513 return tokval->t_type = TOKEN_EQ;
1514 if (!strcmp(tline->text, "<>"))
1515 return tokval->t_type = TOKEN_NE;
1516 if (!strcmp(tline->text, "!="))
1517 return tokval->t_type = TOKEN_NE;
1518 if (!strcmp(tline->text, "<="))
1519 return tokval->t_type = TOKEN_LE;
1520 if (!strcmp(tline->text, ">="))
1521 return tokval->t_type = TOKEN_GE;
1522 if (!strcmp(tline->text, "&&"))
1523 return tokval->t_type = TOKEN_DBL_AND;
1524 if (!strcmp(tline->text, "^^"))
1525 return tokval->t_type = TOKEN_DBL_XOR;
1526 if (!strcmp(tline->text, "||"))
1527 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001528 }
1529
1530 /*
1531 * We have no other options: just return the first character of
1532 * the token text.
1533 */
1534 return tokval->t_type = tline->text[0];
1535}
1536
1537/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001538 * Compare a string to the name of an existing macro; this is a
1539 * simple wrapper which calls either strcmp or nasm_stricmp
1540 * depending on the value of the `casesense' parameter.
1541 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001542static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001543{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001544 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001545}
1546
1547/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001548 * Compare a string to the name of an existing macro; this is a
1549 * simple wrapper which calls either strcmp or nasm_stricmp
1550 * depending on the value of the `casesense' parameter.
1551 */
1552static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1553{
1554 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1555}
1556
1557/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001558 * Return the Context structure associated with a %$ token. Return
1559 * NULL, having _already_ reported an error condition, if the
1560 * context stack isn't deep enough for the supplied number of $
1561 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001562 *
1563 * If "namep" is non-NULL, set it to the pointer to the macro name
1564 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001565 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001566static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001567{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001568 Context *ctx;
1569 int i;
1570
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001571 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001572 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001573
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001574 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001576
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001578 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001580 }
1581
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001582 name += 2;
1583 ctx = cstk;
1584 i = 0;
1585 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001586 name++;
1587 i++;
1588 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001589 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001591 nasm_nonfatal("`%s': context stack is only"
1592 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001594 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001595
1596 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001597 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001598
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001599 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001600}
1601
1602/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001603 * Open an include file. This routine must always return a valid
1604 * file pointer if it returns - it's responsible for throwing an
1605 * ERR_FATAL and bombing out completely if not. It should also try
1606 * the include path one by one until it finds the file or reaches
1607 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001608 *
1609 * Note: for INC_PROBE the function returns NULL at all times;
1610 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001611 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001612enum incopen_mode {
1613 INC_NEEDED, /* File must exist */
1614 INC_OPTIONAL, /* Missing is OK */
1615 INC_PROBE /* Only an existence probe */
1616};
1617
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001618/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001619static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001620 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001621{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001622 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001623 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001624 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001625 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001626 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001627
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001629 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001630 if (omode == INC_PROBE) {
1631 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001632 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001633 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001634 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001635 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001636 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001637 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001638 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001640 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001641
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001642 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001643
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001644 if (!ip) {
1645 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001646 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001647 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001648
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001649 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001650 ip = ip->next;
1651 }
1652}
1653
1654/*
1655 * Open a file, or test for the presence of one (depending on omode),
1656 * considering the include path.
1657 */
1658static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001659 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001660 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001661 enum incopen_mode omode,
1662 enum file_flags fmode)
1663{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001664 struct hash_insert hi;
1665 void **hp;
1666 char *path;
1667 FILE *fp = NULL;
1668
1669 hp = hash_find(&FileHash, file, &hi);
1670 if (hp) {
1671 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001672 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001673 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001674 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001675 } else {
1676 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001677 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001678
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001679 /* Positive or negative result */
1680 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001681
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001682 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001683 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001684 */
1685 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001686 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001687 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001688
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001689 if (!path) {
1690 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001691 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001692 } else {
1693 if (!fp && omode != INC_PROBE)
1694 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001695 }
1696
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001697 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001698 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001699
1700 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001701}
1702
1703/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001704 * Opens an include or input file. Public version, for use by modules
1705 * that get a file:lineno pair and need to look at the file again
1706 * (e.g. the CodeView debug backend). Returns NULL on failure.
1707 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001708FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001709{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001710 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001711}
1712
1713/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001714 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001715 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001716 * return true if _any_ single-line macro of that name is defined.
1717 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001718 * `nparam' or no parameters is defined.
1719 *
1720 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001721 * defined, or nparam is -1, the address of the definition structure
1722 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001723 * is NULL, no action will be taken regarding its contents, and no
1724 * error will occur.
1725 *
1726 * Note that this is also called with nparam zero to resolve
1727 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001728 *
1729 * If you already know which context macro belongs to, you can pass
1730 * the context pointer as first parameter; if you won't but name begins
1731 * with %$ the context will be automatically computed. If all_contexts
1732 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001733 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001734static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001735smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001736 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001737{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001738 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001739 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001740
H. Peter Anvin97a23472007-09-16 17:57:25 -07001741 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001742 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001743 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001744 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001745 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001747 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001748 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001749 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001750 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001751 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001752 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001753
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 while (m) {
1755 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001756 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001758 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 *defn = m;
1760 else
1761 *defn = NULL;
1762 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001763 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 }
1765 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001766 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001767
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001768 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001769}
1770
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001771/* param should be a natural number [0; INT_MAX] */
1772static int read_param_count(const char *str)
1773{
1774 int result;
1775 bool err;
1776
1777 result = readnum(str, &err);
1778 if (result < 0 || result > INT_MAX) {
1779 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001780 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1781 str, 0, INT_MAX);
1782 } else if (err)
1783 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001784 return result;
1785}
1786
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001787/*
1788 * Count and mark off the parameters in a multi-line macro call.
1789 * This is called both from within the multi-line macro expansion
1790 * code, and also to mark off the default parameters when provided
1791 * in a %macro definition line.
1792 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001794{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795 int paramsize, brace;
1796
1797 *nparam = paramsize = 0;
1798 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001800 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001801 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 paramsize += PARAM_DELTA;
1803 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1804 }
1805 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001806 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001808 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001810 if (brace) {
1811 while (brace && (t = t->next) != NULL) {
1812 if (tok_is_(t, "{"))
1813 brace++;
1814 else if (tok_is_(t, "}"))
1815 brace--;
1816 }
1817
1818 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 /*
1820 * Now we've found the closing brace, look further
1821 * for the comma.
1822 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001823 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 skip_white_(t);
1825 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001826 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 while (tok_isnt_(t, ","))
1828 t = t->next;
1829 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001831 } else {
1832 while (tok_isnt_(t, ","))
1833 t = t->next;
1834 }
1835 if (t) { /* got a comma/brace */
1836 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001838 }
1839}
1840
1841/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001842 * Determine whether one of the various `if' conditions is true or
1843 * not.
1844 *
1845 * We must free the tline we get passed.
1846 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001847static enum cond_state if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001848{
H. Peter Anvin70055962007-10-11 00:05:31 -07001849 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001850 Token *t, *tt, *origline;
1851 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001852 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001853 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001854 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001855 char *p;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001856 const char *dname = pp_directives[ct];
1857 bool casesense = true;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001858
1859 origline = tline;
1860
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001861 switch (PP_COND(ct)) {
1862 case PP_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001863 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001864 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001866 if (!tline)
1867 break;
1868 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001869 nasm_nonfatal("`%s' expects context identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001870 dname);
1871 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001873 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001874 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 tline = tline->next;
1876 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001877 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001878
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001879 case PP_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001880 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001881 while (tline) {
1882 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 if (!tline || (tline->type != TOK_ID &&
1884 (tline->type != TOK_PREPROC_ID ||
1885 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001886 nasm_nonfatal("`%s' expects macro identifiers",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001887 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001888 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001890 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001891 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001893 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001895
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001896 case PP_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001897 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001898 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001899 while (tline) {
1900 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001901 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001902 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001903 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001904 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001905 nasm_nonfatal("`%s' expects environment variable names",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001906 dname);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001907 goto fail;
1908 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001909 p = tline->text;
1910 if (tline->type == TOK_PREPROC_ID)
1911 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001912 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001913 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001914 if (getenv(p))
1915 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001916 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001917 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001918 break;
1919
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001920 case PP_IFIDNI:
1921 casesense = false;
1922 /* fall through */
1923 case PP_IFIDN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 tline = expand_smacro(tline);
1925 t = tt = tline;
1926 while (tok_isnt_(tt, ","))
1927 tt = tt->next;
1928 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001929 nasm_nonfatal("`%s' expects two comma-separated arguments",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001930 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 }
1933 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001934 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1936 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001937 nasm_nonfatal("`%s': more than one comma on line",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001938 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001939 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 }
1941 if (t->type == TOK_WHITESPACE) {
1942 t = t->next;
1943 continue;
1944 }
1945 if (tt->type == TOK_WHITESPACE) {
1946 tt = tt->next;
1947 continue;
1948 }
1949 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001950 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 break;
1952 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001953 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001955 size_t l1 = nasm_unquote(t->text, NULL);
1956 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001957
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001958 if (l1 != l2) {
1959 j = false;
1960 break;
1961 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001962 if (mmemcmp(t->text, tt->text, l1, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 j = false;
1964 break;
1965 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001966 } else if (mstrcmp(tt->text, t->text, casesense) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001967 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 break;
1969 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001970
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 t = t->next;
1972 tt = tt->next;
1973 }
1974 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001975 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001977
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001978 case PP_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001979 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001981 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001982
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 skip_white_(tline);
1984 tline = expand_id(tline);
1985 if (!tok_type_(tline, TOK_ID)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07001986 nasm_nonfatal("`%s' expects a macro name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 goto fail;
1988 }
1989 searching.name = nasm_strdup(tline->text);
1990 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001991 searching.plus = false;
1992 searching.nolist = false;
1993 searching.in_progress = 0;
1994 searching.max_depth = 0;
1995 searching.rep_nest = NULL;
1996 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001997 searching.nparam_max = INT_MAX;
1998 tline = expand_smacro(tline->next);
1999 skip_white_(tline);
2000 if (!tline) {
2001 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002002 nasm_nonfatal("`%s' expects a parameter count or nothing",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002003 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 } else {
2005 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002006 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 }
2008 if (tline && tok_is_(tline->next, "-")) {
2009 tline = tline->next->next;
2010 if (tok_is_(tline, "*"))
2011 searching.nparam_max = INT_MAX;
2012 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002013 nasm_nonfatal("`%s' expects a parameter count after `-'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002014 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002015 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002016 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002017 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002018 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002019 searching.nparam_max = searching.nparam_min;
2020 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 }
2022 }
2023 if (tline && tok_is_(tline->next, "+")) {
2024 tline = tline->next;
2025 searching.plus = true;
2026 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002027 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
2028 while (mmac) {
2029 if (!strcmp(mmac->name, searching.name) &&
2030 (mmac->nparam_min <= searching.nparam_max
2031 || searching.plus)
2032 && (searching.nparam_min <= mmac->nparam_max
2033 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002034 found = true;
2035 break;
2036 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002037 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002038 }
2039 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002040 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002041 nasm_free(searching.name);
2042 j = found;
2043 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002044 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002045
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002046 case PP_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002047 needtype = TOK_ID;
2048 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002049 case PP_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002050 needtype = TOK_NUMBER;
2051 goto iftype;
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002052 case PP_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002053 needtype = TOK_STRING;
2054 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002055
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002056iftype:
2057 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002058
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002059 while (tok_type_(t, TOK_WHITESPACE) ||
2060 (needtype == TOK_NUMBER &&
2061 tok_type_(t, TOK_OTHER) &&
2062 (t->text[0] == '-' || t->text[0] == '+') &&
2063 !t->text[1]))
2064 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002065
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002066 j = tok_type_(t, needtype);
2067 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002068
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002069 case PP_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002070 t = tline = expand_smacro(tline);
2071 while (tok_type_(t, TOK_WHITESPACE))
2072 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002073
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002074 j = false;
2075 if (t) {
2076 t = t->next; /* Skip the actual token */
2077 while (tok_type_(t, TOK_WHITESPACE))
2078 t = t->next;
2079 j = !t; /* Should be nothing left */
2080 }
2081 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002082
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002083 case PP_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 t = tline = expand_smacro(tline);
2085 while (tok_type_(t, TOK_WHITESPACE))
2086 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002087
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002088 j = !t; /* Should be empty */
2089 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002090
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002091 case PP_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002092 pps.tptr = tline = expand_smacro(tline);
2093 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002095 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002096 if (!evalresult)
2097 return -1;
2098 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002099 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002100 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002101 nasm_nonfatal("non-constant value given to `%s'",
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002102 dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002105 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002107
H. Peter Anvine2c80182005-01-15 22:15:51 +00002108 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002109 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002110 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002111 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002112
2113 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002114 return (j ^ PP_COND_NEGATIVE(ct)) ? COND_IF_TRUE : COND_IF_FALSE;
H. Peter Anvin70653092007-10-19 14:42:29 -07002115
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002116fail:
2117 free_tlist(origline);
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002118 return COND_NEVER;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002119}
2120
2121/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002122 * Default smacro expansion routine: just returns a copy of the
2123 * expansion list.
2124 */
2125static Token *
2126smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2127{
2128 (void)params;
2129 (void)nparams;
2130
2131 return dup_tlist(s->expansion, NULL);
2132}
2133
2134/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002135 * Emit a macro defintion or undef to the listing file, if
2136 * desired. This is similar to detoken(), but it handles the reverse
2137 * expansion list, does not expand %! or local variable tokens, and
2138 * does some special handling for macro parameters.
2139 */
2140static void
2141list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2142{
2143 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2144 const Token **param_names;
2145 Token *junk_names = NULL;
2146 Token *t;
2147 size_t namelen, size;
2148 unsigned int i;
2149 char *def, *p;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002150 char *context_prefix = NULL;
2151 size_t context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002152
2153 nasm_newn(param_names, m->nparam);
2154
2155 namelen = strlen(m->name);
2156 size = namelen + 2; /* Include room for space after name + NUL */
2157
2158 if (ctx) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002159 int context_depth = cstk->depth - ctx->depth + 1;
2160 context_prefix =
2161 nasm_asprintf("[%s::%"PRIu64"] %%%-*s",
2162 ctx->name ? ctx->name : "",
2163 ctx->number, context_depth, "");
2164
2165 context_len = nasm_last_string_len();
2166 memset(context_prefix + context_len - context_depth,
2167 '$', context_depth);
2168 size += context_len;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002169 }
2170
2171 list_for_each(t, m->expansion) {
2172 if (!t->text) {
2173 size++; /* Whitespace, presumably */
2174 } else {
2175 size += t->len;
2176 if (is_smac_param(t->type))
2177 param_names[smac_nparam(t->type)] = t;
2178 }
2179 }
2180
2181 if (m->nparam) {
2182 /*
2183 * Space for ( and either , or ) around each
2184 * parameter, plus an optional =.
2185 */
2186 size += 1 + 2 * m->nparam;
2187 for (i = 0; i < m->nparam; i++) {
2188 if (!param_names[i])
2189 param_names[i] = &unused_arg_name;
2190 size += param_names[i]->len;
2191 }
2192 }
2193
2194 def = nasm_malloc(size);
2195 p = def+size;
2196 *--p = '\0';
2197
2198 list_for_each(t, m->expansion) {
2199 if (!t->text) {
2200 *--p = ' ';
2201 } else {
2202 p -= t->len;
2203 memcpy(p, t->text, t->len);
2204 }
2205 }
2206
2207 *--p = ' ';
2208
2209 if (m->nparam) {
2210 *--p = ')';
2211 for (i = m->nparam; i--;) {
2212 p -= param_names[i]->len;
2213 memcpy(p, param_names[i]->text, param_names[i]->len);
2214 if (m->eval_param && m->eval_param[i])
2215 *--p = '=';
2216 *--p = ',';
2217 }
2218 *p = '('; /* First parameter starts with ( not , */
2219
2220 free_tlist(junk_names);
2221 }
2222
2223 p -= namelen;
2224 memcpy(p, m->name, namelen);
2225
H. Peter Anvin6686de22019-08-10 05:33:14 -07002226 if (context_prefix) {
2227 p -= context_len;
2228 memcpy(p, context_prefix, context_len);
2229 nasm_free(context_prefix);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002230 }
2231
2232 nasm_listmsg("%s %s", pp_directives[op], p);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002233 nasm_free(def);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002234}
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002235
2236/*
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002237 * Common code for defining an smacro. The tmpl argument, if not NULL,
2238 * contains any macro parameters that aren't explicit arguments;
2239 * those are the more uncommon macro variants.
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002240 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002241static SMacro *define_smacro(const char *mname, bool casesense,
2242 Token *expansion, const SMacro *tmpl)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002243{
2244 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002245 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002246 Context *ctx;
2247 bool defining_alias = false;
2248 unsigned int nparam = 0;
H. Peter Anvin70653092007-10-19 14:42:29 -07002249
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002250 if (tmpl) {
2251 defining_alias = tmpl->alias;
2252 nparam = tmpl->nparam;
2253 }
2254
2255 while (1) {
2256 ctx = get_ctx(mname, &mname);
2257
2258 if (!smacro_defined(ctx, mname, nparam, &smac, casesense)) {
2259 /* Create a new macro */
2260 smtbl = ctx ? &ctx->localmac : &smacros;
2261 smhead = (SMacro **) hash_findi_add(smtbl, mname);
2262 nasm_new(smac);
2263 smac->next = *smhead;
2264 *smhead = smac;
2265 break;
2266 } else if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002267 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002268 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002269 /*
2270 * Some instances of the old code considered this a failure,
2271 * some others didn't. What is the right thing to do here?
2272 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002273 goto fail;
2274 } else if (!smac->alias || defining_alias) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002275 /*
2276 * We're redefining, so we have to take over an
2277 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002278 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002279 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002280 clear_smacro(smac);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002281 break;
2282 } else if (smac->in_progress) {
2283 nasm_nonfatal("macro alias loop");
2284 goto fail;
2285 } else {
2286 /* It is an alias macro; follow the alias link */
2287 SMacro *s;
2288
2289 smac->in_progress = true;
2290 s = define_smacro(smac->expansion->text, casesense,
2291 expansion, tmpl);
2292 smac->in_progress = false;
2293 return s;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002294 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002295 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002296
2297 smac->name = nasm_strdup(mname);
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002298 smac->casesense = casesense;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002299 smac->expansion = expansion;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002300 smac->expand = smacro_expand_default;
2301 if (tmpl) {
2302 smac->nparam = tmpl->nparam;
2303 smac->eval_param = tmpl->eval_param;
2304 smac->alias = tmpl->alias;
2305 if (tmpl->expand)
2306 smac->expand = tmpl->expand;
2307 }
2308 if (list_option('m')) {
2309 static const enum preproc_token op[2][2] = {
2310 { PP_DEFINE, PP_IDEFINE },
2311 { PP_DEFALIAS, PP_IDEFALIAS }
2312 };
2313 list_smacro_def(op[!!smac->alias][casesense], ctx, smac);
2314 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08002315 return smac;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002316
2317fail:
2318 free_tlist(expansion);
2319 if (tmpl && tmpl->eval_param)
2320 nasm_free(tmpl->eval_param);
2321 return NULL;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002322}
2323
2324/*
2325 * Undefine an smacro
2326 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002327static void undef_smacro(const char *mname, bool undefalias)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002328{
2329 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002330 struct hash_table *smtbl;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002331 Context *ctx;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002332
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002333 ctx = get_ctx(mname, &mname);
H. Peter Anvin166c2472008-05-28 12:28:58 -07002334 smtbl = ctx ? &ctx->localmac : &smacros;
2335 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002336
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002337 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002338 /*
2339 * We now have a macro name... go hunt for it.
2340 */
2341 sp = smhead;
2342 while ((s = *sp) != NULL) {
2343 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002344 if (s->alias && !undefalias) {
2345 if (s->in_progress) {
2346 nasm_nonfatal("macro alias loop");
2347 } else {
2348 s->in_progress = true;
2349 undef_smacro(s->expansion->text, false);
2350 s->in_progress = false;
2351 }
2352 } else {
2353 if (list_option('m'))
2354 list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
2355 ctx, s);
2356 *sp = s->next;
2357 free_smacro(s);
2358 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002359 } else {
2360 sp = &s->next;
2361 }
2362 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002363 }
2364}
2365
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002366/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002367 * Parse a mmacro specification.
2368 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002369static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002370{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002371 tline = tline->next;
2372 skip_white_(tline);
2373 tline = expand_id(tline);
2374 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002375 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002376 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002377 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002378
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002379 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002380 def->name = nasm_strdup(tline->text);
2381 def->plus = false;
2382 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002383 def->in_progress = 0;
2384 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002385 def->nparam_min = 0;
2386 def->nparam_max = 0;
2387
H. Peter Anvina26433d2008-07-16 14:40:01 -07002388 tline = expand_smacro(tline->next);
2389 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002390 if (!tok_type_(tline, TOK_NUMBER))
2391 nasm_nonfatal("`%s' expects a parameter count", directive);
2392 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002393 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002394 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002395 tline = tline->next->next;
2396 if (tok_is_(tline, "*")) {
2397 def->nparam_max = INT_MAX;
2398 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002399 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002400 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002401 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002402 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002403 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002404 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002405 }
2406 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002407 }
2408 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002409 tline = tline->next;
2410 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002411 }
2412 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002413 !nasm_stricmp(tline->next->text, ".nolist")) {
2414 tline = tline->next;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002415 def->nolist = !list_option('f') || istk->nolist;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002416 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002417
H. Peter Anvina26433d2008-07-16 14:40:01 -07002418 /*
2419 * Handle default parameters.
2420 */
2421 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002422 def->dlist = tline->next;
2423 tline->next = NULL;
2424 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002425 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002426 def->dlist = NULL;
2427 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002428 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002429 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002430
H. Peter Anvin89cee572009-07-15 09:16:54 -04002431 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002432 !def->plus) {
2433 /*
2434 *!macro-defaults [on] macros with more default than optional parameters
2435 *! warns when a macro has more default parameters than optional parameters.
2436 *! See \k{mlmacdef} for why might want to disable this warning.
2437 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002438 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002439 "too many default macro parameters in macro `%s'", def->name);
2440 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002441
H. Peter Anvina26433d2008-07-16 14:40:01 -07002442 return true;
2443}
2444
2445
2446/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002447 * Decode a size directive
2448 */
2449static int parse_size(const char *str) {
2450 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002451 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002452 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002453 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002454 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002455}
2456
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002457/*
2458 * Process a preprocessor %pragma directive. Currently there are none.
2459 * Gets passed the token list starting with the "preproc" token from
2460 * "%pragma preproc".
2461 */
2462static void do_pragma_preproc(Token *tline)
2463{
2464 /* Skip to the real stuff */
2465 tline = tline->next;
2466 skip_white_(tline);
2467 if (!tline)
2468 return;
2469
2470 (void)tline; /* Nothing else to do at present */
2471}
2472
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002473static bool is_macro_id(const Token *t)
2474{
2475 return t && (t->type == TOK_ID ||
2476 (t->type == TOK_PREPROC_ID && t->text[1] == '$'));
2477}
2478
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002479static char *get_id(Token **tp, const char *dname, const char *err)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002480{
2481 char *id;
2482 Token *t = *tp;
2483
2484 t = t->next; /* Skip directive */
2485 skip_white_(t);
2486 t = expand_id(t);
2487
2488 if (!is_macro_id(t)) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002489 nasm_nonfatal("`%s' expects a %s", dname,
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002490 err ? err : "macro identifier");
2491 return NULL;
2492 }
2493
2494 id = t->text;
2495 skip_white_(t);
2496 *tp = t;
2497 return id;
2498}
2499
Ed Beroset3ab3f412002-06-11 03:31:49 +00002500/**
2501 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002502 * Find out if a line contains a preprocessor directive, and deal
2503 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002504 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002505 * If a directive _is_ found, it is the responsibility of this routine
2506 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002507 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002508 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002509 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002510 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002511 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002512 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002513static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002514{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002515 enum preproc_token i;
2516 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002517 bool err;
2518 int nparam;
2519 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002520 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002521 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002522 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002523 char *p, *pp;
2524 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002525 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002526 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002527 Include *inc;
2528 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002529 Cond *cond;
2530 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002531 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002532 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002533 struct tokenval tokval;
2534 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002535 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002536 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002537 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002538 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002539 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002540
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002541 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002542 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002543
H. Peter Anvineba20a72002-04-30 20:53:55 +00002544 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002545 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002546 (tline->text[0] && (tline->text[1] == '%' ||
2547 tline->text[1] == '$' ||
2548 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002549 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002550
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002551 dname = tline->text;
H. Peter Anvin4169a472007-09-12 01:29:43 +00002552 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002553
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002554 casesense = true;
2555 if (PP_HAS_CASE(i) & PP_INSENSITIVE(i)) {
2556 casesense = false;
2557 i--;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002558 }
2559
2560 /*
2561 * If we're in a non-emitting branch of a condition construct,
2562 * or walking to the end of an already terminated %rep block,
2563 * we should ignore all directives except for condition
2564 * directives.
2565 */
2566 if (((istk->conds && !emitting(istk->conds->state)) ||
2567 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2568 return NO_DIRECTIVE_FOUND;
2569 }
2570
2571 /*
2572 * If we're defining a macro or reading a %rep block, we should
2573 * ignore all directives except for %macro/%imacro (which nest),
2574 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2575 * If we're in a %rep block, another %rep nests, so should be let through.
2576 */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002577 if (defining && i != PP_MACRO && i != PP_RMACRO &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002578 i != PP_ENDMACRO && i != PP_ENDM &&
2579 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2580 return NO_DIRECTIVE_FOUND;
2581 }
2582
2583 if (defining) {
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002584 if (i == PP_MACRO || i == PP_RMACRO) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002585 nested_mac_count++;
2586 return NO_DIRECTIVE_FOUND;
2587 } else if (nested_mac_count > 0) {
2588 if (i == PP_ENDMACRO) {
2589 nested_mac_count--;
2590 return NO_DIRECTIVE_FOUND;
2591 }
2592 }
2593 if (!defining->name) {
2594 if (i == PP_REP) {
2595 nested_rep_count++;
2596 return NO_DIRECTIVE_FOUND;
2597 } else if (nested_rep_count > 0) {
2598 if (i == PP_ENDREP) {
2599 nested_rep_count--;
2600 return NO_DIRECTIVE_FOUND;
2601 }
2602 }
2603 }
2604 }
2605
H. Peter Anvin4169a472007-09-12 01:29:43 +00002606 switch (i) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002607 default:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002608 nasm_nonfatal("unknown preprocessor directive `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002609 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002610
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002611 case PP_PRAGMA:
2612 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002613 * %pragma namespace options...
2614 *
2615 * The namespace "preproc" is reserved for the preprocessor;
2616 * all other namespaces generate a [pragma] assembly directive.
2617 *
2618 * Invalid %pragmas are ignored and may have different
2619 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002620 */
H. Peter Anvinf5d7d902019-08-10 06:21:00 -07002621 t = tline;
2622 tline = tline->next;
2623 t->next = NULL;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002624 tline = expand_smacro(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002625 while (tok_type_(tline, TOK_WHITESPACE)) {
2626 t = tline;
2627 tline = tline->next;
2628 delete_Token(t);
2629 }
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002630 if (tok_type_(tline, TOK_ID)) {
2631 if (!nasm_stricmp(tline->text, "preproc")) {
2632 /* Preprocessor pragma */
2633 do_pragma_preproc(tline);
H. Peter Anvin06335872019-08-10 06:42:55 -07002634 free_tlist(tline);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002635 } else {
2636 /* Build the assembler directive */
H. Peter Anvin06335872019-08-10 06:42:55 -07002637
2638 /* Append bracket to the end of the output */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002639 for (t = tline; t->next; t = t->next)
2640 ;
2641 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin06335872019-08-10 06:42:55 -07002642
2643 /* Prepend "[pragma " */
2644 t = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2645 t = new_Token(t, TOK_ID, "pragma", 6);
2646 t = new_Token(t, TOK_OTHER, "[", 1);
2647 tline = t;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002648 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002649 }
2650 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002651 break;
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002652
H. Peter Anvine2c80182005-01-15 22:15:51 +00002653 case PP_STACKSIZE:
2654 /* Directive to tell NASM what the default stack size is. The
2655 * default is for a 16-bit stack, and this can be overriden with
2656 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 */
2658 tline = tline->next;
2659 if (tline && tline->type == TOK_WHITESPACE)
2660 tline = tline->next;
2661 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002662 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002663 }
2664 if (nasm_stricmp(tline->text, "flat") == 0) {
2665 /* All subsequent ARG directives are for a 32-bit stack */
2666 StackSize = 4;
2667 StackPointer = "ebp";
2668 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002669 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002670 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2671 /* All subsequent ARG directives are for a 64-bit stack */
2672 StackSize = 8;
2673 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002674 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002675 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 } else if (nasm_stricmp(tline->text, "large") == 0) {
2677 /* All subsequent ARG directives are for a 16-bit stack,
2678 * far function call.
2679 */
2680 StackSize = 2;
2681 StackPointer = "bp";
2682 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002683 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002684 } else if (nasm_stricmp(tline->text, "small") == 0) {
2685 /* All subsequent ARG directives are for a 16-bit stack,
2686 * far function call. We don't support near functions.
2687 */
2688 StackSize = 2;
2689 StackPointer = "bp";
2690 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002691 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002693 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002694 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002695 break;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002696
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 case PP_ARG:
2698 /* TASM like ARG directive to define arguments to functions, in
2699 * the following form:
2700 *
2701 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2702 */
2703 offset = ArgOffset;
2704 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002705 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002706 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002707
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 /* Find the argument name */
2709 tline = tline->next;
2710 if (tline && tline->type == TOK_WHITESPACE)
2711 tline = tline->next;
2712 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002713 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002714 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 }
2716 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002717
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 /* Find the argument size type */
2719 tline = tline->next;
2720 if (!tline || tline->type != TOK_OTHER
2721 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002722 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002723 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 }
2725 tline = tline->next;
2726 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002727 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002728 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002730
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002732 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002733 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002734 size = parse_size(tt->text);
2735 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002736 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002738 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 }
2740 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002741
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002742 /* Round up to even stack slots */
2743 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002744
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 /* Now define the macro for the argument */
2746 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2747 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002748 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002750
H. Peter Anvine2c80182005-01-15 22:15:51 +00002751 /* Move to the next argument in the list */
2752 tline = tline->next;
2753 if (tline && tline->type == TOK_WHITESPACE)
2754 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002755 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002756 ArgOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002757 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002758
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 case PP_LOCAL:
2760 /* TASM like LOCAL directive to define local variables for a
2761 * function, in the following form:
2762 *
2763 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2764 *
2765 * The '= LocalSize' at the end is ignored by NASM, but is
2766 * required by TASM to define the local parameter size (and used
2767 * by the TASM macro package).
2768 */
2769 offset = LocalOffset;
2770 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002771 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002773
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 /* Find the argument name */
2775 tline = tline->next;
2776 if (tline && tline->type == TOK_WHITESPACE)
2777 tline = tline->next;
2778 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002779 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002780 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 }
2782 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002783
H. Peter Anvine2c80182005-01-15 22:15:51 +00002784 /* Find the argument size type */
2785 tline = tline->next;
2786 if (!tline || tline->type != TOK_OTHER
2787 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002788 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002789 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002790 }
2791 tline = tline->next;
2792 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002793 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002794 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002796
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002798 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 size = parse_size(tt->text);
2801 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002802 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 free_tlist(tt);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002804 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 }
2806 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002807
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002808 /* Round up to even stack slots */
2809 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002810
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002812
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2815 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002816 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002817
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 /* Now define the assign to setup the enter_c macro correctly */
2819 snprintf(directive, sizeof(directive),
2820 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002821 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002822
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 /* Move to the next argument in the list */
2824 tline = tline->next;
2825 if (tline && tline->type == TOK_WHITESPACE)
2826 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002827 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828 LocalOffset = offset;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002829 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002830
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 case PP_CLEAR:
2832 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002833 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002834 free_macros();
2835 init_macros();
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002836 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002837
H. Peter Anvin418ca702008-05-30 10:42:30 -07002838 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002839 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002840 skip_white_(t);
2841 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002842 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002843 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002844 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002845 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002846 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002847 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002848 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002849 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002850 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002851 strlist_add(deplist, p);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002852 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07002853
2854 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002855 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002856 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002857
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002858 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002859 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002860 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002861 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002863 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002864 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002865 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002866 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002867 nasm_unquote_cstr(p, NULL);
H. Peter Anvin6686de22019-08-10 05:33:14 -07002868 nasm_new(inc);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002869 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002870 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002871 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002872 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002873 (pp_mode == PP_DEPS)
2874 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002875 if (!inc->fp) {
2876 /* -MG given but file not found */
2877 nasm_free(inc);
2878 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002879 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002880 inc->lineno = src_set_linnum(0);
2881 inc->lineinc = 1;
2882 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002883 inc->mstk = NULL;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002884 inc->nolist = istk->nolist;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002885 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002886 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002887 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002888 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002889
H. Peter Anvind2456592008-06-19 15:04:18 -07002890 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002891 {
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07002892 const struct use_package *pkg;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002893
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07002894 if (!(mname = get_id(&tline, dname, "package name")))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002895 goto done;
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002896 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002897 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002898 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002899 nasm_unquote_cstr(tline->text, NULL);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07002900 pkg = nasm_find_use_package(tline->text);
2901 if (!pkg) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002902 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07002903 } else if (!use_loaded[pkg->index]) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07002904 /*
2905 * Not already included, go ahead and include it.
2906 * Treat it as an include file for the purpose of
2907 * producing a listing.
2908 */
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07002909 use_loaded[pkg->index] = true;
2910 stdmacpos = pkg->macros;
H. Peter Anvin6686de22019-08-10 05:33:14 -07002911 nasm_new(inc);
2912 inc->next = istk;
2913 inc->fname = src_set_fname(NULL);
2914 inc->lineno = src_set_linnum(0);
2915 inc->lineinc = 0;
2916 inc->expansion = NULL;
2917 inc->mstk = NULL;
2918 inc->nolist = !list_option('b') || istk->nolist;
2919 istk = inc;
2920 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002921 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002922 break;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002923 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002924 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002925 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002926 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002927 tline = tline->next;
2928 skip_white_(tline);
2929 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002930 if (tline) {
2931 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002932 nasm_nonfatal("`%s' expects a context identifier",
2933 pp_directives[i]);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002934 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002935 }
2936 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002937 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002938 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002939 p = nasm_strdup(tline->text);
2940 } else {
2941 p = NULL; /* Anonymous */
2942 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002943
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002944 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002945 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002946 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002947 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002948 ctx->name = p;
2949 ctx->number = unique++;
2950 cstk = ctx;
2951 } else {
2952 /* %pop or %repl */
2953 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002954 nasm_nonfatal("`%s': context stack is empty",
2955 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002956 } else if (i == PP_POP) {
2957 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002958 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002959 "expected %s",
2960 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002961 else
2962 ctx_pop();
2963 } else {
2964 /* i == PP_REPL */
2965 nasm_free(cstk->name);
2966 cstk->name = p;
2967 p = NULL;
2968 }
2969 nasm_free(p);
2970 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07002971 break;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002972 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002973 severity = ERR_FATAL;
2974 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002976 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002977 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002978 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002979 /*!
2980 *!user [on] %warning directives
2981 *! controls output of \c{%warning} directives (see \k{pperror}).
2982 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002983 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002984 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002985
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002986issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002987 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002988 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002989 tline->next = expand_smacro(tline->next);
2990 tline = tline->next;
2991 skip_white_(tline);
2992 t = tline ? tline->next : NULL;
2993 skip_white_(t);
2994 if (tok_type_(tline, TOK_STRING) && !t) {
2995 /* The line contains only a quoted string */
2996 p = tline->text;
2997 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002998 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002999 } else {
3000 /* Not a quoted string, or more than a quoted string */
3001 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08003002 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003003 nasm_free(p);
3004 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003005 break;
H. Peter Anvin7df04172008-06-10 18:27:38 -07003006 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003007
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003008 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003009 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003010 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003011 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003012 j = if_condition(tline->next, i);
3013 tline->next = NULL; /* it got freed */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003014 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003015 cond = nasm_malloc(sizeof(Cond));
3016 cond->next = istk->conds;
3017 cond->state = j;
3018 istk->conds = cond;
3019 if(istk->mstk)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003020 istk->mstk->condcnt++;
3021 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003022
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00003023 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003024 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003025 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003026 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003027 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003028 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003029 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003030
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003031 case COND_DONE:
3032 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003033 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003034
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003035 case COND_ELSE_TRUE:
3036 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003037 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003038 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003039 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003040 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003041
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003042 case COND_IF_FALSE:
3043 /*
3044 * IMPORTANT: In the case of %if, we will already have
3045 * called expand_mmac_params(); however, if we're
3046 * processing an %elif we must have been in a
3047 * non-emitting mode, which would have inhibited
3048 * the normal invocation of expand_mmac_params().
3049 * Therefore, we have to do it explicitly here.
3050 */
3051 j = if_condition(expand_mmac_params(tline->next), i);
3052 tline->next = NULL; /* it got freed */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003053 istk->conds->state = j;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003054 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 }
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003056 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003057
H. Peter Anvine2c80182005-01-15 22:15:51 +00003058 case PP_ELSE:
3059 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003060 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003061 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003062 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07003063 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003064 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003065 case COND_IF_TRUE:
3066 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003067 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003068 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003069
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003070 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003071 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003072
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003073 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003074 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003075 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003076
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003077 case COND_ELSE_TRUE:
3078 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003079 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003080 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003081 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003082 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02003083 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003084 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003085
H. Peter Anvine2c80182005-01-15 22:15:51 +00003086 case PP_ENDIF:
3087 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003088 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003089 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003090 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003091 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003092 cond = istk->conds;
3093 istk->conds = cond->next;
3094 nasm_free(cond);
3095 if(istk->mstk)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003096 istk->mstk->condcnt--;
3097 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003098
H. Peter Anvin8b262472019-02-26 14:00:54 -08003099 case PP_RMACRO:
3100 case PP_MACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003101 if (defining)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003102 nasm_fatal("`%s': already defining a macro", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003103
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003104 nasm_new(defining);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003105 defining->casesense = casesense;
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07003106 if (i == PP_RMACRO)
3107 defining->max_depth = nasm_limit[LIMIT_MACRO_LEVELS];
H. Peter Anvin8b262472019-02-26 14:00:54 -08003108 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003109 nasm_free(defining);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003110 goto done;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003111 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003112
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003113 src_get(&defining->xline, &defining->fname);
3114
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003115 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3116 while (mmac) {
3117 if (!strcmp(mmac->name, defining->name) &&
3118 (mmac->nparam_min <= defining->nparam_max
3119 || defining->plus)
3120 && (defining->nparam_min <= mmac->nparam_max
3121 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003122 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003123 defining->name);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003124 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003125 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003126 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003127 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003128 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003129
H. Peter Anvine2c80182005-01-15 22:15:51 +00003130 case PP_ENDM:
3131 case PP_ENDMACRO:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003132 if (!(defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003133 nasm_nonfatal("`%s': not defining a macro", tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003134 goto done;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003135 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003136 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3137 defining->next = *mmhead;
3138 *mmhead = defining;
3139 defining = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003140 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003141
H. Peter Anvin89cee572009-07-15 09:16:54 -04003142 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003143 /*
3144 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003145 * macro-end marker for a macro with a name. Then we
3146 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003147 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003148 list_for_each(l, istk->expansion)
3149 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003150 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003151
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003152 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003153 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003154 * Remove all conditional entries relative to this
3155 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003156 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003157 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3158 cond = istk->conds;
3159 istk->conds = cond->next;
3160 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003161 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003162 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003163 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003164 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003165 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003166 break;
Keith Kanios852f1ee2009-07-12 00:19:55 -05003167
H. Peter Anvina26433d2008-07-16 14:40:01 -07003168 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003169 casesense = false;
3170 /* fall through */
3171 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003172 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003173 MMacro **mmac_p;
3174 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003175
H. Peter Anvin8b262472019-02-26 14:00:54 -08003176 spec.casesense = casesense;
3177 if (!parse_mmacro_spec(tline, &spec, dname)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003178 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003179 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003180 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3181 while (mmac_p && *mmac_p) {
3182 mmac = *mmac_p;
3183 if (mmac->casesense == spec.casesense &&
3184 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3185 mmac->nparam_min == spec.nparam_min &&
3186 mmac->nparam_max == spec.nparam_max &&
3187 mmac->plus == spec.plus) {
3188 *mmac_p = mmac->next;
3189 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003190 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003191 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003192 }
3193 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003194 free_tlist(spec.dlist);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003195 break;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003196 }
3197
H. Peter Anvine2c80182005-01-15 22:15:51 +00003198 case PP_ROTATE:
3199 if (tline->next && tline->next->type == TOK_WHITESPACE)
3200 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003201 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003202 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003203 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 return DIRECTIVE_FOUND;
3205 }
3206 t = expand_smacro(tline->next);
3207 tline->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003208 pps.tptr = tline = t;
3209 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 tokval.t_type = TOKEN_INVALID;
3211 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003212 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 free_tlist(tline);
3214 if (!evalresult)
3215 return DIRECTIVE_FOUND;
3216 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003217 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003218 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003219 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003220 return DIRECTIVE_FOUND;
3221 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003222 mmac = istk->mstk;
3223 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3224 mmac = mmac->next_active;
3225 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003226 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003227 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003228 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003229 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003230 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003231
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003232 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003233 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003234 rotate += mmac->nparam;
3235
3236 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003237 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003238 break;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003239
H. Peter Anvine2c80182005-01-15 22:15:51 +00003240 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003241 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003242 do {
3243 tline = tline->next;
3244 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003245
H. Peter Anvine2c80182005-01-15 22:15:51 +00003246 if (tok_type_(tline, TOK_ID) &&
3247 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6686de22019-08-10 05:33:14 -07003248 nolist = !list_option('f') || istk->nolist;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003249 do {
3250 tline = tline->next;
3251 } while (tok_type_(tline, TOK_WHITESPACE));
3252 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003253
H. Peter Anvine2c80182005-01-15 22:15:51 +00003254 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003255 pps.tptr = expand_smacro(tline);
3256 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003257 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003258 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003259 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003260 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003261 if (!evalresult)
3262 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003264 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003266 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003267 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003269 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003270 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003271 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3272 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003273 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003274 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003275 /*!
3276 *!negative-rep [on] regative %rep count
3277 *! warns about negative counts given to the \c{%rep}
3278 *! preprocessor directive.
3279 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003280 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003281 "negative `%%rep' count: %"PRId64, count);
3282 count = 0;
3283 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003284 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003285 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003287 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003288 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003289 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003290 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003291 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003292 defining->nolist = nolist;
3293 defining->in_progress = count;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003294 defining->next_active = istk->mstk;
3295 defining->rep_nest = tmp_defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003296 src_get(&defining->xline, &defining->fname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003297 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003298
H. Peter Anvine2c80182005-01-15 22:15:51 +00003299 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003300 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003301 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003302 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003303 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003304
H. Peter Anvine2c80182005-01-15 22:15:51 +00003305 /*
3306 * Now we have a "macro" defined - although it has no name
3307 * and we won't be entering it in the hash tables - we must
3308 * push a macro-end marker for it on to istk->expansion.
3309 * After that, it will take care of propagating itself (a
3310 * macro-end marker line for a macro which is really a %rep
3311 * block will cause the macro to be re-expanded, complete
3312 * with another macro-end marker to ensure the process
3313 * continues) until the whole expansion is forcibly removed
3314 * from istk->expansion by a %exitrep.
3315 */
H. Peter Anvin6686de22019-08-10 05:33:14 -07003316 nasm_new(l);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003317 l->next = istk->expansion;
3318 l->finishes = defining;
3319 l->first = NULL;
3320 istk->expansion = l;
3321
3322 istk->mstk = defining;
3323
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003324 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003325 tmp_defining = defining;
3326 defining = defining->rep_nest;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003327 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003328
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003330 /*
3331 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003332 * macro-end marker for a macro with no name. Then we set
3333 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003334 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003335 list_for_each(l, istk->expansion)
3336 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003337 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003338
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003339 if (l)
3340 l->finishes->in_progress = 1;
3341 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003342 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003343 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003344
H. Peter Anvin8b262472019-02-26 14:00:54 -08003345 case PP_DEFINE:
3346 case PP_XDEFINE:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003347 case PP_DEFALIAS:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003348 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003349 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003350 bool *eval_params = NULL;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003351 SMacro tmpl;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003352
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003353 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003354 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003355
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003356 nasm_zero(tmpl);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003357 last = tline;
3358 param_start = tline = tline->next;
3359 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003360
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 if (tok_is_(tline, "(")) {
3362 /*
3363 * This macro has parameters.
3364 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003365
H. Peter Anvine2c80182005-01-15 22:15:51 +00003366 tline = tline->next;
3367 while (1) {
3368 skip_white_(tline);
3369 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003370 nasm_nonfatal("parameter identifier expected");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003371 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003372 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003373 if (tok_is_(tline, "=")) {
3374 have_eval_params = true;
3375 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003376 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003377 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003378
3379 /*
3380 * "*" means an unnamed, ignored argument; it can never
3381 * match anything because "*" is not TOK_ID, and so
3382 * none of the expansion entries will be converted
3383 * to parameters.
3384 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003385 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003386 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3387 /*
3388 * Empty name; duplicate the termination
3389 * token and use the first copy as a placeholder.
3390 * It will never match anything, since the
3391 * associated text doesn't correspond to a TOK_ID.
3392 */
3393 tline = dup_Token(tline, tline);
3394 } else {
3395 nasm_nonfatal("`%s': parameter identifier expected",
3396 tline->text);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003397 goto done;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003398 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003400 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003401 tline = tline->next;
3402 skip_white_(tline);
3403 if (tok_is_(tline, ",")) {
3404 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003405 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003406 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003407 nasm_nonfatal("`)' expected to terminate macro template");
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003408 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003409 }
3410 break;
3411 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003412 }
3413 last = tline;
3414 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003415
3416 if (have_eval_params) {
3417 /* Create evaluated parameters table */
3418 bool is_eval = false;
3419
3420 nasm_newn(eval_params, nparam);
3421 list_for_each(tt, param_start) {
3422 if (is_smac_param(tt->type))
3423 eval_params[smac_nparam(tt->type)] = is_eval;
3424 is_eval = tok_is_(tt, "=");
3425 }
3426 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003428
H. Peter Anvine2c80182005-01-15 22:15:51 +00003429 if (tok_type_(tline, TOK_WHITESPACE))
3430 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003432
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003433 if (unlikely(i == PP_DEFALIAS)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003434 macro_start = tline;
3435 if (!is_macro_id(macro_start)) {
3436 nasm_nonfatal("`%s' expects a macro identifier to alias",
3437 dname);
3438 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003440 tt = macro_start->next;
3441 macro_start->next = NULL;
3442 tline = tline->next;
3443 skip_white_(tline);
3444 if (tline && tline->type) {
3445 nasm_warn(WARN_OTHER,
3446 "trailing garbage after aliasing identifier ignored");
3447 }
3448 free_tlist(tt);
3449 tmpl.alias = true;
3450 } else {
3451 /* Expand the macro definition now for %xdefine and %ixdefine */
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003452 if (i == PP_XDEFINE)
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003453 tline = expand_smacro(tline);
3454
3455 macro_start = NULL;
3456 t = tline;
3457 while (t) {
3458 if (t->type == TOK_ID) {
3459 list_for_each(tt, param_start)
3460 if (is_smac_param(tt->type) &&
3461 tt->len == t->len &&
3462 !memcmp(tt->text, t->text, tt->len))
3463 t->type = tt->type;
3464 }
3465 tt = t->next;
3466 t->next = macro_start;
3467 macro_start = t;
3468 t = tt;
3469 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003470 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003471
H. Peter Anvine2c80182005-01-15 22:15:51 +00003472 /*
3473 * Good. We now have a macro name, a parameter count, and a
3474 * token list (in reverse order) for an expansion. We ought
3475 * to be OK just to create an SMacro, store it, and let
3476 * free_tlist have the rest of the line (which we have
3477 * carefully re-terminated after chopping off the expansion
3478 * from the end).
3479 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003480 tmpl.nparam = nparam;
3481 tmpl.eval_param = eval_params;
3482 define_smacro(mname, casesense, macro_start, &tmpl);
3483 break;
3484 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003485
H. Peter Anvine2c80182005-01-15 22:15:51 +00003486 case PP_UNDEF:
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003487 case PP_UNDEFALIAS:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003488 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003489 goto done;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003490 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003491 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003492
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003493 undef_smacro(mname, i == PP_UNDEFALIAS);
3494 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003495
H. Peter Anvin8b262472019-02-26 14:00:54 -08003496 case PP_DEFSTR:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003497 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003498 goto done;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003499
H. Peter Anvin9e200162008-06-04 17:23:14 -07003500 last = tline;
3501 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003502 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003503
3504 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003505 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003506
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003507 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003508 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003509 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003510
3511 /*
3512 * We now have a macro name, an implicit parameter count of
3513 * zero, and a string token to use as an expansion. Create
3514 * and store an SMacro.
3515 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003516 define_smacro(mname, casesense, macro_start, NULL);
3517 break;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003518
H. Peter Anvin8b262472019-02-26 14:00:54 -08003519 case PP_DEFTOK:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003520 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003521 goto done;
3522
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003523 last = tline;
3524 tline = expand_smacro(tline->next);
3525 last->next = NULL;
3526
3527 t = tline;
3528 while (tok_type_(t, TOK_WHITESPACE))
3529 t = t->next;
3530 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003531 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003532 nasm_nonfatal("`%s' requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003533 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003534 goto done;
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003535 }
3536
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003537 /*
3538 * Convert the string to a token stream. Note that smacros
3539 * are stored with the token stream reversed, so we have to
3540 * reverse the output of tokenize().
3541 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003542 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003543 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003544
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003545 /*
3546 * We now have a macro name, an implicit parameter count of
3547 * zero, and a numeric token to use as an expansion. Create
3548 * and store an SMacro.
3549 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003550 define_smacro(mname, casesense, macro_start, NULL);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003551 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003552 break;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003553
H. Peter Anvin418ca702008-05-30 10:42:30 -07003554 case PP_PATHSEARCH:
3555 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003556 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003557
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003558 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003559 goto done;
3560
H. Peter Anvin418ca702008-05-30 10:42:30 -07003561 last = tline;
3562 tline = expand_smacro(tline->next);
3563 last->next = NULL;
3564
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003565 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003566 while (tok_type_(t, TOK_WHITESPACE))
3567 t = t->next;
3568
3569 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003570 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003571 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003572 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003573 goto done;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003574 }
3575 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003576 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003577 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003578 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003579 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003580
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003581 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003582 if (!found_path)
3583 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003584 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003585
3586 /*
3587 * We now have a macro name, an implicit parameter count of
3588 * zero, and a string token to use as an expansion. Create
3589 * and store an SMacro.
3590 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003591 define_smacro(mname, casesense, macro_start, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003592 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003593 break;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003594 }
3595
H. Peter Anvine2c80182005-01-15 22:15:51 +00003596 case PP_STRLEN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003597 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003598 goto done;
3599
H. Peter Anvine2c80182005-01-15 22:15:51 +00003600 last = tline;
3601 tline = expand_smacro(tline->next);
3602 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003603
H. Peter Anvine2c80182005-01-15 22:15:51 +00003604 t = tline;
3605 while (tok_type_(t, TOK_WHITESPACE))
3606 t = t->next;
3607 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003608 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003609 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 free_tlist(tline);
3611 free_tlist(origline);
3612 return DIRECTIVE_FOUND;
3613 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003614
H. Peter Anvin8b262472019-02-26 14:00:54 -08003615 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003616
H. Peter Anvine2c80182005-01-15 22:15:51 +00003617 /*
3618 * We now have a macro name, an implicit parameter count of
3619 * zero, and a numeric token to use as an expansion. Create
3620 * and store an SMacro.
3621 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003622 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003623 free_tlist(tline);
3624 free_tlist(origline);
3625 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003626
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003627 case PP_STRCAT:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003628 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003629 goto done;
3630
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003631 last = tline;
3632 tline = expand_smacro(tline->next);
3633 last->next = NULL;
3634
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003636 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003637 switch (t->type) {
3638 case TOK_WHITESPACE:
3639 break;
3640 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003641 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003642 break;
3643 case TOK_OTHER:
3644 if (!strcmp(t->text, ",")) /* permit comma separators */
3645 break;
3646 /* else fall through */
3647 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003648 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003649 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003650 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003651 }
3652 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003653
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003654 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003655 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003656 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003657 memcpy(p, t->text, t->len);
3658 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003659 }
3660 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003661
3662 /*
3663 * We now have a macro name, an implicit parameter count of
3664 * zero, and a numeric token to use as an expansion. Create
3665 * and store an SMacro.
3666 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003667 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003668 nasm_free(pp);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003669 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003670 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003671 break;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003672
H. Peter Anvine2c80182005-01-15 22:15:51 +00003673 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003674 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003675 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003676 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003677
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003678 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003679 goto done;
3680
H. Peter Anvine2c80182005-01-15 22:15:51 +00003681 last = tline;
3682 tline = expand_smacro(tline->next);
3683 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003684
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003685 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003686 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 while (tok_type_(t, TOK_WHITESPACE))
3688 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003689
H. Peter Anvine2c80182005-01-15 22:15:51 +00003690 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003691 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003692 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003693 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003694 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003696
H. Peter Anvin8b262472019-02-26 14:00:54 -08003697 pps.tptr = t->next;
3698 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003699 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003700 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 if (!evalresult) {
3702 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003703 goto done;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003704 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003705 nasm_nonfatal("non-constant value given to `%s'", 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 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003709 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003710
H. Peter Anvin8b262472019-02-26 14:00:54 -08003711 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3712 pps.tptr = pps.tptr->next;
3713 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003714 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003715 } else {
3716 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003717 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003718 if (!evalresult) {
3719 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003720 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003721 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003722 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003723 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003724 goto done;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003725 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003726 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003727 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003728
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003729 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003730
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003731 /* make start and count being in range */
3732 if (start < 0)
3733 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003734 if (count < 0)
3735 count = len + count + 1 - start;
3736 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003737 count = len - start;
3738 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003739 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003740
H. Peter Anvin8b262472019-02-26 14:00:54 -08003741 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003742 macro_start->len = count;
3743 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3744 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003745
H. Peter Anvine2c80182005-01-15 22:15:51 +00003746 /*
3747 * We now have a macro name, an implicit parameter count of
3748 * zero, and a numeric token to use as an expansion. Create
3749 * and store an SMacro.
3750 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003751 define_smacro(mname, casesense, macro_start, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003752 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003753 break;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003754 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003755
H. Peter Anvin8b262472019-02-26 14:00:54 -08003756 case PP_ASSIGN:
H. Peter Anvin (Intel)97cbdd32019-08-15 01:14:23 -07003757 if (!(mname = get_id(&tline, dname, NULL)))
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003758 goto done;
3759
H. Peter Anvine2c80182005-01-15 22:15:51 +00003760 last = tline;
3761 tline = expand_smacro(tline->next);
3762 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003763
H. Peter Anvin8b262472019-02-26 14:00:54 -08003764 pps.tptr = tline;
3765 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003767 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003768 free_tlist(tline);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003769 if (!evalresult)
3770 goto done;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003771
H. Peter Anvine2c80182005-01-15 22:15:51 +00003772 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003773 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003774
H. Peter Anvine2c80182005-01-15 22:15:51 +00003775 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003776 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003777 free_tlist(origline);
3778 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003779 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003780
H. Peter Anvin8b262472019-02-26 14:00:54 -08003781 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003782
H. Peter Anvine2c80182005-01-15 22:15:51 +00003783 /*
3784 * We now have a macro name, an implicit parameter count of
3785 * zero, and a numeric token to use as an expansion. Create
3786 * and store an SMacro.
3787 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003788 define_smacro(mname, casesense, macro_start, NULL);
3789 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003790
H. Peter Anvine2c80182005-01-15 22:15:51 +00003791 case PP_LINE:
3792 /*
3793 * Syntax is `%line nnn[+mmm] [filename]'
3794 */
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003795 if (unlikely(pp_noline))
3796 goto done;
3797
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 tline = tline->next;
3799 skip_white_(tline);
3800 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003801 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003802 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003803 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003804 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003805 m = 1;
3806 tline = tline->next;
3807 if (tok_is_(tline, "+")) {
3808 tline = tline->next;
3809 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003810 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003811 goto done;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003812 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003813 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 tline = tline->next;
3815 }
3816 skip_white_(tline);
3817 src_set_linnum(k);
3818 istk->lineinc = m;
3819 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003820 char *fname = detoken(tline, false);
3821 src_set_fname(fname);
3822 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003823 }
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07003824 break;
3825 }
3826
3827done:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003828 free_tlist(origline);
3829 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003830}
3831
3832/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003833 * Ensure that a macro parameter contains a condition code and
3834 * nothing else. Return the condition code index if so, or -1
3835 * otherwise.
3836 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003837static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003838{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003839 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003840
H. Peter Anvin25a99342007-09-22 17:45:45 -07003841 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003842 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003843
H. Peter Anvineba20a72002-04-30 20:53:55 +00003844 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003845 if (!t)
3846 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003847 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003848 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003849 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003850 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003851 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003852 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003853
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003854 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003855}
3856
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003857/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003858 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003859 * pasting, if @handle_explicit passed then explicit pasting
3860 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003861 * The @m array can contain a series of token types which are
3862 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003863 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003864static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003865 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003866{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003867 Token *tok, *next, **prev_next, **prev_nonspace;
3868 bool pasted = false;
3869 char *buf, *p;
3870 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003871
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003872 /*
3873 * The last token before pasting. We need it
3874 * to be able to connect new handled tokens.
3875 * In other words if there were a tokens stream
3876 *
3877 * A -> B -> C -> D
3878 *
3879 * and we've joined tokens B and C, the resulting
3880 * stream should be
3881 *
3882 * A -> BC -> D
3883 */
3884 tok = *head;
3885 prev_next = NULL;
3886
3887 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3888 prev_nonspace = head;
3889 else
3890 prev_nonspace = NULL;
3891
3892 while (tok && (next = tok->next)) {
3893
3894 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003895 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003896 /* Zap redundant whitespaces */
3897 while (tok_type_(next, TOK_WHITESPACE))
3898 next = delete_Token(next);
3899 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003900 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003901
3902 case TOK_PASTE:
3903 /* Explicit pasting */
3904 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003905 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003906 next = delete_Token(tok);
3907
3908 while (tok_type_(next, TOK_WHITESPACE))
3909 next = delete_Token(next);
3910
3911 if (!pasted)
3912 pasted = true;
3913
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003914 /* Left pasting token is start of line */
3915 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003916 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003917
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003918 /*
3919 * No ending token, this might happen in two
3920 * cases
3921 *
3922 * 1) There indeed no right token at all
3923 * 2) There is a bare "%define ID" statement,
3924 * and @ID does expand to whitespace.
3925 *
3926 * So technically we need to do a grammar analysis
3927 * in another stage of parsing, but for now lets don't
3928 * change the behaviour people used to. Simply allow
3929 * whitespace after paste token.
3930 */
3931 if (!next) {
3932 /*
3933 * Zap ending space tokens and that's all.
3934 */
3935 tok = (*prev_nonspace)->next;
3936 while (tok_type_(tok, TOK_WHITESPACE))
3937 tok = delete_Token(tok);
3938 tok = *prev_nonspace;
3939 tok->next = NULL;
3940 break;
3941 }
3942
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003943 tok = *prev_nonspace;
3944 while (tok_type_(tok, TOK_WHITESPACE))
3945 tok = delete_Token(tok);
3946 len = strlen(tok->text);
3947 len += strlen(next->text);
3948
3949 p = buf = nasm_malloc(len + 1);
3950 strcpy(p, tok->text);
3951 p = strchr(p, '\0');
3952 strcpy(p, next->text);
3953
3954 delete_Token(tok);
3955
3956 tok = tokenize(buf);
3957 nasm_free(buf);
3958
3959 *prev_nonspace = tok;
3960 while (tok && tok->next)
3961 tok = tok->next;
3962
3963 tok->next = delete_Token(next);
3964
3965 /* Restart from pasted tokens head */
3966 tok = *prev_nonspace;
3967 break;
3968
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003969 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003970 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003971 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003972 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3973 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003974
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003975 len = 0;
3976 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3977 len += strlen(next->text);
3978 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003979 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003980
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003981 /* No match or no text to process */
3982 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003983 break;
3984
3985 len += strlen(tok->text);
3986 p = buf = nasm_malloc(len + 1);
3987
Adam Majer1a069432017-07-25 11:12:35 +02003988 strcpy(p, tok->text);
3989 p = strchr(p, '\0');
3990 tok = delete_Token(tok);
3991
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003992 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003993 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3994 strcpy(p, tok->text);
3995 p = strchr(p, '\0');
3996 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003997 tok = delete_Token(tok);
3998 }
3999
4000 tok = tokenize(buf);
4001 nasm_free(buf);
4002
4003 if (prev_next)
4004 *prev_next = tok;
4005 else
4006 *head = tok;
4007
4008 /*
4009 * Connect pasted into original stream,
4010 * ie A -> new-tokens -> B
4011 */
4012 while (tok && tok->next)
4013 tok = tok->next;
4014 tok->next = next;
4015
4016 if (!pasted)
4017 pasted = true;
4018
4019 /* Restart from pasted tokens head */
4020 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004021 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004022
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004023 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004024 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004025
4026 prev_next = &tok->next;
4027
4028 if (tok->next &&
4029 !tok_type_(tok->next, TOK_WHITESPACE) &&
4030 !tok_type_(tok->next, TOK_PASTE))
4031 prev_nonspace = prev_next;
4032
4033 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004034 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004035
4036 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004037}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004038
4039/*
4040 * expands to a list of tokens from %{x:y}
4041 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004042static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004043{
4044 Token *t = tline, **tt, *tm, *head;
4045 char *pos;
4046 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004047
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004048 pos = strchr(tline->text, ':');
4049 nasm_assert(pos);
4050
4051 lst = atoi(pos + 1);
4052 fst = atoi(tline->text + 1);
4053
4054 /*
4055 * only macros params are accounted so
4056 * if someone passes %0 -- we reject such
4057 * value(s)
4058 */
4059 if (lst == 0 || fst == 0)
4060 goto err;
4061
4062 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004063 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4064 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004065 goto err;
4066
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004067 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4068 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004069
4070 /* counted from zero */
4071 fst--, lst--;
4072
4073 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004074 * It will be at least one token. Note we
4075 * need to scan params until separator, otherwise
4076 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004077 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004078 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004079 if (!tm)
4080 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004081 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004082 tt = &head->next, tm = tm->next;
4083 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004084 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004085 *tt = t, tt = &t->next, tm = tm->next;
4086 }
4087
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004088 if (fst < lst) {
4089 for (i = fst + 1; i <= lst; i++) {
4090 t = new_Token(NULL, TOK_OTHER, ",", 0);
4091 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004092 j = (i + mac->rotate) % mac->nparam;
4093 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004094 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004095 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004096 *tt = t, tt = &t->next, tm = tm->next;
4097 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004098 }
4099 } else {
4100 for (i = fst - 1; i >= lst; i--) {
4101 t = new_Token(NULL, TOK_OTHER, ",", 0);
4102 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004103 j = (i + mac->rotate) % mac->nparam;
4104 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004105 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004106 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004107 *tt = t, tt = &t->next, tm = tm->next;
4108 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004109 }
4110 }
4111
4112 *last = tt;
4113 return head;
4114
4115err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004116 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004117 &tline->text[1]);
4118 return tline;
4119}
4120
H. Peter Anvin76690a12002-04-30 20:52:49 +00004121/*
4122 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004123 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004124 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004125 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004126static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004127{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004128 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004129 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004130 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004131
4132 tail = &thead;
4133 thead = NULL;
4134
H. Peter Anvine2c80182005-01-15 22:15:51 +00004135 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004136 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004137 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4138 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4139 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004140 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004141 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004142 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004143 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004144 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004145 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004146
H. Peter Anvine2c80182005-01-15 22:15:51 +00004147 t = tline;
4148 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004149
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004150 mac = istk->mstk;
4151 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4152 mac = mac->next_active;
4153 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004154 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004155 } else {
4156 pos = strchr(t->text, ':');
4157 if (!pos) {
4158 switch (t->text[1]) {
4159 /*
4160 * We have to make a substitution of one of the
4161 * forms %1, %-1, %+1, %%foo, %0.
4162 */
4163 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004164 type = TOK_NUMBER;
4165 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4166 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004167 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004168 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004169 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004170 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004171 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004172 text = nasm_strcat(tmpbuf, t->text + 2);
4173 break;
4174 case '-':
4175 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004176 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004177 tt = NULL;
4178 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004179 if (mac->nparam > 1)
4180 n = (n + mac->rotate) % mac->nparam;
4181 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004182 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004183 cc = find_cc(tt);
4184 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004185 nasm_nonfatal("macro parameter %d is not a condition code",
4186 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004187 text = NULL;
4188 } else {
4189 type = TOK_ID;
4190 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004191 nasm_nonfatal("condition code `%s' is not invertible",
4192 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004193 text = NULL;
4194 } else
4195 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4196 }
4197 break;
4198 case '+':
4199 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004200 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004201 tt = NULL;
4202 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004203 if (mac->nparam > 1)
4204 n = (n + mac->rotate) % mac->nparam;
4205 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004206 }
4207 cc = find_cc(tt);
4208 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004209 nasm_nonfatal("macro parameter %d is not a condition code",
4210 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004211 text = NULL;
4212 } else {
4213 type = TOK_ID;
4214 text = nasm_strdup(conditions[cc]);
4215 }
4216 break;
4217 default:
4218 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004219 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004220 tt = NULL;
4221 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004222 if (mac->nparam > 1)
4223 n = (n + mac->rotate) % mac->nparam;
4224 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004225 }
4226 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004227 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004228 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004229 tail = &(*tail)->next;
4230 tt = tt->next;
4231 }
4232 }
4233 text = NULL; /* we've done it here */
4234 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004235 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004236 } else {
4237 /*
4238 * seems we have a parameters range here
4239 */
4240 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004241 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004242 if (head != t) {
4243 *tail = head;
4244 *last = tline;
4245 tline = head;
4246 text = NULL;
4247 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004248 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004249 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004250 if (!text) {
4251 delete_Token(t);
4252 } else {
4253 *tail = t;
4254 tail = &t->next;
4255 t->type = type;
4256 nasm_free(t->text);
4257 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004258 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004259 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004260 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004262 } else if (tline->type == TOK_INDIRECT) {
4263 t = tline;
4264 tline = tline->next;
4265 tt = tokenize(t->text);
4266 tt = expand_mmac_params(tt);
4267 tt = expand_smacro(tt);
4268 *tail = tt;
4269 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004270 tail = &tt->next;
4271 tt = tt->next;
4272 }
4273 delete_Token(t);
4274 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004275 } else {
4276 t = *tail = tline;
4277 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004278 tail = &t->next;
4279 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004280 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004281 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004282
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004283 if (changed) {
4284 const struct tokseq_match t[] = {
4285 {
4286 PP_CONCAT_MASK(TOK_ID) |
4287 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4288 PP_CONCAT_MASK(TOK_ID) |
4289 PP_CONCAT_MASK(TOK_NUMBER) |
4290 PP_CONCAT_MASK(TOK_FLOAT) |
4291 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4292 },
4293 {
4294 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4295 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4296 }
4297 };
4298 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4299 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004300
H. Peter Anvin76690a12002-04-30 20:52:49 +00004301 return thead;
4302}
4303
H. Peter Anvin322bee02019-08-10 01:38:06 -07004304static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004305
H. Peter Anvin76690a12002-04-30 20:52:49 +00004306/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004307 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004308 * a macro at all, it is simply copied to the output and the pointer
4309 * advanced. tpp should be a pointer to a pointer (usually the next
4310 * pointer of the previous token) to the first token. **tpp is updated
4311 * to point to the last token of the expansion, and *tpp updated to
4312 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004313 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004314 * If the expansion is empty, *tpp will be unchanged but **tpp will
4315 * be advanced past the macro call.
4316 *
H. Peter Anvin322bee02019-08-10 01:38:06 -07004317 * Return the macro expanded, or NULL if no expansion took place.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004318 */
H. Peter Anvin322bee02019-08-10 01:38:06 -07004319static SMacro *expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004320{
4321 Token **params = NULL;
4322 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004323 Token *tline = **tpp;
4324 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004325 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004326 unsigned int i;
4327 Token *t, *tup, *ttail;
4328 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004329
4330 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004331 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004332
4333 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004334
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004335 smacro_deadman.total--;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004336 smacro_deadman.levels--;
4337
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004338 if (unlikely(smacro_deadman.total < 0 || smacro_deadman.levels < 0)) {
H. Peter Anvin322bee02019-08-10 01:38:06 -07004339 if (unlikely(!smacro_deadman.triggered)) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004340 nasm_nonfatal("interminable macro recursion");
H. Peter Anvin322bee02019-08-10 01:38:06 -07004341 smacro_deadman.triggered = true;
4342 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004343 goto not_a_macro;
4344 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004345 head = (SMacro *)hash_findix(&smacros, mname);
4346 } else if (tline->type == TOK_PREPROC_ID) {
4347 Context *ctx = get_ctx(mname, &mname);
4348 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4349 } else {
4350 goto not_a_macro;
4351 }
4352
4353 /*
4354 * We've hit an identifier of some sort. First check whether the
4355 * identifier is a single-line macro at all, then think about
4356 * checking for parameters if necessary.
4357 */
4358 list_for_each(m, head) {
4359 if (!mstrcmp(m->name, mname, m->casesense))
4360 break;
4361 }
4362
4363 if (!m) {
4364 goto not_a_macro;
4365 }
4366
4367 /* Parse parameters, if applicable */
4368
4369 params = NULL;
4370 nparam = 0;
4371
4372 if (m->nparam == 0) {
4373 /*
4374 * Simple case: the macro is parameterless.
4375 * Nothing to parse; the expansion code will
4376 * drop the macro name token.
4377 */
4378 } else {
4379 /*
4380 * Complicated case: at least one macro with this name
4381 * exists and takes parameters. We must find the
4382 * parameters in the call, count them, find the SMacro
4383 * that corresponds to that form of the macro call, and
4384 * substitute for the parameters when we expand. What a
4385 * pain.
4386 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004387 Token **phead, **pep;
4388 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004389 int white = 0;
4390 int brackets = 0;
4391 bool bracketed = false;
4392 bool bad_bracket = false;
4393 unsigned int sparam = PARAM_DELTA;
4394
4395 tline = tline->next;
4396
4397 while (tok_type_(tline, TOK_WHITESPACE)) {
4398 tline = tline->next;
4399 }
4400 if (!tok_is_(tline, "(")) {
4401 /*
4402 * This macro wasn't called with parameters: ignore
4403 * the call. (Behaviour borrowed from gnu cpp.)
4404 */
4405 goto not_a_macro;
4406 }
4407
4408 paren = 1;
4409 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004410 nasm_newn(params, sparam);
4411 phead = pep = &params[0];
4412 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004413
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004414 while (paren) {
4415 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004416 char ch;
4417
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004418 tline = tline->next;
4419
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004420 if (!tline) {
4421 nasm_nonfatal("macro call expects terminating `)'");
4422 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004423 }
4424
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004425 ch = 0;
4426 skip = false;
4427
4428 switch (tline->type) {
4429 case TOK_OTHER:
4430 if (!tline->text[1])
4431 ch = tline->text[0];
4432 break;
4433
4434 case TOK_WHITESPACE:
4435 if (brackets || *phead)
4436 white++; /* Keep interior whitespace */
4437 skip = true;
4438 break;
4439
4440 default:
4441 break;
4442 }
4443
4444 switch (ch) {
4445 case ',':
4446 if (!brackets) {
4447 if (++nparam >= sparam) {
4448 sparam += PARAM_DELTA;
4449 params = nasm_realloc(params, sparam * sizeof *params);
4450 }
4451 pep = &params[nparam];
4452 *pep = NULL;
4453 bracketed = false;
4454 skip = true;
4455 }
4456 break;
4457
4458 case '{':
4459 if (!bracketed) {
4460 bracketed = !*phead;
4461 skip = true;
4462 }
4463 brackets++;
4464 break;
4465
4466 case '}':
4467 if (brackets > 0) {
4468 if (!--brackets)
4469 skip = bracketed;
4470 }
4471 break;
4472
4473 case '(':
4474 if (!brackets)
4475 paren++;
4476 break;
4477
4478 case ')':
4479 if (!brackets) {
4480 paren--;
4481 if (!paren) {
4482 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004483 /* Count the final argument */
4484 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004485 }
4486 }
4487 break;
4488
4489 default:
4490 break; /* Normal token */
4491 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004492
4493 if (!skip) {
4494 Token *t;
4495
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004496 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004497
4498 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004499 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004500 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004501 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004502 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004503 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004504 pep = &t->next;
4505 white = 0;
4506 }
4507 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004508
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004509 /*
4510 * Look for a macro matching in both name and parameter count.
4511 * We already know any matches cannot be anywhere before the
4512 * current position of "m", so there is no reason to
4513 * backtrack.
4514 */
4515 while (1) {
4516 if (!m) {
4517 /*!
4518 *!macro-params-single [on] single-line macro calls with wrong parameter count
4519 *! warns about \i{single-line macros} being invoked
4520 *! with the wrong number of parameters.
4521 */
4522 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4523 "single-line macro `%s' exists, "
4524 "but not taking %d parameter%s",
4525 mname, nparam, (nparam == 1) ? "" : "s");
4526 goto not_a_macro;
4527 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004528
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004529 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4530 break; /* It's good */
4531
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004532 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004533 }
4534 }
4535
4536 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004537 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004538
4539 /* Expand each parameter */
4540 m->in_progress = true;
4541
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004542 if (m->eval_param) {
4543 for (i = 0; i < nparam; i++) {
4544 if (m->eval_param[i]) {
4545 /* Evaluate this parameter as a number */
4546 struct ppscan pps;
4547 struct tokenval tokval;
4548 expr *evalresult;
4549 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004550
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004551 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4552 pps.ntokens = -1;
4553 tokval.t_type = TOKEN_INVALID;
4554 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004555
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004556 free_tlist(eval_param);
4557 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004558
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004559 if (!evalresult) {
4560 /* Nothing meaningful to do */
4561 } else if (tokval.t_type) {
4562 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4563 } else if (!is_simple(evalresult)) {
4564 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4565 } else {
4566 params[i] = make_tok_num(reloc_value(evalresult));
4567 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004568 }
4569 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004570 }
4571
4572 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004573 tline = tline->next; /* Remove the macro call from the input */
4574 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004575
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004576 /* Note: we own the expansion this returns. */
4577 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004578
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004579 tup = NULL;
4580 ttail = NULL; /* Pointer to the last token of the expansion */
4581 while (t) {
4582 enum pp_token_type type = t->type;
4583 Token *tnext = t->next;
4584 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004585
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004586 switch (type) {
4587 case TOK_PREPROC_Q:
4588 case TOK_PREPROC_QQ:
4589 t->type = TOK_ID;
4590 nasm_free(t->text);
4591 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4592 t->len = nasm_last_string_len();
4593 t->next = tline;
4594 break;
4595
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004596 case TOK_ID:
4597 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004598 /*
4599 * Chain this into the target line *before* expanding,
4600 * that way we pick up any arguments to the new macro call,
4601 * if applicable.
4602 */
4603 t->next = tline;
4604 tp = &t;
4605 expand_one_smacro(&tp);
4606 if (t == tline)
4607 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004608 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004609
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004610 default:
4611 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004612 unsigned int param = smac_nparam(t->type);
4613 nasm_assert(!tup && param < nparam);
4614 delete_Token(t);
4615 t = NULL;
4616 tup = tnext;
4617 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004618 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004619 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004620 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004621 }
4622
4623 if (t) {
4624 tline = t;
4625 if (!ttail)
4626 ttail = t;
4627 }
4628
4629 if (tnext) {
4630 t = tnext;
4631 } else {
4632 t = tup;
4633 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004634 }
4635 }
4636
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004637 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004638 if (ttail)
4639 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004640
4641 m->in_progress = false;
4642
4643 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004644 free_tlist(mstart);
H. Peter Anvin322bee02019-08-10 01:38:06 -07004645 goto done;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004646
4647 /*
4648 * No macro expansion needed; roll back to mstart (if necessary)
H. Peter Anvin322bee02019-08-10 01:38:06 -07004649 * and then advance to the next input token. Note that this is
4650 * by far the common case!
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004651 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004652not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004653 *tpp = &mstart->next;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004654 m = NULL;
4655done:
4656 smacro_deadman.levels++;
4657 if (unlikely(params))
4658 free_tlist_array(params, nparam);
4659 return m;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004660}
4661
4662/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004663 * Expand all single-line macro calls made in the given line.
4664 * Return the expanded version of the line. The original is deemed
4665 * to be destroyed in the process. (In reality we'll just move
4666 * Tokens from input to output a lot of the time, rather than
4667 * actually bothering to destroy and replicate.)
4668 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004669static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004670{
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07004671 smacro_deadman.total = nasm_limit[LIMIT_MACRO_TOKENS];
H. Peter Anvin322bee02019-08-10 01:38:06 -07004672 smacro_deadman.levels = nasm_limit[LIMIT_MACRO_LEVELS];
4673 smacro_deadman.triggered = false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004674 return expand_smacro_noreset(tline);
4675}
4676
4677static Token *expand_smacro_noreset(Token * tline)
4678{
4679 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004680 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004681 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004682
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004683 /*
4684 * Trick: we should avoid changing the start token pointer since it can
4685 * be contained in "next" field of other token. Because of this
4686 * we allocate a copy of first token and work with it; at the end of
4687 * routine we copy it back
4688 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004689 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004690 tline = new_Token(org_tline->next, org_tline->type,
4691 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004692 nasm_free(org_tline->text);
4693 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004694 }
4695
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004696
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004697 /*
4698 * Pretend that we always end up doing expansion on the first pass;
4699 * that way %+ get processed. However, if we process %+ before the
4700 * first pass, we end up with things like MACRO %+ TAIL trying to
4701 * look up the macro "MACROTAIL", which we don't want.
4702 */
4703 expanded = true;
4704 thead = tline;
4705 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004706 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004707 {
4708 PP_CONCAT_MASK(TOK_ID) |
4709 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4710 PP_CONCAT_MASK(TOK_ID) |
4711 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4712 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4713 }
4714 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004715
4716 tail = &thead;
H. Peter Anvin322bee02019-08-10 01:38:06 -07004717 while ((t = *tail)) /* main token loop */
4718 expanded |= !!expand_one_smacro(&tail);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004719
4720 if (!expanded) {
4721 tline = thead;
4722 break; /* Done! */
4723 }
4724
4725 /*
4726 * Now scan the entire line and look for successive TOK_IDs
4727 * that resulted after expansion (they can't be produced by
4728 * tokenize()). The successive TOK_IDs should be concatenated.
4729 * Also we look for %+ tokens and concatenate the tokens
4730 * before and after them (without white spaces in between).
4731 */
4732 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004733
4734 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004735 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004736
H. Peter Anvine2c80182005-01-15 22:15:51 +00004737 if (org_tline) {
4738 if (thead) {
4739 *org_tline = *thead;
4740 /* since we just gave text to org_line, don't free it */
4741 thead->text = NULL;
4742 delete_Token(thead);
4743 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004744 /*
4745 * The expression expanded to empty line;
4746 * we can't return NULL because of the "trick" above.
4747 * Just set the line to a single WHITESPACE token.
4748 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004749 memset(org_tline, 0, sizeof(*org_tline));
4750 org_tline->text = NULL;
4751 org_tline->type = TOK_WHITESPACE;
4752 }
4753 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004754 }
4755
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004756 return thead;
4757}
4758
4759/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004760 * Similar to expand_smacro but used exclusively with macro identifiers
4761 * right before they are fetched in. The reason is that there can be
4762 * identifiers consisting of several subparts. We consider that if there
4763 * are more than one element forming the name, user wants a expansion,
4764 * otherwise it will be left as-is. Example:
4765 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004766 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004767 *
4768 * the identifier %$abc will be left as-is so that the handler for %define
4769 * will suck it and define the corresponding value. Other case:
4770 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004771 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004772 *
4773 * In this case user wants name to be expanded *before* %define starts
4774 * working, so we'll expand %$abc into something (if it has a value;
4775 * otherwise it will be left as-is) then concatenate all successive
4776 * PP_IDs into one.
4777 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004778static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004779{
4780 Token *cur, *oldnext = NULL;
4781
H. Peter Anvin734b1882002-04-30 21:01:08 +00004782 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004783 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784
4785 cur = tline;
4786 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004787 (cur->next->type == TOK_ID ||
4788 cur->next->type == TOK_PREPROC_ID
4789 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004790 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004791
4792 /* If identifier consists of just one token, don't expand */
4793 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004794 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004795
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 if (cur) {
4797 oldnext = cur->next; /* Detach the tail past identifier */
4798 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004799 }
4800
H. Peter Anvin734b1882002-04-30 21:01:08 +00004801 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004802
H. Peter Anvine2c80182005-01-15 22:15:51 +00004803 if (cur) {
4804 /* expand_smacro possibly changhed tline; re-scan for EOL */
4805 cur = tline;
4806 while (cur && cur->next)
4807 cur = cur->next;
4808 if (cur)
4809 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004810 }
4811
4812 return tline;
4813}
4814
4815/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004816 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004817 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004818 * to check for an initial label - that's taken care of in
4819 * expand_mmacro - but must check numbers of parameters. Guaranteed
4820 * to be called with tline->type == TOK_ID, so the putative macro
4821 * name is easy to find.
4822 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004823static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004824{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004825 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004826 Token **params;
4827 int nparam;
4828
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004829 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004830
4831 /*
4832 * Efficiency: first we see if any macro exists with the given
4833 * name. If not, we can return NULL immediately. _Then_ we
4834 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004835 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004836 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004837 list_for_each(m, head)
4838 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004839 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004840 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004841 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004842
4843 /*
4844 * OK, we have a potential macro. Count and demarcate the
4845 * parameters.
4846 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004847 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004848
4849 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004850 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004851 * structure that handles this number.
4852 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004853 while (m) {
4854 if (m->nparam_min <= nparam
4855 && (m->plus || nparam <= m->nparam_max)) {
4856 /*
4857 * This one is right. Just check if cycle removal
4858 * prohibits us using it before we actually celebrate...
4859 */
4860 if (m->in_progress > m->max_depth) {
4861 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004862 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004863 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004864 }
4865 nasm_free(params);
4866 return NULL;
4867 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004868 /*
4869 * It's right, and we can use it. Add its default
4870 * parameters to the end of our list if necessary.
4871 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004872 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004873 params =
4874 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004875 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004876 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004877 while (nparam < m->nparam_min + m->ndefs) {
4878 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004879 nparam++;
4880 }
4881 }
4882 /*
4883 * If we've gone over the maximum parameter count (and
4884 * we're in Plus mode), ignore parameters beyond
4885 * nparam_max.
4886 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004887 if (m->plus && nparam > m->nparam_max)
4888 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004889 /*
4890 * Then terminate the parameter list, and leave.
4891 */
4892 if (!params) { /* need this special case */
4893 params = nasm_malloc(sizeof(*params));
4894 nparam = 0;
4895 }
4896 params[nparam] = NULL;
4897 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004898 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004899 }
4900 /*
4901 * This one wasn't right: look for the next one with the
4902 * same name.
4903 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004904 list_for_each(m, m->next)
4905 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004906 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004907 }
4908
4909 /*
4910 * After all that, we didn't find one with the right number of
4911 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004912 *!
4913 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4914 *! warns about \i{multi-line macros} being invoked
4915 *! with the wrong number of parameters. See \k{mlmacover} for an
4916 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004917 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004918 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4919 "multi-line macro `%s' exists, but not taking %d parameter%s",
4920 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004921 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004922 return NULL;
4923}
4924
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004925
4926/*
4927 * Save MMacro invocation specific fields in
4928 * preparation for a recursive macro expansion
4929 */
4930static void push_mmacro(MMacro *m)
4931{
4932 MMacroInvocation *i;
4933
4934 i = nasm_malloc(sizeof(MMacroInvocation));
4935 i->prev = m->prev;
4936 i->params = m->params;
4937 i->iline = m->iline;
4938 i->nparam = m->nparam;
4939 i->rotate = m->rotate;
4940 i->paramlen = m->paramlen;
4941 i->unique = m->unique;
4942 i->condcnt = m->condcnt;
4943 m->prev = i;
4944}
4945
4946
4947/*
4948 * Restore MMacro invocation specific fields that were
4949 * saved during a previous recursive macro expansion
4950 */
4951static void pop_mmacro(MMacro *m)
4952{
4953 MMacroInvocation *i;
4954
4955 if (m->prev) {
4956 i = m->prev;
4957 m->prev = i->prev;
4958 m->params = i->params;
4959 m->iline = i->iline;
4960 m->nparam = i->nparam;
4961 m->rotate = i->rotate;
4962 m->paramlen = i->paramlen;
4963 m->unique = i->unique;
4964 m->condcnt = i->condcnt;
4965 nasm_free(i);
4966 }
4967}
4968
4969
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004970/*
4971 * Expand the multi-line macro call made by the given line, if
4972 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004973 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004974 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004975static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004976{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004977 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004978 Token *label = NULL;
4979 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004980 Token **params, *t, *tt;
4981 MMacro *m;
4982 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004983 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004984 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004985
4986 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004987 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004988 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004989 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004990 return 0;
4991 m = is_mmacro(t, &params);
4992 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004993 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004994 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004995 Token *last;
4996 /*
4997 * We have an id which isn't a macro call. We'll assume
4998 * it might be a label; we'll also check to see if a
4999 * colon follows it. Then, if there's another id after
5000 * that lot, we'll check it again for macro-hood.
5001 */
5002 label = last = t;
5003 t = t->next;
5004 if (tok_type_(t, TOK_WHITESPACE))
5005 last = t, t = t->next;
5006 if (tok_is_(t, ":")) {
5007 dont_prepend = 1;
5008 last = t, t = t->next;
5009 if (tok_type_(t, TOK_WHITESPACE))
5010 last = t, t = t->next;
5011 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005012 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
5013 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005014 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005015 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005016 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005017 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005018
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005019 if (unlikely(mmacro_deadman.total >= nasm_limit[LIMIT_MMACROS] ||
5020 mmacro_deadman.levels >= nasm_limit[LIMIT_MACRO_LEVELS])) {
5021 if (!mmacro_deadman.triggered) {
5022 nasm_nonfatal("interminable multiline macro recursion");
5023 mmacro_deadman.triggered = true;
5024 }
5025 return 0;
5026 }
5027
5028 mmacro_deadman.total++;
5029 mmacro_deadman.levels++;
5030
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005031 /*
5032 * Fix up the parameters: this involves stripping leading and
5033 * trailing whitespace, then stripping braces if they are
5034 * present.
5035 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005036 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005037 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005038
H. Peter Anvine2c80182005-01-15 22:15:51 +00005039 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005040 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005041 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005042
H. Peter Anvine2c80182005-01-15 22:15:51 +00005043 t = params[i];
5044 skip_white_(t);
5045 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005046 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 params[i] = t;
5048 paramlen[i] = 0;
5049 while (t) {
5050 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5051 break; /* ... because we have hit a comma */
5052 if (comma && t->type == TOK_WHITESPACE
5053 && tok_is_(t->next, ","))
5054 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005055 if (brace && t->type == TOK_OTHER) {
5056 if (t->text[0] == '{')
5057 brace++; /* ... or a nested opening brace */
5058 else if (t->text[0] == '}')
5059 if (!--brace)
5060 break; /* ... or a brace */
5061 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005062 t = t->next;
5063 paramlen[i]++;
5064 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005065 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005066 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005067 }
5068
5069 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 * OK, we have a MMacro structure together with a set of
5071 * parameters. We must now go through the expansion and push
5072 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005073 * parameter tokens and macro-local tokens doesn't get done
5074 * until the single-line macro substitution process; this is
5075 * because delaying them allows us to change the semantics
5076 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005077 *
5078 * First, push an end marker on to istk->expansion, mark this
5079 * macro as in progress, and set up its invocation-specific
5080 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005081 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005082 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083 ll->next = istk->expansion;
5084 ll->finishes = m;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005085 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005086
5087 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005088 * Save the previous MMacro expansion in the case of
5089 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005090 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005091 if (m->max_depth && m->in_progress)
5092 push_mmacro(m);
5093
5094 m->in_progress ++;
5095 m->params = params;
5096 m->iline = tline;
5097 m->nparam = nparam;
5098 m->rotate = 0;
5099 m->paramlen = paramlen;
5100 m->unique = unique++;
5101 m->lineno = 0;
5102 m->condcnt = 0;
5103
5104 m->next_active = istk->mstk;
5105 istk->mstk = m;
5106
5107 list_for_each(l, m->expansion) {
5108 Token **tail;
5109
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005110 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005111 ll->next = istk->expansion;
5112 istk->expansion = ll;
5113 tail = &ll->first;
5114
5115 list_for_each(t, l->first) {
5116 Token *x = t;
5117 switch (t->type) {
5118 case TOK_PREPROC_Q:
5119 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005120 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005121 case TOK_PREPROC_QQ:
5122 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
5123 break;
5124 case TOK_PREPROC_ID:
5125 if (t->text[1] == '0' && t->text[2] == '0') {
5126 dont_prepend = -1;
5127 x = label;
5128 if (!x)
5129 continue;
5130 }
5131 /* fall through */
5132 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005133 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005134 break;
5135 }
5136 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005137 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005138 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005139 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005140
5141 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005142 * If we had a label, push it on as the first line of
5143 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005144 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005145 if (label) {
5146 if (dont_prepend < 0)
5147 free_tlist(startline);
5148 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005149 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005150 ll->finishes = NULL;
5151 ll->next = istk->expansion;
5152 istk->expansion = ll;
5153 ll->first = startline;
5154 if (!dont_prepend) {
5155 while (label->next)
5156 label = label->next;
5157 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005158 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005159 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005160 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005161
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005162 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005163
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005164 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005165}
5166
H. Peter Anvin130736c2016-02-17 20:27:41 -08005167/*
5168 * This function adds macro names to error messages, and suppresses
5169 * them if necessary.
5170 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005171static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005172{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005173 /*
5174 * If we're in a dead branch of IF or something like it, ignore the error.
5175 * However, because %else etc are evaluated in the state context
5176 * of the previous branch, errors might get lost:
5177 * %if 0 ... %else trailing garbage ... %endif
5178 * So %else etc should set the ERR_PP_PRECOND flag.
5179 */
5180 if ((severity & ERR_MASK) < ERR_FATAL &&
5181 istk && istk->conds &&
5182 ((severity & ERR_PP_PRECOND) ?
5183 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005184 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005185 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005186
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005187 /* This doesn't make sense with the macro stack unwinding */
5188 if (0) {
5189 MMacro *mmac = NULL;
5190 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005191
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005192 /* get %macro name */
5193 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
5194 mmac = istk->mstk;
5195 /* but %rep blocks should be skipped */
5196 while (mmac && !mmac->name)
5197 mmac = mmac->next_active, delta++;
5198 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005199
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005200 if (mmac) {
5201 char *buf;
5202 nasm_set_verror(real_verror);
5203 buf = nasm_vasprintf(fmt, arg);
5204 nasm_error(severity, "(%s:%"PRId32") %s",
5205 mmac->name, mmac->lineno - delta, buf);
5206 nasm_set_verror(pp_verror);
5207 nasm_free(buf);
5208 return;
5209 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005210 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005211 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005212}
5213
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005214static Token *
5215stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005216{
5217 (void)s;
5218 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005219 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005220
5221 return make_tok_qstr(src_get_fname());
5222}
5223
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005224static Token *
5225stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005226{
5227 (void)s;
5228 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005229 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005230
5231 return make_tok_num(src_get_linnum());
5232}
5233
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005234static Token *
5235stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005236{
5237 (void)s;
5238 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005239 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005240
5241 return make_tok_num(globalbits);
5242}
5243
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005244static Token *
5245stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005246{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005247 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005248 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5249 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5250 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005251
5252 (void)s;
5253 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005254 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005255
5256 switch (globalbits) {
5257 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005258 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005259 break;
5260 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005261 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005262 break;
5263 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005264 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005265 break;
5266 default:
5267 panic();
5268 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005269
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005270 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005271}
5272
H. Peter Anvin8b262472019-02-26 14:00:54 -08005273/* Add magic standard macros */
5274struct magic_macros {
5275 const char *name;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005276 int nparam;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005277 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005278};
5279static const struct magic_macros magic_macros[] =
5280{
5281 { "__FILE__", 0, stdmac_file },
5282 { "__LINE__", 0, stdmac_line },
5283 { "__BITS__", 0, stdmac_bits },
5284 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005285 { NULL, 0, NULL }
5286};
5287
5288static void pp_add_magic_stdmac(void)
5289{
5290 const struct magic_macros *m;
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005291 SMacro tmpl;
5292
5293 nasm_zero(tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005294
5295 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005296 tmpl.nparam = m->nparam;
5297 tmpl.expand = m->func;
5298 define_smacro(m->name, true, NULL, &tmpl);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005299 }
5300}
5301
H. Peter Anvin734b1882002-04-30 21:01:08 +00005302static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005303pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005304{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005305 int apass;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005306 struct Include *inc;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005307
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005308 cstk = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005309 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005310 nested_mac_count = 0;
5311 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005312 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005313 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005314 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005315 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005316
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005317 if (!use_loaded)
5318 use_loaded = nasm_malloc(use_package_count * sizeof(bool));
5319 memset(use_loaded, 0, use_package_count * sizeof(bool));
5320
H. Peter Anvin6686de22019-08-10 05:33:14 -07005321 /* First set up the top level input file */
5322 nasm_new(istk);
5323 istk->fp = nasm_open_read(file, NF_TEXT);
5324 src_set(0, file);
5325 istk->lineinc = 1;
5326 if (!istk->fp)
5327 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
5328
5329 strlist_add(deplist, file);
5330
5331 /*
5332 * Set up the stdmac packages as a virtual include file,
5333 * indicated by a null file pointer.
5334 */
5335 nasm_new(inc);
5336 inc->next = istk;
5337 inc->fname = src_set_fname(NULL);
5338 inc->nolist = !list_option('b');
5339 istk = inc;
5340 lfmt->uplevel(LIST_INCLUDE, 0);
5341
H. Peter Anvin8b262472019-02-26 14:00:54 -08005342 pp_add_magic_stdmac();
5343
H. Peter Anvinf7606612016-07-13 14:23:48 -07005344 if (tasm_compatible_mode)
5345 pp_add_stdmac(nasm_stdmac_tasm);
5346
5347 pp_add_stdmac(nasm_stdmac_nasm);
5348 pp_add_stdmac(nasm_stdmac_version);
5349
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005350 if (extrastdmac)
5351 pp_add_stdmac(extrastdmac);
5352
H. Peter Anvinf7606612016-07-13 14:23:48 -07005353 stdmacpos = stdmacros[0];
5354 stdmacnext = &stdmacros[1];
5355
H. Peter Anvind2456592008-06-19 15:04:18 -07005356 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005357
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005358 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005359 * Define the __PASS__ macro. This is defined here unlike all the
5360 * other builtins, because it is special -- it varies between
5361 * passes -- but there is really no particular reason to make it
5362 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005363 *
5364 * 0 = dependencies only
5365 * 1 = preparatory passes
5366 * 2 = final pass
5367 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005368 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005369 switch (mode) {
5370 case PP_NORMAL:
5371 apass = pass_final() ? 2 : 1;
5372 break;
5373 case PP_DEPS:
5374 apass = 0;
5375 break;
5376 case PP_PREPROC:
5377 apass = 3;
5378 break;
5379 default:
5380 panic();
5381 }
5382
H. Peter Anvin (Intel)5e3d7412019-08-14 23:45:57 -07005383 define_smacro("__PASS__", true, make_tok_num(apass), NULL);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005384}
5385
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005386static void pp_init(void)
5387{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005388}
5389
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005390/*
5391 * Get a line of tokens. If we popped the macro expansion/include stack,
5392 * we return a pointer to the dummy token tok_pop; at that point if
5393 * istk is NULL then we have reached end of input;
5394 */
5395static Token tok_pop; /* Dummy token placeholder */
5396
5397static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005398{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005399 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005400
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005401 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005402 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005403 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005404 * buffer or from the input file.
5405 */
5406 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005407 while (istk->expansion && istk->expansion->finishes) {
5408 Line *l = istk->expansion;
5409 if (!l->finishes->name && l->finishes->in_progress > 1) {
5410 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005411
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005412 /*
5413 * This is a macro-end marker for a macro with no
5414 * name, which means it's not really a macro at all
5415 * but a %rep block, and the `in_progress' field is
5416 * more than 1, meaning that we still need to
5417 * repeat. (1 means the natural last repetition; 0
5418 * means termination by %exitrep.) We have
5419 * therefore expanded up to the %endrep, and must
5420 * push the whole block on to the expansion buffer
5421 * again. We don't bother to remove the macro-end
5422 * marker: we'd only have to generate another one
5423 * if we did.
5424 */
5425 l->finishes->in_progress--;
5426 list_for_each(l, l->finishes->expansion) {
5427 Token *t, *tt, **tail;
5428
5429 ll = nasm_malloc(sizeof(Line));
5430 ll->next = istk->expansion;
5431 ll->finishes = NULL;
5432 ll->first = NULL;
5433 tail = &ll->first;
5434
5435 list_for_each(t, l->first) {
5436 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005437 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005438 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005439 }
5440 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005441 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005442 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005443 } else {
5444 /*
5445 * Check whether a `%rep' was started and not ended
5446 * within this macro expansion. This can happen and
5447 * should be detected. It's a fatal error because
5448 * I'm too confused to work out how to recover
5449 * sensibly from it.
5450 */
5451 if (defining) {
5452 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005453 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005454 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005455 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005456 " expansion of macro `%s'",
5457 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005458 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005459
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005460 /*
5461 * FIXME: investigate the relationship at this point between
5462 * istk->mstk and l->finishes
5463 */
5464 {
5465 MMacro *m = istk->mstk;
5466 istk->mstk = m->next_active;
5467 if (m->name) {
5468 /*
5469 * This was a real macro call, not a %rep, and
5470 * therefore the parameter information needs to
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005471 * be freed and the iteration count/nesting
5472 * depth adjusted.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005473 */
H. Peter Anvin (Intel)9fbd9fb2019-08-15 19:26:52 -07005474
5475 if (!--mmacro_deadman.levels) {
5476 /*
5477 * If all mmacro processing done,
5478 * clear all counters and the deadman
5479 * message trigger.
5480 */
5481 nasm_zero(mmacro_deadman); /* Clear all counters */
5482 }
5483
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005484 if (m->prev) {
5485 pop_mmacro(m);
5486 l->finishes->in_progress --;
5487 } else {
5488 nasm_free(m->params);
5489 free_tlist(m->iline);
5490 nasm_free(m->paramlen);
5491 l->finishes->in_progress = 0;
5492 }
Adam Majer91e72402017-07-25 10:42:01 +02005493 }
5494
5495 /*
5496 * FIXME It is incorrect to always free_mmacro here.
5497 * It leads to usage-after-free.
5498 *
5499 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5500 */
5501#if 0
5502 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005503 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005504#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005505 }
5506 istk->expansion = l->next;
5507 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005508 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005509 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005510 }
5511 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005512 do { /* until we get a line we can use */
5513 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005514
5515 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005516 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005517 int32_t lineno;
5518
H. Peter Anvin6686de22019-08-10 05:33:14 -07005519 if (istk->mstk) {
5520 istk->mstk->lineno++;
5521 if (istk->mstk->fname)
5522 lineno = istk->mstk->lineno + istk->mstk->xline;
5523 else
5524 lineno = 0; /* Defined at init time or builtin */
5525 } else {
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005526 lineno = src_get_linnum();
H. Peter Anvin6686de22019-08-10 05:33:14 -07005527 }
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005528
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005529 tline = l->first;
5530 istk->expansion = l->next;
5531 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005532
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005533 line = detoken(tline, false);
H. Peter Anvin6686de22019-08-10 05:33:14 -07005534 if (!istk->nolist)
5535 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005536 nasm_free(line);
5537 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005538 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005539 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005540 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005541 } else {
5542 /*
5543 * The current file has ended; work down the istk
5544 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005545 Include *i = istk;
H. Peter Anvin6686de22019-08-10 05:33:14 -07005546 if (i->fp)
5547 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005548 if (i->conds) {
5549 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005550 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005551 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005552 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005553 if (i->next)
5554 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005555 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005556 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005557 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005558 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005559 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005560 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005561
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005562 /*
5563 * We must expand MMacro parameters and MMacro-local labels
5564 * _before_ we plunge into directive processing, to cope
5565 * with things like `%define something %1' such as STRUC
5566 * uses. Unless we're _defining_ a MMacro, in which case
5567 * those tokens should be left alone to go into the
5568 * definition; and unless we're in a non-emitting
5569 * condition, in which case we don't want to meddle with
5570 * anything.
5571 */
5572 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5573 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005574 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005575 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005576
H. Peter Anvine2c80182005-01-15 22:15:51 +00005577 /*
5578 * Check the line to see if it's a preprocessor directive.
5579 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005580 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5581 if (dtline)
5582 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005583 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005584 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005585 * We're defining a multi-line macro. We emit nothing
5586 * at all, and just
5587 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005588 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005589 Line *l = nasm_malloc(sizeof(Line));
5590 l->next = defining->expansion;
5591 l->first = tline;
5592 l->finishes = NULL;
5593 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005594 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005595 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005596 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005597 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005598 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005599 * directive so we keep our place correctly.
5600 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005601 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005602 } else if (istk->mstk && !istk->mstk->in_progress) {
5603 /*
5604 * We're in a %rep block which has been terminated, so
5605 * we're walking through to the %endrep without
5606 * emitting anything. Emit nothing at all, not even a
5607 * blank line: when we emerge from the %rep block we'll
5608 * give a line-number directive so we keep our place
5609 * correctly.
5610 */
5611 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005612 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005613 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005614 if (!expand_mmacro(tline))
5615 return tline;
5616 }
5617 }
5618}
5619
5620static char *pp_getline(void)
5621{
5622 char *line = NULL;
5623 Token *tline;
5624
5625 real_verror = nasm_set_verror(pp_verror);
5626
5627 while (true) {
5628 tline = pp_tokline();
5629 if (tline == &tok_pop) {
5630 /*
5631 * We popped the macro/include stack. If istk is empty,
5632 * we are at end of input, otherwise just loop back.
5633 */
5634 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005635 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005636 } else {
5637 /*
5638 * De-tokenize the line and emit it.
5639 */
5640 line = detoken(tline, true);
5641 free_tlist(tline);
5642 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005643 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005644 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005645
H. Peter Anvin6686de22019-08-10 05:33:14 -07005646 if (list_option('e') && !istk->nolist && line && line[0]) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005647 char *buf = nasm_strcat(" ;;; ", line);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005648 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005649 nasm_free(buf);
5650 }
5651
H. Peter Anvin130736c2016-02-17 20:27:41 -08005652 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005653 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005654}
5655
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005656static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005657{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005658 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005659
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005660 if (defining) {
5661 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005662 nasm_nonfatal("end of file while still defining macro `%s'",
5663 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005664 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005665 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005666 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005667
5668 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005669 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005670 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005671
5672 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005673
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005674 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005675 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005676 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005677 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005678 Include *i = istk;
5679 istk = istk->next;
5680 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005681 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005682 }
5683 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005684 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005685 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005686}
5687
5688static void pp_cleanup_session(void)
5689{
H. Peter Anvin (Intel)4b282d02019-08-15 11:53:19 -07005690 nasm_free(use_loaded);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005691 free_llist(predef);
5692 predef = NULL;
5693 delete_Blocks();
5694 freeTokens = NULL;
5695 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005696}
5697
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005698static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005699{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005700 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005701}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005702
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005703static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005704{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005705 Token *inc, *space, *name;
5706 Line *l;
5707
H. Peter Anvin734b1882002-04-30 21:01:08 +00005708 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5709 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5710 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005711
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005712 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005713 l->next = predef;
5714 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005715 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005716 predef = l;
5717}
5718
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005719static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005720{
5721 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005722 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005723 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005724
H. Peter Anvin130736c2016-02-17 20:27:41 -08005725 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005726
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005727 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005728 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5729 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005730 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005731 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005732 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005733 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005734 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005735
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005736 if (space->next->type != TOK_PREPROC_ID &&
5737 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005738 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005739
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005740 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005741 l->next = predef;
5742 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005743 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005744 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005745
5746 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005747}
5748
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005749static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005750{
5751 Token *def, *space;
5752 Line *l;
5753
H. Peter Anvin734b1882002-04-30 21:01:08 +00005754 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5755 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005756 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005757
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005758 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005759 l->next = predef;
5760 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005761 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005762 predef = l;
5763}
5764
H. Peter Anvin05990342018-06-11 13:32:42 -07005765/* Insert an early preprocessor command that doesn't need special handling */
5766static void pp_pre_command(const char *what, char *string)
5767{
5768 char *cmd;
5769 Token *def, *space;
5770 Line *l;
5771
5772 def = tokenize(string);
5773 if (what) {
5774 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5775 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5776 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5777 }
5778
5779 l = nasm_malloc(sizeof(Line));
5780 l->next = predef;
5781 l->first = def;
5782 l->finishes = NULL;
5783 predef = l;
5784}
5785
H. Peter Anvinf7606612016-07-13 14:23:48 -07005786static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005787{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005788 macros_t **mp;
5789
5790 /* Find the end of the list and avoid duplicates */
5791 for (mp = stdmacros; *mp; mp++) {
5792 if (*mp == macros)
5793 return; /* Nothing to do */
5794 }
5795
5796 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5797
5798 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005799}
5800
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005801static void pp_extra_stdmac(macros_t *macros)
5802{
5803 extrastdmac = macros;
5804}
5805
H. Peter Anvin8b262472019-02-26 14:00:54 -08005806static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005807{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005808 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005809 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5810 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5811}
5812
5813static Token *make_tok_qstr(const char *str)
5814{
5815 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005816 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005817 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005818}
5819
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005820static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005821{
5822 if (!m)
5823 return;
5824
5825 /* We need to print the next_active list in reverse order */
5826 pp_list_one_macro(m->next_active, severity);
5827
5828 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005829 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005830 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005831 }
5832}
5833
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005834static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005835{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005836 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005837
H. Peter Anvinddb29062018-12-11 00:06:29 -08005838 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5839 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005840
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005841 if (istk)
5842 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005843
H. Peter Anvinddb29062018-12-11 00:06:29 -08005844 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005845}
5846
H. Peter Anvine7469712016-02-18 02:20:59 -08005847const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005848 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005849 pp_reset,
5850 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005851 pp_cleanup_pass,
5852 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005853 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005854 pp_pre_define,
5855 pp_pre_undefine,
5856 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005857 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005858 pp_include_path,
5859 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005860};