blob: 10cc219784329d64d0dd04dd28c0d7c378c2d370 [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 Anvin (Intel)98031bf2019-08-09 16:11:28 -0700940 char c;
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) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700946 char *p = line;
947 char *ep = NULL; /* End of token, for trimming the end */
948
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 if (*p == '%') {
950 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300951 if (*p == '+' && !nasm_isdigit(p[1])) {
952 p++;
953 type = TOK_PASTE;
954 } else if (nasm_isdigit(*p) ||
955 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 do {
957 p++;
958 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700959 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 type = TOK_PREPROC_ID;
961 } else if (*p == '{') {
962 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800963 while (*p) {
964 if (*p == '}')
965 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 p[-1] = *p;
967 p++;
968 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800969 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800970 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700971 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 if (*p)
973 p++;
974 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300975 } else if (*p == '[') {
976 int lvl = 1;
977 line += 2; /* Skip the leading %[ */
978 p++;
979 while (lvl && (c = *p++)) {
980 switch (c) {
981 case ']':
982 lvl--;
983 break;
984 case '%':
985 if (*p == '[')
986 lvl++;
987 break;
988 case '\'':
989 case '\"':
990 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300991 p = nasm_skip_string(p - 1);
992 if (*p)
993 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300994 break;
995 default:
996 break;
997 }
998 }
999 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001000 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001001 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001002 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001003 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001004 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001005 type = TOK_INDIRECT;
1006 } else if (*p == '?') {
1007 type = TOK_PREPROC_Q; /* %? */
1008 p++;
1009 if (*p == '?') {
1010 type = TOK_PREPROC_QQ; /* %?? */
1011 p++;
1012 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001013 } else if (*p == '!') {
1014 type = TOK_PREPROC_ID;
1015 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001016 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001017 do {
1018 p++;
1019 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001020 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001021 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001022 p = nasm_skip_string(p);
1023 if (*p)
1024 p++;
1025 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001026 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001027 } else {
1028 /* %! without string or identifier */
1029 type = TOK_OTHER; /* Legacy behavior... */
1030 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001031 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001033 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 do {
1035 p++;
1036 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001037 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 type = TOK_PREPROC_ID;
1039 } else {
1040 type = TOK_OTHER;
1041 if (*p == '%')
1042 p++;
1043 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001044 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 type = TOK_ID;
1046 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001047 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001049 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 /*
1051 * A string token.
1052 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001054 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 if (*p) {
1057 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001058 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001059 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 /* Handling unterminated strings by UNV */
1061 /* type = -1; */
1062 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001063 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001064 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001065 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001066 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001067 bool is_hex = false;
1068 bool is_float = false;
1069 bool has_e = false;
1070 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001073 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001075
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001076 if (*p == '$') {
1077 p++;
1078 is_hex = true;
1079 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001080
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001081 for (;;) {
1082 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001083
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001084 if (!is_hex && (c == 'e' || c == 'E')) {
1085 has_e = true;
1086 if (*p == '+' || *p == '-') {
1087 /*
1088 * e can only be followed by +/- if it is either a
1089 * prefixed hex number or a floating-point number
1090 */
1091 p++;
1092 is_float = true;
1093 }
1094 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1095 is_hex = true;
1096 } else if (c == 'P' || c == 'p') {
1097 is_float = true;
1098 if (*p == '+' || *p == '-')
1099 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001100 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001101 ; /* just advance */
1102 else if (c == '.') {
1103 /*
1104 * we need to deal with consequences of the legacy
1105 * parser, like "1.nolist" being two tokens
1106 * (TOK_NUMBER, TOK_ID) here; at least give it
1107 * a shot for now. In the future, we probably need
1108 * a flex-based scanner with proper pattern matching
1109 * to do it as well as it can be done. Nothing in
1110 * the world is going to help the person who wants
1111 * 0x123.p16 interpreted as two tokens, though.
1112 */
1113 r = p;
1114 while (*r == '_')
1115 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001116
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001117 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1118 (!is_hex && (*r == 'e' || *r == 'E')) ||
1119 (*r == 'p' || *r == 'P')) {
1120 p = r;
1121 is_float = true;
1122 } else
1123 break; /* Terminate the token */
1124 } else
1125 break;
1126 }
1127 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001128
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001129 if (p == line+1 && *line == '$') {
1130 type = TOK_OTHER; /* TOKEN_HERE */
1131 } else {
1132 if (has_e && !is_hex) {
1133 /* 1e13 is floating-point, but 1e13h is not */
1134 is_float = true;
1135 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001136
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001137 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1138 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001139 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001141 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 /*
1143 * Whitespace just before end-of-line is discarded by
1144 * pretending it's a comment; whitespace just before a
1145 * comment gets lumped into the comment.
1146 */
1147 if (!*p || *p == ';') {
1148 type = TOK_COMMENT;
1149 while (*p)
1150 p++;
1151 }
1152 } else if (*p == ';') {
1153 type = TOK_COMMENT;
1154 while (*p)
1155 p++;
1156 } else {
1157 /*
1158 * Anything else is an operator of some kind. We check
1159 * for all the double-character operators (>>, <<, //,
1160 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001161 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 */
1163 type = TOK_OTHER;
1164 if ((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[0] == '&' && p[1] == '&') ||
1173 (p[0] == '|' && p[1] == '|') ||
1174 (p[0] == '^' && p[1] == '^')) {
1175 p++;
1176 }
1177 p++;
1178 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001179
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 /* Handling unterminated string by UNV */
1181 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001182 {
1183 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1184 t->text[p-line] = *line;
1185 tail = &t->next;
1186 }
1187 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001189 if (!ep)
1190 ep = p;
1191 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 tail = &t->next;
1193 }
1194 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001195 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001196 return list;
1197}
1198
H. Peter Anvince616072002-04-30 21:02:23 +00001199/*
1200 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001201 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001202 * deleted only all at once by the delete_Blocks function.
1203 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001205{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001206 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001208 /* first, get to the end of the linked list */
1209 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001211 /* now allocate the requested chunk */
1212 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001214 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001215 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001216 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001217}
1218
1219/*
1220 * this function deletes all managed blocks of memory
1221 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001223{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001225
H. Peter Anvin70653092007-10-19 14:42:29 -07001226 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001227 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001228 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001229 * free it.
1230 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001231 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001232 if (b->chunk)
1233 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 a = b;
1235 b = b->next;
1236 if (a != &blocks)
1237 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001238 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001239 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001241
1242/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001243 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001244 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001245 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001246static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001247 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001248{
1249 Token *t;
1250 int i;
1251
H. Peter Anvin89cee572009-07-15 09:16:54 -04001252 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001253 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1254 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1255 freeTokens[i].next = &freeTokens[i + 1];
1256 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001257 }
1258 t = freeTokens;
1259 freeTokens = t->next;
1260 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001261 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001262 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001263 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 t->text = NULL;
1265 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001266 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001268 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001269 t->text = nasm_malloc(txtlen+1);
1270 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001272 }
1273 return t;
1274}
1275
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001276static Token *dup_Token(Token *next, const Token *src)
1277{
1278 return new_Token(next, src->type, src->text, src->len);
1279}
1280
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001282{
1283 Token *next = t->next;
1284 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001285 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001286 freeTokens = t;
1287 return next;
1288}
1289
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001290/*
1291 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001292 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1293 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001294 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001295static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001296{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001297 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001298 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001299 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001300
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001301 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001302 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001303 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001304 char *v;
1305 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001306
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001307 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001308 if (nasm_isquote(*v))
1309 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001310
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001311 if (v) {
1312 char *p = getenv(v);
1313 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001314 /*!
1315 *!environment [on] nonexistent environment variable
1316 *! warns if a nonexistent environment variable
1317 *! is accessed using the \c{%!} preprocessor
1318 *! construct (see \k{getenv}.) Such environment
1319 *! variables are treated as empty (with this
1320 *! warning issued) starting in NASM 2.15;
1321 *! earlier versions of NASM would treat this as
1322 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001323 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001324 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1325 p = "";
1326 }
1327 t->text = nasm_strdup(p);
1328 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001329 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001330 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001331 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001332
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 /* Expand local macros here and not during preprocessing */
1334 if (expand_locals &&
1335 t->type == TOK_PREPROC_ID && t->text &&
1336 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001337 const char *q;
1338 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001339 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001341 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1342 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 nasm_free(t->text);
1344 t->text = p;
1345 }
1346 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001347 if (t->text) {
1348 if (debug_level(2)) {
1349 unsigned long t_len = t->len;
1350 unsigned long s_len = strlen(t->text);
1351 if (t_len != s_len) {
1352 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1353 t->text, t->type, s_len, t_len);
1354 t->len = s_len;
1355 }
1356 }
1357 len += t->len;
1358 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001359 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001360 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001361 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001362
H. Peter Anvin734b1882002-04-30 21:01:08 +00001363 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001364
1365 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001366 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001367 memcpy(p, t->text, t->len);
1368 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001369 } else if (t->type == TOK_WHITESPACE) {
1370 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001372 }
1373 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001374
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001375 return line;
1376}
1377
1378/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001379 * A scanner, suitable for use by the expression evaluator, which
1380 * operates on a line of Tokens. Expects a pointer to a pointer to
1381 * the first token in the line to be passed in as its private_data
1382 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001383 *
1384 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001385 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001386struct ppscan {
1387 Token *tptr;
1388 int ntokens;
1389};
1390
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001392{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001393 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001394 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001395 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001396
H. Peter Anvine2c80182005-01-15 22:15:51 +00001397 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001398 if (pps->ntokens && (tline = pps->tptr)) {
1399 pps->ntokens--;
1400 pps->tptr = tline->next;
1401 } else {
1402 pps->tptr = NULL;
1403 pps->ntokens = 0;
1404 return tokval->t_type = TOKEN_EOS;
1405 }
1406 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001407
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001408 tokval->t_charptr = tline->text;
1409
H. Peter Anvin76690a12002-04-30 20:52:49 +00001410 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001412 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001414
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001416 p = tokval->t_charptr = tline->text;
1417 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001418 tokval->t_charptr++;
1419 return tokval->t_type = TOKEN_ID;
1420 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001421
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001422 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001423 if (r >= p+MAX_KEYWORD)
1424 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001425 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001426 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001427 *s = '\0';
1428 /* right, so we have an identifier sitting in temp storage. now,
1429 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001430 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001431 }
1432
H. Peter Anvine2c80182005-01-15 22:15:51 +00001433 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001434 bool rn_error;
1435 tokval->t_integer = readnum(tline->text, &rn_error);
1436 tokval->t_charptr = tline->text;
1437 if (rn_error)
1438 return tokval->t_type = TOKEN_ERRNUM;
1439 else
1440 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001441 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001442
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001443 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001444 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001445 }
1446
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001448 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001449
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001450 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001451 tokval->t_charptr = tline->text;
1452 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001453
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001454 if (ep[0] != bq || ep[1] != '\0')
1455 return tokval->t_type = TOKEN_ERRSTR;
1456 else
1457 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001458 }
1459
H. Peter Anvine2c80182005-01-15 22:15:51 +00001460 if (tline->type == TOK_OTHER) {
1461 if (!strcmp(tline->text, "<<"))
1462 return tokval->t_type = TOKEN_SHL;
1463 if (!strcmp(tline->text, ">>"))
1464 return tokval->t_type = TOKEN_SHR;
1465 if (!strcmp(tline->text, "//"))
1466 return tokval->t_type = TOKEN_SDIV;
1467 if (!strcmp(tline->text, "%%"))
1468 return tokval->t_type = TOKEN_SMOD;
1469 if (!strcmp(tline->text, "=="))
1470 return tokval->t_type = TOKEN_EQ;
1471 if (!strcmp(tline->text, "<>"))
1472 return tokval->t_type = TOKEN_NE;
1473 if (!strcmp(tline->text, "!="))
1474 return tokval->t_type = TOKEN_NE;
1475 if (!strcmp(tline->text, "<="))
1476 return tokval->t_type = TOKEN_LE;
1477 if (!strcmp(tline->text, ">="))
1478 return tokval->t_type = TOKEN_GE;
1479 if (!strcmp(tline->text, "&&"))
1480 return tokval->t_type = TOKEN_DBL_AND;
1481 if (!strcmp(tline->text, "^^"))
1482 return tokval->t_type = TOKEN_DBL_XOR;
1483 if (!strcmp(tline->text, "||"))
1484 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001485 }
1486
1487 /*
1488 * We have no other options: just return the first character of
1489 * the token text.
1490 */
1491 return tokval->t_type = tline->text[0];
1492}
1493
1494/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001495 * Compare a string to the name of an existing macro; this is a
1496 * simple wrapper which calls either strcmp or nasm_stricmp
1497 * depending on the value of the `casesense' parameter.
1498 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001499static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001500{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001501 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001502}
1503
1504/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001505 * Compare a string to the name of an existing macro; this is a
1506 * simple wrapper which calls either strcmp or nasm_stricmp
1507 * depending on the value of the `casesense' parameter.
1508 */
1509static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1510{
1511 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1512}
1513
1514/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001515 * Return the Context structure associated with a %$ token. Return
1516 * NULL, having _already_ reported an error condition, if the
1517 * context stack isn't deep enough for the supplied number of $
1518 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001519 *
1520 * If "namep" is non-NULL, set it to the pointer to the macro name
1521 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001522 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001523static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001524{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001525 Context *ctx;
1526 int i;
1527
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001528 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001529 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001530
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001531 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001533
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001535 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001537 }
1538
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001539 name += 2;
1540 ctx = cstk;
1541 i = 0;
1542 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001543 name++;
1544 i++;
1545 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001546 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001547 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001548 nasm_nonfatal("`%s': context stack is only"
1549 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001550 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001551 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001552
1553 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001554 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001555
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001556 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001557}
1558
1559/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001560 * Open an include file. This routine must always return a valid
1561 * file pointer if it returns - it's responsible for throwing an
1562 * ERR_FATAL and bombing out completely if not. It should also try
1563 * the include path one by one until it finds the file or reaches
1564 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001565 *
1566 * Note: for INC_PROBE the function returns NULL at all times;
1567 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001568 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001569enum incopen_mode {
1570 INC_NEEDED, /* File must exist */
1571 INC_OPTIONAL, /* Missing is OK */
1572 INC_PROBE /* Only an existence probe */
1573};
1574
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001575/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001576static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001577 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001578{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001579 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001580 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001581 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001582 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001583 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001584
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001586 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001587 if (omode == INC_PROBE) {
1588 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001589 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001590 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001591 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001592 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001593 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001594 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001595 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001597 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001598
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001599 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001600
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001601 if (!ip) {
1602 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001603 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001604 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001605
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001606 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001607 ip = ip->next;
1608 }
1609}
1610
1611/*
1612 * Open a file, or test for the presence of one (depending on omode),
1613 * considering the include path.
1614 */
1615static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001616 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001617 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001618 enum incopen_mode omode,
1619 enum file_flags fmode)
1620{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001621 struct hash_insert hi;
1622 void **hp;
1623 char *path;
1624 FILE *fp = NULL;
1625
1626 hp = hash_find(&FileHash, file, &hi);
1627 if (hp) {
1628 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001629 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001630 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001631 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001632 } else {
1633 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001634 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001635
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001636 /* Positive or negative result */
1637 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001638
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001639 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001640 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001641 */
1642 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001643 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001644 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001645
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001646 if (!path) {
1647 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001648 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001649 } else {
1650 if (!fp && omode != INC_PROBE)
1651 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001652 }
1653
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001654 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001655 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001656
1657 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001658}
1659
1660/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001661 * Opens an include or input file. Public version, for use by modules
1662 * that get a file:lineno pair and need to look at the file again
1663 * (e.g. the CodeView debug backend). Returns NULL on failure.
1664 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001665FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001666{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001667 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001668}
1669
1670/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001671 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001672 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001673 * return true if _any_ single-line macro of that name is defined.
1674 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001675 * `nparam' or no parameters is defined.
1676 *
1677 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001678 * defined, or nparam is -1, the address of the definition structure
1679 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001680 * is NULL, no action will be taken regarding its contents, and no
1681 * error will occur.
1682 *
1683 * Note that this is also called with nparam zero to resolve
1684 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001685 *
1686 * If you already know which context macro belongs to, you can pass
1687 * the context pointer as first parameter; if you won't but name begins
1688 * with %$ the context will be automatically computed. If all_contexts
1689 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001690 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001691static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001692smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001693 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001694{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001695 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001697
H. Peter Anvin97a23472007-09-16 17:57:25 -07001698 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001699 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001700 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001701 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001702 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001704 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001705 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001706 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001707 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001708 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001709 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001710
H. Peter Anvine2c80182005-01-15 22:15:51 +00001711 while (m) {
1712 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001713 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001714 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001715 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 *defn = m;
1717 else
1718 *defn = NULL;
1719 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001720 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001721 }
1722 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001723 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001724
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001725 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001726}
1727
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001728/* param should be a natural number [0; INT_MAX] */
1729static int read_param_count(const char *str)
1730{
1731 int result;
1732 bool err;
1733
1734 result = readnum(str, &err);
1735 if (result < 0 || result > INT_MAX) {
1736 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001737 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1738 str, 0, INT_MAX);
1739 } else if (err)
1740 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001741 return result;
1742}
1743
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001744/*
1745 * Count and mark off the parameters in a multi-line macro call.
1746 * This is called both from within the multi-line macro expansion
1747 * code, and also to mark off the default parameters when provided
1748 * in a %macro definition line.
1749 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001751{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001752 int paramsize, brace;
1753
1754 *nparam = paramsize = 0;
1755 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001757 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001758 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 paramsize += PARAM_DELTA;
1760 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1761 }
1762 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001763 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001765 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001767 if (brace) {
1768 while (brace && (t = t->next) != NULL) {
1769 if (tok_is_(t, "{"))
1770 brace++;
1771 else if (tok_is_(t, "}"))
1772 brace--;
1773 }
1774
1775 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 /*
1777 * Now we've found the closing brace, look further
1778 * for the comma.
1779 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001780 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 skip_white_(t);
1782 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001783 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 while (tok_isnt_(t, ","))
1785 t = t->next;
1786 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001788 } else {
1789 while (tok_isnt_(t, ","))
1790 t = t->next;
1791 }
1792 if (t) { /* got a comma/brace */
1793 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001795 }
1796}
1797
1798/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001799 * Determine whether one of the various `if' conditions is true or
1800 * not.
1801 *
1802 * We must free the tline we get passed.
1803 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001804static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001805{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001806 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001807 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001808 Token *t, *tt, *origline;
1809 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001810 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001811 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001812 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001813 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001814
1815 origline = tline;
1816
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001818 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001819 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001820 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001822 if (!tline)
1823 break;
1824 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001825 nasm_nonfatal("`%s' expects context identifiers",
1826 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 free_tlist(origline);
1828 return -1;
1829 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001830 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001831 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 tline = tline->next;
1833 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001834 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001835
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001836 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001837 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001838 while (tline) {
1839 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 if (!tline || (tline->type != TOK_ID &&
1841 (tline->type != TOK_PREPROC_ID ||
1842 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001843 nasm_nonfatal("`%s' expects macro identifiers",
1844 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001845 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001847 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001848 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001850 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001851 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001852
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001853 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001854 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001855 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001856 while (tline) {
1857 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001858 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001859 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001860 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001861 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001862 nasm_nonfatal("`%s' expects environment variable names",
1863 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001864 goto fail;
1865 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001866 p = tline->text;
1867 if (tline->type == TOK_PREPROC_ID)
1868 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001869 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001870 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001871 if (getenv(p))
1872 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001873 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001874 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001875 break;
1876
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001877 case PPC_IFIDN:
1878 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 tline = expand_smacro(tline);
1880 t = tt = tline;
1881 while (tok_isnt_(tt, ","))
1882 tt = tt->next;
1883 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001884 nasm_nonfatal("`%s' expects two comma-separated arguments",
1885 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 }
1888 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001889 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1891 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001892 nasm_nonfatal("`%s': more than one comma on line",
1893 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001894 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 }
1896 if (t->type == TOK_WHITESPACE) {
1897 t = t->next;
1898 continue;
1899 }
1900 if (tt->type == TOK_WHITESPACE) {
1901 tt = tt->next;
1902 continue;
1903 }
1904 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001905 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001906 break;
1907 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001908 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001910 size_t l1 = nasm_unquote(t->text, NULL);
1911 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001912
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001913 if (l1 != l2) {
1914 j = false;
1915 break;
1916 }
1917 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1918 j = false;
1919 break;
1920 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001921 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001922 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 break;
1924 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001925
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 t = t->next;
1927 tt = tt->next;
1928 }
1929 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001930 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001931 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001932
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001933 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001934 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001936 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001937
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 skip_white_(tline);
1939 tline = expand_id(tline);
1940 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001941 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001942 goto fail;
1943 }
1944 searching.name = nasm_strdup(tline->text);
1945 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001946 searching.plus = false;
1947 searching.nolist = false;
1948 searching.in_progress = 0;
1949 searching.max_depth = 0;
1950 searching.rep_nest = NULL;
1951 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 searching.nparam_max = INT_MAX;
1953 tline = expand_smacro(tline->next);
1954 skip_white_(tline);
1955 if (!tline) {
1956 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001957 nasm_nonfatal("`%s' expects a parameter count or nothing",
1958 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959 } else {
1960 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001961 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001962 }
1963 if (tline && tok_is_(tline->next, "-")) {
1964 tline = tline->next->next;
1965 if (tok_is_(tline, "*"))
1966 searching.nparam_max = INT_MAX;
1967 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001968 nasm_nonfatal("`%s' expects a parameter count after `-'",
1969 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001970 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001971 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001972 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001973 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001974 searching.nparam_max = searching.nparam_min;
1975 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 }
1977 }
1978 if (tline && tok_is_(tline->next, "+")) {
1979 tline = tline->next;
1980 searching.plus = true;
1981 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001982 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1983 while (mmac) {
1984 if (!strcmp(mmac->name, searching.name) &&
1985 (mmac->nparam_min <= searching.nparam_max
1986 || searching.plus)
1987 && (searching.nparam_min <= mmac->nparam_max
1988 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 found = true;
1990 break;
1991 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001992 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001993 }
1994 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001995 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001996 nasm_free(searching.name);
1997 j = found;
1998 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001999 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002000
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002001 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002002 needtype = TOK_ID;
2003 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002004 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 needtype = TOK_NUMBER;
2006 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002007 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 needtype = TOK_STRING;
2009 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002010
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002011iftype:
2012 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002013
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002014 while (tok_type_(t, TOK_WHITESPACE) ||
2015 (needtype == TOK_NUMBER &&
2016 tok_type_(t, TOK_OTHER) &&
2017 (t->text[0] == '-' || t->text[0] == '+') &&
2018 !t->text[1]))
2019 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002020
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002021 j = tok_type_(t, needtype);
2022 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002023
2024 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002025 t = tline = expand_smacro(tline);
2026 while (tok_type_(t, TOK_WHITESPACE))
2027 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002028
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002029 j = false;
2030 if (t) {
2031 t = t->next; /* Skip the actual token */
2032 while (tok_type_(t, TOK_WHITESPACE))
2033 t = t->next;
2034 j = !t; /* Should be nothing left */
2035 }
2036 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002037
H. Peter Anvin134b9462008-02-16 17:01:40 -08002038 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002039 t = tline = expand_smacro(tline);
2040 while (tok_type_(t, TOK_WHITESPACE))
2041 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002042
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002043 j = !t; /* Should be empty */
2044 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002045
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002046 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002047 pps.tptr = tline = expand_smacro(tline);
2048 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002049 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002050 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002051 if (!evalresult)
2052 return -1;
2053 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002054 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002055 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002056 nasm_nonfatal("non-constant value given to `%s'",
2057 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002058 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002059 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002060 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002061 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002062
H. Peter Anvine2c80182005-01-15 22:15:51 +00002063 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002064 nasm_fatal("preprocessor directive `%s' not yet implemented",
2065 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002066 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002067 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002068
2069 free_tlist(origline);
2070 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002071
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002072fail:
2073 free_tlist(origline);
2074 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002075}
2076
2077/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002078 * Default smacro expansion routine: just returns a copy of the
2079 * expansion list.
2080 */
2081static Token *
2082smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2083{
2084 (void)params;
2085 (void)nparams;
2086
2087 return dup_tlist(s->expansion, NULL);
2088}
2089
2090/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002091 * Emit a macro defintion or undef to the listing file, if
2092 * desired. This is similar to detoken(), but it handles the reverse
2093 * expansion list, does not expand %! or local variable tokens, and
2094 * does some special handling for macro parameters.
2095 */
2096static void
2097list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2098{
2099 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2100 const Token **param_names;
2101 Token *junk_names = NULL;
2102 Token *t;
2103 size_t namelen, size;
2104 unsigned int i;
2105 char *def, *p;
2106 unsigned int context_depth = 0;
2107
2108 nasm_newn(param_names, m->nparam);
2109
2110 namelen = strlen(m->name);
2111 size = namelen + 2; /* Include room for space after name + NUL */
2112
2113 if (ctx) {
2114 context_depth = cstk->depth - ctx->depth + 1;
2115 size += context_depth + 1; /* % + some number of $ */
2116 }
2117
2118 list_for_each(t, m->expansion) {
2119 if (!t->text) {
2120 size++; /* Whitespace, presumably */
2121 } else {
2122 size += t->len;
2123 if (is_smac_param(t->type))
2124 param_names[smac_nparam(t->type)] = t;
2125 }
2126 }
2127
2128 if (m->nparam) {
2129 /*
2130 * Space for ( and either , or ) around each
2131 * parameter, plus an optional =.
2132 */
2133 size += 1 + 2 * m->nparam;
2134 for (i = 0; i < m->nparam; i++) {
2135 if (!param_names[i])
2136 param_names[i] = &unused_arg_name;
2137 size += param_names[i]->len;
2138 }
2139 }
2140
2141 def = nasm_malloc(size);
2142 p = def+size;
2143 *--p = '\0';
2144
2145 list_for_each(t, m->expansion) {
2146 if (!t->text) {
2147 *--p = ' ';
2148 } else {
2149 p -= t->len;
2150 memcpy(p, t->text, t->len);
2151 }
2152 }
2153
2154 *--p = ' ';
2155
2156 if (m->nparam) {
2157 *--p = ')';
2158 for (i = m->nparam; i--;) {
2159 p -= param_names[i]->len;
2160 memcpy(p, param_names[i]->text, param_names[i]->len);
2161 if (m->eval_param && m->eval_param[i])
2162 *--p = '=';
2163 *--p = ',';
2164 }
2165 *p = '('; /* First parameter starts with ( not , */
2166
2167 free_tlist(junk_names);
2168 }
2169
2170 p -= namelen;
2171 memcpy(p, m->name, namelen);
2172
2173 if (context_depth) {
2174 for (i = 0; i < context_depth; i++)
2175 *--p = '$';
2176 *--p = '%';
2177 }
2178
2179 nasm_listmsg("%s %s", pp_directives[op], p);
2180
2181 nasm_free(def);
2182 }
2183
2184/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002185 * Common code for defining an smacro
2186 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002187static SMacro *define_smacro(Context *ctx, const char *mname,
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002188 bool casesense, int nparam,
2189 bool *eval_param, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002190{
2191 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002192 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002193
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002194 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002195 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002196 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002197 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002198 /*
2199 * Some instances of the old code considered this a failure,
2200 * some others didn't. What is the right thing to do here?
2201 */
2202 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002203 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002204 } else {
2205 /*
2206 * We're redefining, so we have to take over an
2207 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002208 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002209 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002210 clear_smacro(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002211 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002212 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002213 smtbl = ctx ? &ctx->localmac : &smacros;
2214 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002215 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002216 smac->next = *smhead;
2217 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002218 }
2219 smac->name = nasm_strdup(mname);
2220 smac->casesense = casesense;
2221 smac->nparam = nparam;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002222 smac->eval_param = eval_param;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002223 smac->expansion = expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002224 smac->expand = smacro_expand_default;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002225
2226 if (list_option('m'))
2227 list_smacro_def(casesense ? PP_DEFINE : PP_IDEFINE, ctx, smac);
2228
H. Peter Anvin8b262472019-02-26 14:00:54 -08002229 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002230}
2231
2232/*
2233 * Undefine an smacro
2234 */
2235static void undef_smacro(Context *ctx, const char *mname)
2236{
2237 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002238 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002239
H. Peter Anvin166c2472008-05-28 12:28:58 -07002240 smtbl = ctx ? &ctx->localmac : &smacros;
2241 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002242
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002243 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002244 /*
2245 * We now have a macro name... go hunt for it.
2246 */
2247 sp = smhead;
2248 while ((s = *sp) != NULL) {
2249 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002250 if (list_option('m'))
2251 list_smacro_def(PP_UNDEF, ctx, s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002252 *sp = s->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002253 free_smacro(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002254 } else {
2255 sp = &s->next;
2256 }
2257 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002258 }
2259}
2260
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002261/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002262 * Parse a mmacro specification.
2263 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002264static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002265{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002266 tline = tline->next;
2267 skip_white_(tline);
2268 tline = expand_id(tline);
2269 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002270 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002271 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002272 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002273
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002274 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002275 def->name = nasm_strdup(tline->text);
2276 def->plus = false;
2277 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002278 def->in_progress = 0;
2279 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002280 def->nparam_min = 0;
2281 def->nparam_max = 0;
2282
H. Peter Anvina26433d2008-07-16 14:40:01 -07002283 tline = expand_smacro(tline->next);
2284 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002285 if (!tok_type_(tline, TOK_NUMBER))
2286 nasm_nonfatal("`%s' expects a parameter count", directive);
2287 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002288 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002289 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002290 tline = tline->next->next;
2291 if (tok_is_(tline, "*")) {
2292 def->nparam_max = INT_MAX;
2293 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002294 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002295 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002296 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002297 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002298 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002299 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002300 }
2301 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002302 }
2303 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002304 tline = tline->next;
2305 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002306 }
2307 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002308 !nasm_stricmp(tline->next->text, ".nolist")) {
2309 tline = tline->next;
2310 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002311 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002312
H. Peter Anvina26433d2008-07-16 14:40:01 -07002313 /*
2314 * Handle default parameters.
2315 */
2316 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002317 def->dlist = tline->next;
2318 tline->next = NULL;
2319 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002320 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002321 def->dlist = NULL;
2322 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002323 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002324 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002325
H. Peter Anvin89cee572009-07-15 09:16:54 -04002326 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002327 !def->plus) {
2328 /*
2329 *!macro-defaults [on] macros with more default than optional parameters
2330 *! warns when a macro has more default parameters than optional parameters.
2331 *! See \k{mlmacdef} for why might want to disable this warning.
2332 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002333 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002334 "too many default macro parameters in macro `%s'", def->name);
2335 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002336
H. Peter Anvina26433d2008-07-16 14:40:01 -07002337 return true;
2338}
2339
2340
2341/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002342 * Decode a size directive
2343 */
2344static int parse_size(const char *str) {
2345 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002346 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002347 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002348 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002349 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002350}
2351
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002352/*
2353 * Process a preprocessor %pragma directive. Currently there are none.
2354 * Gets passed the token list starting with the "preproc" token from
2355 * "%pragma preproc".
2356 */
2357static void do_pragma_preproc(Token *tline)
2358{
2359 /* Skip to the real stuff */
2360 tline = tline->next;
2361 skip_white_(tline);
2362 if (!tline)
2363 return;
2364
2365 (void)tline; /* Nothing else to do at present */
2366}
2367
Ed Beroset3ab3f412002-06-11 03:31:49 +00002368/**
2369 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002370 * Find out if a line contains a preprocessor directive, and deal
2371 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002372 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002373 * If a directive _is_ found, it is the responsibility of this routine
2374 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002375 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002376 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002377 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002378 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002379 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002380 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002381static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002382{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002383 enum preproc_token i;
2384 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002385 bool err;
2386 int nparam;
2387 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002388 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002389 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002390 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002391 char *p, *pp;
2392 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002393 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002394 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002395 Include *inc;
2396 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002397 Cond *cond;
2398 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002399 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002400 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002401 struct tokenval tokval;
2402 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002403 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002404 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002405 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002406 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002407 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002408
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002409 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002410 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002411
H. Peter Anvineba20a72002-04-30 20:53:55 +00002412 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002413 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002414 (tline->text[0] && (tline->text[1] == '%' ||
2415 tline->text[1] == '$' ||
2416 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002417 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002418
H. Peter Anvin4169a472007-09-12 01:29:43 +00002419 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002420
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002421 /*
2422 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2423 * since they are known to be buggy at moment, we need to fix them
2424 * in future release (2.09-2.10)
2425 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002426 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002427 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2428 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002429 }
2430
2431 /*
2432 * If we're in a non-emitting branch of a condition construct,
2433 * or walking to the end of an already terminated %rep block,
2434 * we should ignore all directives except for condition
2435 * directives.
2436 */
2437 if (((istk->conds && !emitting(istk->conds->state)) ||
2438 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2439 return NO_DIRECTIVE_FOUND;
2440 }
2441
2442 /*
2443 * If we're defining a macro or reading a %rep block, we should
2444 * ignore all directives except for %macro/%imacro (which nest),
2445 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2446 * If we're in a %rep block, another %rep nests, so should be let through.
2447 */
2448 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2449 i != PP_RMACRO && i != PP_IRMACRO &&
2450 i != PP_ENDMACRO && i != PP_ENDM &&
2451 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2452 return NO_DIRECTIVE_FOUND;
2453 }
2454
2455 if (defining) {
2456 if (i == PP_MACRO || i == PP_IMACRO ||
2457 i == PP_RMACRO || i == PP_IRMACRO) {
2458 nested_mac_count++;
2459 return NO_DIRECTIVE_FOUND;
2460 } else if (nested_mac_count > 0) {
2461 if (i == PP_ENDMACRO) {
2462 nested_mac_count--;
2463 return NO_DIRECTIVE_FOUND;
2464 }
2465 }
2466 if (!defining->name) {
2467 if (i == PP_REP) {
2468 nested_rep_count++;
2469 return NO_DIRECTIVE_FOUND;
2470 } else if (nested_rep_count > 0) {
2471 if (i == PP_ENDREP) {
2472 nested_rep_count--;
2473 return NO_DIRECTIVE_FOUND;
2474 }
2475 }
2476 }
2477 }
2478
H. Peter Anvin8b262472019-02-26 14:00:54 -08002479 dname = pp_directives[i]; /* Directive name, for error messages */
2480 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002481 switch (i) {
2482 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002483 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002485
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002486 case PP_PRAGMA:
2487 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002488 * %pragma namespace options...
2489 *
2490 * The namespace "preproc" is reserved for the preprocessor;
2491 * all other namespaces generate a [pragma] assembly directive.
2492 *
2493 * Invalid %pragmas are ignored and may have different
2494 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002495 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002496 tline = tline->next;
2497 skip_white_(tline);
2498 tline = expand_smacro(tline);
2499 if (tok_type_(tline, TOK_ID)) {
2500 if (!nasm_stricmp(tline->text, "preproc")) {
2501 /* Preprocessor pragma */
2502 do_pragma_preproc(tline);
2503 } else {
2504 /* Build the assembler directive */
2505 t = new_Token(NULL, TOK_OTHER, "[", 1);
2506 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2507 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2508 tline = t;
2509 for (t = tline; t->next; t = t->next)
2510 ;
2511 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002512 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002513 }
2514 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002515 free_tlist(origline);
2516 return DIRECTIVE_FOUND;
2517
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 case PP_STACKSIZE:
2519 /* Directive to tell NASM what the default stack size is. The
2520 * default is for a 16-bit stack, and this can be overriden with
2521 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002522 */
2523 tline = tline->next;
2524 if (tline && tline->type == TOK_WHITESPACE)
2525 tline = tline->next;
2526 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002527 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002528 free_tlist(origline);
2529 return DIRECTIVE_FOUND;
2530 }
2531 if (nasm_stricmp(tline->text, "flat") == 0) {
2532 /* All subsequent ARG directives are for a 32-bit stack */
2533 StackSize = 4;
2534 StackPointer = "ebp";
2535 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002536 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002537 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2538 /* All subsequent ARG directives are for a 64-bit stack */
2539 StackSize = 8;
2540 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002541 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002542 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 } else if (nasm_stricmp(tline->text, "large") == 0) {
2544 /* All subsequent ARG directives are for a 16-bit stack,
2545 * far function call.
2546 */
2547 StackSize = 2;
2548 StackPointer = "bp";
2549 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002550 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 } else if (nasm_stricmp(tline->text, "small") == 0) {
2552 /* All subsequent ARG directives are for a 16-bit stack,
2553 * far function call. We don't support near functions.
2554 */
2555 StackSize = 2;
2556 StackPointer = "bp";
2557 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002558 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002560 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 free_tlist(origline);
2562 return DIRECTIVE_FOUND;
2563 }
2564 free_tlist(origline);
2565 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002566
H. Peter Anvine2c80182005-01-15 22:15:51 +00002567 case PP_ARG:
2568 /* TASM like ARG directive to define arguments to functions, in
2569 * the following form:
2570 *
2571 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2572 */
2573 offset = ArgOffset;
2574 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002575 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002576 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002577
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 /* Find the argument name */
2579 tline = tline->next;
2580 if (tline && tline->type == TOK_WHITESPACE)
2581 tline = tline->next;
2582 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002583 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 free_tlist(origline);
2585 return DIRECTIVE_FOUND;
2586 }
2587 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002588
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589 /* Find the argument size type */
2590 tline = tline->next;
2591 if (!tline || tline->type != TOK_OTHER
2592 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002593 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002594 free_tlist(origline);
2595 return DIRECTIVE_FOUND;
2596 }
2597 tline = tline->next;
2598 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002599 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002600 free_tlist(origline);
2601 return DIRECTIVE_FOUND;
2602 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002603
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002605 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002606 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002607 size = parse_size(tt->text);
2608 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002609 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002610 free_tlist(tt);
2611 free_tlist(origline);
2612 return DIRECTIVE_FOUND;
2613 }
2614 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002615
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002616 /* Round up to even stack slots */
2617 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002618
H. Peter Anvine2c80182005-01-15 22:15:51 +00002619 /* Now define the macro for the argument */
2620 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2621 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002622 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002623 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002624
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 /* Move to the next argument in the list */
2626 tline = tline->next;
2627 if (tline && tline->type == TOK_WHITESPACE)
2628 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002629 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002631 free_tlist(origline);
2632 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002633
H. Peter Anvine2c80182005-01-15 22:15:51 +00002634 case PP_LOCAL:
2635 /* TASM like LOCAL directive to define local variables for a
2636 * function, in the following form:
2637 *
2638 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2639 *
2640 * The '= LocalSize' at the end is ignored by NASM, but is
2641 * required by TASM to define the local parameter size (and used
2642 * by the TASM macro package).
2643 */
2644 offset = LocalOffset;
2645 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002646 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002648
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 /* Find the argument name */
2650 tline = tline->next;
2651 if (tline && tline->type == TOK_WHITESPACE)
2652 tline = tline->next;
2653 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002654 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002655 free_tlist(origline);
2656 return DIRECTIVE_FOUND;
2657 }
2658 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002659
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 /* Find the argument size type */
2661 tline = tline->next;
2662 if (!tline || tline->type != TOK_OTHER
2663 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002664 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002665 free_tlist(origline);
2666 return DIRECTIVE_FOUND;
2667 }
2668 tline = tline->next;
2669 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002670 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002671 free_tlist(origline);
2672 return DIRECTIVE_FOUND;
2673 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002674
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002676 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002678 size = parse_size(tt->text);
2679 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002680 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681 free_tlist(tt);
2682 free_tlist(origline);
2683 return DIRECTIVE_FOUND;
2684 }
2685 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002686
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002687 /* Round up to even stack slots */
2688 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002689
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002691
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2694 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002695 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002696
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 /* Now define the assign to setup the enter_c macro correctly */
2698 snprintf(directive, sizeof(directive),
2699 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002700 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002701
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 /* Move to the next argument in the list */
2703 tline = tline->next;
2704 if (tline && tline->type == TOK_WHITESPACE)
2705 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002706 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 free_tlist(origline);
2709 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002710
H. Peter Anvine2c80182005-01-15 22:15:51 +00002711 case PP_CLEAR:
2712 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002713 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 free_macros();
2715 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 free_tlist(origline);
2717 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002718
H. Peter Anvin418ca702008-05-30 10:42:30 -07002719 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002720 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002721 skip_white_(t);
2722 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002724 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002725 free_tlist(origline);
2726 return DIRECTIVE_FOUND; /* but we did _something_ */
2727 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002728 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002729 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002731 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002732 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002733 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002734 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002735 return DIRECTIVE_FOUND;
2736
2737 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002738 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002739 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002740
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002741 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002742 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002743 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002744 free_tlist(origline);
2745 return DIRECTIVE_FOUND; /* but we did _something_ */
2746 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002747 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002748 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002749 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002750 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002751 nasm_unquote_cstr(p, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002752 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002754 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002755 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002756 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002757 (pp_mode == PP_DEPS)
2758 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002759 if (!inc->fp) {
2760 /* -MG given but file not found */
2761 nasm_free(inc);
2762 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002763 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002764 inc->lineno = src_set_linnum(0);
2765 inc->lineinc = 1;
2766 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002767 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002768 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002769 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002770 }
2771 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002772 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002773
H. Peter Anvind2456592008-06-19 15:04:18 -07002774 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002775 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002776 static macros_t *use_pkg;
2777 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002778
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002779 tline = tline->next;
2780 skip_white_(tline);
2781 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002782
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002783 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002784 tline->type != TOK_INTERNAL_STRING &&
2785 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002786 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002787 free_tlist(origline);
2788 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002789 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002790 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002791 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002792 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002793 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 use_pkg = nasm_stdmac_find_package(tline->text);
2795 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002796 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 else
2798 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002799 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002800 /* Not already included, go ahead and include it */
2801 stdmacpos = use_pkg;
2802 }
2803 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002804 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002805 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002808 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 tline = tline->next;
2810 skip_white_(tline);
2811 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002812 if (tline) {
2813 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002814 nasm_nonfatal("`%s' expects a context identifier",
2815 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002816 free_tlist(origline);
2817 return DIRECTIVE_FOUND; /* but we did _something_ */
2818 }
2819 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002820 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002821 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002822 p = nasm_strdup(tline->text);
2823 } else {
2824 p = NULL; /* Anonymous */
2825 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002826
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002827 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002828 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002829 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002830 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002831 ctx->name = p;
2832 ctx->number = unique++;
2833 cstk = ctx;
2834 } else {
2835 /* %pop or %repl */
2836 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002837 nasm_nonfatal("`%s': context stack is empty",
2838 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002839 } else if (i == PP_POP) {
2840 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002841 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002842 "expected %s",
2843 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002844 else
2845 ctx_pop();
2846 } else {
2847 /* i == PP_REPL */
2848 nasm_free(cstk->name);
2849 cstk->name = p;
2850 p = NULL;
2851 }
2852 nasm_free(p);
2853 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002854 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002855 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002856 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002857 severity = ERR_FATAL;
2858 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002860 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002861 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002862 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002863 /*!
2864 *!user [on] %warning directives
2865 *! controls output of \c{%warning} directives (see \k{pperror}).
2866 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002867 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002868 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002869
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002870issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002871 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002872 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002873 tline->next = expand_smacro(tline->next);
2874 tline = tline->next;
2875 skip_white_(tline);
2876 t = tline ? tline->next : NULL;
2877 skip_white_(t);
2878 if (tok_type_(tline, TOK_STRING) && !t) {
2879 /* The line contains only a quoted string */
2880 p = tline->text;
2881 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002882 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002883 } else {
2884 /* Not a quoted string, or more than a quoted string */
2885 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002886 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002887 nasm_free(p);
2888 }
2889 free_tlist(origline);
2890 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002891 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002892
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002893 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002894 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002896 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002897 j = if_condition(tline->next, i);
2898 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002899 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002900 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002901 cond = nasm_malloc(sizeof(Cond));
2902 cond->next = istk->conds;
2903 cond->state = j;
2904 istk->conds = cond;
2905 if(istk->mstk)
2906 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002907 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002908 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002909
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002910 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002911 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002912 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002913 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002914 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002915 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002917
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002918 case COND_DONE:
2919 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002920 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002921
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002922 case COND_ELSE_TRUE:
2923 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002924 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002925 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002926 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002927 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002928
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002929 case COND_IF_FALSE:
2930 /*
2931 * IMPORTANT: In the case of %if, we will already have
2932 * called expand_mmac_params(); however, if we're
2933 * processing an %elif we must have been in a
2934 * non-emitting mode, which would have inhibited
2935 * the normal invocation of expand_mmac_params().
2936 * Therefore, we have to do it explicitly here.
2937 */
2938 j = if_condition(expand_mmac_params(tline->next), i);
2939 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002940 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002941 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2942 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002943 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002944 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002946
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 case PP_ELSE:
2948 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002949 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002950 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002951 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002952 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002953 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002954 case COND_IF_TRUE:
2955 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002956 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002957 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002958
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002959 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002960 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002961
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002962 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002963 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002964 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002965
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002966 case COND_ELSE_TRUE:
2967 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002968 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002969 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002970 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002971 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002972 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 free_tlist(origline);
2974 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002975
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 case PP_ENDIF:
2977 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002978 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002979 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002980 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002981 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002982 cond = istk->conds;
2983 istk->conds = cond->next;
2984 nasm_free(cond);
2985 if(istk->mstk)
2986 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 free_tlist(origline);
2988 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002989
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002990 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002992 casesense = false;
2993 /* fall through */
2994 case PP_RMACRO:
2995 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002996 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002997 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002998 return DIRECTIVE_FOUND;
2999 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003000 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003001 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
3002 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003003 defining->casesense = casesense;
3004 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003005 nasm_free(defining);
3006 defining = NULL;
3007 return DIRECTIVE_FOUND;
3008 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003009
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003010 src_get(&defining->xline, &defining->fname);
3011
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003012 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3013 while (mmac) {
3014 if (!strcmp(mmac->name, defining->name) &&
3015 (mmac->nparam_min <= defining->nparam_max
3016 || defining->plus)
3017 && (defining->nparam_min <= mmac->nparam_max
3018 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003019 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003020 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003021 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003022 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003023 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003024 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003025 free_tlist(origline);
3026 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003027
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 case PP_ENDM:
3029 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003030 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003031 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003032 return DIRECTIVE_FOUND;
3033 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003034 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3035 defining->next = *mmhead;
3036 *mmhead = defining;
3037 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003038 free_tlist(origline);
3039 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003040
H. Peter Anvin89cee572009-07-15 09:16:54 -04003041 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003042 /*
3043 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003044 * macro-end marker for a macro with a name. Then we
3045 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003046 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003047 list_for_each(l, istk->expansion)
3048 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003049 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003050
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003051 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003052 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003053 * Remove all conditional entries relative to this
3054 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003055 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003056 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3057 cond = istk->conds;
3058 istk->conds = cond->next;
3059 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003060 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003061 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003062 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003063 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003064 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003065 free_tlist(origline);
3066 return DIRECTIVE_FOUND;
3067
H. Peter Anvina26433d2008-07-16 14:40:01 -07003068 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003069 casesense = false;
3070 /* fall through */
3071 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003072 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003073 MMacro **mmac_p;
3074 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003075
H. Peter Anvin8b262472019-02-26 14:00:54 -08003076 spec.casesense = casesense;
3077 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003078 return DIRECTIVE_FOUND;
3079 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003080 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3081 while (mmac_p && *mmac_p) {
3082 mmac = *mmac_p;
3083 if (mmac->casesense == spec.casesense &&
3084 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3085 mmac->nparam_min == spec.nparam_min &&
3086 mmac->nparam_max == spec.nparam_max &&
3087 mmac->plus == spec.plus) {
3088 *mmac_p = mmac->next;
3089 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003090 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003091 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003092 }
3093 }
3094 free_tlist(origline);
3095 free_tlist(spec.dlist);
3096 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003097 }
3098
H. Peter Anvine2c80182005-01-15 22:15:51 +00003099 case PP_ROTATE:
3100 if (tline->next && tline->next->type == TOK_WHITESPACE)
3101 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003102 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003103 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003104 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 return DIRECTIVE_FOUND;
3106 }
3107 t = expand_smacro(tline->next);
3108 tline->next = NULL;
3109 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003110 pps.tptr = tline = t;
3111 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003112 tokval.t_type = TOKEN_INVALID;
3113 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003114 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 free_tlist(tline);
3116 if (!evalresult)
3117 return DIRECTIVE_FOUND;
3118 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003119 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003120 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003121 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 return DIRECTIVE_FOUND;
3123 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003124 mmac = istk->mstk;
3125 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3126 mmac = mmac->next_active;
3127 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003128 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003129 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003130 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003131 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003132 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003133
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003134 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003135 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003136 rotate += mmac->nparam;
3137
3138 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003139 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003141
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003143 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 do {
3145 tline = tline->next;
3146 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003147
H. Peter Anvine2c80182005-01-15 22:15:51 +00003148 if (tok_type_(tline, TOK_ID) &&
3149 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003150 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003151 do {
3152 tline = tline->next;
3153 } while (tok_type_(tline, TOK_WHITESPACE));
3154 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003155
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003157 pps.tptr = expand_smacro(tline);
3158 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003159 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003160 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003161 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003162 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003163 if (!evalresult) {
3164 free_tlist(origline);
3165 return DIRECTIVE_FOUND;
3166 }
3167 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003168 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003169 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003170 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 return DIRECTIVE_FOUND;
3172 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003173 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003174 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003175 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3176 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003177 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003178 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003179 /*!
3180 *!negative-rep [on] regative %rep count
3181 *! warns about negative counts given to the \c{%rep}
3182 *! preprocessor directive.
3183 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003184 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003185 "negative `%%rep' count: %"PRId64, count);
3186 count = 0;
3187 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003188 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003189 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003190 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003191 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003192 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003193 }
3194 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003195
3196 tmp_defining = defining;
3197 defining = nasm_malloc(sizeof(MMacro));
3198 defining->prev = NULL;
3199 defining->name = NULL; /* flags this macro as a %rep block */
3200 defining->casesense = false;
3201 defining->plus = false;
3202 defining->nolist = nolist;
3203 defining->in_progress = count;
3204 defining->max_depth = 0;
3205 defining->nparam_min = defining->nparam_max = 0;
3206 defining->defaults = NULL;
3207 defining->dlist = NULL;
3208 defining->expansion = NULL;
3209 defining->next_active = istk->mstk;
3210 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003211 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003212
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003214 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003215 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003216 return DIRECTIVE_FOUND;
3217 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003218
H. Peter Anvine2c80182005-01-15 22:15:51 +00003219 /*
3220 * Now we have a "macro" defined - although it has no name
3221 * and we won't be entering it in the hash tables - we must
3222 * push a macro-end marker for it on to istk->expansion.
3223 * After that, it will take care of propagating itself (a
3224 * macro-end marker line for a macro which is really a %rep
3225 * block will cause the macro to be re-expanded, complete
3226 * with another macro-end marker to ensure the process
3227 * continues) until the whole expansion is forcibly removed
3228 * from istk->expansion by a %exitrep.
3229 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003230 l = nasm_malloc(sizeof(Line));
3231 l->next = istk->expansion;
3232 l->finishes = defining;
3233 l->first = NULL;
3234 istk->expansion = l;
3235
3236 istk->mstk = defining;
3237
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003238 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003239 tmp_defining = defining;
3240 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003241 free_tlist(origline);
3242 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003243
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003245 /*
3246 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003247 * macro-end marker for a macro with no name. Then we set
3248 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003249 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003250 list_for_each(l, istk->expansion)
3251 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003252 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003253
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003254 if (l)
3255 l->finishes->in_progress = 1;
3256 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003257 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003258 free_tlist(origline);
3259 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003260
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003262 case PP_IXDEFINE:
3263 casesense = false;
3264 /* fall through */
3265 case PP_DEFINE:
3266 case PP_XDEFINE:
3267 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003268 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003269 bool *eval_params = NULL;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003270
H. Peter Anvine2c80182005-01-15 22:15:51 +00003271 tline = tline->next;
3272 skip_white_(tline);
3273 tline = expand_id(tline);
3274 if (!tline || (tline->type != TOK_ID &&
3275 (tline->type != TOK_PREPROC_ID ||
3276 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003277 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003278 free_tlist(origline);
3279 return DIRECTIVE_FOUND;
3280 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003281
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003282 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003283 last = tline;
3284 param_start = tline = tline->next;
3285 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003286
H. Peter Anvine2c80182005-01-15 22:15:51 +00003287 if (tok_is_(tline, "(")) {
3288 /*
3289 * This macro has parameters.
3290 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003291
H. Peter Anvine2c80182005-01-15 22:15:51 +00003292 tline = tline->next;
3293 while (1) {
3294 skip_white_(tline);
3295 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003296 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3299 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003300 if (tok_is_(tline, "=")) {
3301 have_eval_params = true;
3302 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003303 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003304 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003305
3306 /*
3307 * "*" means an unnamed, ignored argument; it can never
3308 * match anything because "*" is not TOK_ID, and so
3309 * none of the expansion entries will be converted
3310 * to parameters.
3311 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003312 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003313 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3314 /*
3315 * Empty name; duplicate the termination
3316 * token and use the first copy as a placeholder.
3317 * It will never match anything, since the
3318 * associated text doesn't correspond to a TOK_ID.
3319 */
3320 tline = dup_Token(tline, tline);
3321 } else {
3322 nasm_nonfatal("`%s': parameter identifier expected",
3323 tline->text);
3324 free_tlist(origline);
3325 return DIRECTIVE_FOUND;
3326 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003327 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003328 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003329 tline = tline->next;
3330 skip_white_(tline);
3331 if (tok_is_(tline, ",")) {
3332 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003333 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003334 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003335 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003336 free_tlist(origline);
3337 return DIRECTIVE_FOUND;
3338 }
3339 break;
3340 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003341 }
3342 last = tline;
3343 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003344
3345 if (have_eval_params) {
3346 /* Create evaluated parameters table */
3347 bool is_eval = false;
3348
3349 nasm_newn(eval_params, nparam);
3350 list_for_each(tt, param_start) {
3351 if (is_smac_param(tt->type))
3352 eval_params[smac_nparam(tt->type)] = is_eval;
3353 is_eval = tok_is_(tt, "=");
3354 }
3355 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003356 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003357
H. Peter Anvine2c80182005-01-15 22:15:51 +00003358 if (tok_type_(tline, TOK_WHITESPACE))
3359 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003360 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003361
3362 /* Expand the macro definition now for %xdefine and %ixdefine */
3363 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3364 tline = expand_smacro(tline);
3365
3366 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003367 t = tline;
3368 while (t) {
3369 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003370 list_for_each(tt, param_start)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003371 if (is_smac_param(tt->type) &&
3372 tt->len == t->len &&
3373 !memcmp(tt->text, t->text, tt->len))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003374 t->type = tt->type;
3375 }
3376 tt = t->next;
3377 t->next = macro_start;
3378 macro_start = t;
3379 t = tt;
3380 }
3381 /*
3382 * Good. We now have a macro name, a parameter count, and a
3383 * token list (in reverse order) for an expansion. We ought
3384 * to be OK just to create an SMacro, store it, and let
3385 * free_tlist have the rest of the line (which we have
3386 * carefully re-terminated after chopping off the expansion
3387 * from the end).
3388 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003389 define_smacro(ctx, mname, casesense, nparam, eval_params, macro_start);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003390
H. Peter Anvine2c80182005-01-15 22:15:51 +00003391 free_tlist(origline);
3392 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003393 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003394
H. Peter Anvine2c80182005-01-15 22:15:51 +00003395 case PP_UNDEF:
3396 tline = tline->next;
3397 skip_white_(tline);
3398 tline = expand_id(tline);
3399 if (!tline || (tline->type != TOK_ID &&
3400 (tline->type != TOK_PREPROC_ID ||
3401 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003402 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 free_tlist(origline);
3404 return DIRECTIVE_FOUND;
3405 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003406 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003407 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003408
H. Peter Anvine2c80182005-01-15 22:15:51 +00003409 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003410 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003411 undef_smacro(ctx, mname);
3412 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003413 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003414
H. Peter Anvin9e200162008-06-04 17:23:14 -07003415 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003416 casesense = false;
3417 /* fall through */
3418 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003419 tline = tline->next;
3420 skip_white_(tline);
3421 tline = expand_id(tline);
3422 if (!tline || (tline->type != TOK_ID &&
3423 (tline->type != TOK_PREPROC_ID ||
3424 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003425 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003426 free_tlist(origline);
3427 return DIRECTIVE_FOUND;
3428 }
3429
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003430 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003431 last = tline;
3432 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003433 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003434
3435 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003436 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003437
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003438 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003439 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003440 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003441
3442 /*
3443 * We now have a macro name, an implicit parameter count of
3444 * zero, and a string token to use as an expansion. Create
3445 * and store an SMacro.
3446 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003447 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003448 free_tlist(origline);
3449 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003450
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003451 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003452 casesense = false;
3453 /* fall through */
3454 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003455 tline = tline->next;
3456 skip_white_(tline);
3457 tline = expand_id(tline);
3458 if (!tline || (tline->type != TOK_ID &&
3459 (tline->type != TOK_PREPROC_ID ||
3460 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003461 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003462 free_tlist(origline);
3463 return DIRECTIVE_FOUND;
3464 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003465 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003466 last = tline;
3467 tline = expand_smacro(tline->next);
3468 last->next = NULL;
3469
3470 t = tline;
3471 while (tok_type_(t, TOK_WHITESPACE))
3472 t = t->next;
3473 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003474 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003475 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003476 free_tlist(tline);
3477 free_tlist(origline);
3478 return DIRECTIVE_FOUND;
3479 }
3480
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003481 /*
3482 * Convert the string to a token stream. Note that smacros
3483 * are stored with the token stream reversed, so we have to
3484 * reverse the output of tokenize().
3485 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003486 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003487 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003488
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003489 /*
3490 * We now have a macro name, an implicit parameter count of
3491 * zero, and a numeric token to use as an expansion. Create
3492 * and store an SMacro.
3493 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003494 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003495 free_tlist(tline);
3496 free_tlist(origline);
3497 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003498
H. Peter Anvin8b262472019-02-26 14:00:54 -08003499 case PP_IPATHSEARCH:
3500 casesense = false;
3501 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003502 case PP_PATHSEARCH:
3503 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003504 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003505
H. Peter Anvin418ca702008-05-30 10:42:30 -07003506 tline = tline->next;
3507 skip_white_(tline);
3508 tline = expand_id(tline);
3509 if (!tline || (tline->type != TOK_ID &&
3510 (tline->type != TOK_PREPROC_ID ||
3511 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003512 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003513 free_tlist(origline);
3514 return DIRECTIVE_FOUND;
3515 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003516 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003517 last = tline;
3518 tline = expand_smacro(tline->next);
3519 last->next = NULL;
3520
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003521 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003522 while (tok_type_(t, TOK_WHITESPACE))
3523 t = t->next;
3524
3525 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003526 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003527 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003528 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003529 free_tlist(origline);
3530 return DIRECTIVE_FOUND; /* but we did _something_ */
3531 }
3532 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003533 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003534 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003535 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003536 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003537
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003538 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003539 if (!found_path)
3540 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003541 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003542
3543 /*
3544 * We now have a macro name, an implicit parameter count of
3545 * zero, and a string token to use as an expansion. Create
3546 * and store an SMacro.
3547 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003548 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003550 free_tlist(origline);
3551 return DIRECTIVE_FOUND;
3552 }
3553
H. Peter Anvin8b262472019-02-26 14:00:54 -08003554 case PP_ISTRLEN:
3555 casesense = false;
3556 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003557 case PP_STRLEN:
3558 tline = tline->next;
3559 skip_white_(tline);
3560 tline = expand_id(tline);
3561 if (!tline || (tline->type != TOK_ID &&
3562 (tline->type != TOK_PREPROC_ID ||
3563 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003564 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003565 free_tlist(origline);
3566 return DIRECTIVE_FOUND;
3567 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003568 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003569 last = tline;
3570 tline = expand_smacro(tline->next);
3571 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003572
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 t = tline;
3574 while (tok_type_(t, TOK_WHITESPACE))
3575 t = t->next;
3576 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003577 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003578 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 free_tlist(tline);
3580 free_tlist(origline);
3581 return DIRECTIVE_FOUND;
3582 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003583
H. Peter Anvin8b262472019-02-26 14:00:54 -08003584 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003585
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 /*
3587 * We now have a macro name, an implicit parameter count of
3588 * zero, and a numeric token to use as an expansion. Create
3589 * and store an SMacro.
3590 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003591 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 free_tlist(tline);
3593 free_tlist(origline);
3594 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003595
H. Peter Anvin8b262472019-02-26 14:00:54 -08003596 case PP_ISTRCAT:
3597 casesense = false;
3598 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003599 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003600 tline = tline->next;
3601 skip_white_(tline);
3602 tline = expand_id(tline);
3603 if (!tline || (tline->type != TOK_ID &&
3604 (tline->type != TOK_PREPROC_ID ||
3605 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003606 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003607 free_tlist(origline);
3608 return DIRECTIVE_FOUND;
3609 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003610 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003611 last = tline;
3612 tline = expand_smacro(tline->next);
3613 last->next = NULL;
3614
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003615 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003616 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003617 switch (t->type) {
3618 case TOK_WHITESPACE:
3619 break;
3620 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003621 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003622 break;
3623 case TOK_OTHER:
3624 if (!strcmp(t->text, ",")) /* permit comma separators */
3625 break;
3626 /* else fall through */
3627 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003628 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003629 free_tlist(tline);
3630 free_tlist(origline);
3631 return DIRECTIVE_FOUND;
3632 }
3633 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003634
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003636 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003637 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003638 memcpy(p, t->text, t->len);
3639 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003640 }
3641 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003642
3643 /*
3644 * We now have a macro name, an implicit parameter count of
3645 * zero, and a numeric token to use as an expansion. Create
3646 * and store an SMacro.
3647 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003648 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003649 nasm_free(pp);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003650 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003651 free_tlist(tline);
3652 free_tlist(origline);
3653 return DIRECTIVE_FOUND;
3654
H. Peter Anvin8b262472019-02-26 14:00:54 -08003655 case PP_ISUBSTR:
3656 casesense = false;
3657 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003659 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003660 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003661 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003662
H. Peter Anvine2c80182005-01-15 22:15:51 +00003663 tline = tline->next;
3664 skip_white_(tline);
3665 tline = expand_id(tline);
3666 if (!tline || (tline->type != TOK_ID &&
3667 (tline->type != TOK_PREPROC_ID ||
3668 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003669 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003670 free_tlist(origline);
3671 return DIRECTIVE_FOUND;
3672 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003673 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 last = tline;
3675 tline = expand_smacro(tline->next);
3676 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003677
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003678 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003679 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003680 while (tok_type_(t, TOK_WHITESPACE))
3681 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003682
H. Peter Anvine2c80182005-01-15 22:15:51 +00003683 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003684 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003685 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 free_tlist(tline);
3687 free_tlist(origline);
3688 return DIRECTIVE_FOUND;
3689 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003690
H. Peter Anvin8b262472019-02-26 14:00:54 -08003691 pps.tptr = t->next;
3692 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003693 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003694 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 if (!evalresult) {
3696 free_tlist(tline);
3697 free_tlist(origline);
3698 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003699 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003700 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003701 free_tlist(tline);
3702 free_tlist(origline);
3703 return DIRECTIVE_FOUND;
3704 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003705 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003706
H. Peter Anvin8b262472019-02-26 14:00:54 -08003707 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3708 pps.tptr = pps.tptr->next;
3709 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003710 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003711 } else {
3712 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003713 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003714 if (!evalresult) {
3715 free_tlist(tline);
3716 free_tlist(origline);
3717 return DIRECTIVE_FOUND;
3718 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003719 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003720 free_tlist(tline);
3721 free_tlist(origline);
3722 return DIRECTIVE_FOUND;
3723 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003724 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003725 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003726
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003727 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003728
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003729 /* make start and count being in range */
3730 if (start < 0)
3731 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003732 if (count < 0)
3733 count = len + count + 1 - start;
3734 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003735 count = len - start;
3736 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003737 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003738
H. Peter Anvin8b262472019-02-26 14:00:54 -08003739 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003740 macro_start->len = count;
3741 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3742 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003743
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 /*
3745 * We now have a macro name, an implicit parameter count of
3746 * zero, and a numeric token to use as an expansion. Create
3747 * and store an SMacro.
3748 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003749 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003750 free_tlist(tline);
3751 free_tlist(origline);
3752 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003753 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003754
H. Peter Anvine2c80182005-01-15 22:15:51 +00003755 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003756 casesense = false;
3757 /* fall through */
3758 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003759 tline = tline->next;
3760 skip_white_(tline);
3761 tline = expand_id(tline);
3762 if (!tline || (tline->type != TOK_ID &&
3763 (tline->type != TOK_PREPROC_ID ||
3764 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003765 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003766 free_tlist(origline);
3767 return DIRECTIVE_FOUND;
3768 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003769 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003770 last = tline;
3771 tline = expand_smacro(tline->next);
3772 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003773
H. Peter Anvin8b262472019-02-26 14:00:54 -08003774 pps.tptr = tline;
3775 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003776 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003777 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003778 free_tlist(tline);
3779 if (!evalresult) {
3780 free_tlist(origline);
3781 return DIRECTIVE_FOUND;
3782 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003783
H. Peter Anvine2c80182005-01-15 22:15:51 +00003784 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003785 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003786
H. Peter Anvine2c80182005-01-15 22:15:51 +00003787 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003788 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 free_tlist(origline);
3790 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003791 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003792
H. Peter Anvin8b262472019-02-26 14:00:54 -08003793 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003794
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 /*
3796 * We now have a macro name, an implicit parameter count of
3797 * zero, and a numeric token to use as an expansion. Create
3798 * and store an SMacro.
3799 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003800 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003801 free_tlist(origline);
3802 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003803
H. Peter Anvine2c80182005-01-15 22:15:51 +00003804 case PP_LINE:
3805 /*
3806 * Syntax is `%line nnn[+mmm] [filename]'
3807 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003808 if (unlikely(pp_noline)) {
3809 free_tlist(origline);
3810 return DIRECTIVE_FOUND;
3811 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003812 tline = tline->next;
3813 skip_white_(tline);
3814 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003815 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003816 free_tlist(origline);
3817 return DIRECTIVE_FOUND;
3818 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003819 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003820 m = 1;
3821 tline = tline->next;
3822 if (tok_is_(tline, "+")) {
3823 tline = tline->next;
3824 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003825 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003826 free_tlist(origline);
3827 return DIRECTIVE_FOUND;
3828 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003829 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003830 tline = tline->next;
3831 }
3832 skip_white_(tline);
3833 src_set_linnum(k);
3834 istk->lineinc = m;
3835 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003836 char *fname = detoken(tline, false);
3837 src_set_fname(fname);
3838 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003839 }
3840 free_tlist(origline);
3841 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003842
H. Peter Anvine2c80182005-01-15 22:15:51 +00003843 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003844 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003845 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003846 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003847}
3848
3849/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003850 * Ensure that a macro parameter contains a condition code and
3851 * nothing else. Return the condition code index if so, or -1
3852 * otherwise.
3853 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003854static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003855{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003856 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003857
H. Peter Anvin25a99342007-09-22 17:45:45 -07003858 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003859 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003860
H. Peter Anvineba20a72002-04-30 20:53:55 +00003861 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003862 if (!t)
3863 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003864 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003865 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003866 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003867 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003868 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003869 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003870
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003871 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003872}
3873
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003874/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003875 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003876 * pasting, if @handle_explicit passed then explicit pasting
3877 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003878 * The @m array can contain a series of token types which are
3879 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003880 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003881static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003882 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003883{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003884 Token *tok, *next, **prev_next, **prev_nonspace;
3885 bool pasted = false;
3886 char *buf, *p;
3887 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003888
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003889 /*
3890 * The last token before pasting. We need it
3891 * to be able to connect new handled tokens.
3892 * In other words if there were a tokens stream
3893 *
3894 * A -> B -> C -> D
3895 *
3896 * and we've joined tokens B and C, the resulting
3897 * stream should be
3898 *
3899 * A -> BC -> D
3900 */
3901 tok = *head;
3902 prev_next = NULL;
3903
3904 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3905 prev_nonspace = head;
3906 else
3907 prev_nonspace = NULL;
3908
3909 while (tok && (next = tok->next)) {
3910
3911 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003912 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003913 /* Zap redundant whitespaces */
3914 while (tok_type_(next, TOK_WHITESPACE))
3915 next = delete_Token(next);
3916 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003917 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003918
3919 case TOK_PASTE:
3920 /* Explicit pasting */
3921 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003922 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003923 next = delete_Token(tok);
3924
3925 while (tok_type_(next, TOK_WHITESPACE))
3926 next = delete_Token(next);
3927
3928 if (!pasted)
3929 pasted = true;
3930
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003931 /* Left pasting token is start of line */
3932 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003933 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003934
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003935 /*
3936 * No ending token, this might happen in two
3937 * cases
3938 *
3939 * 1) There indeed no right token at all
3940 * 2) There is a bare "%define ID" statement,
3941 * and @ID does expand to whitespace.
3942 *
3943 * So technically we need to do a grammar analysis
3944 * in another stage of parsing, but for now lets don't
3945 * change the behaviour people used to. Simply allow
3946 * whitespace after paste token.
3947 */
3948 if (!next) {
3949 /*
3950 * Zap ending space tokens and that's all.
3951 */
3952 tok = (*prev_nonspace)->next;
3953 while (tok_type_(tok, TOK_WHITESPACE))
3954 tok = delete_Token(tok);
3955 tok = *prev_nonspace;
3956 tok->next = NULL;
3957 break;
3958 }
3959
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003960 tok = *prev_nonspace;
3961 while (tok_type_(tok, TOK_WHITESPACE))
3962 tok = delete_Token(tok);
3963 len = strlen(tok->text);
3964 len += strlen(next->text);
3965
3966 p = buf = nasm_malloc(len + 1);
3967 strcpy(p, tok->text);
3968 p = strchr(p, '\0');
3969 strcpy(p, next->text);
3970
3971 delete_Token(tok);
3972
3973 tok = tokenize(buf);
3974 nasm_free(buf);
3975
3976 *prev_nonspace = tok;
3977 while (tok && tok->next)
3978 tok = tok->next;
3979
3980 tok->next = delete_Token(next);
3981
3982 /* Restart from pasted tokens head */
3983 tok = *prev_nonspace;
3984 break;
3985
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003986 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003987 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003988 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003989 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3990 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003991
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003992 len = 0;
3993 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3994 len += strlen(next->text);
3995 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003996 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003997
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003998 /* No match or no text to process */
3999 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004000 break;
4001
4002 len += strlen(tok->text);
4003 p = buf = nasm_malloc(len + 1);
4004
Adam Majer1a069432017-07-25 11:12:35 +02004005 strcpy(p, tok->text);
4006 p = strchr(p, '\0');
4007 tok = delete_Token(tok);
4008
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004009 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004010 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4011 strcpy(p, tok->text);
4012 p = strchr(p, '\0');
4013 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004014 tok = delete_Token(tok);
4015 }
4016
4017 tok = tokenize(buf);
4018 nasm_free(buf);
4019
4020 if (prev_next)
4021 *prev_next = tok;
4022 else
4023 *head = tok;
4024
4025 /*
4026 * Connect pasted into original stream,
4027 * ie A -> new-tokens -> B
4028 */
4029 while (tok && tok->next)
4030 tok = tok->next;
4031 tok->next = next;
4032
4033 if (!pasted)
4034 pasted = true;
4035
4036 /* Restart from pasted tokens head */
4037 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004038 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004039
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004040 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004041 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004042
4043 prev_next = &tok->next;
4044
4045 if (tok->next &&
4046 !tok_type_(tok->next, TOK_WHITESPACE) &&
4047 !tok_type_(tok->next, TOK_PASTE))
4048 prev_nonspace = prev_next;
4049
4050 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004051 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004052
4053 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004054}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004055
4056/*
4057 * expands to a list of tokens from %{x:y}
4058 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004059static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004060{
4061 Token *t = tline, **tt, *tm, *head;
4062 char *pos;
4063 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004064
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004065 pos = strchr(tline->text, ':');
4066 nasm_assert(pos);
4067
4068 lst = atoi(pos + 1);
4069 fst = atoi(tline->text + 1);
4070
4071 /*
4072 * only macros params are accounted so
4073 * if someone passes %0 -- we reject such
4074 * value(s)
4075 */
4076 if (lst == 0 || fst == 0)
4077 goto err;
4078
4079 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004080 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4081 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004082 goto err;
4083
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004084 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4085 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004086
4087 /* counted from zero */
4088 fst--, lst--;
4089
4090 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004091 * It will be at least one token. Note we
4092 * need to scan params until separator, otherwise
4093 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004094 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004095 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004096 if (!tm)
4097 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004098 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004099 tt = &head->next, tm = tm->next;
4100 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004101 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004102 *tt = t, tt = &t->next, tm = tm->next;
4103 }
4104
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004105 if (fst < lst) {
4106 for (i = fst + 1; i <= lst; i++) {
4107 t = new_Token(NULL, TOK_OTHER, ",", 0);
4108 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004109 j = (i + mac->rotate) % mac->nparam;
4110 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004111 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004112 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004113 *tt = t, tt = &t->next, tm = tm->next;
4114 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004115 }
4116 } else {
4117 for (i = fst - 1; i >= lst; i--) {
4118 t = new_Token(NULL, TOK_OTHER, ",", 0);
4119 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004120 j = (i + mac->rotate) % mac->nparam;
4121 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004122 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004123 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004124 *tt = t, tt = &t->next, tm = tm->next;
4125 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004126 }
4127 }
4128
4129 *last = tt;
4130 return head;
4131
4132err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004133 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004134 &tline->text[1]);
4135 return tline;
4136}
4137
H. Peter Anvin76690a12002-04-30 20:52:49 +00004138/*
4139 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004140 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004141 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004142 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004143static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004144{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004145 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004146 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004147 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004148
4149 tail = &thead;
4150 thead = NULL;
4151
H. Peter Anvine2c80182005-01-15 22:15:51 +00004152 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004153 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004154 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4155 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4156 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004157 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004158 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004159 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004160 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004161 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004162 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004163
H. Peter Anvine2c80182005-01-15 22:15:51 +00004164 t = tline;
4165 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004166
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004167 mac = istk->mstk;
4168 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4169 mac = mac->next_active;
4170 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004171 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004172 } else {
4173 pos = strchr(t->text, ':');
4174 if (!pos) {
4175 switch (t->text[1]) {
4176 /*
4177 * We have to make a substitution of one of the
4178 * forms %1, %-1, %+1, %%foo, %0.
4179 */
4180 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004181 type = TOK_NUMBER;
4182 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4183 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004184 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004185 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004186 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004187 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004188 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004189 text = nasm_strcat(tmpbuf, t->text + 2);
4190 break;
4191 case '-':
4192 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004193 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004194 tt = NULL;
4195 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004196 if (mac->nparam > 1)
4197 n = (n + mac->rotate) % mac->nparam;
4198 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004199 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004200 cc = find_cc(tt);
4201 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004202 nasm_nonfatal("macro parameter %d is not a condition code",
4203 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204 text = NULL;
4205 } else {
4206 type = TOK_ID;
4207 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004208 nasm_nonfatal("condition code `%s' is not invertible",
4209 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004210 text = NULL;
4211 } else
4212 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4213 }
4214 break;
4215 case '+':
4216 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004217 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004218 tt = NULL;
4219 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004220 if (mac->nparam > 1)
4221 n = (n + mac->rotate) % mac->nparam;
4222 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004223 }
4224 cc = find_cc(tt);
4225 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004226 nasm_nonfatal("macro parameter %d is not a condition code",
4227 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004228 text = NULL;
4229 } else {
4230 type = TOK_ID;
4231 text = nasm_strdup(conditions[cc]);
4232 }
4233 break;
4234 default:
4235 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004236 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004237 tt = NULL;
4238 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004239 if (mac->nparam > 1)
4240 n = (n + mac->rotate) % mac->nparam;
4241 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004242 }
4243 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004244 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004245 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004246 tail = &(*tail)->next;
4247 tt = tt->next;
4248 }
4249 }
4250 text = NULL; /* we've done it here */
4251 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004252 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004253 } else {
4254 /*
4255 * seems we have a parameters range here
4256 */
4257 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004258 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004259 if (head != t) {
4260 *tail = head;
4261 *last = tline;
4262 tline = head;
4263 text = NULL;
4264 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004265 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004266 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004267 if (!text) {
4268 delete_Token(t);
4269 } else {
4270 *tail = t;
4271 tail = &t->next;
4272 t->type = type;
4273 nasm_free(t->text);
4274 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004275 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004276 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004277 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004278 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004279 } else if (tline->type == TOK_INDIRECT) {
4280 t = tline;
4281 tline = tline->next;
4282 tt = tokenize(t->text);
4283 tt = expand_mmac_params(tt);
4284 tt = expand_smacro(tt);
4285 *tail = tt;
4286 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004287 tail = &tt->next;
4288 tt = tt->next;
4289 }
4290 delete_Token(t);
4291 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004292 } else {
4293 t = *tail = tline;
4294 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004295 tail = &t->next;
4296 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004297 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004298 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004299
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004300 if (changed) {
4301 const struct tokseq_match t[] = {
4302 {
4303 PP_CONCAT_MASK(TOK_ID) |
4304 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4305 PP_CONCAT_MASK(TOK_ID) |
4306 PP_CONCAT_MASK(TOK_NUMBER) |
4307 PP_CONCAT_MASK(TOK_FLOAT) |
4308 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4309 },
4310 {
4311 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4312 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4313 }
4314 };
4315 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4316 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004317
H. Peter Anvin76690a12002-04-30 20:52:49 +00004318 return thead;
4319}
4320
4321/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004322 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004323 * a macro at all, it is simply copied to the output and the pointer
4324 * advanced. tpp should be a pointer to a pointer (usually the next
4325 * pointer of the previous token) to the first token. **tpp is updated
4326 * to point to the last token of the expansion, and *tpp updated to
4327 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004328 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004329 * If the expansion is empty, *tpp will be unchanged but **tpp will
4330 * be advanced past the macro call.
4331 *
4332 * The return value equals **tpp.
4333 *
4334 * Return false if no expansion took place; true if it did.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004335 *
4336 */
4337static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004338static int64_t smacro_deadman;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004339
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004340static bool expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004341{
4342 Token **params = NULL;
4343 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004344 Token *tline = **tpp;
4345 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004346 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004347 unsigned int i;
4348 Token *t, *tup, *ttail;
4349 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004350
4351 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004352 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004353
4354 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004355
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004356 if (--smacro_deadman <= 0) {
4357 if (smacro_deadman == 0)
4358 nasm_nonfatal("interminable macro recursion");
4359 goto not_a_macro;
4360 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004361 head = (SMacro *)hash_findix(&smacros, mname);
4362 } else if (tline->type == TOK_PREPROC_ID) {
4363 Context *ctx = get_ctx(mname, &mname);
4364 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4365 } else {
4366 goto not_a_macro;
4367 }
4368
4369 /*
4370 * We've hit an identifier of some sort. First check whether the
4371 * identifier is a single-line macro at all, then think about
4372 * checking for parameters if necessary.
4373 */
4374 list_for_each(m, head) {
4375 if (!mstrcmp(m->name, mname, m->casesense))
4376 break;
4377 }
4378
4379 if (!m) {
4380 goto not_a_macro;
4381 }
4382
4383 /* Parse parameters, if applicable */
4384
4385 params = NULL;
4386 nparam = 0;
4387
4388 if (m->nparam == 0) {
4389 /*
4390 * Simple case: the macro is parameterless.
4391 * Nothing to parse; the expansion code will
4392 * drop the macro name token.
4393 */
4394 } else {
4395 /*
4396 * Complicated case: at least one macro with this name
4397 * exists and takes parameters. We must find the
4398 * parameters in the call, count them, find the SMacro
4399 * that corresponds to that form of the macro call, and
4400 * substitute for the parameters when we expand. What a
4401 * pain.
4402 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004403 Token **phead, **pep;
4404 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004405 int white = 0;
4406 int brackets = 0;
4407 bool bracketed = false;
4408 bool bad_bracket = false;
4409 unsigned int sparam = PARAM_DELTA;
4410
4411 tline = tline->next;
4412
4413 while (tok_type_(tline, TOK_WHITESPACE)) {
4414 tline = tline->next;
4415 }
4416 if (!tok_is_(tline, "(")) {
4417 /*
4418 * This macro wasn't called with parameters: ignore
4419 * the call. (Behaviour borrowed from gnu cpp.)
4420 */
4421 goto not_a_macro;
4422 }
4423
4424 paren = 1;
4425 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004426 nasm_newn(params, sparam);
4427 phead = pep = &params[0];
4428 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004429
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004430 while (paren) {
4431 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004432 char ch;
4433
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004434 tline = tline->next;
4435
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004436 if (!tline) {
4437 nasm_nonfatal("macro call expects terminating `)'");
4438 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004439 }
4440
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004441 ch = 0;
4442 skip = false;
4443
4444 switch (tline->type) {
4445 case TOK_OTHER:
4446 if (!tline->text[1])
4447 ch = tline->text[0];
4448 break;
4449
4450 case TOK_WHITESPACE:
4451 if (brackets || *phead)
4452 white++; /* Keep interior whitespace */
4453 skip = true;
4454 break;
4455
4456 default:
4457 break;
4458 }
4459
4460 switch (ch) {
4461 case ',':
4462 if (!brackets) {
4463 if (++nparam >= sparam) {
4464 sparam += PARAM_DELTA;
4465 params = nasm_realloc(params, sparam * sizeof *params);
4466 }
4467 pep = &params[nparam];
4468 *pep = NULL;
4469 bracketed = false;
4470 skip = true;
4471 }
4472 break;
4473
4474 case '{':
4475 if (!bracketed) {
4476 bracketed = !*phead;
4477 skip = true;
4478 }
4479 brackets++;
4480 break;
4481
4482 case '}':
4483 if (brackets > 0) {
4484 if (!--brackets)
4485 skip = bracketed;
4486 }
4487 break;
4488
4489 case '(':
4490 if (!brackets)
4491 paren++;
4492 break;
4493
4494 case ')':
4495 if (!brackets) {
4496 paren--;
4497 if (!paren) {
4498 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004499 /* Count the final argument */
4500 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004501 }
4502 }
4503 break;
4504
4505 default:
4506 break; /* Normal token */
4507 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004508
4509 if (!skip) {
4510 Token *t;
4511
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004512 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004513
4514 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004515 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004516 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004517 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004518 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004519 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004520 pep = &t->next;
4521 white = 0;
4522 }
4523 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004524
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004525 /*
4526 * Look for a macro matching in both name and parameter count.
4527 * We already know any matches cannot be anywhere before the
4528 * current position of "m", so there is no reason to
4529 * backtrack.
4530 */
4531 while (1) {
4532 if (!m) {
4533 /*!
4534 *!macro-params-single [on] single-line macro calls with wrong parameter count
4535 *! warns about \i{single-line macros} being invoked
4536 *! with the wrong number of parameters.
4537 */
4538 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4539 "single-line macro `%s' exists, "
4540 "but not taking %d parameter%s",
4541 mname, nparam, (nparam == 1) ? "" : "s");
4542 goto not_a_macro;
4543 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004544
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004545 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4546 break; /* It's good */
4547
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004548 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004549 }
4550 }
4551
4552 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004553 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004554
4555 /* Expand each parameter */
4556 m->in_progress = true;
4557
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004558 if (m->eval_param) {
4559 for (i = 0; i < nparam; i++) {
4560 if (m->eval_param[i]) {
4561 /* Evaluate this parameter as a number */
4562 struct ppscan pps;
4563 struct tokenval tokval;
4564 expr *evalresult;
4565 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004566
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004567 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4568 pps.ntokens = -1;
4569 tokval.t_type = TOKEN_INVALID;
4570 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004571
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004572 free_tlist(eval_param);
4573 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004574
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004575 if (!evalresult) {
4576 /* Nothing meaningful to do */
4577 } else if (tokval.t_type) {
4578 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4579 } else if (!is_simple(evalresult)) {
4580 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4581 } else {
4582 params[i] = make_tok_num(reloc_value(evalresult));
4583 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004584 }
4585 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004586 }
4587
4588 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004589 tline = tline->next; /* Remove the macro call from the input */
4590 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004591
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004592 /* Note: we own the expansion this returns. */
4593 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004594
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004595 tup = NULL;
4596 ttail = NULL; /* Pointer to the last token of the expansion */
4597 while (t) {
4598 enum pp_token_type type = t->type;
4599 Token *tnext = t->next;
4600 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004601
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004602 switch (type) {
4603 case TOK_PREPROC_Q:
4604 case TOK_PREPROC_QQ:
4605 t->type = TOK_ID;
4606 nasm_free(t->text);
4607 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4608 t->len = nasm_last_string_len();
4609 t->next = tline;
4610 break;
4611
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004612 case TOK_ID:
4613 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004614 /*
4615 * Chain this into the target line *before* expanding,
4616 * that way we pick up any arguments to the new macro call,
4617 * if applicable.
4618 */
4619 t->next = tline;
4620 tp = &t;
4621 expand_one_smacro(&tp);
4622 if (t == tline)
4623 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004624 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004625
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004626 default:
4627 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004628 unsigned int param = smac_nparam(t->type);
4629 nasm_assert(!tup && param < nparam);
4630 delete_Token(t);
4631 t = NULL;
4632 tup = tnext;
4633 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004634 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004635 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004636 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004637 }
4638
4639 if (t) {
4640 tline = t;
4641 if (!ttail)
4642 ttail = t;
4643 }
4644
4645 if (tnext) {
4646 t = tnext;
4647 } else {
4648 t = tup;
4649 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004650 }
4651 }
4652
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004653 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004654 if (ttail)
4655 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004656
4657 m->in_progress = false;
4658
4659 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004660 free_tlist(mstart);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004661 free_tlist_array(params, nparam);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004662 return true;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004663
4664 /*
4665 * No macro expansion needed; roll back to mstart (if necessary)
4666 * and then advance to the next input token.
4667 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004668not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004669 *tpp = &mstart->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004670 free_tlist_array(params, nparam);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004671 return false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004672}
4673
4674/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004675 * Expand all single-line macro calls made in the given line.
4676 * Return the expanded version of the line. The original is deemed
4677 * to be destroyed in the process. (In reality we'll just move
4678 * Tokens from input to output a lot of the time, rather than
4679 * actually bothering to destroy and replicate.)
4680 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004681static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004682{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004683 smacro_deadman = nasm_limit[LIMIT_MACROS];
4684 return expand_smacro_noreset(tline);
4685}
4686
4687static Token *expand_smacro_noreset(Token * tline)
4688{
4689 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004690 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004691 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004692
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004693 /*
4694 * Trick: we should avoid changing the start token pointer since it can
4695 * be contained in "next" field of other token. Because of this
4696 * we allocate a copy of first token and work with it; at the end of
4697 * routine we copy it back
4698 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004699 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004700 tline = new_Token(org_tline->next, org_tline->type,
4701 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004702 nasm_free(org_tline->text);
4703 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004704 }
4705
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004706
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004707 /*
4708 * Pretend that we always end up doing expansion on the first pass;
4709 * that way %+ get processed. However, if we process %+ before the
4710 * first pass, we end up with things like MACRO %+ TAIL trying to
4711 * look up the macro "MACROTAIL", which we don't want.
4712 */
4713 expanded = true;
4714 thead = tline;
4715 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004716 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004717 {
4718 PP_CONCAT_MASK(TOK_ID) |
4719 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4720 PP_CONCAT_MASK(TOK_ID) |
4721 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4722 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4723 }
4724 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004725
4726 tail = &thead;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004727 while ((t = *tail)) { /* main token loop */
4728
4729 expanded |= expand_one_smacro(&tail);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004730 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004731
4732 if (!expanded) {
4733 tline = thead;
4734 break; /* Done! */
4735 }
4736
4737 /*
4738 * Now scan the entire line and look for successive TOK_IDs
4739 * that resulted after expansion (they can't be produced by
4740 * tokenize()). The successive TOK_IDs should be concatenated.
4741 * Also we look for %+ tokens and concatenate the tokens
4742 * before and after them (without white spaces in between).
4743 */
4744 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004745
4746 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004747 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004748
H. Peter Anvine2c80182005-01-15 22:15:51 +00004749 if (org_tline) {
4750 if (thead) {
4751 *org_tline = *thead;
4752 /* since we just gave text to org_line, don't free it */
4753 thead->text = NULL;
4754 delete_Token(thead);
4755 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004756 /*
4757 * The expression expanded to empty line;
4758 * we can't return NULL because of the "trick" above.
4759 * Just set the line to a single WHITESPACE token.
4760 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004761 memset(org_tline, 0, sizeof(*org_tline));
4762 org_tline->text = NULL;
4763 org_tline->type = TOK_WHITESPACE;
4764 }
4765 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004766 }
4767
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004768 return thead;
4769}
4770
4771/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004772 * Similar to expand_smacro but used exclusively with macro identifiers
4773 * right before they are fetched in. The reason is that there can be
4774 * identifiers consisting of several subparts. We consider that if there
4775 * are more than one element forming the name, user wants a expansion,
4776 * otherwise it will be left as-is. Example:
4777 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004778 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004779 *
4780 * the identifier %$abc will be left as-is so that the handler for %define
4781 * will suck it and define the corresponding value. Other case:
4782 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004783 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004784 *
4785 * In this case user wants name to be expanded *before* %define starts
4786 * working, so we'll expand %$abc into something (if it has a value;
4787 * otherwise it will be left as-is) then concatenate all successive
4788 * PP_IDs into one.
4789 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004790static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004791{
4792 Token *cur, *oldnext = NULL;
4793
H. Peter Anvin734b1882002-04-30 21:01:08 +00004794 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004795 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004796
4797 cur = tline;
4798 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004799 (cur->next->type == TOK_ID ||
4800 cur->next->type == TOK_PREPROC_ID
4801 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004802 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004803
4804 /* If identifier consists of just one token, don't expand */
4805 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004806 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004807
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 if (cur) {
4809 oldnext = cur->next; /* Detach the tail past identifier */
4810 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004811 }
4812
H. Peter Anvin734b1882002-04-30 21:01:08 +00004813 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004814
H. Peter Anvine2c80182005-01-15 22:15:51 +00004815 if (cur) {
4816 /* expand_smacro possibly changhed tline; re-scan for EOL */
4817 cur = tline;
4818 while (cur && cur->next)
4819 cur = cur->next;
4820 if (cur)
4821 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004822 }
4823
4824 return tline;
4825}
4826
4827/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004828 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004829 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004830 * to check for an initial label - that's taken care of in
4831 * expand_mmacro - but must check numbers of parameters. Guaranteed
4832 * to be called with tline->type == TOK_ID, so the putative macro
4833 * name is easy to find.
4834 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004835static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004836{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004837 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004838 Token **params;
4839 int nparam;
4840
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004841 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004842
4843 /*
4844 * Efficiency: first we see if any macro exists with the given
4845 * name. If not, we can return NULL immediately. _Then_ we
4846 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004847 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004848 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004849 list_for_each(m, head)
4850 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004851 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004852 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004853 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854
4855 /*
4856 * OK, we have a potential macro. Count and demarcate the
4857 * parameters.
4858 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004859 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004860
4861 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004862 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004863 * structure that handles this number.
4864 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004865 while (m) {
4866 if (m->nparam_min <= nparam
4867 && (m->plus || nparam <= m->nparam_max)) {
4868 /*
4869 * This one is right. Just check if cycle removal
4870 * prohibits us using it before we actually celebrate...
4871 */
4872 if (m->in_progress > m->max_depth) {
4873 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004874 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004875 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004876 }
4877 nasm_free(params);
4878 return NULL;
4879 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004880 /*
4881 * It's right, and we can use it. Add its default
4882 * parameters to the end of our list if necessary.
4883 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004884 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004885 params =
4886 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004887 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004888 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004889 while (nparam < m->nparam_min + m->ndefs) {
4890 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004891 nparam++;
4892 }
4893 }
4894 /*
4895 * If we've gone over the maximum parameter count (and
4896 * we're in Plus mode), ignore parameters beyond
4897 * nparam_max.
4898 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004899 if (m->plus && nparam > m->nparam_max)
4900 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004901 /*
4902 * Then terminate the parameter list, and leave.
4903 */
4904 if (!params) { /* need this special case */
4905 params = nasm_malloc(sizeof(*params));
4906 nparam = 0;
4907 }
4908 params[nparam] = NULL;
4909 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004910 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 }
4912 /*
4913 * This one wasn't right: look for the next one with the
4914 * same name.
4915 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004916 list_for_each(m, m->next)
4917 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004918 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004919 }
4920
4921 /*
4922 * After all that, we didn't find one with the right number of
4923 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004924 *!
4925 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4926 *! warns about \i{multi-line macros} being invoked
4927 *! with the wrong number of parameters. See \k{mlmacover} for an
4928 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004929 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004930 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4931 "multi-line macro `%s' exists, but not taking %d parameter%s",
4932 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004933 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004934 return NULL;
4935}
4936
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004937
4938/*
4939 * Save MMacro invocation specific fields in
4940 * preparation for a recursive macro expansion
4941 */
4942static void push_mmacro(MMacro *m)
4943{
4944 MMacroInvocation *i;
4945
4946 i = nasm_malloc(sizeof(MMacroInvocation));
4947 i->prev = m->prev;
4948 i->params = m->params;
4949 i->iline = m->iline;
4950 i->nparam = m->nparam;
4951 i->rotate = m->rotate;
4952 i->paramlen = m->paramlen;
4953 i->unique = m->unique;
4954 i->condcnt = m->condcnt;
4955 m->prev = i;
4956}
4957
4958
4959/*
4960 * Restore MMacro invocation specific fields that were
4961 * saved during a previous recursive macro expansion
4962 */
4963static void pop_mmacro(MMacro *m)
4964{
4965 MMacroInvocation *i;
4966
4967 if (m->prev) {
4968 i = m->prev;
4969 m->prev = i->prev;
4970 m->params = i->params;
4971 m->iline = i->iline;
4972 m->nparam = i->nparam;
4973 m->rotate = i->rotate;
4974 m->paramlen = i->paramlen;
4975 m->unique = i->unique;
4976 m->condcnt = i->condcnt;
4977 nasm_free(i);
4978 }
4979}
4980
4981
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004982/*
4983 * Expand the multi-line macro call made by the given line, if
4984 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004985 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004986 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004987static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004988{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004989 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004990 Token *label = NULL;
4991 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004992 Token **params, *t, *tt;
4993 MMacro *m;
4994 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004995 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004996 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004997
4998 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004999 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07005000 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00005001 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005002 return 0;
5003 m = is_mmacro(t, &params);
5004 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03005005 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005006 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005007 Token *last;
5008 /*
5009 * We have an id which isn't a macro call. We'll assume
5010 * it might be a label; we'll also check to see if a
5011 * colon follows it. Then, if there's another id after
5012 * that lot, we'll check it again for macro-hood.
5013 */
5014 label = last = t;
5015 t = t->next;
5016 if (tok_type_(t, TOK_WHITESPACE))
5017 last = t, t = t->next;
5018 if (tok_is_(t, ":")) {
5019 dont_prepend = 1;
5020 last = t, t = t->next;
5021 if (tok_type_(t, TOK_WHITESPACE))
5022 last = t, t = t->next;
5023 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005024 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
5025 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005026 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005027 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005028 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005029 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005030
5031 /*
5032 * Fix up the parameters: this involves stripping leading and
5033 * trailing whitespace, then stripping braces if they are
5034 * present.
5035 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005036 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005037 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005038
H. Peter Anvine2c80182005-01-15 22:15:51 +00005039 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005040 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005041 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005042
H. Peter Anvine2c80182005-01-15 22:15:51 +00005043 t = params[i];
5044 skip_white_(t);
5045 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005046 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005047 params[i] = t;
5048 paramlen[i] = 0;
5049 while (t) {
5050 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5051 break; /* ... because we have hit a comma */
5052 if (comma && t->type == TOK_WHITESPACE
5053 && tok_is_(t->next, ","))
5054 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005055 if (brace && t->type == TOK_OTHER) {
5056 if (t->text[0] == '{')
5057 brace++; /* ... or a nested opening brace */
5058 else if (t->text[0] == '}')
5059 if (!--brace)
5060 break; /* ... or a brace */
5061 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005062 t = t->next;
5063 paramlen[i]++;
5064 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005065 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005066 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005067 }
5068
5069 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005070 * OK, we have a MMacro structure together with a set of
5071 * parameters. We must now go through the expansion and push
5072 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005073 * parameter tokens and macro-local tokens doesn't get done
5074 * until the single-line macro substitution process; this is
5075 * because delaying them allows us to change the semantics
5076 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005077 *
5078 * First, push an end marker on to istk->expansion, mark this
5079 * macro as in progress, and set up its invocation-specific
5080 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005081 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005082 ll = nasm_malloc(sizeof(Line));
5083 ll->next = istk->expansion;
5084 ll->finishes = m;
5085 ll->first = NULL;
5086 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005087
5088 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005089 * Save the previous MMacro expansion in the case of
5090 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005091 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005092 if (m->max_depth && m->in_progress)
5093 push_mmacro(m);
5094
5095 m->in_progress ++;
5096 m->params = params;
5097 m->iline = tline;
5098 m->nparam = nparam;
5099 m->rotate = 0;
5100 m->paramlen = paramlen;
5101 m->unique = unique++;
5102 m->lineno = 0;
5103 m->condcnt = 0;
5104
5105 m->next_active = istk->mstk;
5106 istk->mstk = m;
5107
5108 list_for_each(l, m->expansion) {
5109 Token **tail;
5110
5111 ll = nasm_malloc(sizeof(Line));
5112 ll->finishes = NULL;
5113 ll->next = istk->expansion;
5114 istk->expansion = ll;
5115 tail = &ll->first;
5116
5117 list_for_each(t, l->first) {
5118 Token *x = t;
5119 switch (t->type) {
5120 case TOK_PREPROC_Q:
5121 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005122 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005123 case TOK_PREPROC_QQ:
5124 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
5125 break;
5126 case TOK_PREPROC_ID:
5127 if (t->text[1] == '0' && t->text[2] == '0') {
5128 dont_prepend = -1;
5129 x = label;
5130 if (!x)
5131 continue;
5132 }
5133 /* fall through */
5134 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005135 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005136 break;
5137 }
5138 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005139 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005140 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005141 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005142
5143 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005144 * If we had a label, push it on as the first line of
5145 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005146 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005147 if (label) {
5148 if (dont_prepend < 0)
5149 free_tlist(startline);
5150 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005151 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005152 ll->finishes = NULL;
5153 ll->next = istk->expansion;
5154 istk->expansion = ll;
5155 ll->first = startline;
5156 if (!dont_prepend) {
5157 while (label->next)
5158 label = label->next;
5159 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005160 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005161 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005162 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005163
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005164 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005165
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005166 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005167}
5168
H. Peter Anvin130736c2016-02-17 20:27:41 -08005169/*
5170 * This function adds macro names to error messages, and suppresses
5171 * them if necessary.
5172 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005173static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005174{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005175 /*
5176 * If we're in a dead branch of IF or something like it, ignore the error.
5177 * However, because %else etc are evaluated in the state context
5178 * of the previous branch, errors might get lost:
5179 * %if 0 ... %else trailing garbage ... %endif
5180 * So %else etc should set the ERR_PP_PRECOND flag.
5181 */
5182 if ((severity & ERR_MASK) < ERR_FATAL &&
5183 istk && istk->conds &&
5184 ((severity & ERR_PP_PRECOND) ?
5185 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005186 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005187 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005188
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005189 /* This doesn't make sense with the macro stack unwinding */
5190 if (0) {
5191 MMacro *mmac = NULL;
5192 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005193
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005194 /* get %macro name */
5195 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
5196 mmac = istk->mstk;
5197 /* but %rep blocks should be skipped */
5198 while (mmac && !mmac->name)
5199 mmac = mmac->next_active, delta++;
5200 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005201
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005202 if (mmac) {
5203 char *buf;
5204 nasm_set_verror(real_verror);
5205 buf = nasm_vasprintf(fmt, arg);
5206 nasm_error(severity, "(%s:%"PRId32") %s",
5207 mmac->name, mmac->lineno - delta, buf);
5208 nasm_set_verror(pp_verror);
5209 nasm_free(buf);
5210 return;
5211 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005212 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005213 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005214}
5215
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005216static Token *
5217stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005218{
5219 (void)s;
5220 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005221 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005222
5223 return make_tok_qstr(src_get_fname());
5224}
5225
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005226static Token *
5227stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005228{
5229 (void)s;
5230 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005231 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005232
5233 return make_tok_num(src_get_linnum());
5234}
5235
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005236static Token *
5237stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005238{
5239 (void)s;
5240 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005241 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005242
5243 return make_tok_num(globalbits);
5244}
5245
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005246static Token *
5247stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005248{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005249 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005250 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5251 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5252 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005253
5254 (void)s;
5255 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005256 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005257
5258 switch (globalbits) {
5259 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005260 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005261 break;
5262 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005263 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005264 break;
5265 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005266 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005267 break;
5268 default:
5269 panic();
5270 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005271
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005272 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005273}
5274
H. Peter Anvin8b262472019-02-26 14:00:54 -08005275/* Add magic standard macros */
5276struct magic_macros {
5277 const char *name;
5278 int nparams;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005279 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005280};
5281static const struct magic_macros magic_macros[] =
5282{
5283 { "__FILE__", 0, stdmac_file },
5284 { "__LINE__", 0, stdmac_line },
5285 { "__BITS__", 0, stdmac_bits },
5286 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005287 { NULL, 0, NULL }
5288};
5289
5290static void pp_add_magic_stdmac(void)
5291{
5292 const struct magic_macros *m;
5293 SMacro *s;
5294
5295 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005296 s = define_smacro(NULL, m->name, true, m->nparams, NULL, NULL);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005297 s->expand = m->func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005298 }
5299}
5300
H. Peter Anvin734b1882002-04-30 21:01:08 +00005301static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005302pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005303{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005304 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005305
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005306 cstk = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005307 nasm_new(istk);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07005308 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin274cda82016-05-10 02:56:29 -07005309 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005310 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005311 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03005312 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005313 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005314 nested_mac_count = 0;
5315 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005316 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005317 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005318 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005319 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005320
H. Peter Anvin8b262472019-02-26 14:00:54 -08005321 pp_add_magic_stdmac();
5322
H. Peter Anvinf7606612016-07-13 14:23:48 -07005323 if (tasm_compatible_mode)
5324 pp_add_stdmac(nasm_stdmac_tasm);
5325
5326 pp_add_stdmac(nasm_stdmac_nasm);
5327 pp_add_stdmac(nasm_stdmac_version);
5328
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005329 if (extrastdmac)
5330 pp_add_stdmac(extrastdmac);
5331
H. Peter Anvinf7606612016-07-13 14:23:48 -07005332 stdmacpos = stdmacros[0];
5333 stdmacnext = &stdmacros[1];
5334
H. Peter Anvind2456592008-06-19 15:04:18 -07005335 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005336
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03005337 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005338
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005339 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005340 * Define the __PASS__ macro. This is defined here unlike all the
5341 * other builtins, because it is special -- it varies between
5342 * passes -- but there is really no particular reason to make it
5343 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005344 *
5345 * 0 = dependencies only
5346 * 1 = preparatory passes
5347 * 2 = final pass
5348 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005349 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005350 switch (mode) {
5351 case PP_NORMAL:
5352 apass = pass_final() ? 2 : 1;
5353 break;
5354 case PP_DEPS:
5355 apass = 0;
5356 break;
5357 case PP_PREPROC:
5358 apass = 3;
5359 break;
5360 default:
5361 panic();
5362 }
5363
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005364 define_smacro(NULL, "__PASS__", true, 0, NULL, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005365}
5366
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005367static void pp_init(void)
5368{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005369}
5370
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005371/*
5372 * Get a line of tokens. If we popped the macro expansion/include stack,
5373 * we return a pointer to the dummy token tok_pop; at that point if
5374 * istk is NULL then we have reached end of input;
5375 */
5376static Token tok_pop; /* Dummy token placeholder */
5377
5378static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005379{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005380 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005381
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005382 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005383 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005384 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005385 * buffer or from the input file.
5386 */
5387 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005388 while (istk->expansion && istk->expansion->finishes) {
5389 Line *l = istk->expansion;
5390 if (!l->finishes->name && l->finishes->in_progress > 1) {
5391 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005392
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005393 /*
5394 * This is a macro-end marker for a macro with no
5395 * name, which means it's not really a macro at all
5396 * but a %rep block, and the `in_progress' field is
5397 * more than 1, meaning that we still need to
5398 * repeat. (1 means the natural last repetition; 0
5399 * means termination by %exitrep.) We have
5400 * therefore expanded up to the %endrep, and must
5401 * push the whole block on to the expansion buffer
5402 * again. We don't bother to remove the macro-end
5403 * marker: we'd only have to generate another one
5404 * if we did.
5405 */
5406 l->finishes->in_progress--;
5407 list_for_each(l, l->finishes->expansion) {
5408 Token *t, *tt, **tail;
5409
5410 ll = nasm_malloc(sizeof(Line));
5411 ll->next = istk->expansion;
5412 ll->finishes = NULL;
5413 ll->first = NULL;
5414 tail = &ll->first;
5415
5416 list_for_each(t, l->first) {
5417 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005418 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005419 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005420 }
5421 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005422 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005423 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005424 } else {
5425 /*
5426 * Check whether a `%rep' was started and not ended
5427 * within this macro expansion. This can happen and
5428 * should be detected. It's a fatal error because
5429 * I'm too confused to work out how to recover
5430 * sensibly from it.
5431 */
5432 if (defining) {
5433 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005434 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005435 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005436 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005437 " expansion of macro `%s'",
5438 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005439 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005440
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005441 /*
5442 * FIXME: investigate the relationship at this point between
5443 * istk->mstk and l->finishes
5444 */
5445 {
5446 MMacro *m = istk->mstk;
5447 istk->mstk = m->next_active;
5448 if (m->name) {
5449 /*
5450 * This was a real macro call, not a %rep, and
5451 * therefore the parameter information needs to
5452 * be freed.
5453 */
5454 if (m->prev) {
5455 pop_mmacro(m);
5456 l->finishes->in_progress --;
5457 } else {
5458 nasm_free(m->params);
5459 free_tlist(m->iline);
5460 nasm_free(m->paramlen);
5461 l->finishes->in_progress = 0;
5462 }
Adam Majer91e72402017-07-25 10:42:01 +02005463 }
5464
5465 /*
5466 * FIXME It is incorrect to always free_mmacro here.
5467 * It leads to usage-after-free.
5468 *
5469 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5470 */
5471#if 0
5472 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005473 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005474#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005475 }
5476 istk->expansion = l->next;
5477 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005478 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005479 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005480 }
5481 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005482 do { /* until we get a line we can use */
5483 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005484
5485 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005486 Line *l = istk->expansion;
5487 if (istk->mstk)
5488 istk->mstk->lineno++;
5489 tline = l->first;
5490 istk->expansion = l->next;
5491 nasm_free(l);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005492 line = detoken(tline, false);
5493 lfmt->line(LIST_MACRO, line);
5494 nasm_free(line);
5495 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005496 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005497 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005498 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005499 } else {
5500 /*
5501 * The current file has ended; work down the istk
5502 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005503 Include *i = istk;
5504 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005505 if (i->conds) {
5506 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005507 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005508 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005509 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005510 if (i->next)
5511 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005512 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005513 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005514 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005515 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005516 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005517 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005518
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005519 /*
5520 * We must expand MMacro parameters and MMacro-local labels
5521 * _before_ we plunge into directive processing, to cope
5522 * with things like `%define something %1' such as STRUC
5523 * uses. Unless we're _defining_ a MMacro, in which case
5524 * those tokens should be left alone to go into the
5525 * definition; and unless we're in a non-emitting
5526 * condition, in which case we don't want to meddle with
5527 * anything.
5528 */
5529 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5530 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005531 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005532 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005533
H. Peter Anvine2c80182005-01-15 22:15:51 +00005534 /*
5535 * Check the line to see if it's a preprocessor directive.
5536 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005537 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5538 if (dtline)
5539 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005540 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005541 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005542 * We're defining a multi-line macro. We emit nothing
5543 * at all, and just
5544 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005545 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005546 Line *l = nasm_malloc(sizeof(Line));
5547 l->next = defining->expansion;
5548 l->first = tline;
5549 l->finishes = NULL;
5550 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005551 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005552 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005553 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005554 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005555 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005556 * directive so we keep our place correctly.
5557 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005558 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005559 } else if (istk->mstk && !istk->mstk->in_progress) {
5560 /*
5561 * We're in a %rep block which has been terminated, so
5562 * we're walking through to the %endrep without
5563 * emitting anything. Emit nothing at all, not even a
5564 * blank line: when we emerge from the %rep block we'll
5565 * give a line-number directive so we keep our place
5566 * correctly.
5567 */
5568 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005569 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005570 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005571 if (!expand_mmacro(tline))
5572 return tline;
5573 }
5574 }
5575}
5576
5577static char *pp_getline(void)
5578{
5579 char *line = NULL;
5580 Token *tline;
5581
5582 real_verror = nasm_set_verror(pp_verror);
5583
5584 while (true) {
5585 tline = pp_tokline();
5586 if (tline == &tok_pop) {
5587 /*
5588 * We popped the macro/include stack. If istk is empty,
5589 * we are at end of input, otherwise just loop back.
5590 */
5591 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005592 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005593 } else {
5594 /*
5595 * De-tokenize the line and emit it.
5596 */
5597 line = detoken(tline, true);
5598 free_tlist(tline);
5599 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005600 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005601 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005602
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005603 if (list_option('e') && line && line[0]) {
5604 char *buf = nasm_strcat(" ;;; ", line);
5605 lfmt->line(LIST_MACRO, buf);
5606 nasm_free(buf);
5607 }
5608
H. Peter Anvin130736c2016-02-17 20:27:41 -08005609 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005610 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005611}
5612
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005613static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005614{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005615 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005616
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005617 if (defining) {
5618 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005619 nasm_nonfatal("end of file while still defining macro `%s'",
5620 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005621 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005622 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005623 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005624
5625 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005626 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005627 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005628
5629 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005630
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005631 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005632 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005633 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005634 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005635 Include *i = istk;
5636 istk = istk->next;
5637 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005638 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005639 }
5640 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005641 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005642 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005643}
5644
5645static void pp_cleanup_session(void)
5646{
5647 free_llist(predef);
5648 predef = NULL;
5649 delete_Blocks();
5650 freeTokens = NULL;
5651 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005652}
5653
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005654static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005655{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005656 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005657}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005658
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005659static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005660{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005661 Token *inc, *space, *name;
5662 Line *l;
5663
H. Peter Anvin734b1882002-04-30 21:01:08 +00005664 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5665 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5666 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005667
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005668 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005669 l->next = predef;
5670 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005671 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005672 predef = l;
5673}
5674
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005675static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005676{
5677 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005678 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005679 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005680
H. Peter Anvin130736c2016-02-17 20:27:41 -08005681 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005682
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005683 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005684 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5685 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005686 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005687 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005688 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005689 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005690 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005691
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005692 if (space->next->type != TOK_PREPROC_ID &&
5693 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005694 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005695
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005696 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005697 l->next = predef;
5698 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005699 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005700 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005701
5702 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005703}
5704
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005705static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005706{
5707 Token *def, *space;
5708 Line *l;
5709
H. Peter Anvin734b1882002-04-30 21:01:08 +00005710 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5711 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005712 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005713
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005714 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005715 l->next = predef;
5716 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005717 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005718 predef = l;
5719}
5720
H. Peter Anvin05990342018-06-11 13:32:42 -07005721/* Insert an early preprocessor command that doesn't need special handling */
5722static void pp_pre_command(const char *what, char *string)
5723{
5724 char *cmd;
5725 Token *def, *space;
5726 Line *l;
5727
5728 def = tokenize(string);
5729 if (what) {
5730 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5731 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5732 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5733 }
5734
5735 l = nasm_malloc(sizeof(Line));
5736 l->next = predef;
5737 l->first = def;
5738 l->finishes = NULL;
5739 predef = l;
5740}
5741
H. Peter Anvinf7606612016-07-13 14:23:48 -07005742static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005743{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005744 macros_t **mp;
5745
5746 /* Find the end of the list and avoid duplicates */
5747 for (mp = stdmacros; *mp; mp++) {
5748 if (*mp == macros)
5749 return; /* Nothing to do */
5750 }
5751
5752 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5753
5754 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005755}
5756
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005757static void pp_extra_stdmac(macros_t *macros)
5758{
5759 extrastdmac = macros;
5760}
5761
H. Peter Anvin8b262472019-02-26 14:00:54 -08005762static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005763{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005764 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005765 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5766 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5767}
5768
5769static Token *make_tok_qstr(const char *str)
5770{
5771 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005772 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005773 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005774}
5775
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005776static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005777{
5778 if (!m)
5779 return;
5780
5781 /* We need to print the next_active list in reverse order */
5782 pp_list_one_macro(m->next_active, severity);
5783
5784 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005785 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005786 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005787 }
5788}
5789
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005790static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005791{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005792 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005793
H. Peter Anvinddb29062018-12-11 00:06:29 -08005794 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5795 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005796
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005797 if (istk)
5798 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005799
H. Peter Anvinddb29062018-12-11 00:06:29 -08005800 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005801}
5802
H. Peter Anvine7469712016-02-18 02:20:59 -08005803const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005804 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005805 pp_reset,
5806 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005807 pp_cleanup_pass,
5808 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005809 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005810 pp_pre_define,
5811 pp_pre_undefine,
5812 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005813 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005814 pp_include_path,
5815 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005816};