blob: b16ed0c98219091a511b054cd7f048b060642129 [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 Anvind7ed89e2002-04-30 20:52:08 +0000117};
118
119/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800120 * Store the definition of a multi-line macro. This is also used to
121 * store the interiors of `%rep...%endrep' blocks, which are
122 * effectively self-re-invoking multi-line macros which simply
123 * don't have a name or bother to appear in the hash tables. %rep
124 * blocks are signified by having a NULL `name' field.
125 *
126 * In a MMacro describing a `%rep' block, the `in_progress' field
127 * isn't merely boolean, but gives the number of repeats left to
128 * run.
129 *
130 * The `next' field is used for storing MMacros in hash tables; the
131 * `next_active' field is for stacking them on istk entries.
132 *
133 * When a MMacro is being expanded, `params', `iline', `nparam',
134 * `paramlen', `rotate' and `unique' are local to the invocation.
135 */
136struct MMacro {
137 MMacro *next;
138 MMacroInvocation *prev; /* previous invocation */
139 char *name;
140 int nparam_min, nparam_max;
141 bool casesense;
142 bool plus; /* is the last parameter greedy? */
143 bool nolist; /* is this macro listing-inhibited? */
144 int64_t in_progress; /* is this macro currently being expanded? */
145 int32_t max_depth; /* maximum number of recursive expansions allowed */
146 Token *dlist; /* All defaults as one list */
147 Token **defaults; /* Parameter default pointers */
148 int ndefs; /* number of default parameters */
149 Line *expansion;
150
151 MMacro *next_active;
152 MMacro *rep_nest; /* used for nesting %rep */
153 Token **params; /* actual parameters */
154 Token *iline; /* invocation line */
155 unsigned int nparam, rotate;
156 int *paramlen;
157 uint64_t unique;
158 int lineno; /* Current line number on expansion */
159 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700160
H. Peter Anvin274cda82016-05-10 02:56:29 -0700161 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700162 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800163};
164
165
166/* Store the definition of a multi-line macro, as defined in a
167 * previous recursive macro expansion.
168 */
169struct MMacroInvocation {
170 MMacroInvocation *prev; /* previous invocation */
171 Token **params; /* actual parameters */
172 Token *iline; /* invocation line */
173 unsigned int nparam, rotate;
174 int *paramlen;
175 uint64_t unique;
176 uint64_t condcnt;
177};
178
179
180/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000181 * The context stack is composed of a linked list of these.
182 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000183struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800184 Context *next;
185 char *name;
186 struct hash_table localmac;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -0700187 uint64_t number;
188 unsigned int depth;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000189};
190
191/*
192 * This is the internal form which we break input lines up into.
193 * Typically stored in linked lists.
194 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800195 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
196 * not necessarily used as-is, but is also used to encode the number
197 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000198 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800199 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700200 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000201 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800202 * tok_smac_param(0) but the one representing `y' will be
203 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000204 *
205 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
206 * which doesn't need quotes around it. Used in the pre-include
207 * mechanism as an alternative to trying to find a sensible type of
208 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000209 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000210enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800211 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
212 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800213 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700214 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800215 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300216 TOK_PASTE, /* %+ */
217 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800218 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300219 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000220};
221
H. Peter Anvin8b262472019-02-26 14:00:54 -0800222static inline enum pp_token_type tok_smac_param(int param)
223{
224 return TOK_SMAC_START_PARAMS + param;
225}
226static int smac_nparam(enum pp_token_type toktype)
227{
228 return toktype - TOK_SMAC_START_PARAMS;
229}
230static bool is_smac_param(enum pp_token_type toktype)
231{
232 return toktype >= TOK_SMAC_START_PARAMS;
233}
234
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400235#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400236#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400237
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400238struct tokseq_match {
239 int mask_head;
240 int mask_tail;
241};
242
H. Peter Anvine2c80182005-01-15 22:15:51 +0000243struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800244 Token *next;
245 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700246 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800247 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000248};
249
250/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800251 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000252 * these, which is essentially a container to allow several linked
253 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700254 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000255 * Note that in this module, linked lists are treated as stacks
256 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800257 * `expansion' field in MMacro structures, so that the linked list,
258 * if walked, would give the macro lines in reverse order; this
259 * means that we can walk the list when expanding a macro, and thus
260 * push the lines on to the `expansion' field in _istk_ in reverse
261 * order (so that when popped back off they are in the right
262 * order). It may seem cockeyed, and it relies on my design having
263 * an even number of steps in, but it works...
264 *
265 * Some of these structures, rather than being actual lines, are
266 * markers delimiting the end of the expansion of a given macro.
267 * This is for use in the cycle-tracking and %rep-handling code.
268 * Such structures have `finishes' non-NULL, and `first' NULL. All
269 * others have `finishes' NULL, but `first' may still be NULL if
270 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000271 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000272struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800273 Line *next;
274 MMacro *finishes;
275 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500276};
277
278/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000279 * To handle an arbitrary level of file inclusion, we maintain a
280 * stack (ie linked list) of these things.
281 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000282struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800283 Include *next;
284 FILE *fp;
285 Cond *conds;
286 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700287 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800288 int lineno, lineinc;
289 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000290};
291
292/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700293 * File real name hash, so we don't have to re-search the include
294 * path for every pass (and potentially more than that if a file
295 * is used more than once.)
296 */
297struct hash_table FileHash;
298
299/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000300 * Conditional assembly: we maintain a separate stack of these for
301 * each level of file inclusion. (The only reason we keep the
302 * stacks separate is to ensure that a stray `%endif' in a file
303 * included from within the true branch of a `%if' won't terminate
304 * it and cause confusion: instead, rightly, it'll cause an error.)
305 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800306struct Cond {
307 Cond *next;
308 int state;
309};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000310enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000311 /*
312 * These states are for use just after %if or %elif: IF_TRUE
313 * means the condition has evaluated to truth so we are
314 * currently emitting, whereas IF_FALSE means we are not
315 * currently emitting but will start doing so if a %else comes
316 * up. In these states, all directives are admissible: %elif,
317 * %else and %endif. (And of course %if.)
318 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800319 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000320 /*
321 * These states come up after a %else: ELSE_TRUE means we're
322 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
323 * any %elif or %else will cause an error.
324 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800325 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000326 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200327 * These states mean that we're not emitting now, and also that
328 * nothing until %endif will be emitted at all. COND_DONE is
329 * used when we've had our moment of emission
330 * and have now started seeing %elifs. COND_NEVER is used when
331 * the condition construct in question is contained within a
332 * non-emitting branch of a larger condition construct,
333 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000334 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800335 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000336};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800337#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000338
H. Peter Anvin70653092007-10-19 14:42:29 -0700339/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000340 * These defines are used as the possible return values for do_directive
341 */
342#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300343#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000344
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400345/* max reps */
346#define REP_LIMIT ((INT64_C(1) << 62))
347
Keith Kanios852f1ee2009-07-12 00:19:55 -0500348/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000349 * Condition codes. Note that we use c_ prefix not C_ because C_ is
350 * used in nasm.h for the "real" condition codes. At _this_ level,
351 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
352 * ones, so we need a different enum...
353 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700354static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000355 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
356 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000357 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000358};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700359enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000360 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
361 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 -0700362 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
363 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000364};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700365static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000366 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
367 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 +0000368 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000369};
370
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800371/*
372 * Directive names.
373 */
374/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
375static int is_condition(enum preproc_token arg)
376{
377 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
378}
379
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000380/* For TASM compatibility we need to be able to recognise TASM compatible
381 * conditional compilation directives. Using the NASM pre-processor does
382 * not work, so we look for them specifically from the following list and
383 * then jam in the equivalent NASM directive into the input stream.
384 */
385
H. Peter Anvine2c80182005-01-15 22:15:51 +0000386enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000387 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
388 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
389};
390
H. Peter Anvin476d2862007-10-02 22:04:15 -0700391static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000392 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
393 "ifndef", "include", "local"
394};
395
396static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700397static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000398static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800399static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000400
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000401static Context *cstk;
402static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300403static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000404
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300405static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000406
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300407static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000408
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800409static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700410static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800411static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000412
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000413/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800414 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000415 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800416static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000417
418/*
419 * The current set of single-line macros we have defined.
420 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700421static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000422
423/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800424 * The multi-line macro we are currently defining, or the %rep
425 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000426 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800427static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000428
Charles Crayned4200be2008-07-12 16:42:33 -0700429static uint64_t nested_mac_count;
430static uint64_t nested_rep_count;
431
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000432/*
433 * The number of macro parameters to allocate space for at a time.
434 */
435#define PARAM_DELTA 16
436
437/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700438 * The standard macro set: defined in macros.c in a set of arrays.
439 * This gives our position in any macro set, while we are processing it.
440 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000441 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700442static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700443static macros_t **stdmacnext;
444static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300445static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000446
447/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000448 * Tokens are allocated in blocks to improve speed
449 */
450#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800451static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000452struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000453 Blocks *next;
454 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000455};
456
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800457static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000458
459/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000460 * Forward declarations.
461 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700462static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000463static Token *expand_mmac_params(Token * tline);
464static Token *expand_smacro(Token * tline);
465static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400466static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800467static Token *make_tok_num(int64_t val);
468static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800469static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800470static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000471static void *new_Block(size_t size);
472static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700473static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700474 const char *text, size_t txtlen);
475static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000476static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000477
478/*
479 * Macros for safe checking of token pointers, avoid *(NULL)
480 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300481#define tok_type_(x,t) ((x) && (x)->type == (t))
482#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
483#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
484#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000485
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400486/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700487 * In-place reverse a list of tokens.
488 */
489static Token *reverse_tokens(Token *t)
490{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800491 Token *prev = NULL;
492 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700493
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800494 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400495 next = t->next;
496 t->next = prev;
497 prev = t;
498 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800499 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700500
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800501 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700502}
503
504/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300505 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000506 * front of them. We do it here because I could not find any other
507 * place to do it for the moment, and it is a hack (ideally it would
508 * be nice to be able to use the NASM pre-processor to do it).
509 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000510static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000511{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000512 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400513 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000514
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400515 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000516
517 /* Binary search for the directive name */
518 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400519 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400520 q = nasm_skip_word(p);
521 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 if (len) {
523 oldchar = p[len];
524 p[len] = 0;
525 while (j - i > 1) {
526 k = (j + i) / 2;
527 m = nasm_stricmp(p, tasm_directives[k]);
528 if (m == 0) {
529 /* We have found a directive, so jam a % in front of it
530 * so that NASM will then recognise it as one if it's own.
531 */
532 p[len] = oldchar;
533 len = strlen(p);
534 oldline = line;
535 line = nasm_malloc(len + 2);
536 line[0] = '%';
537 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700538 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300539 * NASM does not recognise IFDIFI, so we convert
540 * it to %if 0. This is not used in NASM
541 * compatible code, but does need to parse for the
542 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700544 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545 } else {
546 memcpy(line + 1, p, len + 1);
547 }
548 nasm_free(oldline);
549 return line;
550 } else if (m < 0) {
551 j = k;
552 } else
553 i = k;
554 }
555 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000556 }
557 return line;
558}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000559
H. Peter Anvin76690a12002-04-30 20:52:49 +0000560/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000561 * The pre-preprocessing stage... This function translates line
562 * number indications as they emerge from GNU cpp (`# lineno "file"
563 * flags') into NASM preprocessor line number indications (`%line
564 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000565 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000566static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000567{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000568 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000569 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000570
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 if (line[0] == '#' && line[1] == ' ') {
572 oldline = line;
573 fname = oldline + 2;
574 lineno = atoi(fname);
575 fname += strspn(fname, "0123456789 ");
576 if (*fname == '"')
577 fname++;
578 fnlen = strcspn(fname, "\"");
579 line = nasm_malloc(20 + fnlen);
580 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
581 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000582 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000583 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000585 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586}
587
588/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 * Free a linked list of tokens.
590 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000592{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400593 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595}
596
597/*
598 * Free a linked list of lines.
599 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000601{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400602 Line *l, *tmp;
603 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 free_tlist(l->first);
605 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000606 }
607}
608
609/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700610 * Free an array of linked lists of tokens
611 */
612static void free_tlist_array(Token **array, size_t nlists)
613{
614 Token **listp = array;
615
616 while (nlists--)
617 free_tlist(*listp++);
618
619 nasm_free(array);
620}
621
622/*
623 * Duplicate a linked list of tokens.
624 */
625static Token *dup_tlist(const Token *list, Token ***tailp)
626{
627 Token *newlist = NULL;
628 Token **tailpp = &newlist;
629 const Token *t;
630
631 list_for_each(t, list) {
632 Token *nt;
633 *tailpp = nt = dup_Token(NULL, t);
634 tailpp = &nt->next;
635 }
636
637 if (tailp)
638 *tailp = tailpp;
639
640 return newlist;
641}
642
643/*
644 * Duplicate a linked list of tokens in reverse order
645 */
646static Token *dup_tlist_reverse(const Token *list, Token *tail)
647{
648 const Token *t;
649
650 list_for_each(t, list)
651 tail = dup_Token(tail, t);
652
653 return tail;
654}
655
656/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800657 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000658 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800659static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000660{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800661 nasm_free(m->name);
662 free_tlist(m->dlist);
663 nasm_free(m->defaults);
664 free_llist(m->expansion);
665 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000666}
667
668/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700669 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800670 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700671static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800672{
673 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700674 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800675 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700676}
677
678static void clear_smacro(SMacro *s)
679{
680 free_smacro_members(s);
681 /* Wipe everything except the next pointer */
682 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
683}
684
685/*
686 * Free an SMacro
687 */
688static void free_smacro(SMacro *s)
689{
690 free_smacro_members(s);
691 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800692}
693
694/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700695 * Free all currently defined macros, and free the hash tables
696 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700697static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700698{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800699 struct hash_iterator it;
700 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700701
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800702 hash_for_each(smt, it, np) {
703 SMacro *tmp;
704 SMacro *s = np->data;
705 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800706 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700707 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700708 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700709 hash_free(smt);
710}
711
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800712static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700713{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800714 struct hash_iterator it;
715 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700716
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800717 hash_for_each(mmt, it, np) {
718 MMacro *tmp;
719 MMacro *m = np->data;
720 nasm_free((void *)np->key);
721 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800722 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700723 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800724 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700725}
726
727static void free_macros(void)
728{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700729 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800730 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700731}
732
733/*
734 * Initialize the hash tables
735 */
736static void init_macros(void)
737{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700738}
739
740/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000741 * Pop the context stack.
742 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000744{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000745 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000746
747 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700748 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000749 nasm_free(c->name);
750 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000751}
752
H. Peter Anvin072771e2008-05-22 13:17:51 -0700753/*
754 * Search for a key in the hash index; adding it if necessary
755 * (in which case we initialize the data pointer to NULL.)
756 */
757static void **
758hash_findi_add(struct hash_table *hash, const char *str)
759{
760 struct hash_insert hi;
761 void **r;
762 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800763 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700764
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800765 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700766 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300767 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700768
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800769 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
770 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700771 return hash_add(&hi, strx, NULL);
772}
773
774/*
775 * Like hash_findi, but returns the data element rather than a pointer
776 * to it. Used only when not adding a new element, hence no third
777 * argument.
778 */
779static void *
780hash_findix(struct hash_table *hash, const char *str)
781{
782 void **p;
783
784 p = hash_findi(hash, str, NULL);
785 return p ? *p : NULL;
786}
787
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400788/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800789 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400790 * if there no more left -- return NULL
791 */
792static char *line_from_stdmac(void)
793{
794 unsigned char c;
795 const unsigned char *p = stdmacpos;
796 char *line, *q;
797 size_t len = 0;
798
799 if (!stdmacpos)
800 return NULL;
801
802 while ((c = *p++)) {
803 if (c >= 0x80)
804 len += pp_directives_len[c - 0x80] + 1;
805 else
806 len++;
807 }
808
809 line = nasm_malloc(len + 1);
810 q = line;
811 while ((c = *stdmacpos++)) {
812 if (c >= 0x80) {
813 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
814 q += pp_directives_len[c - 0x80];
815 *q++ = ' ';
816 } else {
817 *q++ = c;
818 }
819 }
820 stdmacpos = p;
821 *q = '\0';
822
823 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700824 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400825 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700826 if (*stdmacnext) {
827 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400828 } else if (do_predef) {
829 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400830
831 /*
832 * Nasty hack: here we push the contents of
833 * `predef' on to the top-level expansion stack,
834 * since this is the most convenient way to
835 * implement the pre-include and pre-define
836 * features.
837 */
838 list_for_each(pd, predef) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800839 l = nasm_malloc(sizeof(Line));
840 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700841 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800842 l->finishes = NULL;
843
844 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400845 }
846 do_predef = false;
847 }
848 }
849
850 return line;
851}
852
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000853static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000854{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700855 int c;
856 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400857 const unsigned int delta = 512;
858 const unsigned int pad = 8;
859 unsigned int nr_cont = 0;
860 bool cont = false;
861 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000862
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400863 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400864 p = line_from_stdmac();
865 if (p)
866 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700867
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400868 size = delta;
869 p = buffer = nasm_malloc(size);
870
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700871 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400872 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400873
874 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700875 case EOF:
876 if (p == buffer) {
877 nasm_free(buffer);
878 return NULL;
879 }
880 c = 0;
881 break;
882
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400883 case '\r':
884 next = fgetc(istk->fp);
885 if (next != '\n')
886 ungetc(next, istk->fp);
887 if (cont) {
888 cont = false;
889 continue;
890 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700891 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400892 break;
893
894 case '\n':
895 if (cont) {
896 cont = false;
897 continue;
898 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700899 c = 0;
900 break;
901
902 case 032: /* ^Z = legacy MS-DOS end of file mark */
903 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400904 break;
905
906 case '\\':
907 next = fgetc(istk->fp);
908 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400909 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400910 cont = true;
911 nr_cont++;
912 continue;
913 }
914 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000915 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400916
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400917 if (p >= (buffer + size - pad)) {
918 buffer = nasm_realloc(buffer, size + delta);
919 p = buffer + size - pad;
920 size += delta;
921 }
922
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700923 *p++ = c;
924 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000925
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300926 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400927 (nr_cont * istk->lineinc));
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800928 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000929
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000930 return buffer;
931}
932
933/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000934 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000935 * don't need to parse the value out of e.g. numeric tokens: we
936 * simply split one string into many.
937 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000938static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000939{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700940 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000941 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000942 Token *list = NULL;
943 Token *t, **tail = &list;
944
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 while (*line) {
946 p = line;
947 if (*p == '%') {
948 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300949 if (*p == '+' && !nasm_isdigit(p[1])) {
950 p++;
951 type = TOK_PASTE;
952 } else if (nasm_isdigit(*p) ||
953 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 do {
955 p++;
956 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700957 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 type = TOK_PREPROC_ID;
959 } else if (*p == '{') {
960 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800961 while (*p) {
962 if (*p == '}')
963 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 p[-1] = *p;
965 p++;
966 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800967 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800968 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 p[-1] = '\0';
970 if (*p)
971 p++;
972 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300973 } else if (*p == '[') {
974 int lvl = 1;
975 line += 2; /* Skip the leading %[ */
976 p++;
977 while (lvl && (c = *p++)) {
978 switch (c) {
979 case ']':
980 lvl--;
981 break;
982 case '%':
983 if (*p == '[')
984 lvl++;
985 break;
986 case '\'':
987 case '\"':
988 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300989 p = nasm_skip_string(p - 1);
990 if (*p)
991 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300992 break;
993 default:
994 break;
995 }
996 }
997 p--;
998 if (*p)
999 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001000 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001001 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001002 type = TOK_INDIRECT;
1003 } else if (*p == '?') {
1004 type = TOK_PREPROC_Q; /* %? */
1005 p++;
1006 if (*p == '?') {
1007 type = TOK_PREPROC_QQ; /* %?? */
1008 p++;
1009 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001010 } else if (*p == '!') {
1011 type = TOK_PREPROC_ID;
1012 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001013 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001014 do {
1015 p++;
1016 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001017 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001018 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001019 p = nasm_skip_string(p);
1020 if (*p)
1021 p++;
1022 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001023 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001024 } else {
1025 /* %! without string or identifier */
1026 type = TOK_OTHER; /* Legacy behavior... */
1027 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001028 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001030 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 do {
1032 p++;
1033 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001034 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 type = TOK_PREPROC_ID;
1036 } else {
1037 type = TOK_OTHER;
1038 if (*p == '%')
1039 p++;
1040 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001041 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 type = TOK_ID;
1043 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001044 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001046 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 /*
1048 * A string token.
1049 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001051 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001052
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 if (*p) {
1054 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001055 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001056 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 /* Handling unterminated strings by UNV */
1058 /* type = -1; */
1059 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001060 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001061 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001062 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001063 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001064 bool is_hex = false;
1065 bool is_float = false;
1066 bool has_e = false;
1067 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001068
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001070 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001072
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001073 if (*p == '$') {
1074 p++;
1075 is_hex = true;
1076 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001077
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001078 for (;;) {
1079 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001080
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001081 if (!is_hex && (c == 'e' || c == 'E')) {
1082 has_e = true;
1083 if (*p == '+' || *p == '-') {
1084 /*
1085 * e can only be followed by +/- if it is either a
1086 * prefixed hex number or a floating-point number
1087 */
1088 p++;
1089 is_float = true;
1090 }
1091 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1092 is_hex = true;
1093 } else if (c == 'P' || c == 'p') {
1094 is_float = true;
1095 if (*p == '+' || *p == '-')
1096 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001097 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001098 ; /* just advance */
1099 else if (c == '.') {
1100 /*
1101 * we need to deal with consequences of the legacy
1102 * parser, like "1.nolist" being two tokens
1103 * (TOK_NUMBER, TOK_ID) here; at least give it
1104 * a shot for now. In the future, we probably need
1105 * a flex-based scanner with proper pattern matching
1106 * to do it as well as it can be done. Nothing in
1107 * the world is going to help the person who wants
1108 * 0x123.p16 interpreted as two tokens, though.
1109 */
1110 r = p;
1111 while (*r == '_')
1112 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001113
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001114 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1115 (!is_hex && (*r == 'e' || *r == 'E')) ||
1116 (*r == 'p' || *r == 'P')) {
1117 p = r;
1118 is_float = true;
1119 } else
1120 break; /* Terminate the token */
1121 } else
1122 break;
1123 }
1124 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001125
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001126 if (p == line+1 && *line == '$') {
1127 type = TOK_OTHER; /* TOKEN_HERE */
1128 } else {
1129 if (has_e && !is_hex) {
1130 /* 1e13 is floating-point, but 1e13h is not */
1131 is_float = true;
1132 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001133
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001134 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1135 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001136 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001138 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 /*
1140 * Whitespace just before end-of-line is discarded by
1141 * pretending it's a comment; whitespace just before a
1142 * comment gets lumped into the comment.
1143 */
1144 if (!*p || *p == ';') {
1145 type = TOK_COMMENT;
1146 while (*p)
1147 p++;
1148 }
1149 } else if (*p == ';') {
1150 type = TOK_COMMENT;
1151 while (*p)
1152 p++;
1153 } else {
1154 /*
1155 * Anything else is an operator of some kind. We check
1156 * for all the double-character operators (>>, <<, //,
1157 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001158 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 */
1160 type = TOK_OTHER;
1161 if ((p[0] == '>' && p[1] == '>') ||
1162 (p[0] == '<' && p[1] == '<') ||
1163 (p[0] == '/' && p[1] == '/') ||
1164 (p[0] == '<' && p[1] == '=') ||
1165 (p[0] == '>' && p[1] == '=') ||
1166 (p[0] == '=' && p[1] == '=') ||
1167 (p[0] == '!' && p[1] == '=') ||
1168 (p[0] == '<' && p[1] == '>') ||
1169 (p[0] == '&' && p[1] == '&') ||
1170 (p[0] == '|' && p[1] == '|') ||
1171 (p[0] == '^' && p[1] == '^')) {
1172 p++;
1173 }
1174 p++;
1175 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001176
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 /* Handling unterminated string by UNV */
1178 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001179 {
1180 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1181 t->text[p-line] = *line;
1182 tail = &t->next;
1183 }
1184 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 if (type != TOK_COMMENT) {
1186 *tail = t = new_Token(NULL, type, line, p - line);
1187 tail = &t->next;
1188 }
1189 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001190 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001191 return list;
1192}
1193
H. Peter Anvince616072002-04-30 21:02:23 +00001194/*
1195 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001196 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001197 * deleted only all at once by the delete_Blocks function.
1198 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001200{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001201 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001202
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001203 /* first, get to the end of the linked list */
1204 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001206 /* now allocate the requested chunk */
1207 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001209 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001210 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001211 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001212}
1213
1214/*
1215 * this function deletes all managed blocks of memory
1216 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001218{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001220
H. Peter Anvin70653092007-10-19 14:42:29 -07001221 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001222 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001223 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001224 * free it.
1225 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001227 if (b->chunk)
1228 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001229 a = b;
1230 b = b->next;
1231 if (a != &blocks)
1232 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001233 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001234 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001235}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001236
1237/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001238 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001239 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001240 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001241static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001242 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001243{
1244 Token *t;
1245 int i;
1246
H. Peter Anvin89cee572009-07-15 09:16:54 -04001247 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1249 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1250 freeTokens[i].next = &freeTokens[i + 1];
1251 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001252 }
1253 t = freeTokens;
1254 freeTokens = t->next;
1255 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001256 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001257 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001258 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001259 t->text = NULL;
1260 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001261 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001263 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001264 t->text = nasm_malloc(txtlen+1);
1265 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001267 }
1268 return t;
1269}
1270
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001271static Token *dup_Token(Token *next, const Token *src)
1272{
1273 return new_Token(next, src->type, src->text, src->len);
1274}
1275
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001277{
1278 Token *next = t->next;
1279 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001280 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001281 freeTokens = t;
1282 return next;
1283}
1284
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001285/*
1286 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001287 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1288 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001289 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001290static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001291{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001292 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001293 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001294 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001295
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001296 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001297 if (t->type == TOK_PREPROC_ID && t->text &&
1298 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001299 char *v;
1300 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001301
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001302 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001303 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001304 size_t len = nasm_unquote(v, NULL);
1305 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001306
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001307 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001308 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001309 v = NULL;
1310 }
1311 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001312
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001313 if (v) {
1314 char *p = getenv(v);
1315 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001316 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001317 /*
1318 * FIXME We better should investigate if accessing
1319 * ->text[1] without ->text[0] is safe enough.
1320 */
1321 t->text = nasm_zalloc(2);
1322 } else
1323 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001324 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001325 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001327
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 /* Expand local macros here and not during preprocessing */
1329 if (expand_locals &&
1330 t->type == TOK_PREPROC_ID && t->text &&
1331 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001332 const char *q;
1333 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001334 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001336 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1337 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 nasm_free(t->text);
1339 t->text = p;
1340 }
1341 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001342 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001344 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001346 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001347
H. Peter Anvin734b1882002-04-30 21:01:08 +00001348 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001349
1350 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001352 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 } else if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001354 memcpy(p, t->text, t->len);
1355 p += t->len;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001357 }
1358 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001359
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001360 return line;
1361}
1362
1363/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001364 * A scanner, suitable for use by the expression evaluator, which
1365 * operates on a line of Tokens. Expects a pointer to a pointer to
1366 * the first token in the line to be passed in as its private_data
1367 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001368 *
1369 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001370 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001371struct ppscan {
1372 Token *tptr;
1373 int ntokens;
1374};
1375
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001377{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001378 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001379 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001380 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001381
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001383 if (pps->ntokens && (tline = pps->tptr)) {
1384 pps->ntokens--;
1385 pps->tptr = tline->next;
1386 } else {
1387 pps->tptr = NULL;
1388 pps->ntokens = 0;
1389 return tokval->t_type = TOKEN_EOS;
1390 }
1391 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001392
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001393 tokval->t_charptr = tline->text;
1394
H. Peter Anvin76690a12002-04-30 20:52:49 +00001395 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001396 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001397 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001399
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001401 p = tokval->t_charptr = tline->text;
1402 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 tokval->t_charptr++;
1404 return tokval->t_type = TOKEN_ID;
1405 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001406
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001407 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001408 if (r >= p+MAX_KEYWORD)
1409 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001410 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001411 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001412 *s = '\0';
1413 /* right, so we have an identifier sitting in temp storage. now,
1414 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001415 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001416 }
1417
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001419 bool rn_error;
1420 tokval->t_integer = readnum(tline->text, &rn_error);
1421 tokval->t_charptr = tline->text;
1422 if (rn_error)
1423 return tokval->t_type = TOKEN_ERRNUM;
1424 else
1425 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001426 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001427
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001428 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001429 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001430 }
1431
H. Peter Anvine2c80182005-01-15 22:15:51 +00001432 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001433 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001434
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001435 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001436 tokval->t_charptr = tline->text;
1437 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001438
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001439 if (ep[0] != bq || ep[1] != '\0')
1440 return tokval->t_type = TOKEN_ERRSTR;
1441 else
1442 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001443 }
1444
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 if (tline->type == TOK_OTHER) {
1446 if (!strcmp(tline->text, "<<"))
1447 return tokval->t_type = TOKEN_SHL;
1448 if (!strcmp(tline->text, ">>"))
1449 return tokval->t_type = TOKEN_SHR;
1450 if (!strcmp(tline->text, "//"))
1451 return tokval->t_type = TOKEN_SDIV;
1452 if (!strcmp(tline->text, "%%"))
1453 return tokval->t_type = TOKEN_SMOD;
1454 if (!strcmp(tline->text, "=="))
1455 return tokval->t_type = TOKEN_EQ;
1456 if (!strcmp(tline->text, "<>"))
1457 return tokval->t_type = TOKEN_NE;
1458 if (!strcmp(tline->text, "!="))
1459 return tokval->t_type = TOKEN_NE;
1460 if (!strcmp(tline->text, "<="))
1461 return tokval->t_type = TOKEN_LE;
1462 if (!strcmp(tline->text, ">="))
1463 return tokval->t_type = TOKEN_GE;
1464 if (!strcmp(tline->text, "&&"))
1465 return tokval->t_type = TOKEN_DBL_AND;
1466 if (!strcmp(tline->text, "^^"))
1467 return tokval->t_type = TOKEN_DBL_XOR;
1468 if (!strcmp(tline->text, "||"))
1469 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001470 }
1471
1472 /*
1473 * We have no other options: just return the first character of
1474 * the token text.
1475 */
1476 return tokval->t_type = tline->text[0];
1477}
1478
1479/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001480 * Compare a string to the name of an existing macro; this is a
1481 * simple wrapper which calls either strcmp or nasm_stricmp
1482 * depending on the value of the `casesense' parameter.
1483 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001484static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001485{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001486 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001487}
1488
1489/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001490 * Compare a string to the name of an existing macro; this is a
1491 * simple wrapper which calls either strcmp or nasm_stricmp
1492 * depending on the value of the `casesense' parameter.
1493 */
1494static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1495{
1496 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1497}
1498
1499/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001500 * Return the Context structure associated with a %$ token. Return
1501 * NULL, having _already_ reported an error condition, if the
1502 * context stack isn't deep enough for the supplied number of $
1503 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001504 *
1505 * If "namep" is non-NULL, set it to the pointer to the macro name
1506 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001507 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001508static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001509{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001510 Context *ctx;
1511 int i;
1512
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001513 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001514 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001515
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001516 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001518
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001520 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001522 }
1523
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001524 name += 2;
1525 ctx = cstk;
1526 i = 0;
1527 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001528 name++;
1529 i++;
1530 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001531 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001533 nasm_nonfatal("`%s': context stack is only"
1534 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001535 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001536 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001537
1538 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001539 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001540
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001541 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001542}
1543
1544/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001545 * Open an include file. This routine must always return a valid
1546 * file pointer if it returns - it's responsible for throwing an
1547 * ERR_FATAL and bombing out completely if not. It should also try
1548 * the include path one by one until it finds the file or reaches
1549 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001550 *
1551 * Note: for INC_PROBE the function returns NULL at all times;
1552 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001553 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001554enum incopen_mode {
1555 INC_NEEDED, /* File must exist */
1556 INC_OPTIONAL, /* Missing is OK */
1557 INC_PROBE /* Only an existence probe */
1558};
1559
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001560/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001561static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001562 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001563{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001564 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001565 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001566 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001567 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001568 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001569
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001571 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001572 if (omode == INC_PROBE) {
1573 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001574 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001575 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001576 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001577 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001578 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001579 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001580 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001582 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001583
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001584 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001585
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001586 if (!ip) {
1587 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001588 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001589 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001590
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001591 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001592 ip = ip->next;
1593 }
1594}
1595
1596/*
1597 * Open a file, or test for the presence of one (depending on omode),
1598 * considering the include path.
1599 */
1600static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001601 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001602 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001603 enum incopen_mode omode,
1604 enum file_flags fmode)
1605{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001606 struct hash_insert hi;
1607 void **hp;
1608 char *path;
1609 FILE *fp = NULL;
1610
1611 hp = hash_find(&FileHash, file, &hi);
1612 if (hp) {
1613 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001614 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001615 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001616 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001617 } else {
1618 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001619 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001620
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001621 /* Positive or negative result */
1622 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001623
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001624 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001625 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001626 */
1627 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001628 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001629 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001630
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001631 if (!path) {
1632 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001633 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001634 } else {
1635 if (!fp && omode != INC_PROBE)
1636 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001637 }
1638
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001639 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001640 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001641
1642 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001643}
1644
1645/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001646 * Opens an include or input file. Public version, for use by modules
1647 * that get a file:lineno pair and need to look at the file again
1648 * (e.g. the CodeView debug backend). Returns NULL on failure.
1649 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001650FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001651{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001652 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001653}
1654
1655/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001656 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001657 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001658 * return true if _any_ single-line macro of that name is defined.
1659 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001660 * `nparam' or no parameters is defined.
1661 *
1662 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001663 * defined, or nparam is -1, the address of the definition structure
1664 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001665 * is NULL, no action will be taken regarding its contents, and no
1666 * error will occur.
1667 *
1668 * Note that this is also called with nparam zero to resolve
1669 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001670 *
1671 * If you already know which context macro belongs to, you can pass
1672 * the context pointer as first parameter; if you won't but name begins
1673 * with %$ the context will be automatically computed. If all_contexts
1674 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001675 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001676static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001677smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001678 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001679{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001680 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001681 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001682
H. Peter Anvin97a23472007-09-16 17:57:25 -07001683 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001684 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001685 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001686 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001687 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001688 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001689 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001690 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001691 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001692 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001693 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001694 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001695
H. Peter Anvine2c80182005-01-15 22:15:51 +00001696 while (m) {
1697 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001698 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001699 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001700 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 *defn = m;
1702 else
1703 *defn = NULL;
1704 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001705 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001706 }
1707 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001708 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001709
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001710 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001711}
1712
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001713/* param should be a natural number [0; INT_MAX] */
1714static int read_param_count(const char *str)
1715{
1716 int result;
1717 bool err;
1718
1719 result = readnum(str, &err);
1720 if (result < 0 || result > INT_MAX) {
1721 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001722 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1723 str, 0, INT_MAX);
1724 } else if (err)
1725 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001726 return result;
1727}
1728
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001729/*
1730 * Count and mark off the parameters in a multi-line macro call.
1731 * This is called both from within the multi-line macro expansion
1732 * code, and also to mark off the default parameters when provided
1733 * in a %macro definition line.
1734 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001736{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001737 int paramsize, brace;
1738
1739 *nparam = paramsize = 0;
1740 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001742 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001743 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001744 paramsize += PARAM_DELTA;
1745 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1746 }
1747 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001748 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001750 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001752 if (brace) {
1753 while (brace && (t = t->next) != NULL) {
1754 if (tok_is_(t, "{"))
1755 brace++;
1756 else if (tok_is_(t, "}"))
1757 brace--;
1758 }
1759
1760 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 /*
1762 * Now we've found the closing brace, look further
1763 * for the comma.
1764 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001765 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 skip_white_(t);
1767 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001768 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 while (tok_isnt_(t, ","))
1770 t = t->next;
1771 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001773 } else {
1774 while (tok_isnt_(t, ","))
1775 t = t->next;
1776 }
1777 if (t) { /* got a comma/brace */
1778 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001780 }
1781}
1782
1783/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001784 * Determine whether one of the various `if' conditions is true or
1785 * not.
1786 *
1787 * We must free the tline we get passed.
1788 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001789static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001790{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001791 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001792 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001793 Token *t, *tt, *origline;
1794 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001795 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001796 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001797 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001798 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001799
1800 origline = tline;
1801
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001803 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001804 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001805 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001807 if (!tline)
1808 break;
1809 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001810 nasm_nonfatal("`%s' expects context identifiers",
1811 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 free_tlist(origline);
1813 return -1;
1814 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001815 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001816 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 tline = tline->next;
1818 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001819 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001820
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001821 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001822 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001823 while (tline) {
1824 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 if (!tline || (tline->type != TOK_ID &&
1826 (tline->type != TOK_PREPROC_ID ||
1827 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001828 nasm_nonfatal("`%s' expects macro identifiers",
1829 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001830 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001832 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001833 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001835 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001836 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001837
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001838 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001839 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001840 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001841 while (tline) {
1842 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001843 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001844 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001845 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001846 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001847 nasm_nonfatal("`%s' expects environment variable names",
1848 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001849 goto fail;
1850 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001851 p = tline->text;
1852 if (tline->type == TOK_PREPROC_ID)
1853 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001854 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001855 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001856 if (getenv(p))
1857 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001858 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001859 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001860 break;
1861
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001862 case PPC_IFIDN:
1863 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 tline = expand_smacro(tline);
1865 t = tt = tline;
1866 while (tok_isnt_(tt, ","))
1867 tt = tt->next;
1868 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001869 nasm_nonfatal("`%s' expects two comma-separated arguments",
1870 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001871 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 }
1873 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001874 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1876 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001877 nasm_nonfatal("`%s': more than one comma on line",
1878 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001879 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 }
1881 if (t->type == TOK_WHITESPACE) {
1882 t = t->next;
1883 continue;
1884 }
1885 if (tt->type == TOK_WHITESPACE) {
1886 tt = tt->next;
1887 continue;
1888 }
1889 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001890 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 break;
1892 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001893 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001895 size_t l1 = nasm_unquote(t->text, NULL);
1896 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001897
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001898 if (l1 != l2) {
1899 j = false;
1900 break;
1901 }
1902 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1903 j = false;
1904 break;
1905 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001906 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001907 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 break;
1909 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001910
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 t = t->next;
1912 tt = tt->next;
1913 }
1914 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001915 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001916 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001917
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001918 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001919 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001920 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001921 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001922
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001923 skip_white_(tline);
1924 tline = expand_id(tline);
1925 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001926 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001927 goto fail;
1928 }
1929 searching.name = nasm_strdup(tline->text);
1930 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001931 searching.plus = false;
1932 searching.nolist = false;
1933 searching.in_progress = 0;
1934 searching.max_depth = 0;
1935 searching.rep_nest = NULL;
1936 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 searching.nparam_max = INT_MAX;
1938 tline = expand_smacro(tline->next);
1939 skip_white_(tline);
1940 if (!tline) {
1941 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001942 nasm_nonfatal("`%s' expects a parameter count or nothing",
1943 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 } else {
1945 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001946 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001947 }
1948 if (tline && tok_is_(tline->next, "-")) {
1949 tline = tline->next->next;
1950 if (tok_is_(tline, "*"))
1951 searching.nparam_max = INT_MAX;
1952 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001953 nasm_nonfatal("`%s' expects a parameter count after `-'",
1954 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001955 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001956 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001957 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001958 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001959 searching.nparam_max = searching.nparam_min;
1960 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 }
1962 }
1963 if (tline && tok_is_(tline->next, "+")) {
1964 tline = tline->next;
1965 searching.plus = true;
1966 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001967 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1968 while (mmac) {
1969 if (!strcmp(mmac->name, searching.name) &&
1970 (mmac->nparam_min <= searching.nparam_max
1971 || searching.plus)
1972 && (searching.nparam_min <= mmac->nparam_max
1973 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001974 found = true;
1975 break;
1976 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001977 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 }
1979 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001980 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 nasm_free(searching.name);
1982 j = found;
1983 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001984 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001985
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001986 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987 needtype = TOK_ID;
1988 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001989 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990 needtype = TOK_NUMBER;
1991 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001992 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 needtype = TOK_STRING;
1994 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001995
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996iftype:
1997 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001998
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999 while (tok_type_(t, TOK_WHITESPACE) ||
2000 (needtype == TOK_NUMBER &&
2001 tok_type_(t, TOK_OTHER) &&
2002 (t->text[0] == '-' || t->text[0] == '+') &&
2003 !t->text[1]))
2004 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002005
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002006 j = tok_type_(t, needtype);
2007 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002008
2009 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 t = tline = expand_smacro(tline);
2011 while (tok_type_(t, TOK_WHITESPACE))
2012 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002013
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 j = false;
2015 if (t) {
2016 t = t->next; /* Skip the actual token */
2017 while (tok_type_(t, TOK_WHITESPACE))
2018 t = t->next;
2019 j = !t; /* Should be nothing left */
2020 }
2021 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002022
H. Peter Anvin134b9462008-02-16 17:01:40 -08002023 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002024 t = tline = expand_smacro(tline);
2025 while (tok_type_(t, TOK_WHITESPACE))
2026 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002027
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002028 j = !t; /* Should be empty */
2029 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002030
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002031 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002032 pps.tptr = tline = expand_smacro(tline);
2033 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002034 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002035 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002036 if (!evalresult)
2037 return -1;
2038 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002039 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002040 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002041 nasm_nonfatal("non-constant value given to `%s'",
2042 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002043 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002045 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002046 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002047
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002049 nasm_fatal("preprocessor directive `%s' not yet implemented",
2050 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002051 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002052 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002053
2054 free_tlist(origline);
2055 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002056
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002057fail:
2058 free_tlist(origline);
2059 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002060}
2061
2062/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002063 * Default smacro expansion routine: just returns a copy of the
2064 * expansion list.
2065 */
2066static Token *
2067smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2068{
2069 (void)params;
2070 (void)nparams;
2071
2072 return dup_tlist(s->expansion, NULL);
2073}
2074
2075/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002076 * Emit a macro defintion or undef to the listing file, if
2077 * desired. This is similar to detoken(), but it handles the reverse
2078 * expansion list, does not expand %! or local variable tokens, and
2079 * does some special handling for macro parameters.
2080 */
2081static void
2082list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2083{
2084 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2085 const Token **param_names;
2086 Token *junk_names = NULL;
2087 Token *t;
2088 size_t namelen, size;
2089 unsigned int i;
2090 char *def, *p;
2091 unsigned int context_depth = 0;
2092
2093 nasm_newn(param_names, m->nparam);
2094
2095 namelen = strlen(m->name);
2096 size = namelen + 2; /* Include room for space after name + NUL */
2097
2098 if (ctx) {
2099 context_depth = cstk->depth - ctx->depth + 1;
2100 size += context_depth + 1; /* % + some number of $ */
2101 }
2102
2103 list_for_each(t, m->expansion) {
2104 if (!t->text) {
2105 size++; /* Whitespace, presumably */
2106 } else {
2107 size += t->len;
2108 if (is_smac_param(t->type))
2109 param_names[smac_nparam(t->type)] = t;
2110 }
2111 }
2112
2113 if (m->nparam) {
2114 /*
2115 * Space for ( and either , or ) around each
2116 * parameter, plus an optional =.
2117 */
2118 size += 1 + 2 * m->nparam;
2119 for (i = 0; i < m->nparam; i++) {
2120 if (!param_names[i])
2121 param_names[i] = &unused_arg_name;
2122 size += param_names[i]->len;
2123 }
2124 }
2125
2126 def = nasm_malloc(size);
2127 p = def+size;
2128 *--p = '\0';
2129
2130 list_for_each(t, m->expansion) {
2131 if (!t->text) {
2132 *--p = ' ';
2133 } else {
2134 p -= t->len;
2135 memcpy(p, t->text, t->len);
2136 }
2137 }
2138
2139 *--p = ' ';
2140
2141 if (m->nparam) {
2142 *--p = ')';
2143 for (i = m->nparam; i--;) {
2144 p -= param_names[i]->len;
2145 memcpy(p, param_names[i]->text, param_names[i]->len);
2146 if (m->eval_param && m->eval_param[i])
2147 *--p = '=';
2148 *--p = ',';
2149 }
2150 *p = '('; /* First parameter starts with ( not , */
2151
2152 free_tlist(junk_names);
2153 }
2154
2155 p -= namelen;
2156 memcpy(p, m->name, namelen);
2157
2158 if (context_depth) {
2159 for (i = 0; i < context_depth; i++)
2160 *--p = '$';
2161 *--p = '%';
2162 }
2163
2164 nasm_listmsg("%s %s", pp_directives[op], p);
2165
2166 nasm_free(def);
2167 }
2168
2169/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002170 * Common code for defining an smacro
2171 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002172static SMacro *define_smacro(Context *ctx, const char *mname,
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002173 bool casesense, int nparam,
2174 bool *eval_param, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002175{
2176 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002177 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002178
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002179 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002181 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002182 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002183 /*
2184 * Some instances of the old code considered this a failure,
2185 * some others didn't. What is the right thing to do here?
2186 */
2187 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002188 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002189 } else {
2190 /*
2191 * We're redefining, so we have to take over an
2192 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002193 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002194 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002195 clear_smacro(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002196 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002197 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002198 smtbl = ctx ? &ctx->localmac : &smacros;
2199 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002200 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002201 smac->next = *smhead;
2202 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002203 }
2204 smac->name = nasm_strdup(mname);
2205 smac->casesense = casesense;
2206 smac->nparam = nparam;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002207 smac->eval_param = eval_param;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002208 smac->expansion = expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002209 smac->expand = smacro_expand_default;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002210
2211 if (list_option('m'))
2212 list_smacro_def(casesense ? PP_DEFINE : PP_IDEFINE, ctx, smac);
2213
H. Peter Anvin8b262472019-02-26 14:00:54 -08002214 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002215}
2216
2217/*
2218 * Undefine an smacro
2219 */
2220static void undef_smacro(Context *ctx, const char *mname)
2221{
2222 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002223 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002224
H. Peter Anvin166c2472008-05-28 12:28:58 -07002225 smtbl = ctx ? &ctx->localmac : &smacros;
2226 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002227
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002228 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002229 /*
2230 * We now have a macro name... go hunt for it.
2231 */
2232 sp = smhead;
2233 while ((s = *sp) != NULL) {
2234 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002235 if (list_option('m'))
2236 list_smacro_def(PP_UNDEF, ctx, s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002237 *sp = s->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002238 free_smacro(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002239 } else {
2240 sp = &s->next;
2241 }
2242 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002243 }
2244}
2245
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002246/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002247 * Parse a mmacro specification.
2248 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002249static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002250{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002251 tline = tline->next;
2252 skip_white_(tline);
2253 tline = expand_id(tline);
2254 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002255 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002256 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002257 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002258
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002259 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002260 def->name = nasm_strdup(tline->text);
2261 def->plus = false;
2262 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002263 def->in_progress = 0;
2264 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002265 def->nparam_min = 0;
2266 def->nparam_max = 0;
2267
H. Peter Anvina26433d2008-07-16 14:40:01 -07002268 tline = expand_smacro(tline->next);
2269 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002270 if (!tok_type_(tline, TOK_NUMBER))
2271 nasm_nonfatal("`%s' expects a parameter count", directive);
2272 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002273 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002274 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002275 tline = tline->next->next;
2276 if (tok_is_(tline, "*")) {
2277 def->nparam_max = INT_MAX;
2278 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002279 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002280 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002281 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002282 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002283 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002284 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002285 }
2286 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002287 }
2288 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002289 tline = tline->next;
2290 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002291 }
2292 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002293 !nasm_stricmp(tline->next->text, ".nolist")) {
2294 tline = tline->next;
2295 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002296 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002297
H. Peter Anvina26433d2008-07-16 14:40:01 -07002298 /*
2299 * Handle default parameters.
2300 */
2301 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002302 def->dlist = tline->next;
2303 tline->next = NULL;
2304 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002305 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002306 def->dlist = NULL;
2307 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002308 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002309 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002310
H. Peter Anvin89cee572009-07-15 09:16:54 -04002311 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002312 !def->plus) {
2313 /*
2314 *!macro-defaults [on] macros with more default than optional parameters
2315 *! warns when a macro has more default parameters than optional parameters.
2316 *! See \k{mlmacdef} for why might want to disable this warning.
2317 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002318 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002319 "too many default macro parameters in macro `%s'", def->name);
2320 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002321
H. Peter Anvina26433d2008-07-16 14:40:01 -07002322 return true;
2323}
2324
2325
2326/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002327 * Decode a size directive
2328 */
2329static int parse_size(const char *str) {
2330 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002331 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002332 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002333 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002334 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002335}
2336
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002337/*
2338 * Process a preprocessor %pragma directive. Currently there are none.
2339 * Gets passed the token list starting with the "preproc" token from
2340 * "%pragma preproc".
2341 */
2342static void do_pragma_preproc(Token *tline)
2343{
2344 /* Skip to the real stuff */
2345 tline = tline->next;
2346 skip_white_(tline);
2347 if (!tline)
2348 return;
2349
2350 (void)tline; /* Nothing else to do at present */
2351}
2352
Ed Beroset3ab3f412002-06-11 03:31:49 +00002353/**
2354 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002355 * Find out if a line contains a preprocessor directive, and deal
2356 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002357 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002358 * If a directive _is_ found, it is the responsibility of this routine
2359 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002360 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002361 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002362 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002363 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002364 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002365 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002366static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002367{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002368 enum preproc_token i;
2369 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002370 bool err;
2371 int nparam;
2372 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002373 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002374 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002375 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002376 char *p, *pp;
2377 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002378 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002379 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002380 Include *inc;
2381 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002382 Cond *cond;
2383 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002384 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002385 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002386 struct tokenval tokval;
2387 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002388 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002389 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002390 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002391 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002392 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002393
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002394 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002395 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002396
H. Peter Anvineba20a72002-04-30 20:53:55 +00002397 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002398 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002399 (tline->text[0] && (tline->text[1] == '%' ||
2400 tline->text[1] == '$' ||
2401 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002402 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002403
H. Peter Anvin4169a472007-09-12 01:29:43 +00002404 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002405
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002406 /*
2407 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2408 * since they are known to be buggy at moment, we need to fix them
2409 * in future release (2.09-2.10)
2410 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002411 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002412 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2413 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002414 }
2415
2416 /*
2417 * If we're in a non-emitting branch of a condition construct,
2418 * or walking to the end of an already terminated %rep block,
2419 * we should ignore all directives except for condition
2420 * directives.
2421 */
2422 if (((istk->conds && !emitting(istk->conds->state)) ||
2423 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2424 return NO_DIRECTIVE_FOUND;
2425 }
2426
2427 /*
2428 * If we're defining a macro or reading a %rep block, we should
2429 * ignore all directives except for %macro/%imacro (which nest),
2430 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2431 * If we're in a %rep block, another %rep nests, so should be let through.
2432 */
2433 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2434 i != PP_RMACRO && i != PP_IRMACRO &&
2435 i != PP_ENDMACRO && i != PP_ENDM &&
2436 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2437 return NO_DIRECTIVE_FOUND;
2438 }
2439
2440 if (defining) {
2441 if (i == PP_MACRO || i == PP_IMACRO ||
2442 i == PP_RMACRO || i == PP_IRMACRO) {
2443 nested_mac_count++;
2444 return NO_DIRECTIVE_FOUND;
2445 } else if (nested_mac_count > 0) {
2446 if (i == PP_ENDMACRO) {
2447 nested_mac_count--;
2448 return NO_DIRECTIVE_FOUND;
2449 }
2450 }
2451 if (!defining->name) {
2452 if (i == PP_REP) {
2453 nested_rep_count++;
2454 return NO_DIRECTIVE_FOUND;
2455 } else if (nested_rep_count > 0) {
2456 if (i == PP_ENDREP) {
2457 nested_rep_count--;
2458 return NO_DIRECTIVE_FOUND;
2459 }
2460 }
2461 }
2462 }
2463
H. Peter Anvin8b262472019-02-26 14:00:54 -08002464 dname = pp_directives[i]; /* Directive name, for error messages */
2465 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002466 switch (i) {
2467 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002468 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002470
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002471 case PP_PRAGMA:
2472 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002473 * %pragma namespace options...
2474 *
2475 * The namespace "preproc" is reserved for the preprocessor;
2476 * all other namespaces generate a [pragma] assembly directive.
2477 *
2478 * Invalid %pragmas are ignored and may have different
2479 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002480 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002481 tline = tline->next;
2482 skip_white_(tline);
2483 tline = expand_smacro(tline);
2484 if (tok_type_(tline, TOK_ID)) {
2485 if (!nasm_stricmp(tline->text, "preproc")) {
2486 /* Preprocessor pragma */
2487 do_pragma_preproc(tline);
2488 } else {
2489 /* Build the assembler directive */
2490 t = new_Token(NULL, TOK_OTHER, "[", 1);
2491 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2492 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2493 tline = t;
2494 for (t = tline; t->next; t = t->next)
2495 ;
2496 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002497 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002498 }
2499 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002500 free_tlist(origline);
2501 return DIRECTIVE_FOUND;
2502
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 case PP_STACKSIZE:
2504 /* Directive to tell NASM what the default stack size is. The
2505 * default is for a 16-bit stack, and this can be overriden with
2506 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 */
2508 tline = tline->next;
2509 if (tline && tline->type == TOK_WHITESPACE)
2510 tline = tline->next;
2511 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002512 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 free_tlist(origline);
2514 return DIRECTIVE_FOUND;
2515 }
2516 if (nasm_stricmp(tline->text, "flat") == 0) {
2517 /* All subsequent ARG directives are for a 32-bit stack */
2518 StackSize = 4;
2519 StackPointer = "ebp";
2520 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002521 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002522 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2523 /* All subsequent ARG directives are for a 64-bit stack */
2524 StackSize = 8;
2525 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002526 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002527 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002528 } else if (nasm_stricmp(tline->text, "large") == 0) {
2529 /* All subsequent ARG directives are for a 16-bit stack,
2530 * far function call.
2531 */
2532 StackSize = 2;
2533 StackPointer = "bp";
2534 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002535 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 } else if (nasm_stricmp(tline->text, "small") == 0) {
2537 /* All subsequent ARG directives are for a 16-bit stack,
2538 * far function call. We don't support near functions.
2539 */
2540 StackSize = 2;
2541 StackPointer = "bp";
2542 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002543 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002545 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 free_tlist(origline);
2547 return DIRECTIVE_FOUND;
2548 }
2549 free_tlist(origline);
2550 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002551
H. Peter Anvine2c80182005-01-15 22:15:51 +00002552 case PP_ARG:
2553 /* TASM like ARG directive to define arguments to functions, in
2554 * the following form:
2555 *
2556 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2557 */
2558 offset = ArgOffset;
2559 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002560 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002562
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 /* Find the argument name */
2564 tline = tline->next;
2565 if (tline && tline->type == TOK_WHITESPACE)
2566 tline = tline->next;
2567 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002568 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 free_tlist(origline);
2570 return DIRECTIVE_FOUND;
2571 }
2572 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002573
H. Peter Anvine2c80182005-01-15 22:15:51 +00002574 /* Find the argument size type */
2575 tline = tline->next;
2576 if (!tline || tline->type != TOK_OTHER
2577 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002578 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002579 free_tlist(origline);
2580 return DIRECTIVE_FOUND;
2581 }
2582 tline = tline->next;
2583 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002584 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585 free_tlist(origline);
2586 return DIRECTIVE_FOUND;
2587 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002588
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002590 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 size = parse_size(tt->text);
2593 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002594 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002595 free_tlist(tt);
2596 free_tlist(origline);
2597 return DIRECTIVE_FOUND;
2598 }
2599 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002600
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002601 /* Round up to even stack slots */
2602 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002603
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 /* Now define the macro for the argument */
2605 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2606 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002607 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002608 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002609
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 /* Move to the next argument in the list */
2611 tline = tline->next;
2612 if (tline && tline->type == TOK_WHITESPACE)
2613 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002614 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002616 free_tlist(origline);
2617 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002618
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 case PP_LOCAL:
2620 /* TASM like LOCAL directive to define local variables for a
2621 * function, in the following form:
2622 *
2623 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2624 *
2625 * The '= LocalSize' at the end is ignored by NASM, but is
2626 * required by TASM to define the local parameter size (and used
2627 * by the TASM macro package).
2628 */
2629 offset = LocalOffset;
2630 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002631 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002632 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002633
H. Peter Anvine2c80182005-01-15 22:15:51 +00002634 /* Find the argument name */
2635 tline = tline->next;
2636 if (tline && tline->type == TOK_WHITESPACE)
2637 tline = tline->next;
2638 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002639 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002640 free_tlist(origline);
2641 return DIRECTIVE_FOUND;
2642 }
2643 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002644
H. Peter Anvine2c80182005-01-15 22:15:51 +00002645 /* Find the argument size type */
2646 tline = tline->next;
2647 if (!tline || tline->type != TOK_OTHER
2648 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002649 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002650 free_tlist(origline);
2651 return DIRECTIVE_FOUND;
2652 }
2653 tline = tline->next;
2654 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002655 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 free_tlist(origline);
2657 return DIRECTIVE_FOUND;
2658 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002659
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002661 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002663 size = parse_size(tt->text);
2664 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002665 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 free_tlist(tt);
2667 free_tlist(origline);
2668 return DIRECTIVE_FOUND;
2669 }
2670 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002671
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002672 /* Round up to even stack slots */
2673 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002674
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002675 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002676
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2679 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002680 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002681
H. Peter Anvine2c80182005-01-15 22:15:51 +00002682 /* Now define the assign to setup the enter_c macro correctly */
2683 snprintf(directive, sizeof(directive),
2684 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002685 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002686
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 /* Move to the next argument in the list */
2688 tline = tline->next;
2689 if (tline && tline->type == TOK_WHITESPACE)
2690 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002691 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 free_tlist(origline);
2694 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002695
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 case PP_CLEAR:
2697 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002698 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 free_macros();
2700 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002701 free_tlist(origline);
2702 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002703
H. Peter Anvin418ca702008-05-30 10:42:30 -07002704 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002705 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002706 skip_white_(t);
2707 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002708 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002709 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002710 free_tlist(origline);
2711 return DIRECTIVE_FOUND; /* but we did _something_ */
2712 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002713 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002714 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002716 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002717 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002718 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002719 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002720 return DIRECTIVE_FOUND;
2721
2722 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002724 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002725
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002726 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002727 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002728 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 free_tlist(origline);
2730 return DIRECTIVE_FOUND; /* but we did _something_ */
2731 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002732 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002733 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002734 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002735 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002736 nasm_unquote_cstr(p, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002737 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002739 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002740 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002741 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002742 (pp_mode == PP_DEPS)
2743 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002744 if (!inc->fp) {
2745 /* -MG given but file not found */
2746 nasm_free(inc);
2747 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002748 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002749 inc->lineno = src_set_linnum(0);
2750 inc->lineinc = 1;
2751 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002752 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002753 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002754 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002755 }
2756 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002758
H. Peter Anvind2456592008-06-19 15:04:18 -07002759 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002760 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 static macros_t *use_pkg;
2762 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002763
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002764 tline = tline->next;
2765 skip_white_(tline);
2766 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002767
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002768 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002769 tline->type != TOK_INTERNAL_STRING &&
2770 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002771 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002772 free_tlist(origline);
2773 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002774 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002775 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002776 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002777 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002778 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002779 use_pkg = nasm_stdmac_find_package(tline->text);
2780 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002781 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002782 else
2783 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002784 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002785 /* Not already included, go ahead and include it */
2786 stdmacpos = use_pkg;
2787 }
2788 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002789 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002790 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002791 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002793 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002794 tline = tline->next;
2795 skip_white_(tline);
2796 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 if (tline) {
2798 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002799 nasm_nonfatal("`%s' expects a context identifier",
2800 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002801 free_tlist(origline);
2802 return DIRECTIVE_FOUND; /* but we did _something_ */
2803 }
2804 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002805 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002806 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002807 p = nasm_strdup(tline->text);
2808 } else {
2809 p = NULL; /* Anonymous */
2810 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002811
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002812 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002813 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002814 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002815 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002816 ctx->name = p;
2817 ctx->number = unique++;
2818 cstk = ctx;
2819 } else {
2820 /* %pop or %repl */
2821 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002822 nasm_nonfatal("`%s': context stack is empty",
2823 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002824 } else if (i == PP_POP) {
2825 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002826 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002827 "expected %s",
2828 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 else
2830 ctx_pop();
2831 } else {
2832 /* i == PP_REPL */
2833 nasm_free(cstk->name);
2834 cstk->name = p;
2835 p = NULL;
2836 }
2837 nasm_free(p);
2838 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002839 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002840 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002841 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002842 severity = ERR_FATAL;
2843 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002845 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002846 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002847 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002848 /*!
2849 *!user [on] %warning directives
2850 *! controls output of \c{%warning} directives (see \k{pperror}).
2851 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002852 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002853 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002854
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002855issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002856 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002857 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002858 tline->next = expand_smacro(tline->next);
2859 tline = tline->next;
2860 skip_white_(tline);
2861 t = tline ? tline->next : NULL;
2862 skip_white_(t);
2863 if (tok_type_(tline, TOK_STRING) && !t) {
2864 /* The line contains only a quoted string */
2865 p = tline->text;
2866 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002867 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002868 } else {
2869 /* Not a quoted string, or more than a quoted string */
2870 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002871 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002872 nasm_free(p);
2873 }
2874 free_tlist(origline);
2875 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002876 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002877
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002878 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002879 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002880 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002881 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002882 j = if_condition(tline->next, i);
2883 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002884 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002885 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002886 cond = nasm_malloc(sizeof(Cond));
2887 cond->next = istk->conds;
2888 cond->state = j;
2889 istk->conds = cond;
2890 if(istk->mstk)
2891 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002892 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002893 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002894
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002895 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002896 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002897 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002898 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002901 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002902
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002903 case COND_DONE:
2904 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002905 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002906
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002907 case COND_ELSE_TRUE:
2908 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002909 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002910 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002911 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002912 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002913
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002914 case COND_IF_FALSE:
2915 /*
2916 * IMPORTANT: In the case of %if, we will already have
2917 * called expand_mmac_params(); however, if we're
2918 * processing an %elif we must have been in a
2919 * non-emitting mode, which would have inhibited
2920 * the normal invocation of expand_mmac_params().
2921 * Therefore, we have to do it explicitly here.
2922 */
2923 j = if_condition(expand_mmac_params(tline->next), i);
2924 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002925 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002926 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2927 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002928 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002929 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002931
H. Peter Anvine2c80182005-01-15 22:15:51 +00002932 case PP_ELSE:
2933 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002934 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002935 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002936 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002937 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002938 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002939 case COND_IF_TRUE:
2940 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002941 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002942 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002943
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002944 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002945 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002946
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002947 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002948 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002949 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002950
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002951 case COND_ELSE_TRUE:
2952 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002953 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002954 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002955 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002956 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002957 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 free_tlist(origline);
2959 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002960
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 case PP_ENDIF:
2962 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002963 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002964 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002965 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002966 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002967 cond = istk->conds;
2968 istk->conds = cond->next;
2969 nasm_free(cond);
2970 if(istk->mstk)
2971 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 free_tlist(origline);
2973 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002974
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002975 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002977 casesense = false;
2978 /* fall through */
2979 case PP_RMACRO:
2980 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002981 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002982 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002983 return DIRECTIVE_FOUND;
2984 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002985 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002986 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2987 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002988 defining->casesense = casesense;
2989 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002990 nasm_free(defining);
2991 defining = NULL;
2992 return DIRECTIVE_FOUND;
2993 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002994
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002995 src_get(&defining->xline, &defining->fname);
2996
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002997 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2998 while (mmac) {
2999 if (!strcmp(mmac->name, defining->name) &&
3000 (mmac->nparam_min <= defining->nparam_max
3001 || defining->plus)
3002 && (defining->nparam_min <= mmac->nparam_max
3003 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003004 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003005 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003006 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003007 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003008 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003009 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 free_tlist(origline);
3011 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003012
H. Peter Anvine2c80182005-01-15 22:15:51 +00003013 case PP_ENDM:
3014 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003015 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003016 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003017 return DIRECTIVE_FOUND;
3018 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003019 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3020 defining->next = *mmhead;
3021 *mmhead = defining;
3022 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 free_tlist(origline);
3024 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003025
H. Peter Anvin89cee572009-07-15 09:16:54 -04003026 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003027 /*
3028 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003029 * macro-end marker for a macro with a name. Then we
3030 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003031 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003032 list_for_each(l, istk->expansion)
3033 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003034 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003035
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003036 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003037 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003038 * Remove all conditional entries relative to this
3039 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003040 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003041 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3042 cond = istk->conds;
3043 istk->conds = cond->next;
3044 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003045 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003046 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003047 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003048 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003049 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003050 free_tlist(origline);
3051 return DIRECTIVE_FOUND;
3052
H. Peter Anvina26433d2008-07-16 14:40:01 -07003053 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003054 casesense = false;
3055 /* fall through */
3056 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003057 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003058 MMacro **mmac_p;
3059 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003060
H. Peter Anvin8b262472019-02-26 14:00:54 -08003061 spec.casesense = casesense;
3062 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003063 return DIRECTIVE_FOUND;
3064 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003065 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3066 while (mmac_p && *mmac_p) {
3067 mmac = *mmac_p;
3068 if (mmac->casesense == spec.casesense &&
3069 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3070 mmac->nparam_min == spec.nparam_min &&
3071 mmac->nparam_max == spec.nparam_max &&
3072 mmac->plus == spec.plus) {
3073 *mmac_p = mmac->next;
3074 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003075 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003076 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003077 }
3078 }
3079 free_tlist(origline);
3080 free_tlist(spec.dlist);
3081 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003082 }
3083
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 case PP_ROTATE:
3085 if (tline->next && tline->next->type == TOK_WHITESPACE)
3086 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003087 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003088 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003089 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003090 return DIRECTIVE_FOUND;
3091 }
3092 t = expand_smacro(tline->next);
3093 tline->next = NULL;
3094 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003095 pps.tptr = tline = t;
3096 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 tokval.t_type = TOKEN_INVALID;
3098 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003099 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 free_tlist(tline);
3101 if (!evalresult)
3102 return DIRECTIVE_FOUND;
3103 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003104 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003106 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 return DIRECTIVE_FOUND;
3108 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003109 mmac = istk->mstk;
3110 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3111 mmac = mmac->next_active;
3112 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003113 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003114 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003115 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003116 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003117 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003118
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003119 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003120 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003121 rotate += mmac->nparam;
3122
3123 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003124 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003126
H. Peter Anvine2c80182005-01-15 22:15:51 +00003127 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003128 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 do {
3130 tline = tline->next;
3131 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003132
H. Peter Anvine2c80182005-01-15 22:15:51 +00003133 if (tok_type_(tline, TOK_ID) &&
3134 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003135 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 do {
3137 tline = tline->next;
3138 } while (tok_type_(tline, TOK_WHITESPACE));
3139 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003140
H. Peter Anvine2c80182005-01-15 22:15:51 +00003141 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003142 pps.tptr = expand_smacro(tline);
3143 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003145 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003147 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003148 if (!evalresult) {
3149 free_tlist(origline);
3150 return DIRECTIVE_FOUND;
3151 }
3152 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003153 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003154 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003155 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 return DIRECTIVE_FOUND;
3157 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003158 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003159 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003160 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3161 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003162 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003163 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003164 /*!
3165 *!negative-rep [on] regative %rep count
3166 *! warns about negative counts given to the \c{%rep}
3167 *! preprocessor directive.
3168 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003169 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003170 "negative `%%rep' count: %"PRId64, count);
3171 count = 0;
3172 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003173 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003174 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003175 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003176 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003177 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003178 }
3179 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003180
3181 tmp_defining = defining;
3182 defining = nasm_malloc(sizeof(MMacro));
3183 defining->prev = NULL;
3184 defining->name = NULL; /* flags this macro as a %rep block */
3185 defining->casesense = false;
3186 defining->plus = false;
3187 defining->nolist = nolist;
3188 defining->in_progress = count;
3189 defining->max_depth = 0;
3190 defining->nparam_min = defining->nparam_max = 0;
3191 defining->defaults = NULL;
3192 defining->dlist = NULL;
3193 defining->expansion = NULL;
3194 defining->next_active = istk->mstk;
3195 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003197
H. Peter Anvine2c80182005-01-15 22:15:51 +00003198 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003199 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003200 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003201 return DIRECTIVE_FOUND;
3202 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003203
H. Peter Anvine2c80182005-01-15 22:15:51 +00003204 /*
3205 * Now we have a "macro" defined - although it has no name
3206 * and we won't be entering it in the hash tables - we must
3207 * push a macro-end marker for it on to istk->expansion.
3208 * After that, it will take care of propagating itself (a
3209 * macro-end marker line for a macro which is really a %rep
3210 * block will cause the macro to be re-expanded, complete
3211 * with another macro-end marker to ensure the process
3212 * continues) until the whole expansion is forcibly removed
3213 * from istk->expansion by a %exitrep.
3214 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003215 l = nasm_malloc(sizeof(Line));
3216 l->next = istk->expansion;
3217 l->finishes = defining;
3218 l->first = NULL;
3219 istk->expansion = l;
3220
3221 istk->mstk = defining;
3222
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003223 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003224 tmp_defining = defining;
3225 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 free_tlist(origline);
3227 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003228
H. Peter Anvine2c80182005-01-15 22:15:51 +00003229 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003230 /*
3231 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003232 * macro-end marker for a macro with no name. Then we set
3233 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003234 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003235 list_for_each(l, istk->expansion)
3236 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003237 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003238
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003239 if (l)
3240 l->finishes->in_progress = 1;
3241 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003242 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003243 free_tlist(origline);
3244 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003245
H. Peter Anvine2c80182005-01-15 22:15:51 +00003246 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003247 case PP_IXDEFINE:
3248 casesense = false;
3249 /* fall through */
3250 case PP_DEFINE:
3251 case PP_XDEFINE:
3252 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003253 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003254 bool *eval_params = NULL;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003255
H. Peter Anvine2c80182005-01-15 22:15:51 +00003256 tline = tline->next;
3257 skip_white_(tline);
3258 tline = expand_id(tline);
3259 if (!tline || (tline->type != TOK_ID &&
3260 (tline->type != TOK_PREPROC_ID ||
3261 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003262 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003263 free_tlist(origline);
3264 return DIRECTIVE_FOUND;
3265 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003266
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003267 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003268 last = tline;
3269 param_start = tline = tline->next;
3270 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003271
H. Peter Anvine2c80182005-01-15 22:15:51 +00003272 if (tok_is_(tline, "(")) {
3273 /*
3274 * This macro has parameters.
3275 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003276
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 tline = tline->next;
3278 while (1) {
3279 skip_white_(tline);
3280 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003281 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003282 free_tlist(origline);
3283 return DIRECTIVE_FOUND;
3284 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003285 if (tok_is_(tline, "=")) {
3286 have_eval_params = true;
3287 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003288 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003289 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003290
3291 /*
3292 * "*" means an unnamed, ignored argument; it can never
3293 * match anything because "*" is not TOK_ID, and so
3294 * none of the expansion entries will be converted
3295 * to parameters.
3296 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003298 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3299 /*
3300 * Empty name; duplicate the termination
3301 * token and use the first copy as a placeholder.
3302 * It will never match anything, since the
3303 * associated text doesn't correspond to a TOK_ID.
3304 */
3305 tline = dup_Token(tline, tline);
3306 } else {
3307 nasm_nonfatal("`%s': parameter identifier expected",
3308 tline->text);
3309 free_tlist(origline);
3310 return DIRECTIVE_FOUND;
3311 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003313 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003314 tline = tline->next;
3315 skip_white_(tline);
3316 if (tok_is_(tline, ",")) {
3317 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003318 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003319 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003320 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003321 free_tlist(origline);
3322 return DIRECTIVE_FOUND;
3323 }
3324 break;
3325 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003326 }
3327 last = tline;
3328 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003329
3330 if (have_eval_params) {
3331 /* Create evaluated parameters table */
3332 bool is_eval = false;
3333
3334 nasm_newn(eval_params, nparam);
3335 list_for_each(tt, param_start) {
3336 if (is_smac_param(tt->type))
3337 eval_params[smac_nparam(tt->type)] = is_eval;
3338 is_eval = tok_is_(tt, "=");
3339 }
3340 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003342
H. Peter Anvine2c80182005-01-15 22:15:51 +00003343 if (tok_type_(tline, TOK_WHITESPACE))
3344 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003345 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003346
3347 /* Expand the macro definition now for %xdefine and %ixdefine */
3348 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3349 tline = expand_smacro(tline);
3350
3351 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 t = tline;
3353 while (t) {
3354 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003355 list_for_each(tt, param_start)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003356 if (is_smac_param(tt->type) &&
3357 tt->len == t->len &&
3358 !memcmp(tt->text, t->text, tt->len))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003359 t->type = tt->type;
3360 }
3361 tt = t->next;
3362 t->next = macro_start;
3363 macro_start = t;
3364 t = tt;
3365 }
3366 /*
3367 * Good. We now have a macro name, a parameter count, and a
3368 * token list (in reverse order) for an expansion. We ought
3369 * to be OK just to create an SMacro, store it, and let
3370 * free_tlist have the rest of the line (which we have
3371 * carefully re-terminated after chopping off the expansion
3372 * from the end).
3373 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003374 define_smacro(ctx, mname, casesense, nparam, eval_params, macro_start);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003375
H. Peter Anvine2c80182005-01-15 22:15:51 +00003376 free_tlist(origline);
3377 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003378 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003379
H. Peter Anvine2c80182005-01-15 22:15:51 +00003380 case PP_UNDEF:
3381 tline = tline->next;
3382 skip_white_(tline);
3383 tline = expand_id(tline);
3384 if (!tline || (tline->type != TOK_ID &&
3385 (tline->type != TOK_PREPROC_ID ||
3386 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003387 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003388 free_tlist(origline);
3389 return DIRECTIVE_FOUND;
3390 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003391 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003392 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003393
H. Peter Anvine2c80182005-01-15 22:15:51 +00003394 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003395 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003396 undef_smacro(ctx, mname);
3397 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003398 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003399
H. Peter Anvin9e200162008-06-04 17:23:14 -07003400 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003401 casesense = false;
3402 /* fall through */
3403 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003404 tline = tline->next;
3405 skip_white_(tline);
3406 tline = expand_id(tline);
3407 if (!tline || (tline->type != TOK_ID &&
3408 (tline->type != TOK_PREPROC_ID ||
3409 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003410 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003411 free_tlist(origline);
3412 return DIRECTIVE_FOUND;
3413 }
3414
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003415 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003416 last = tline;
3417 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003418 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003419
3420 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003421 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003422
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003423 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003424 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003425 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003426
3427 /*
3428 * We now have a macro name, an implicit parameter count of
3429 * zero, and a string token to use as an expansion. Create
3430 * and store an SMacro.
3431 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003432 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003433 free_tlist(origline);
3434 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003435
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003436 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003437 casesense = false;
3438 /* fall through */
3439 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003440 tline = tline->next;
3441 skip_white_(tline);
3442 tline = expand_id(tline);
3443 if (!tline || (tline->type != TOK_ID &&
3444 (tline->type != TOK_PREPROC_ID ||
3445 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003446 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003447 free_tlist(origline);
3448 return DIRECTIVE_FOUND;
3449 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003450 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003451 last = tline;
3452 tline = expand_smacro(tline->next);
3453 last->next = NULL;
3454
3455 t = tline;
3456 while (tok_type_(t, TOK_WHITESPACE))
3457 t = t->next;
3458 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003459 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003460 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003461 free_tlist(tline);
3462 free_tlist(origline);
3463 return DIRECTIVE_FOUND;
3464 }
3465
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003466 /*
3467 * Convert the string to a token stream. Note that smacros
3468 * are stored with the token stream reversed, so we have to
3469 * reverse the output of tokenize().
3470 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003471 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003472 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003473
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003474 /*
3475 * We now have a macro name, an implicit parameter count of
3476 * zero, and a numeric token to use as an expansion. Create
3477 * and store an SMacro.
3478 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003479 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003480 free_tlist(tline);
3481 free_tlist(origline);
3482 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003483
H. Peter Anvin8b262472019-02-26 14:00:54 -08003484 case PP_IPATHSEARCH:
3485 casesense = false;
3486 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003487 case PP_PATHSEARCH:
3488 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003489 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003490
H. Peter Anvin418ca702008-05-30 10:42:30 -07003491 tline = tline->next;
3492 skip_white_(tline);
3493 tline = expand_id(tline);
3494 if (!tline || (tline->type != TOK_ID &&
3495 (tline->type != TOK_PREPROC_ID ||
3496 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003497 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003498 free_tlist(origline);
3499 return DIRECTIVE_FOUND;
3500 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003501 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003502 last = tline;
3503 tline = expand_smacro(tline->next);
3504 last->next = NULL;
3505
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003506 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003507 while (tok_type_(t, TOK_WHITESPACE))
3508 t = t->next;
3509
3510 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003511 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003512 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003513 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003514 free_tlist(origline);
3515 return DIRECTIVE_FOUND; /* but we did _something_ */
3516 }
3517 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003518 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003519 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003520 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003521 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003522
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003523 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003524 if (!found_path)
3525 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003526 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003527
3528 /*
3529 * We now have a macro name, an implicit parameter count of
3530 * zero, and a string token to use as an expansion. Create
3531 * and store an SMacro.
3532 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003533 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003534 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003535 free_tlist(origline);
3536 return DIRECTIVE_FOUND;
3537 }
3538
H. Peter Anvin8b262472019-02-26 14:00:54 -08003539 case PP_ISTRLEN:
3540 casesense = false;
3541 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003542 case PP_STRLEN:
3543 tline = tline->next;
3544 skip_white_(tline);
3545 tline = expand_id(tline);
3546 if (!tline || (tline->type != TOK_ID &&
3547 (tline->type != TOK_PREPROC_ID ||
3548 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003549 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003550 free_tlist(origline);
3551 return DIRECTIVE_FOUND;
3552 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003553 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003554 last = tline;
3555 tline = expand_smacro(tline->next);
3556 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003557
H. Peter Anvine2c80182005-01-15 22:15:51 +00003558 t = tline;
3559 while (tok_type_(t, TOK_WHITESPACE))
3560 t = t->next;
3561 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003562 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003563 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003564 free_tlist(tline);
3565 free_tlist(origline);
3566 return DIRECTIVE_FOUND;
3567 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003568
H. Peter Anvin8b262472019-02-26 14:00:54 -08003569 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003570
H. Peter Anvine2c80182005-01-15 22:15:51 +00003571 /*
3572 * We now have a macro name, an implicit parameter count of
3573 * zero, and a numeric token to use as an expansion. Create
3574 * and store an SMacro.
3575 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003576 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003577 free_tlist(tline);
3578 free_tlist(origline);
3579 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003580
H. Peter Anvin8b262472019-02-26 14:00:54 -08003581 case PP_ISTRCAT:
3582 casesense = false;
3583 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003584 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003585 tline = tline->next;
3586 skip_white_(tline);
3587 tline = expand_id(tline);
3588 if (!tline || (tline->type != TOK_ID &&
3589 (tline->type != TOK_PREPROC_ID ||
3590 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003591 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003592 free_tlist(origline);
3593 return DIRECTIVE_FOUND;
3594 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003595 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003596 last = tline;
3597 tline = expand_smacro(tline->next);
3598 last->next = NULL;
3599
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003600 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003601 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003602 switch (t->type) {
3603 case TOK_WHITESPACE:
3604 break;
3605 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003606 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003607 break;
3608 case TOK_OTHER:
3609 if (!strcmp(t->text, ",")) /* permit comma separators */
3610 break;
3611 /* else fall through */
3612 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003613 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003614 free_tlist(tline);
3615 free_tlist(origline);
3616 return DIRECTIVE_FOUND;
3617 }
3618 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003619
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003620 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003621 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003622 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003623 memcpy(p, t->text, t->len);
3624 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003625 }
3626 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003627
3628 /*
3629 * We now have a macro name, an implicit parameter count of
3630 * zero, and a numeric token to use as an expansion. Create
3631 * and store an SMacro.
3632 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003633 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003634 nasm_free(pp);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003635 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003636 free_tlist(tline);
3637 free_tlist(origline);
3638 return DIRECTIVE_FOUND;
3639
H. Peter Anvin8b262472019-02-26 14:00:54 -08003640 case PP_ISUBSTR:
3641 casesense = false;
3642 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003644 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003645 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003646 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003647
H. Peter Anvine2c80182005-01-15 22:15:51 +00003648 tline = tline->next;
3649 skip_white_(tline);
3650 tline = expand_id(tline);
3651 if (!tline || (tline->type != TOK_ID &&
3652 (tline->type != TOK_PREPROC_ID ||
3653 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003654 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
3657 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003658 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003659 last = tline;
3660 tline = expand_smacro(tline->next);
3661 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003662
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003663 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003664 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 while (tok_type_(t, TOK_WHITESPACE))
3666 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003667
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003669 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003670 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003671 free_tlist(tline);
3672 free_tlist(origline);
3673 return DIRECTIVE_FOUND;
3674 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003675
H. Peter Anvin8b262472019-02-26 14:00:54 -08003676 pps.tptr = t->next;
3677 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003679 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003680 if (!evalresult) {
3681 free_tlist(tline);
3682 free_tlist(origline);
3683 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003684 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003685 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 free_tlist(tline);
3687 free_tlist(origline);
3688 return DIRECTIVE_FOUND;
3689 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003690 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003691
H. Peter Anvin8b262472019-02-26 14:00:54 -08003692 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3693 pps.tptr = pps.tptr->next;
3694 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003695 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003696 } else {
3697 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003698 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003699 if (!evalresult) {
3700 free_tlist(tline);
3701 free_tlist(origline);
3702 return DIRECTIVE_FOUND;
3703 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003704 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 free_tlist(tline);
3706 free_tlist(origline);
3707 return DIRECTIVE_FOUND;
3708 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003709 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003710 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003711
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003712 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003713
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003714 /* make start and count being in range */
3715 if (start < 0)
3716 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003717 if (count < 0)
3718 count = len + count + 1 - start;
3719 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003720 count = len - start;
3721 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003722 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003723
H. Peter Anvin8b262472019-02-26 14:00:54 -08003724 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003725 macro_start->len = count;
3726 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3727 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003728
H. Peter Anvine2c80182005-01-15 22:15:51 +00003729 /*
3730 * We now have a macro name, an implicit parameter count of
3731 * zero, and a numeric token to use as an expansion. Create
3732 * and store an SMacro.
3733 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003734 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003735 free_tlist(tline);
3736 free_tlist(origline);
3737 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003738 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003739
H. Peter Anvine2c80182005-01-15 22:15:51 +00003740 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003741 casesense = false;
3742 /* fall through */
3743 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 tline = tline->next;
3745 skip_white_(tline);
3746 tline = expand_id(tline);
3747 if (!tline || (tline->type != TOK_ID &&
3748 (tline->type != TOK_PREPROC_ID ||
3749 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003750 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003751 free_tlist(origline);
3752 return DIRECTIVE_FOUND;
3753 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003754 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 last = tline;
3756 tline = expand_smacro(tline->next);
3757 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003758
H. Peter Anvin8b262472019-02-26 14:00:54 -08003759 pps.tptr = tline;
3760 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003761 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003762 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003763 free_tlist(tline);
3764 if (!evalresult) {
3765 free_tlist(origline);
3766 return DIRECTIVE_FOUND;
3767 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003768
H. Peter Anvine2c80182005-01-15 22:15:51 +00003769 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003770 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003771
H. Peter Anvine2c80182005-01-15 22:15:51 +00003772 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003773 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003774 free_tlist(origline);
3775 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003776 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003777
H. Peter Anvin8b262472019-02-26 14:00:54 -08003778 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003779
H. Peter Anvine2c80182005-01-15 22:15:51 +00003780 /*
3781 * We now have a macro name, an implicit parameter count of
3782 * zero, and a numeric token to use as an expansion. Create
3783 * and store an SMacro.
3784 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003785 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003786 free_tlist(origline);
3787 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003788
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 case PP_LINE:
3790 /*
3791 * Syntax is `%line nnn[+mmm] [filename]'
3792 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003793 if (unlikely(pp_noline)) {
3794 free_tlist(origline);
3795 return DIRECTIVE_FOUND;
3796 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003797 tline = tline->next;
3798 skip_white_(tline);
3799 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003800 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 free_tlist(origline);
3802 return DIRECTIVE_FOUND;
3803 }
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 Anvine2c80182005-01-15 22:15:51 +00003811 free_tlist(origline);
3812 return DIRECTIVE_FOUND;
3813 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003814 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003815 tline = tline->next;
3816 }
3817 skip_white_(tline);
3818 src_set_linnum(k);
3819 istk->lineinc = m;
3820 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003821 char *fname = detoken(tline, false);
3822 src_set_fname(fname);
3823 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003824 }
3825 free_tlist(origline);
3826 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003827
H. Peter Anvine2c80182005-01-15 22:15:51 +00003828 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003829 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003830 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003831 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003832}
3833
3834/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003835 * Ensure that a macro parameter contains a condition code and
3836 * nothing else. Return the condition code index if so, or -1
3837 * otherwise.
3838 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003839static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003840{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003841 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003842
H. Peter Anvin25a99342007-09-22 17:45:45 -07003843 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003844 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003845
H. Peter Anvineba20a72002-04-30 20:53:55 +00003846 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003847 if (!t)
3848 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003849 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003850 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003851 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003852 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003853 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003855
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003856 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003857}
3858
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003859/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003860 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003861 * pasting, if @handle_explicit passed then explicit pasting
3862 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003863 * The @m array can contain a series of token types which are
3864 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003865 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003866static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003867 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003868{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003869 Token *tok, *next, **prev_next, **prev_nonspace;
3870 bool pasted = false;
3871 char *buf, *p;
3872 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003873
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003874 /*
3875 * The last token before pasting. We need it
3876 * to be able to connect new handled tokens.
3877 * In other words if there were a tokens stream
3878 *
3879 * A -> B -> C -> D
3880 *
3881 * and we've joined tokens B and C, the resulting
3882 * stream should be
3883 *
3884 * A -> BC -> D
3885 */
3886 tok = *head;
3887 prev_next = NULL;
3888
3889 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3890 prev_nonspace = head;
3891 else
3892 prev_nonspace = NULL;
3893
3894 while (tok && (next = tok->next)) {
3895
3896 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003897 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003898 /* Zap redundant whitespaces */
3899 while (tok_type_(next, TOK_WHITESPACE))
3900 next = delete_Token(next);
3901 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003902 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003903
3904 case TOK_PASTE:
3905 /* Explicit pasting */
3906 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003907 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003908 next = delete_Token(tok);
3909
3910 while (tok_type_(next, TOK_WHITESPACE))
3911 next = delete_Token(next);
3912
3913 if (!pasted)
3914 pasted = true;
3915
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003916 /* Left pasting token is start of line */
3917 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003918 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003919
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003920 /*
3921 * No ending token, this might happen in two
3922 * cases
3923 *
3924 * 1) There indeed no right token at all
3925 * 2) There is a bare "%define ID" statement,
3926 * and @ID does expand to whitespace.
3927 *
3928 * So technically we need to do a grammar analysis
3929 * in another stage of parsing, but for now lets don't
3930 * change the behaviour people used to. Simply allow
3931 * whitespace after paste token.
3932 */
3933 if (!next) {
3934 /*
3935 * Zap ending space tokens and that's all.
3936 */
3937 tok = (*prev_nonspace)->next;
3938 while (tok_type_(tok, TOK_WHITESPACE))
3939 tok = delete_Token(tok);
3940 tok = *prev_nonspace;
3941 tok->next = NULL;
3942 break;
3943 }
3944
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003945 tok = *prev_nonspace;
3946 while (tok_type_(tok, TOK_WHITESPACE))
3947 tok = delete_Token(tok);
3948 len = strlen(tok->text);
3949 len += strlen(next->text);
3950
3951 p = buf = nasm_malloc(len + 1);
3952 strcpy(p, tok->text);
3953 p = strchr(p, '\0');
3954 strcpy(p, next->text);
3955
3956 delete_Token(tok);
3957
3958 tok = tokenize(buf);
3959 nasm_free(buf);
3960
3961 *prev_nonspace = tok;
3962 while (tok && tok->next)
3963 tok = tok->next;
3964
3965 tok->next = delete_Token(next);
3966
3967 /* Restart from pasted tokens head */
3968 tok = *prev_nonspace;
3969 break;
3970
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003971 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003972 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003973 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003974 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3975 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003976
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003977 len = 0;
3978 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3979 len += strlen(next->text);
3980 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003981 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003982
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003983 /* No match or no text to process */
3984 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003985 break;
3986
3987 len += strlen(tok->text);
3988 p = buf = nasm_malloc(len + 1);
3989
Adam Majer1a069432017-07-25 11:12:35 +02003990 strcpy(p, tok->text);
3991 p = strchr(p, '\0');
3992 tok = delete_Token(tok);
3993
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003994 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003995 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3996 strcpy(p, tok->text);
3997 p = strchr(p, '\0');
3998 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003999 tok = delete_Token(tok);
4000 }
4001
4002 tok = tokenize(buf);
4003 nasm_free(buf);
4004
4005 if (prev_next)
4006 *prev_next = tok;
4007 else
4008 *head = tok;
4009
4010 /*
4011 * Connect pasted into original stream,
4012 * ie A -> new-tokens -> B
4013 */
4014 while (tok && tok->next)
4015 tok = tok->next;
4016 tok->next = next;
4017
4018 if (!pasted)
4019 pasted = true;
4020
4021 /* Restart from pasted tokens head */
4022 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004023 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004024
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004025 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004026 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004027
4028 prev_next = &tok->next;
4029
4030 if (tok->next &&
4031 !tok_type_(tok->next, TOK_WHITESPACE) &&
4032 !tok_type_(tok->next, TOK_PASTE))
4033 prev_nonspace = prev_next;
4034
4035 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004036 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004037
4038 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004039}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004040
4041/*
4042 * expands to a list of tokens from %{x:y}
4043 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004044static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004045{
4046 Token *t = tline, **tt, *tm, *head;
4047 char *pos;
4048 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004049
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004050 pos = strchr(tline->text, ':');
4051 nasm_assert(pos);
4052
4053 lst = atoi(pos + 1);
4054 fst = atoi(tline->text + 1);
4055
4056 /*
4057 * only macros params are accounted so
4058 * if someone passes %0 -- we reject such
4059 * value(s)
4060 */
4061 if (lst == 0 || fst == 0)
4062 goto err;
4063
4064 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004065 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4066 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004067 goto err;
4068
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004069 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4070 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004071
4072 /* counted from zero */
4073 fst--, lst--;
4074
4075 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004076 * It will be at least one token. Note we
4077 * need to scan params until separator, otherwise
4078 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004079 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004080 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004081 if (!tm)
4082 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004083 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004084 tt = &head->next, tm = tm->next;
4085 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004086 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004087 *tt = t, tt = &t->next, tm = tm->next;
4088 }
4089
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004090 if (fst < lst) {
4091 for (i = fst + 1; i <= lst; i++) {
4092 t = new_Token(NULL, TOK_OTHER, ",", 0);
4093 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004094 j = (i + mac->rotate) % mac->nparam;
4095 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004096 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004097 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004098 *tt = t, tt = &t->next, tm = tm->next;
4099 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004100 }
4101 } else {
4102 for (i = fst - 1; i >= lst; i--) {
4103 t = new_Token(NULL, TOK_OTHER, ",", 0);
4104 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004105 j = (i + mac->rotate) % mac->nparam;
4106 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004107 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004108 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004109 *tt = t, tt = &t->next, tm = tm->next;
4110 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004111 }
4112 }
4113
4114 *last = tt;
4115 return head;
4116
4117err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004118 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004119 &tline->text[1]);
4120 return tline;
4121}
4122
H. Peter Anvin76690a12002-04-30 20:52:49 +00004123/*
4124 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004125 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004126 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004127 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004128static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004129{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004130 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004131 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004132 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004133
4134 tail = &thead;
4135 thead = NULL;
4136
H. Peter Anvine2c80182005-01-15 22:15:51 +00004137 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004138 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004139 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4140 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4141 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004142 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004143 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004144 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004145 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004146 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004147 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004148
H. Peter Anvine2c80182005-01-15 22:15:51 +00004149 t = tline;
4150 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004151
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004152 mac = istk->mstk;
4153 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4154 mac = mac->next_active;
4155 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004156 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004157 } else {
4158 pos = strchr(t->text, ':');
4159 if (!pos) {
4160 switch (t->text[1]) {
4161 /*
4162 * We have to make a substitution of one of the
4163 * forms %1, %-1, %+1, %%foo, %0.
4164 */
4165 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004166 type = TOK_NUMBER;
4167 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4168 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004169 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004170 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004171 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004172 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004173 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004174 text = nasm_strcat(tmpbuf, t->text + 2);
4175 break;
4176 case '-':
4177 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004178 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004179 tt = NULL;
4180 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004181 if (mac->nparam > 1)
4182 n = (n + mac->rotate) % mac->nparam;
4183 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004184 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004185 cc = find_cc(tt);
4186 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004187 nasm_nonfatal("macro parameter %d is not a condition code",
4188 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004189 text = NULL;
4190 } else {
4191 type = TOK_ID;
4192 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004193 nasm_nonfatal("condition code `%s' is not invertible",
4194 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004195 text = NULL;
4196 } else
4197 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4198 }
4199 break;
4200 case '+':
4201 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004202 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004203 tt = NULL;
4204 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004205 if (mac->nparam > 1)
4206 n = (n + mac->rotate) % mac->nparam;
4207 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004208 }
4209 cc = find_cc(tt);
4210 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004211 nasm_nonfatal("macro parameter %d is not a condition code",
4212 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004213 text = NULL;
4214 } else {
4215 type = TOK_ID;
4216 text = nasm_strdup(conditions[cc]);
4217 }
4218 break;
4219 default:
4220 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004221 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004222 tt = NULL;
4223 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004224 if (mac->nparam > 1)
4225 n = (n + mac->rotate) % mac->nparam;
4226 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004227 }
4228 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004229 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004230 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004231 tail = &(*tail)->next;
4232 tt = tt->next;
4233 }
4234 }
4235 text = NULL; /* we've done it here */
4236 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004237 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004238 } else {
4239 /*
4240 * seems we have a parameters range here
4241 */
4242 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004243 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004244 if (head != t) {
4245 *tail = head;
4246 *last = tline;
4247 tline = head;
4248 text = NULL;
4249 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004250 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004251 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004252 if (!text) {
4253 delete_Token(t);
4254 } else {
4255 *tail = t;
4256 tail = &t->next;
4257 t->type = type;
4258 nasm_free(t->text);
4259 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004260 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004262 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004263 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004264 } else if (tline->type == TOK_INDIRECT) {
4265 t = tline;
4266 tline = tline->next;
4267 tt = tokenize(t->text);
4268 tt = expand_mmac_params(tt);
4269 tt = expand_smacro(tt);
4270 *tail = tt;
4271 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004272 tail = &tt->next;
4273 tt = tt->next;
4274 }
4275 delete_Token(t);
4276 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004277 } else {
4278 t = *tail = tline;
4279 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004280 tail = &t->next;
4281 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004282 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004283 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004284
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004285 if (changed) {
4286 const struct tokseq_match t[] = {
4287 {
4288 PP_CONCAT_MASK(TOK_ID) |
4289 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4290 PP_CONCAT_MASK(TOK_ID) |
4291 PP_CONCAT_MASK(TOK_NUMBER) |
4292 PP_CONCAT_MASK(TOK_FLOAT) |
4293 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4294 },
4295 {
4296 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4297 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4298 }
4299 };
4300 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4301 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004302
H. Peter Anvin76690a12002-04-30 20:52:49 +00004303 return thead;
4304}
4305
4306/*
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 *
4317 * The return value equals **tpp.
4318 *
4319 * Return false if no expansion took place; true if it did.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004320 *
4321 */
4322static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004323static int64_t smacro_deadman;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004324
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004325static bool expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004326{
4327 Token **params = NULL;
4328 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004329 Token *tline = **tpp;
4330 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004331 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004332 unsigned int i;
4333 Token *t, *tup, *ttail;
4334 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004335
4336 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004337 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004338
4339 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004340
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004341 if (--smacro_deadman <= 0) {
4342 if (smacro_deadman == 0)
4343 nasm_nonfatal("interminable macro recursion");
4344 goto not_a_macro;
4345 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004346 head = (SMacro *)hash_findix(&smacros, mname);
4347 } else if (tline->type == TOK_PREPROC_ID) {
4348 Context *ctx = get_ctx(mname, &mname);
4349 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4350 } else {
4351 goto not_a_macro;
4352 }
4353
4354 /*
4355 * We've hit an identifier of some sort. First check whether the
4356 * identifier is a single-line macro at all, then think about
4357 * checking for parameters if necessary.
4358 */
4359 list_for_each(m, head) {
4360 if (!mstrcmp(m->name, mname, m->casesense))
4361 break;
4362 }
4363
4364 if (!m) {
4365 goto not_a_macro;
4366 }
4367
4368 /* Parse parameters, if applicable */
4369
4370 params = NULL;
4371 nparam = 0;
4372
4373 if (m->nparam == 0) {
4374 /*
4375 * Simple case: the macro is parameterless.
4376 * Nothing to parse; the expansion code will
4377 * drop the macro name token.
4378 */
4379 } else {
4380 /*
4381 * Complicated case: at least one macro with this name
4382 * exists and takes parameters. We must find the
4383 * parameters in the call, count them, find the SMacro
4384 * that corresponds to that form of the macro call, and
4385 * substitute for the parameters when we expand. What a
4386 * pain.
4387 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004388 Token **phead, **pep;
4389 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004390 int white = 0;
4391 int brackets = 0;
4392 bool bracketed = false;
4393 bool bad_bracket = false;
4394 unsigned int sparam = PARAM_DELTA;
4395
4396 tline = tline->next;
4397
4398 while (tok_type_(tline, TOK_WHITESPACE)) {
4399 tline = tline->next;
4400 }
4401 if (!tok_is_(tline, "(")) {
4402 /*
4403 * This macro wasn't called with parameters: ignore
4404 * the call. (Behaviour borrowed from gnu cpp.)
4405 */
4406 goto not_a_macro;
4407 }
4408
4409 paren = 1;
4410 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004411 nasm_newn(params, sparam);
4412 phead = pep = &params[0];
4413 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004414
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004415 while (paren) {
4416 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004417 char ch;
4418
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004419 tline = tline->next;
4420
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004421 if (!tline) {
4422 nasm_nonfatal("macro call expects terminating `)'");
4423 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004424 }
4425
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004426 ch = 0;
4427 skip = false;
4428
4429 switch (tline->type) {
4430 case TOK_OTHER:
4431 if (!tline->text[1])
4432 ch = tline->text[0];
4433 break;
4434
4435 case TOK_WHITESPACE:
4436 if (brackets || *phead)
4437 white++; /* Keep interior whitespace */
4438 skip = true;
4439 break;
4440
4441 default:
4442 break;
4443 }
4444
4445 switch (ch) {
4446 case ',':
4447 if (!brackets) {
4448 if (++nparam >= sparam) {
4449 sparam += PARAM_DELTA;
4450 params = nasm_realloc(params, sparam * sizeof *params);
4451 }
4452 pep = &params[nparam];
4453 *pep = NULL;
4454 bracketed = false;
4455 skip = true;
4456 }
4457 break;
4458
4459 case '{':
4460 if (!bracketed) {
4461 bracketed = !*phead;
4462 skip = true;
4463 }
4464 brackets++;
4465 break;
4466
4467 case '}':
4468 if (brackets > 0) {
4469 if (!--brackets)
4470 skip = bracketed;
4471 }
4472 break;
4473
4474 case '(':
4475 if (!brackets)
4476 paren++;
4477 break;
4478
4479 case ')':
4480 if (!brackets) {
4481 paren--;
4482 if (!paren) {
4483 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004484 /* Count the final argument */
4485 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004486 }
4487 }
4488 break;
4489
4490 default:
4491 break; /* Normal token */
4492 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004493
4494 if (!skip) {
4495 Token *t;
4496
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004497 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004498
4499 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004500 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004501 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004502 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004503 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004504 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004505 pep = &t->next;
4506 white = 0;
4507 }
4508 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004509
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004510 /*
4511 * Look for a macro matching in both name and parameter count.
4512 * We already know any matches cannot be anywhere before the
4513 * current position of "m", so there is no reason to
4514 * backtrack.
4515 */
4516 while (1) {
4517 if (!m) {
4518 /*!
4519 *!macro-params-single [on] single-line macro calls with wrong parameter count
4520 *! warns about \i{single-line macros} being invoked
4521 *! with the wrong number of parameters.
4522 */
4523 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4524 "single-line macro `%s' exists, "
4525 "but not taking %d parameter%s",
4526 mname, nparam, (nparam == 1) ? "" : "s");
4527 goto not_a_macro;
4528 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004529
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004530 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4531 break; /* It's good */
4532
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004533 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004534 }
4535 }
4536
4537 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004538 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004539
4540 /* Expand each parameter */
4541 m->in_progress = true;
4542
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004543 if (m->eval_param) {
4544 for (i = 0; i < nparam; i++) {
4545 if (m->eval_param[i]) {
4546 /* Evaluate this parameter as a number */
4547 struct ppscan pps;
4548 struct tokenval tokval;
4549 expr *evalresult;
4550 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004551
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004552 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4553 pps.ntokens = -1;
4554 tokval.t_type = TOKEN_INVALID;
4555 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004556
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004557 free_tlist(eval_param);
4558 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004559
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004560 if (!evalresult) {
4561 /* Nothing meaningful to do */
4562 } else if (tokval.t_type) {
4563 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4564 } else if (!is_simple(evalresult)) {
4565 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4566 } else {
4567 params[i] = make_tok_num(reloc_value(evalresult));
4568 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004569 }
4570 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004571 }
4572
4573 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004574 tline = tline->next; /* Remove the macro call from the input */
4575 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004576
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004577 /* Note: we own the expansion this returns. */
4578 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004579
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004580 tup = NULL;
4581 ttail = NULL; /* Pointer to the last token of the expansion */
4582 while (t) {
4583 enum pp_token_type type = t->type;
4584 Token *tnext = t->next;
4585 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004586
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004587 switch (type) {
4588 case TOK_PREPROC_Q:
4589 case TOK_PREPROC_QQ:
4590 t->type = TOK_ID;
4591 nasm_free(t->text);
4592 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4593 t->len = nasm_last_string_len();
4594 t->next = tline;
4595 break;
4596
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004597 case TOK_ID:
4598 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004599 /*
4600 * Chain this into the target line *before* expanding,
4601 * that way we pick up any arguments to the new macro call,
4602 * if applicable.
4603 */
4604 t->next = tline;
4605 tp = &t;
4606 expand_one_smacro(&tp);
4607 if (t == tline)
4608 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004609 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004610
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004611 default:
4612 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004613 unsigned int param = smac_nparam(t->type);
4614 nasm_assert(!tup && param < nparam);
4615 delete_Token(t);
4616 t = NULL;
4617 tup = tnext;
4618 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004619 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004620 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004621 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004622 }
4623
4624 if (t) {
4625 tline = t;
4626 if (!ttail)
4627 ttail = t;
4628 }
4629
4630 if (tnext) {
4631 t = tnext;
4632 } else {
4633 t = tup;
4634 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004635 }
4636 }
4637
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004638 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004639 if (ttail)
4640 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004641
4642 m->in_progress = false;
4643
4644 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004645 free_tlist(mstart);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004646 free_tlist_array(params, nparam);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004647 return true;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004648
4649 /*
4650 * No macro expansion needed; roll back to mstart (if necessary)
4651 * and then advance to the next input token.
4652 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004653not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004654 *tpp = &mstart->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004655 free_tlist_array(params, nparam);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004656 return false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004657}
4658
4659/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004660 * Expand all single-line macro calls made in the given line.
4661 * Return the expanded version of the line. The original is deemed
4662 * to be destroyed in the process. (In reality we'll just move
4663 * Tokens from input to output a lot of the time, rather than
4664 * actually bothering to destroy and replicate.)
4665 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004666static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004667{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004668 smacro_deadman = nasm_limit[LIMIT_MACROS];
4669 return expand_smacro_noreset(tline);
4670}
4671
4672static Token *expand_smacro_noreset(Token * tline)
4673{
4674 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004675 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004676 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004677
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004678 /*
4679 * Trick: we should avoid changing the start token pointer since it can
4680 * be contained in "next" field of other token. Because of this
4681 * we allocate a copy of first token and work with it; at the end of
4682 * routine we copy it back
4683 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004684 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004685 tline = new_Token(org_tline->next, org_tline->type,
4686 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004687 nasm_free(org_tline->text);
4688 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004689 }
4690
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004691
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004692 /*
4693 * Pretend that we always end up doing expansion on the first pass;
4694 * that way %+ get processed. However, if we process %+ before the
4695 * first pass, we end up with things like MACRO %+ TAIL trying to
4696 * look up the macro "MACROTAIL", which we don't want.
4697 */
4698 expanded = true;
4699 thead = tline;
4700 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004701 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004702 {
4703 PP_CONCAT_MASK(TOK_ID) |
4704 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4705 PP_CONCAT_MASK(TOK_ID) |
4706 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4707 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4708 }
4709 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004710
4711 tail = &thead;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004712 while ((t = *tail)) { /* main token loop */
4713
4714 expanded |= expand_one_smacro(&tail);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004715 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004716
4717 if (!expanded) {
4718 tline = thead;
4719 break; /* Done! */
4720 }
4721
4722 /*
4723 * Now scan the entire line and look for successive TOK_IDs
4724 * that resulted after expansion (they can't be produced by
4725 * tokenize()). The successive TOK_IDs should be concatenated.
4726 * Also we look for %+ tokens and concatenate the tokens
4727 * before and after them (without white spaces in between).
4728 */
4729 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004730
4731 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004732 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004733
H. Peter Anvine2c80182005-01-15 22:15:51 +00004734 if (org_tline) {
4735 if (thead) {
4736 *org_tline = *thead;
4737 /* since we just gave text to org_line, don't free it */
4738 thead->text = NULL;
4739 delete_Token(thead);
4740 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004741 /*
4742 * The expression expanded to empty line;
4743 * we can't return NULL because of the "trick" above.
4744 * Just set the line to a single WHITESPACE token.
4745 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004746 memset(org_tline, 0, sizeof(*org_tline));
4747 org_tline->text = NULL;
4748 org_tline->type = TOK_WHITESPACE;
4749 }
4750 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004751 }
4752
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004753 return thead;
4754}
4755
4756/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004757 * Similar to expand_smacro but used exclusively with macro identifiers
4758 * right before they are fetched in. The reason is that there can be
4759 * identifiers consisting of several subparts. We consider that if there
4760 * are more than one element forming the name, user wants a expansion,
4761 * otherwise it will be left as-is. Example:
4762 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004763 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004764 *
4765 * the identifier %$abc will be left as-is so that the handler for %define
4766 * will suck it and define the corresponding value. Other case:
4767 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004768 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004769 *
4770 * In this case user wants name to be expanded *before* %define starts
4771 * working, so we'll expand %$abc into something (if it has a value;
4772 * otherwise it will be left as-is) then concatenate all successive
4773 * PP_IDs into one.
4774 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004775static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004776{
4777 Token *cur, *oldnext = NULL;
4778
H. Peter Anvin734b1882002-04-30 21:01:08 +00004779 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004780 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004781
4782 cur = tline;
4783 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004784 (cur->next->type == TOK_ID ||
4785 cur->next->type == TOK_PREPROC_ID
4786 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004787 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004788
4789 /* If identifier consists of just one token, don't expand */
4790 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004791 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004792
H. Peter Anvine2c80182005-01-15 22:15:51 +00004793 if (cur) {
4794 oldnext = cur->next; /* Detach the tail past identifier */
4795 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004796 }
4797
H. Peter Anvin734b1882002-04-30 21:01:08 +00004798 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004799
H. Peter Anvine2c80182005-01-15 22:15:51 +00004800 if (cur) {
4801 /* expand_smacro possibly changhed tline; re-scan for EOL */
4802 cur = tline;
4803 while (cur && cur->next)
4804 cur = cur->next;
4805 if (cur)
4806 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004807 }
4808
4809 return tline;
4810}
4811
4812/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004813 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004814 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004815 * to check for an initial label - that's taken care of in
4816 * expand_mmacro - but must check numbers of parameters. Guaranteed
4817 * to be called with tline->type == TOK_ID, so the putative macro
4818 * name is easy to find.
4819 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004820static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004821{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004822 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004823 Token **params;
4824 int nparam;
4825
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004826 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004827
4828 /*
4829 * Efficiency: first we see if any macro exists with the given
4830 * name. If not, we can return NULL immediately. _Then_ we
4831 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004832 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004833 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004834 list_for_each(m, head)
4835 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004836 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004837 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004838 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004839
4840 /*
4841 * OK, we have a potential macro. Count and demarcate the
4842 * parameters.
4843 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004844 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004845
4846 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004847 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004848 * structure that handles this number.
4849 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004850 while (m) {
4851 if (m->nparam_min <= nparam
4852 && (m->plus || nparam <= m->nparam_max)) {
4853 /*
4854 * This one is right. Just check if cycle removal
4855 * prohibits us using it before we actually celebrate...
4856 */
4857 if (m->in_progress > m->max_depth) {
4858 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004859 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004860 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004861 }
4862 nasm_free(params);
4863 return NULL;
4864 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004865 /*
4866 * It's right, and we can use it. Add its default
4867 * parameters to the end of our list if necessary.
4868 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004869 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004870 params =
4871 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004872 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004873 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004874 while (nparam < m->nparam_min + m->ndefs) {
4875 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004876 nparam++;
4877 }
4878 }
4879 /*
4880 * If we've gone over the maximum parameter count (and
4881 * we're in Plus mode), ignore parameters beyond
4882 * nparam_max.
4883 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004884 if (m->plus && nparam > m->nparam_max)
4885 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004886 /*
4887 * Then terminate the parameter list, and leave.
4888 */
4889 if (!params) { /* need this special case */
4890 params = nasm_malloc(sizeof(*params));
4891 nparam = 0;
4892 }
4893 params[nparam] = NULL;
4894 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004895 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004896 }
4897 /*
4898 * This one wasn't right: look for the next one with the
4899 * same name.
4900 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004901 list_for_each(m, m->next)
4902 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004903 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004904 }
4905
4906 /*
4907 * After all that, we didn't find one with the right number of
4908 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004909 *!
4910 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4911 *! warns about \i{multi-line macros} being invoked
4912 *! with the wrong number of parameters. See \k{mlmacover} for an
4913 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004914 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004915 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4916 "multi-line macro `%s' exists, but not taking %d parameter%s",
4917 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004918 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004919 return NULL;
4920}
4921
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004922
4923/*
4924 * Save MMacro invocation specific fields in
4925 * preparation for a recursive macro expansion
4926 */
4927static void push_mmacro(MMacro *m)
4928{
4929 MMacroInvocation *i;
4930
4931 i = nasm_malloc(sizeof(MMacroInvocation));
4932 i->prev = m->prev;
4933 i->params = m->params;
4934 i->iline = m->iline;
4935 i->nparam = m->nparam;
4936 i->rotate = m->rotate;
4937 i->paramlen = m->paramlen;
4938 i->unique = m->unique;
4939 i->condcnt = m->condcnt;
4940 m->prev = i;
4941}
4942
4943
4944/*
4945 * Restore MMacro invocation specific fields that were
4946 * saved during a previous recursive macro expansion
4947 */
4948static void pop_mmacro(MMacro *m)
4949{
4950 MMacroInvocation *i;
4951
4952 if (m->prev) {
4953 i = m->prev;
4954 m->prev = i->prev;
4955 m->params = i->params;
4956 m->iline = i->iline;
4957 m->nparam = i->nparam;
4958 m->rotate = i->rotate;
4959 m->paramlen = i->paramlen;
4960 m->unique = i->unique;
4961 m->condcnt = i->condcnt;
4962 nasm_free(i);
4963 }
4964}
4965
4966
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004967/*
4968 * Expand the multi-line macro call made by the given line, if
4969 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004970 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004971 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004972static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004973{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004974 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004975 Token *label = NULL;
4976 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004977 Token **params, *t, *tt;
4978 MMacro *m;
4979 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004980 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004981 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004982
4983 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004984 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004985 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004986 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004987 return 0;
4988 m = is_mmacro(t, &params);
4989 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004990 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004991 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004992 Token *last;
4993 /*
4994 * We have an id which isn't a macro call. We'll assume
4995 * it might be a label; we'll also check to see if a
4996 * colon follows it. Then, if there's another id after
4997 * that lot, we'll check it again for macro-hood.
4998 */
4999 label = last = t;
5000 t = t->next;
5001 if (tok_type_(t, TOK_WHITESPACE))
5002 last = t, t = t->next;
5003 if (tok_is_(t, ":")) {
5004 dont_prepend = 1;
5005 last = t, t = t->next;
5006 if (tok_type_(t, TOK_WHITESPACE))
5007 last = t, t = t->next;
5008 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005009 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
5010 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005011 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005012 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005013 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005014 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005015
5016 /*
5017 * Fix up the parameters: this involves stripping leading and
5018 * trailing whitespace, then stripping braces if they are
5019 * present.
5020 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005021 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005022 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005023
H. Peter Anvine2c80182005-01-15 22:15:51 +00005024 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005025 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005026 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005027
H. Peter Anvine2c80182005-01-15 22:15:51 +00005028 t = params[i];
5029 skip_white_(t);
5030 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005031 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005032 params[i] = t;
5033 paramlen[i] = 0;
5034 while (t) {
5035 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5036 break; /* ... because we have hit a comma */
5037 if (comma && t->type == TOK_WHITESPACE
5038 && tok_is_(t->next, ","))
5039 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005040 if (brace && t->type == TOK_OTHER) {
5041 if (t->text[0] == '{')
5042 brace++; /* ... or a nested opening brace */
5043 else if (t->text[0] == '}')
5044 if (!--brace)
5045 break; /* ... or a brace */
5046 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 t = t->next;
5048 paramlen[i]++;
5049 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005050 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005051 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005052 }
5053
5054 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005055 * OK, we have a MMacro structure together with a set of
5056 * parameters. We must now go through the expansion and push
5057 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005058 * parameter tokens and macro-local tokens doesn't get done
5059 * until the single-line macro substitution process; this is
5060 * because delaying them allows us to change the semantics
5061 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005062 *
5063 * First, push an end marker on to istk->expansion, mark this
5064 * macro as in progress, and set up its invocation-specific
5065 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005066 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005067 ll = nasm_malloc(sizeof(Line));
5068 ll->next = istk->expansion;
5069 ll->finishes = m;
5070 ll->first = NULL;
5071 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005072
5073 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005074 * Save the previous MMacro expansion in the case of
5075 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005076 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005077 if (m->max_depth && m->in_progress)
5078 push_mmacro(m);
5079
5080 m->in_progress ++;
5081 m->params = params;
5082 m->iline = tline;
5083 m->nparam = nparam;
5084 m->rotate = 0;
5085 m->paramlen = paramlen;
5086 m->unique = unique++;
5087 m->lineno = 0;
5088 m->condcnt = 0;
5089
5090 m->next_active = istk->mstk;
5091 istk->mstk = m;
5092
5093 list_for_each(l, m->expansion) {
5094 Token **tail;
5095
5096 ll = nasm_malloc(sizeof(Line));
5097 ll->finishes = NULL;
5098 ll->next = istk->expansion;
5099 istk->expansion = ll;
5100 tail = &ll->first;
5101
5102 list_for_each(t, l->first) {
5103 Token *x = t;
5104 switch (t->type) {
5105 case TOK_PREPROC_Q:
5106 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005107 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005108 case TOK_PREPROC_QQ:
5109 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
5110 break;
5111 case TOK_PREPROC_ID:
5112 if (t->text[1] == '0' && t->text[2] == '0') {
5113 dont_prepend = -1;
5114 x = label;
5115 if (!x)
5116 continue;
5117 }
5118 /* fall through */
5119 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005120 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005121 break;
5122 }
5123 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005124 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005125 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005126 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005127
5128 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005129 * If we had a label, push it on as the first line of
5130 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005131 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005132 if (label) {
5133 if (dont_prepend < 0)
5134 free_tlist(startline);
5135 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005136 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005137 ll->finishes = NULL;
5138 ll->next = istk->expansion;
5139 istk->expansion = ll;
5140 ll->first = startline;
5141 if (!dont_prepend) {
5142 while (label->next)
5143 label = label->next;
5144 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005145 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005146 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005147 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005148
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005149 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005150
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005151 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005152}
5153
H. Peter Anvin130736c2016-02-17 20:27:41 -08005154/*
5155 * This function adds macro names to error messages, and suppresses
5156 * them if necessary.
5157 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005158static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005159{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005160 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005161 MMacro *mmac = NULL;
5162 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005163
H. Peter Anvin130736c2016-02-17 20:27:41 -08005164 /*
5165 * If we're in a dead branch of IF or something like it, ignore the error.
5166 * However, because %else etc are evaluated in the state context
5167 * of the previous branch, errors might get lost:
5168 * %if 0 ... %else trailing garbage ... %endif
5169 * So %else etc should set the ERR_PP_PRECOND flag.
5170 */
5171 if ((severity & ERR_MASK) < ERR_FATAL &&
5172 istk && istk->conds &&
5173 ((severity & ERR_PP_PRECOND) ?
5174 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005175 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005176 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005177
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005178 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08005179 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005180 mmac = istk->mstk;
5181 /* but %rep blocks should be skipped */
5182 while (mmac && !mmac->name)
5183 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005184 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005185
H. Peter Anvin130736c2016-02-17 20:27:41 -08005186 if (mmac) {
5187 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005188
H. Peter Anvin130736c2016-02-17 20:27:41 -08005189 nasm_set_verror(real_verror);
5190 nasm_error(severity, "(%s:%d) %s",
5191 mmac->name, mmac->lineno - delta, buff);
5192 nasm_set_verror(pp_verror);
5193 } else {
5194 real_verror(severity, fmt, arg);
5195 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005196}
5197
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005198static Token *
5199stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005200{
5201 (void)s;
5202 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005203 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005204
5205 return make_tok_qstr(src_get_fname());
5206}
5207
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005208static Token *
5209stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005210{
5211 (void)s;
5212 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005213 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005214
5215 return make_tok_num(src_get_linnum());
5216}
5217
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005218static Token *
5219stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005220{
5221 (void)s;
5222 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005223 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005224
5225 return make_tok_num(globalbits);
5226}
5227
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005228static Token *
5229stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005230{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005231 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005232 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5233 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5234 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005235
5236 (void)s;
5237 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005238 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005239
5240 switch (globalbits) {
5241 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005242 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005243 break;
5244 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005245 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005246 break;
5247 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005248 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005249 break;
5250 default:
5251 panic();
5252 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005253
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005254 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005255}
5256
H. Peter Anvin8b262472019-02-26 14:00:54 -08005257/* Add magic standard macros */
5258struct magic_macros {
5259 const char *name;
5260 int nparams;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005261 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005262};
5263static const struct magic_macros magic_macros[] =
5264{
5265 { "__FILE__", 0, stdmac_file },
5266 { "__LINE__", 0, stdmac_line },
5267 { "__BITS__", 0, stdmac_bits },
5268 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005269 { NULL, 0, NULL }
5270};
5271
5272static void pp_add_magic_stdmac(void)
5273{
5274 const struct magic_macros *m;
5275 SMacro *s;
5276
5277 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005278 s = define_smacro(NULL, m->name, true, m->nparams, NULL, NULL);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005279 s->expand = m->func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005280 }
5281}
5282
H. Peter Anvin734b1882002-04-30 21:01:08 +00005283static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005284pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005285{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005286 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005287
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005288 cstk = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005289 nasm_new(istk);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07005290 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin274cda82016-05-10 02:56:29 -07005291 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005292 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005293 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03005294 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005295 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005296 nested_mac_count = 0;
5297 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005298 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005299 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005300 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005301 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005302
H. Peter Anvin8b262472019-02-26 14:00:54 -08005303 pp_add_magic_stdmac();
5304
H. Peter Anvinf7606612016-07-13 14:23:48 -07005305 if (tasm_compatible_mode)
5306 pp_add_stdmac(nasm_stdmac_tasm);
5307
5308 pp_add_stdmac(nasm_stdmac_nasm);
5309 pp_add_stdmac(nasm_stdmac_version);
5310
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005311 if (extrastdmac)
5312 pp_add_stdmac(extrastdmac);
5313
H. Peter Anvinf7606612016-07-13 14:23:48 -07005314 stdmacpos = stdmacros[0];
5315 stdmacnext = &stdmacros[1];
5316
H. Peter Anvind2456592008-06-19 15:04:18 -07005317 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005318
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03005319 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005320
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005321 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005322 * Define the __PASS__ macro. This is defined here unlike all the
5323 * other builtins, because it is special -- it varies between
5324 * passes -- but there is really no particular reason to make it
5325 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005326 *
5327 * 0 = dependencies only
5328 * 1 = preparatory passes
5329 * 2 = final pass
5330 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005331 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005332 switch (mode) {
5333 case PP_NORMAL:
5334 apass = pass_final() ? 2 : 1;
5335 break;
5336 case PP_DEPS:
5337 apass = 0;
5338 break;
5339 case PP_PREPROC:
5340 apass = 3;
5341 break;
5342 default:
5343 panic();
5344 }
5345
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005346 define_smacro(NULL, "__PASS__", true, 0, NULL, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005347}
5348
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005349static void pp_init(void)
5350{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005351}
5352
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005353/*
5354 * Get a line of tokens. If we popped the macro expansion/include stack,
5355 * we return a pointer to the dummy token tok_pop; at that point if
5356 * istk is NULL then we have reached end of input;
5357 */
5358static Token tok_pop; /* Dummy token placeholder */
5359
5360static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005361{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005362 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005363
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005364 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005365 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005366 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005367 * buffer or from the input file.
5368 */
5369 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005370 while (istk->expansion && istk->expansion->finishes) {
5371 Line *l = istk->expansion;
5372 if (!l->finishes->name && l->finishes->in_progress > 1) {
5373 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005374
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005375 /*
5376 * This is a macro-end marker for a macro with no
5377 * name, which means it's not really a macro at all
5378 * but a %rep block, and the `in_progress' field is
5379 * more than 1, meaning that we still need to
5380 * repeat. (1 means the natural last repetition; 0
5381 * means termination by %exitrep.) We have
5382 * therefore expanded up to the %endrep, and must
5383 * push the whole block on to the expansion buffer
5384 * again. We don't bother to remove the macro-end
5385 * marker: we'd only have to generate another one
5386 * if we did.
5387 */
5388 l->finishes->in_progress--;
5389 list_for_each(l, l->finishes->expansion) {
5390 Token *t, *tt, **tail;
5391
5392 ll = nasm_malloc(sizeof(Line));
5393 ll->next = istk->expansion;
5394 ll->finishes = NULL;
5395 ll->first = NULL;
5396 tail = &ll->first;
5397
5398 list_for_each(t, l->first) {
5399 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005400 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005401 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005402 }
5403 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005404 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005405 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005406 } else {
5407 /*
5408 * Check whether a `%rep' was started and not ended
5409 * within this macro expansion. This can happen and
5410 * should be detected. It's a fatal error because
5411 * I'm too confused to work out how to recover
5412 * sensibly from it.
5413 */
5414 if (defining) {
5415 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005416 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005417 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005418 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005419 " expansion of macro `%s'",
5420 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005421 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005422
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005423 /*
5424 * FIXME: investigate the relationship at this point between
5425 * istk->mstk and l->finishes
5426 */
5427 {
5428 MMacro *m = istk->mstk;
5429 istk->mstk = m->next_active;
5430 if (m->name) {
5431 /*
5432 * This was a real macro call, not a %rep, and
5433 * therefore the parameter information needs to
5434 * be freed.
5435 */
5436 if (m->prev) {
5437 pop_mmacro(m);
5438 l->finishes->in_progress --;
5439 } else {
5440 nasm_free(m->params);
5441 free_tlist(m->iline);
5442 nasm_free(m->paramlen);
5443 l->finishes->in_progress = 0;
5444 }
Adam Majer91e72402017-07-25 10:42:01 +02005445 }
5446
5447 /*
5448 * FIXME It is incorrect to always free_mmacro here.
5449 * It leads to usage-after-free.
5450 *
5451 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5452 */
5453#if 0
5454 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005455 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005456#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005457 }
5458 istk->expansion = l->next;
5459 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005460 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005461 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005462 }
5463 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005464 do { /* until we get a line we can use */
5465 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005466
5467 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005468 Line *l = istk->expansion;
5469 if (istk->mstk)
5470 istk->mstk->lineno++;
5471 tline = l->first;
5472 istk->expansion = l->next;
5473 nasm_free(l);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005474 line = detoken(tline, false);
5475 lfmt->line(LIST_MACRO, line);
5476 nasm_free(line);
5477 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005478 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005479 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005480 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005481 } else {
5482 /*
5483 * The current file has ended; work down the istk
5484 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005485 Include *i = istk;
5486 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005487 if (i->conds) {
5488 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005489 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005490 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005491 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005492 if (i->next)
5493 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005494 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005495 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005496 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005497 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005498 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005499 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005500
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005501 /*
5502 * We must expand MMacro parameters and MMacro-local labels
5503 * _before_ we plunge into directive processing, to cope
5504 * with things like `%define something %1' such as STRUC
5505 * uses. Unless we're _defining_ a MMacro, in which case
5506 * those tokens should be left alone to go into the
5507 * definition; and unless we're in a non-emitting
5508 * condition, in which case we don't want to meddle with
5509 * anything.
5510 */
5511 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5512 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005513 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005514 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005515
H. Peter Anvine2c80182005-01-15 22:15:51 +00005516 /*
5517 * Check the line to see if it's a preprocessor directive.
5518 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005519 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5520 if (dtline)
5521 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005522 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005523 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005524 * We're defining a multi-line macro. We emit nothing
5525 * at all, and just
5526 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005527 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005528 Line *l = nasm_malloc(sizeof(Line));
5529 l->next = defining->expansion;
5530 l->first = tline;
5531 l->finishes = NULL;
5532 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005533 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005534 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005535 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005536 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005537 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005538 * directive so we keep our place correctly.
5539 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005540 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005541 } else if (istk->mstk && !istk->mstk->in_progress) {
5542 /*
5543 * We're in a %rep block which has been terminated, so
5544 * we're walking through to the %endrep without
5545 * emitting anything. Emit nothing at all, not even a
5546 * blank line: when we emerge from the %rep block we'll
5547 * give a line-number directive so we keep our place
5548 * correctly.
5549 */
5550 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005551 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005552 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005553 if (!expand_mmacro(tline))
5554 return tline;
5555 }
5556 }
5557}
5558
5559static char *pp_getline(void)
5560{
5561 char *line = NULL;
5562 Token *tline;
5563
5564 real_verror = nasm_set_verror(pp_verror);
5565
5566 while (true) {
5567 tline = pp_tokline();
5568 if (tline == &tok_pop) {
5569 /*
5570 * We popped the macro/include stack. If istk is empty,
5571 * we are at end of input, otherwise just loop back.
5572 */
5573 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005574 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005575 } else {
5576 /*
5577 * De-tokenize the line and emit it.
5578 */
5579 line = detoken(tline, true);
5580 free_tlist(tline);
5581 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005582 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005583 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005584
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005585 if (list_option('e') && line && line[0]) {
5586 char *buf = nasm_strcat(" ;;; ", line);
5587 lfmt->line(LIST_MACRO, buf);
5588 nasm_free(buf);
5589 }
5590
H. Peter Anvin130736c2016-02-17 20:27:41 -08005591 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005592 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005593}
5594
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005595static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005596{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005597 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005598
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005599 if (defining) {
5600 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005601 nasm_nonfatal("end of file while still defining macro `%s'",
5602 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005603 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005604 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005605 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005606
5607 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005608 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005609 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005610
5611 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005612
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005613 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005614 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005615 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005616 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005617 Include *i = istk;
5618 istk = istk->next;
5619 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005620 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005621 }
5622 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005623 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005624 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005625}
5626
5627static void pp_cleanup_session(void)
5628{
5629 free_llist(predef);
5630 predef = NULL;
5631 delete_Blocks();
5632 freeTokens = NULL;
5633 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005634}
5635
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005636static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005637{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005638 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005639}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005640
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005641static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005642{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005643 Token *inc, *space, *name;
5644 Line *l;
5645
H. Peter Anvin734b1882002-04-30 21:01:08 +00005646 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5647 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5648 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005649
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005650 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005651 l->next = predef;
5652 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005653 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005654 predef = l;
5655}
5656
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005657static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005658{
5659 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005660 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005661 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005662
H. Peter Anvin130736c2016-02-17 20:27:41 -08005663 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005664
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005665 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005666 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5667 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005668 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005669 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005670 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005671 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005672 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005673
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005674 if (space->next->type != TOK_PREPROC_ID &&
5675 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005676 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005677
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005678 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005679 l->next = predef;
5680 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005681 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005682 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005683
5684 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005685}
5686
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005687static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005688{
5689 Token *def, *space;
5690 Line *l;
5691
H. Peter Anvin734b1882002-04-30 21:01:08 +00005692 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5693 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005694 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005695
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005696 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005697 l->next = predef;
5698 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005699 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005700 predef = l;
5701}
5702
H. Peter Anvin05990342018-06-11 13:32:42 -07005703/* Insert an early preprocessor command that doesn't need special handling */
5704static void pp_pre_command(const char *what, char *string)
5705{
5706 char *cmd;
5707 Token *def, *space;
5708 Line *l;
5709
5710 def = tokenize(string);
5711 if (what) {
5712 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5713 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5714 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5715 }
5716
5717 l = nasm_malloc(sizeof(Line));
5718 l->next = predef;
5719 l->first = def;
5720 l->finishes = NULL;
5721 predef = l;
5722}
5723
H. Peter Anvinf7606612016-07-13 14:23:48 -07005724static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005725{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005726 macros_t **mp;
5727
5728 /* Find the end of the list and avoid duplicates */
5729 for (mp = stdmacros; *mp; mp++) {
5730 if (*mp == macros)
5731 return; /* Nothing to do */
5732 }
5733
5734 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5735
5736 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005737}
5738
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005739static void pp_extra_stdmac(macros_t *macros)
5740{
5741 extrastdmac = macros;
5742}
5743
H. Peter Anvin8b262472019-02-26 14:00:54 -08005744static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005745{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005746 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005747 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5748 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5749}
5750
5751static Token *make_tok_qstr(const char *str)
5752{
5753 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005754 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005755 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005756}
5757
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005758static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005759{
5760 if (!m)
5761 return;
5762
5763 /* We need to print the next_active list in reverse order */
5764 pp_list_one_macro(m->next_active, severity);
5765
5766 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005767 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005768 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005769 }
5770}
5771
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005772static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005773{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005774 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005775
H. Peter Anvinddb29062018-12-11 00:06:29 -08005776 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5777 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005778
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005779 if (istk)
5780 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005781
H. Peter Anvinddb29062018-12-11 00:06:29 -08005782 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005783}
5784
H. Peter Anvine7469712016-02-18 02:20:59 -08005785const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005786 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005787 pp_reset,
5788 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005789 pp_cleanup_pass,
5790 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005791 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005792 pp_pre_define,
5793 pp_pre_undefine,
5794 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005795 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005796 pp_include_path,
5797 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005798};