blob: 031764022e4806b6e9160722a5f9f033dd6dffc2 [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 Anvinab6f8312019-08-09 22:31:45 -0700862 int32_t lineno;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000863
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400864 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400865 p = line_from_stdmac();
866 if (p)
867 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700868
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400869 size = delta;
870 p = buffer = nasm_malloc(size);
871
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700872 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400873 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400874
875 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700876 case EOF:
877 if (p == buffer) {
878 nasm_free(buffer);
879 return NULL;
880 }
881 c = 0;
882 break;
883
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400884 case '\r':
885 next = fgetc(istk->fp);
886 if (next != '\n')
887 ungetc(next, istk->fp);
888 if (cont) {
889 cont = false;
890 continue;
891 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700892 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400893 break;
894
895 case '\n':
896 if (cont) {
897 cont = false;
898 continue;
899 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700900 c = 0;
901 break;
902
903 case 032: /* ^Z = legacy MS-DOS end of file mark */
904 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400905 break;
906
907 case '\\':
908 next = fgetc(istk->fp);
909 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400910 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400911 cont = true;
912 nr_cont++;
913 continue;
914 }
915 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000916 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400917
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400918 if (p >= (buffer + size - pad)) {
919 buffer = nasm_realloc(buffer, size + delta);
920 p = buffer + size - pad;
921 size += delta;
922 }
923
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700924 *p++ = c;
925 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000926
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700927 lineno = src_get_linnum() + istk->lineinc +
928 (nr_cont * istk->lineinc);
929 src_set_linnum(lineno);
930 lfmt->line(LIST_READ, lineno, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000931
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000932 return buffer;
933}
934
935/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000936 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000937 * don't need to parse the value out of e.g. numeric tokens: we
938 * simply split one string into many.
939 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000940static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000941{
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700942 char c;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000943 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000944 Token *list = NULL;
945 Token *t, **tail = &list;
946
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 while (*line) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700948 char *p = line;
949 char *ep = NULL; /* End of token, for trimming the end */
950
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 if (*p == '%') {
952 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300953 if (*p == '+' && !nasm_isdigit(p[1])) {
954 p++;
955 type = TOK_PASTE;
956 } else if (nasm_isdigit(*p) ||
957 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 do {
959 p++;
960 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700961 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 type = TOK_PREPROC_ID;
963 } else if (*p == '{') {
964 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800965 while (*p) {
966 if (*p == '}')
967 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 p[-1] = *p;
969 p++;
970 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800971 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800972 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -0700973 ep = &p[-1];
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 if (*p)
975 p++;
976 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300977 } else if (*p == '[') {
978 int lvl = 1;
979 line += 2; /* Skip the leading %[ */
980 p++;
981 while (lvl && (c = *p++)) {
982 switch (c) {
983 case ']':
984 lvl--;
985 break;
986 case '%':
987 if (*p == '[')
988 lvl++;
989 break;
990 case '\'':
991 case '\"':
992 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300993 p = nasm_skip_string(p - 1);
994 if (*p)
995 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300996 break;
997 default:
998 break;
999 }
1000 }
1001 p--;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001002 ep = p;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001003 if (*p)
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001004 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001005 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001006 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001007 type = TOK_INDIRECT;
1008 } else if (*p == '?') {
1009 type = TOK_PREPROC_Q; /* %? */
1010 p++;
1011 if (*p == '?') {
1012 type = TOK_PREPROC_QQ; /* %?? */
1013 p++;
1014 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001015 } else if (*p == '!') {
1016 type = TOK_PREPROC_ID;
1017 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001018 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001019 do {
1020 p++;
1021 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001022 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001023 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001024 p = nasm_skip_string(p);
1025 if (*p)
1026 p++;
1027 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001028 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001029 } else {
1030 /* %! without string or identifier */
1031 type = TOK_OTHER; /* Legacy behavior... */
1032 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001033 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001035 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 do {
1037 p++;
1038 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001039 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 type = TOK_PREPROC_ID;
1041 } else {
1042 type = TOK_OTHER;
1043 if (*p == '%')
1044 p++;
1045 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001046 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 type = TOK_ID;
1048 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001049 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001051 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 /*
1053 * A string token.
1054 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001056 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001057
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 if (*p) {
1059 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001060 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001061 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 /* Handling unterminated strings by UNV */
1063 /* type = -1; */
1064 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001065 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001066 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001067 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001068 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001069 bool is_hex = false;
1070 bool is_float = false;
1071 bool has_e = false;
1072 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001075 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001077
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001078 if (*p == '$') {
1079 p++;
1080 is_hex = true;
1081 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001082
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001083 for (;;) {
1084 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001085
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001086 if (!is_hex && (c == 'e' || c == 'E')) {
1087 has_e = true;
1088 if (*p == '+' || *p == '-') {
1089 /*
1090 * e can only be followed by +/- if it is either a
1091 * prefixed hex number or a floating-point number
1092 */
1093 p++;
1094 is_float = true;
1095 }
1096 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1097 is_hex = true;
1098 } else if (c == 'P' || c == 'p') {
1099 is_float = true;
1100 if (*p == '+' || *p == '-')
1101 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001102 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001103 ; /* just advance */
1104 else if (c == '.') {
1105 /*
1106 * we need to deal with consequences of the legacy
1107 * parser, like "1.nolist" being two tokens
1108 * (TOK_NUMBER, TOK_ID) here; at least give it
1109 * a shot for now. In the future, we probably need
1110 * a flex-based scanner with proper pattern matching
1111 * to do it as well as it can be done. Nothing in
1112 * the world is going to help the person who wants
1113 * 0x123.p16 interpreted as two tokens, though.
1114 */
1115 r = p;
1116 while (*r == '_')
1117 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001118
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001119 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1120 (!is_hex && (*r == 'e' || *r == 'E')) ||
1121 (*r == 'p' || *r == 'P')) {
1122 p = r;
1123 is_float = true;
1124 } else
1125 break; /* Terminate the token */
1126 } else
1127 break;
1128 }
1129 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001130
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001131 if (p == line+1 && *line == '$') {
1132 type = TOK_OTHER; /* TOKEN_HERE */
1133 } else {
1134 if (has_e && !is_hex) {
1135 /* 1e13 is floating-point, but 1e13h is not */
1136 is_float = true;
1137 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001138
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001139 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1140 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001141 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001143 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 /*
1145 * Whitespace just before end-of-line is discarded by
1146 * pretending it's a comment; whitespace just before a
1147 * comment gets lumped into the comment.
1148 */
1149 if (!*p || *p == ';') {
1150 type = TOK_COMMENT;
1151 while (*p)
1152 p++;
1153 }
1154 } else if (*p == ';') {
1155 type = TOK_COMMENT;
1156 while (*p)
1157 p++;
1158 } else {
1159 /*
1160 * Anything else is an operator of some kind. We check
1161 * for all the double-character operators (>>, <<, //,
1162 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001163 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 */
1165 type = TOK_OTHER;
1166 if ((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[0] == '|' && p[1] == '|') ||
1176 (p[0] == '^' && p[1] == '^')) {
1177 p++;
1178 }
1179 p++;
1180 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001181
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 /* Handling unterminated string by UNV */
1183 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001184 {
1185 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1186 t->text[p-line] = *line;
1187 tail = &t->next;
1188 }
1189 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 if (type != TOK_COMMENT) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001191 if (!ep)
1192 ep = p;
1193 *tail = t = new_Token(NULL, type, line, ep - line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 tail = &t->next;
1195 }
1196 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001197 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001198 return list;
1199}
1200
H. Peter Anvince616072002-04-30 21:02:23 +00001201/*
1202 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001203 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001204 * deleted only all at once by the delete_Blocks function.
1205 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001207{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001208 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001210 /* first, get to the end of the linked list */
1211 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001213 /* now allocate the requested chunk */
1214 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001216 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001217 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001218 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001219}
1220
1221/*
1222 * this function deletes all managed blocks of memory
1223 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001225{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001227
H. Peter Anvin70653092007-10-19 14:42:29 -07001228 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001229 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001230 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001231 * free it.
1232 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001234 if (b->chunk)
1235 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 a = b;
1237 b = b->next;
1238 if (a != &blocks)
1239 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001240 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001241 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001243
1244/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001245 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001246 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001247 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001248static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001249 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001250{
1251 Token *t;
1252 int i;
1253
H. Peter Anvin89cee572009-07-15 09:16:54 -04001254 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1256 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1257 freeTokens[i].next = &freeTokens[i + 1];
1258 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001259 }
1260 t = freeTokens;
1261 freeTokens = t->next;
1262 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001263 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001264 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001265 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 t->text = NULL;
1267 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001268 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001269 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001270 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001271 t->text = nasm_malloc(txtlen+1);
1272 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001274 }
1275 return t;
1276}
1277
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001278static Token *dup_Token(Token *next, const Token *src)
1279{
1280 return new_Token(next, src->type, src->text, src->len);
1281}
1282
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001284{
1285 Token *next = t->next;
1286 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001287 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001288 freeTokens = t;
1289 return next;
1290}
1291
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001292/*
1293 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001294 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1295 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001296 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001297static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001298{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001299 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001300 char *line, *p;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001301 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001302
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001303 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001304 if (t->type == TOK_PREPROC_ID && t->text &&
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001305 t->text[0] == '%' && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001306 char *v;
1307 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001308
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001309 v = t->text + 2;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001310 if (nasm_isquote(*v))
1311 nasm_unquote_cstr(v, NULL);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001312
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001313 if (v) {
1314 char *p = getenv(v);
1315 if (!p) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001316 /*!
1317 *!environment [on] nonexistent environment variable
1318 *! warns if a nonexistent environment variable
1319 *! is accessed using the \c{%!} preprocessor
1320 *! construct (see \k{getenv}.) Such environment
1321 *! variables are treated as empty (with this
1322 *! warning issued) starting in NASM 2.15;
1323 *! earlier versions of NASM would treat this as
1324 *! an error.
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001325 */
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001326 nasm_warn(WARN_ENVIRONMENT, "nonexistent environment variable `%s'", v);
1327 p = "";
1328 }
1329 t->text = nasm_strdup(p);
1330 t->len = nasm_last_string_len();
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001331 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001332 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001334
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 /* Expand local macros here and not during preprocessing */
1336 if (expand_locals &&
1337 t->type == TOK_PREPROC_ID && t->text &&
1338 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001339 const char *q;
1340 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001341 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001342 if (ctx) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001343 p = nasm_asprintf("..@%"PRIu64".%s", ctx->number, q);
1344 t->len = nasm_last_string_len();
H. Peter Anvine2c80182005-01-15 22:15:51 +00001345 nasm_free(t->text);
1346 t->text = p;
1347 }
1348 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001349 if (t->text) {
1350 if (debug_level(2)) {
1351 unsigned long t_len = t->len;
1352 unsigned long s_len = strlen(t->text);
1353 if (t_len != s_len) {
1354 nasm_panic("assertion failed: token \"%s\" type %u len %lu has t->len %lu\n",
1355 t->text, t->type, s_len, t_len);
1356 t->len = s_len;
1357 }
1358 }
1359 len += t->len;
1360 } else if (t->type == TOK_WHITESPACE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 len++;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001362 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001363 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001364
H. Peter Anvin734b1882002-04-30 21:01:08 +00001365 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001366
1367 list_for_each(t, tlist) {
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001368 if (t->text) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07001369 memcpy(p, t->text, t->len);
1370 p += t->len;
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07001371 } else if (t->type == TOK_WHITESPACE) {
1372 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001374 }
1375 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001376
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001377 return line;
1378}
1379
1380/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001381 * A scanner, suitable for use by the expression evaluator, which
1382 * operates on a line of Tokens. Expects a pointer to a pointer to
1383 * the first token in the line to be passed in as its private_data
1384 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001385 *
1386 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001387 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001388struct ppscan {
1389 Token *tptr;
1390 int ntokens;
1391};
1392
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001394{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001395 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001396 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001397 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001398
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001400 if (pps->ntokens && (tline = pps->tptr)) {
1401 pps->ntokens--;
1402 pps->tptr = tline->next;
1403 } else {
1404 pps->tptr = NULL;
1405 pps->ntokens = 0;
1406 return tokval->t_type = TOKEN_EOS;
1407 }
1408 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001409
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001410 tokval->t_charptr = tline->text;
1411
H. Peter Anvin76690a12002-04-30 20:52:49 +00001412 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001413 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001414 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001415 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001416
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001418 p = tokval->t_charptr = tline->text;
1419 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 tokval->t_charptr++;
1421 return tokval->t_type = TOKEN_ID;
1422 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001423
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001424 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001425 if (r >= p+MAX_KEYWORD)
1426 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001427 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001428 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001429 *s = '\0';
1430 /* right, so we have an identifier sitting in temp storage. now,
1431 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001432 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001433 }
1434
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001436 bool rn_error;
1437 tokval->t_integer = readnum(tline->text, &rn_error);
1438 tokval->t_charptr = tline->text;
1439 if (rn_error)
1440 return tokval->t_type = TOKEN_ERRNUM;
1441 else
1442 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001443 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001444
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001445 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001446 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001447 }
1448
H. Peter Anvine2c80182005-01-15 22:15:51 +00001449 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001450 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001451
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001452 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001453 tokval->t_charptr = tline->text;
1454 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001455
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001456 if (ep[0] != bq || ep[1] != '\0')
1457 return tokval->t_type = TOKEN_ERRSTR;
1458 else
1459 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001460 }
1461
H. Peter Anvine2c80182005-01-15 22:15:51 +00001462 if (tline->type == TOK_OTHER) {
1463 if (!strcmp(tline->text, "<<"))
1464 return tokval->t_type = TOKEN_SHL;
1465 if (!strcmp(tline->text, ">>"))
1466 return tokval->t_type = TOKEN_SHR;
1467 if (!strcmp(tline->text, "//"))
1468 return tokval->t_type = TOKEN_SDIV;
1469 if (!strcmp(tline->text, "%%"))
1470 return tokval->t_type = TOKEN_SMOD;
1471 if (!strcmp(tline->text, "=="))
1472 return tokval->t_type = TOKEN_EQ;
1473 if (!strcmp(tline->text, "<>"))
1474 return tokval->t_type = TOKEN_NE;
1475 if (!strcmp(tline->text, "!="))
1476 return tokval->t_type = TOKEN_NE;
1477 if (!strcmp(tline->text, "<="))
1478 return tokval->t_type = TOKEN_LE;
1479 if (!strcmp(tline->text, ">="))
1480 return tokval->t_type = TOKEN_GE;
1481 if (!strcmp(tline->text, "&&"))
1482 return tokval->t_type = TOKEN_DBL_AND;
1483 if (!strcmp(tline->text, "^^"))
1484 return tokval->t_type = TOKEN_DBL_XOR;
1485 if (!strcmp(tline->text, "||"))
1486 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001487 }
1488
1489 /*
1490 * We have no other options: just return the first character of
1491 * the token text.
1492 */
1493 return tokval->t_type = tline->text[0];
1494}
1495
1496/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001497 * Compare a string to the name of an existing macro; this is a
1498 * simple wrapper which calls either strcmp or nasm_stricmp
1499 * depending on the value of the `casesense' parameter.
1500 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001501static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001502{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001503 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001504}
1505
1506/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001507 * Compare a string to the name of an existing macro; this is a
1508 * simple wrapper which calls either strcmp or nasm_stricmp
1509 * depending on the value of the `casesense' parameter.
1510 */
1511static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1512{
1513 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1514}
1515
1516/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001517 * Return the Context structure associated with a %$ token. Return
1518 * NULL, having _already_ reported an error condition, if the
1519 * context stack isn't deep enough for the supplied number of $
1520 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001521 *
1522 * If "namep" is non-NULL, set it to the pointer to the macro name
1523 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001524 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001525static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001526{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001527 Context *ctx;
1528 int i;
1529
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001530 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001531 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001532
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001533 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001535
H. Peter Anvine2c80182005-01-15 22:15:51 +00001536 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001537 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001538 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001539 }
1540
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001541 name += 2;
1542 ctx = cstk;
1543 i = 0;
1544 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001545 name++;
1546 i++;
1547 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001548 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001549 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001550 nasm_nonfatal("`%s': context stack is only"
1551 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001553 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001554
1555 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001556 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001557
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001558 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001559}
1560
1561/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001562 * Open an include file. This routine must always return a valid
1563 * file pointer if it returns - it's responsible for throwing an
1564 * ERR_FATAL and bombing out completely if not. It should also try
1565 * the include path one by one until it finds the file or reaches
1566 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001567 *
1568 * Note: for INC_PROBE the function returns NULL at all times;
1569 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001570 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001571enum incopen_mode {
1572 INC_NEEDED, /* File must exist */
1573 INC_OPTIONAL, /* Missing is OK */
1574 INC_PROBE /* Only an existence probe */
1575};
1576
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001577/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001578static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001579 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001580{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001581 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001582 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001583 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001584 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001585 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001586
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001588 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001589 if (omode == INC_PROBE) {
1590 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001591 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001592 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001593 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001594 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001595 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001596 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001597 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001599 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001600
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001601 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001602
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001603 if (!ip) {
1604 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001605 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001606 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001607
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001608 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001609 ip = ip->next;
1610 }
1611}
1612
1613/*
1614 * Open a file, or test for the presence of one (depending on omode),
1615 * considering the include path.
1616 */
1617static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001618 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001619 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001620 enum incopen_mode omode,
1621 enum file_flags fmode)
1622{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001623 struct hash_insert hi;
1624 void **hp;
1625 char *path;
1626 FILE *fp = NULL;
1627
1628 hp = hash_find(&FileHash, file, &hi);
1629 if (hp) {
1630 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001631 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001632 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001633 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001634 } else {
1635 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001636 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001637
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001638 /* Positive or negative result */
1639 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001640
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001641 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001642 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001643 */
1644 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001645 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001646 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001647
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001648 if (!path) {
1649 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001650 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001651 } else {
1652 if (!fp && omode != INC_PROBE)
1653 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001654 }
1655
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001656 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001657 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001658
1659 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001660}
1661
1662/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001663 * Opens an include or input file. Public version, for use by modules
1664 * that get a file:lineno pair and need to look at the file again
1665 * (e.g. the CodeView debug backend). Returns NULL on failure.
1666 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001667FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001668{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001669 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001670}
1671
1672/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001673 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001674 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001675 * return true if _any_ single-line macro of that name is defined.
1676 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001677 * `nparam' or no parameters is defined.
1678 *
1679 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001680 * defined, or nparam is -1, the address of the definition structure
1681 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001682 * is NULL, no action will be taken regarding its contents, and no
1683 * error will occur.
1684 *
1685 * Note that this is also called with nparam zero to resolve
1686 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001687 *
1688 * If you already know which context macro belongs to, you can pass
1689 * the context pointer as first parameter; if you won't but name begins
1690 * with %$ the context will be automatically computed. If all_contexts
1691 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001692 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001693static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001694smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001695 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001696{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001697 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001698 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001699
H. Peter Anvin97a23472007-09-16 17:57:25 -07001700 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001701 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001702 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001703 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001704 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001705 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001706 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001707 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001708 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001709 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001710 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001711 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001712
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 while (m) {
1714 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001715 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001716 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001717 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001718 *defn = m;
1719 else
1720 *defn = NULL;
1721 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001722 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 }
1724 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001725 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001726
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001727 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001728}
1729
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001730/* param should be a natural number [0; INT_MAX] */
1731static int read_param_count(const char *str)
1732{
1733 int result;
1734 bool err;
1735
1736 result = readnum(str, &err);
1737 if (result < 0 || result > INT_MAX) {
1738 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001739 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1740 str, 0, INT_MAX);
1741 } else if (err)
1742 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001743 return result;
1744}
1745
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001746/*
1747 * Count and mark off the parameters in a multi-line macro call.
1748 * This is called both from within the multi-line macro expansion
1749 * code, and also to mark off the default parameters when provided
1750 * in a %macro definition line.
1751 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001753{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001754 int paramsize, brace;
1755
1756 *nparam = paramsize = 0;
1757 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001759 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001760 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 paramsize += PARAM_DELTA;
1762 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1763 }
1764 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001765 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001767 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001769 if (brace) {
1770 while (brace && (t = t->next) != NULL) {
1771 if (tok_is_(t, "{"))
1772 brace++;
1773 else if (tok_is_(t, "}"))
1774 brace--;
1775 }
1776
1777 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 /*
1779 * Now we've found the closing brace, look further
1780 * for the comma.
1781 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001782 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 skip_white_(t);
1784 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001785 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 while (tok_isnt_(t, ","))
1787 t = t->next;
1788 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001790 } else {
1791 while (tok_isnt_(t, ","))
1792 t = t->next;
1793 }
1794 if (t) { /* got a comma/brace */
1795 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001797 }
1798}
1799
1800/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001801 * Determine whether one of the various `if' conditions is true or
1802 * not.
1803 *
1804 * We must free the tline we get passed.
1805 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001806static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001807{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001808 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001809 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001810 Token *t, *tt, *origline;
1811 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001812 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001813 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001814 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001815 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001816
1817 origline = tline;
1818
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001820 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001821 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001822 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001824 if (!tline)
1825 break;
1826 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001827 nasm_nonfatal("`%s' expects context identifiers",
1828 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 free_tlist(origline);
1830 return -1;
1831 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001832 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001833 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 tline = tline->next;
1835 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001836 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001837
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001838 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001839 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001840 while (tline) {
1841 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 if (!tline || (tline->type != TOK_ID &&
1843 (tline->type != TOK_PREPROC_ID ||
1844 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001845 nasm_nonfatal("`%s' expects macro identifiers",
1846 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001847 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001849 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001850 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001852 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001853 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001854
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001855 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001856 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001857 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001858 while (tline) {
1859 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001860 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001861 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001862 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001863 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001864 nasm_nonfatal("`%s' expects environment variable names",
1865 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001866 goto fail;
1867 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001868 p = tline->text;
1869 if (tline->type == TOK_PREPROC_ID)
1870 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001871 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001872 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001873 if (getenv(p))
1874 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001875 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001876 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001877 break;
1878
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001879 case PPC_IFIDN:
1880 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 tline = expand_smacro(tline);
1882 t = tt = tline;
1883 while (tok_isnt_(tt, ","))
1884 tt = tt->next;
1885 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001886 nasm_nonfatal("`%s' expects two comma-separated arguments",
1887 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001888 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889 }
1890 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001891 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1893 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001894 nasm_nonfatal("`%s': more than one comma on line",
1895 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001896 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 }
1898 if (t->type == TOK_WHITESPACE) {
1899 t = t->next;
1900 continue;
1901 }
1902 if (tt->type == TOK_WHITESPACE) {
1903 tt = tt->next;
1904 continue;
1905 }
1906 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001907 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001908 break;
1909 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001910 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 size_t l1 = nasm_unquote(t->text, NULL);
1913 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001914
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001915 if (l1 != l2) {
1916 j = false;
1917 break;
1918 }
1919 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1920 j = false;
1921 break;
1922 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001923 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001924 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 break;
1926 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001927
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 t = t->next;
1929 tt = tt->next;
1930 }
1931 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001932 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001933 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001934
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001935 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001936 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001937 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001938 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001939
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 skip_white_(tline);
1941 tline = expand_id(tline);
1942 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001943 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001944 goto fail;
1945 }
1946 searching.name = nasm_strdup(tline->text);
1947 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001948 searching.plus = false;
1949 searching.nolist = false;
1950 searching.in_progress = 0;
1951 searching.max_depth = 0;
1952 searching.rep_nest = NULL;
1953 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001954 searching.nparam_max = INT_MAX;
1955 tline = expand_smacro(tline->next);
1956 skip_white_(tline);
1957 if (!tline) {
1958 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001959 nasm_nonfatal("`%s' expects a parameter count or nothing",
1960 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001961 } else {
1962 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001963 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001964 }
1965 if (tline && tok_is_(tline->next, "-")) {
1966 tline = tline->next->next;
1967 if (tok_is_(tline, "*"))
1968 searching.nparam_max = INT_MAX;
1969 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001970 nasm_nonfatal("`%s' expects a parameter count after `-'",
1971 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001973 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001974 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001975 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001976 searching.nparam_max = searching.nparam_min;
1977 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 }
1979 }
1980 if (tline && tok_is_(tline->next, "+")) {
1981 tline = tline->next;
1982 searching.plus = true;
1983 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001984 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1985 while (mmac) {
1986 if (!strcmp(mmac->name, searching.name) &&
1987 (mmac->nparam_min <= searching.nparam_max
1988 || searching.plus)
1989 && (searching.nparam_min <= mmac->nparam_max
1990 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 found = true;
1992 break;
1993 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001994 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 }
1996 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001997 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001998 nasm_free(searching.name);
1999 j = found;
2000 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002001 }
H. Peter Anvin65747262002-05-07 00:10:05 +00002002
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002003 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002004 needtype = TOK_ID;
2005 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002006 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002007 needtype = TOK_NUMBER;
2008 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002009 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002010 needtype = TOK_STRING;
2011 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002012
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013iftype:
2014 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002015
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002016 while (tok_type_(t, TOK_WHITESPACE) ||
2017 (needtype == TOK_NUMBER &&
2018 tok_type_(t, TOK_OTHER) &&
2019 (t->text[0] == '-' || t->text[0] == '+') &&
2020 !t->text[1]))
2021 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002022
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002023 j = tok_type_(t, needtype);
2024 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002025
2026 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002027 t = tline = expand_smacro(tline);
2028 while (tok_type_(t, TOK_WHITESPACE))
2029 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002030
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002031 j = false;
2032 if (t) {
2033 t = t->next; /* Skip the actual token */
2034 while (tok_type_(t, TOK_WHITESPACE))
2035 t = t->next;
2036 j = !t; /* Should be nothing left */
2037 }
2038 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002039
H. Peter Anvin134b9462008-02-16 17:01:40 -08002040 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002041 t = tline = expand_smacro(tline);
2042 while (tok_type_(t, TOK_WHITESPACE))
2043 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002044
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002045 j = !t; /* Should be empty */
2046 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002047
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002048 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002049 pps.tptr = tline = expand_smacro(tline);
2050 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002051 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002052 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002053 if (!evalresult)
2054 return -1;
2055 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002056 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002057 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002058 nasm_nonfatal("non-constant value given to `%s'",
2059 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002060 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002062 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002063 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002064
H. Peter Anvine2c80182005-01-15 22:15:51 +00002065 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002066 nasm_fatal("preprocessor directive `%s' not yet implemented",
2067 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002068 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002069 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002070
2071 free_tlist(origline);
2072 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002073
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002074fail:
2075 free_tlist(origline);
2076 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002077}
2078
2079/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002080 * Default smacro expansion routine: just returns a copy of the
2081 * expansion list.
2082 */
2083static Token *
2084smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2085{
2086 (void)params;
2087 (void)nparams;
2088
2089 return dup_tlist(s->expansion, NULL);
2090}
2091
2092/*
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002093 * Emit a macro defintion or undef to the listing file, if
2094 * desired. This is similar to detoken(), but it handles the reverse
2095 * expansion list, does not expand %! or local variable tokens, and
2096 * does some special handling for macro parameters.
2097 */
2098static void
2099list_smacro_def(enum preproc_token op, const Context *ctx, const SMacro *m)
2100{
2101 static const Token unused_arg_name = { NULL, "", 0, TOK_OTHER };
2102 const Token **param_names;
2103 Token *junk_names = NULL;
2104 Token *t;
2105 size_t namelen, size;
2106 unsigned int i;
2107 char *def, *p;
2108 unsigned int context_depth = 0;
2109
2110 nasm_newn(param_names, m->nparam);
2111
2112 namelen = strlen(m->name);
2113 size = namelen + 2; /* Include room for space after name + NUL */
2114
2115 if (ctx) {
2116 context_depth = cstk->depth - ctx->depth + 1;
2117 size += context_depth + 1; /* % + some number of $ */
2118 }
2119
2120 list_for_each(t, m->expansion) {
2121 if (!t->text) {
2122 size++; /* Whitespace, presumably */
2123 } else {
2124 size += t->len;
2125 if (is_smac_param(t->type))
2126 param_names[smac_nparam(t->type)] = t;
2127 }
2128 }
2129
2130 if (m->nparam) {
2131 /*
2132 * Space for ( and either , or ) around each
2133 * parameter, plus an optional =.
2134 */
2135 size += 1 + 2 * m->nparam;
2136 for (i = 0; i < m->nparam; i++) {
2137 if (!param_names[i])
2138 param_names[i] = &unused_arg_name;
2139 size += param_names[i]->len;
2140 }
2141 }
2142
2143 def = nasm_malloc(size);
2144 p = def+size;
2145 *--p = '\0';
2146
2147 list_for_each(t, m->expansion) {
2148 if (!t->text) {
2149 *--p = ' ';
2150 } else {
2151 p -= t->len;
2152 memcpy(p, t->text, t->len);
2153 }
2154 }
2155
2156 *--p = ' ';
2157
2158 if (m->nparam) {
2159 *--p = ')';
2160 for (i = m->nparam; i--;) {
2161 p -= param_names[i]->len;
2162 memcpy(p, param_names[i]->text, param_names[i]->len);
2163 if (m->eval_param && m->eval_param[i])
2164 *--p = '=';
2165 *--p = ',';
2166 }
2167 *p = '('; /* First parameter starts with ( not , */
2168
2169 free_tlist(junk_names);
2170 }
2171
2172 p -= namelen;
2173 memcpy(p, m->name, namelen);
2174
2175 if (context_depth) {
2176 for (i = 0; i < context_depth; i++)
2177 *--p = '$';
2178 *--p = '%';
2179 }
2180
2181 nasm_listmsg("%s %s", pp_directives[op], p);
2182
2183 nasm_free(def);
2184 }
2185
2186/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002187 * Common code for defining an smacro
2188 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002189static SMacro *define_smacro(Context *ctx, const char *mname,
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002190 bool casesense, int nparam,
2191 bool *eval_param, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002192{
2193 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002194 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002195
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002196 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002197 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002198 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002199 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002200 /*
2201 * Some instances of the old code considered this a failure,
2202 * some others didn't. What is the right thing to do here?
2203 */
2204 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002205 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002206 } else {
2207 /*
2208 * We're redefining, so we have to take over an
2209 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002210 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002211 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002212 clear_smacro(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002213 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002214 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002215 smtbl = ctx ? &ctx->localmac : &smacros;
2216 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002217 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002218 smac->next = *smhead;
2219 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002220 }
2221 smac->name = nasm_strdup(mname);
2222 smac->casesense = casesense;
2223 smac->nparam = nparam;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002224 smac->eval_param = eval_param;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002225 smac->expansion = expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002226 smac->expand = smacro_expand_default;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002227
2228 if (list_option('m'))
2229 list_smacro_def(casesense ? PP_DEFINE : PP_IDEFINE, ctx, smac);
2230
H. Peter Anvin8b262472019-02-26 14:00:54 -08002231 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002232}
2233
2234/*
2235 * Undefine an smacro
2236 */
2237static void undef_smacro(Context *ctx, const char *mname)
2238{
2239 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002240 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002241
H. Peter Anvin166c2472008-05-28 12:28:58 -07002242 smtbl = ctx ? &ctx->localmac : &smacros;
2243 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002244
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002245 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002246 /*
2247 * We now have a macro name... go hunt for it.
2248 */
2249 sp = smhead;
2250 while ((s = *sp) != NULL) {
2251 if (!mstrcmp(s->name, mname, s->casesense)) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002252 if (list_option('m'))
2253 list_smacro_def(PP_UNDEF, ctx, s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002254 *sp = s->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002255 free_smacro(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002256 } else {
2257 sp = &s->next;
2258 }
2259 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002260 }
2261}
2262
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002263/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002264 * Parse a mmacro specification.
2265 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002266static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002267{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002268 tline = tline->next;
2269 skip_white_(tline);
2270 tline = expand_id(tline);
2271 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002272 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002273 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002274 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002275
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002276 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002277 def->name = nasm_strdup(tline->text);
2278 def->plus = false;
2279 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002280 def->in_progress = 0;
2281 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002282 def->nparam_min = 0;
2283 def->nparam_max = 0;
2284
H. Peter Anvina26433d2008-07-16 14:40:01 -07002285 tline = expand_smacro(tline->next);
2286 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002287 if (!tok_type_(tline, TOK_NUMBER))
2288 nasm_nonfatal("`%s' expects a parameter count", directive);
2289 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002290 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002291 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002292 tline = tline->next->next;
2293 if (tok_is_(tline, "*")) {
2294 def->nparam_max = INT_MAX;
2295 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002296 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002297 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002298 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002299 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002300 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002301 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002302 }
2303 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002304 }
2305 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002306 tline = tline->next;
2307 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002308 }
2309 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002310 !nasm_stricmp(tline->next->text, ".nolist")) {
2311 tline = tline->next;
2312 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002313 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002314
H. Peter Anvina26433d2008-07-16 14:40:01 -07002315 /*
2316 * Handle default parameters.
2317 */
2318 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002319 def->dlist = tline->next;
2320 tline->next = NULL;
2321 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002322 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002323 def->dlist = NULL;
2324 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002325 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002326 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002327
H. Peter Anvin89cee572009-07-15 09:16:54 -04002328 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002329 !def->plus) {
2330 /*
2331 *!macro-defaults [on] macros with more default than optional parameters
2332 *! warns when a macro has more default parameters than optional parameters.
2333 *! See \k{mlmacdef} for why might want to disable this warning.
2334 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002335 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002336 "too many default macro parameters in macro `%s'", def->name);
2337 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002338
H. Peter Anvina26433d2008-07-16 14:40:01 -07002339 return true;
2340}
2341
2342
2343/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002344 * Decode a size directive
2345 */
2346static int parse_size(const char *str) {
2347 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002348 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002349 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002350 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002351 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002352}
2353
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002354/*
2355 * Process a preprocessor %pragma directive. Currently there are none.
2356 * Gets passed the token list starting with the "preproc" token from
2357 * "%pragma preproc".
2358 */
2359static void do_pragma_preproc(Token *tline)
2360{
2361 /* Skip to the real stuff */
2362 tline = tline->next;
2363 skip_white_(tline);
2364 if (!tline)
2365 return;
2366
2367 (void)tline; /* Nothing else to do at present */
2368}
2369
Ed Beroset3ab3f412002-06-11 03:31:49 +00002370/**
2371 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002372 * Find out if a line contains a preprocessor directive, and deal
2373 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002374 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002375 * If a directive _is_ found, it is the responsibility of this routine
2376 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002377 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002378 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002379 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002380 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002381 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002382 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002383static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002384{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002385 enum preproc_token i;
2386 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002387 bool err;
2388 int nparam;
2389 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002390 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002391 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002392 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002393 char *p, *pp;
2394 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002395 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002396 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002397 Include *inc;
2398 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002399 Cond *cond;
2400 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002401 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002402 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002403 struct tokenval tokval;
2404 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002405 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002406 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002407 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002408 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002409 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002410
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002411 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002412 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002413
H. Peter Anvineba20a72002-04-30 20:53:55 +00002414 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002415 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002416 (tline->text[0] && (tline->text[1] == '%' ||
2417 tline->text[1] == '$' ||
2418 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002419 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002420
H. Peter Anvin4169a472007-09-12 01:29:43 +00002421 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002422
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002423 /*
2424 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2425 * since they are known to be buggy at moment, we need to fix them
2426 * in future release (2.09-2.10)
2427 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002428 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002429 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2430 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002431 }
2432
2433 /*
2434 * If we're in a non-emitting branch of a condition construct,
2435 * or walking to the end of an already terminated %rep block,
2436 * we should ignore all directives except for condition
2437 * directives.
2438 */
2439 if (((istk->conds && !emitting(istk->conds->state)) ||
2440 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2441 return NO_DIRECTIVE_FOUND;
2442 }
2443
2444 /*
2445 * If we're defining a macro or reading a %rep block, we should
2446 * ignore all directives except for %macro/%imacro (which nest),
2447 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2448 * If we're in a %rep block, another %rep nests, so should be let through.
2449 */
2450 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2451 i != PP_RMACRO && i != PP_IRMACRO &&
2452 i != PP_ENDMACRO && i != PP_ENDM &&
2453 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2454 return NO_DIRECTIVE_FOUND;
2455 }
2456
2457 if (defining) {
2458 if (i == PP_MACRO || i == PP_IMACRO ||
2459 i == PP_RMACRO || i == PP_IRMACRO) {
2460 nested_mac_count++;
2461 return NO_DIRECTIVE_FOUND;
2462 } else if (nested_mac_count > 0) {
2463 if (i == PP_ENDMACRO) {
2464 nested_mac_count--;
2465 return NO_DIRECTIVE_FOUND;
2466 }
2467 }
2468 if (!defining->name) {
2469 if (i == PP_REP) {
2470 nested_rep_count++;
2471 return NO_DIRECTIVE_FOUND;
2472 } else if (nested_rep_count > 0) {
2473 if (i == PP_ENDREP) {
2474 nested_rep_count--;
2475 return NO_DIRECTIVE_FOUND;
2476 }
2477 }
2478 }
2479 }
2480
H. Peter Anvin8b262472019-02-26 14:00:54 -08002481 dname = pp_directives[i]; /* Directive name, for error messages */
2482 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002483 switch (i) {
2484 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002485 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002487
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002488 case PP_PRAGMA:
2489 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002490 * %pragma namespace options...
2491 *
2492 * The namespace "preproc" is reserved for the preprocessor;
2493 * all other namespaces generate a [pragma] assembly directive.
2494 *
2495 * Invalid %pragmas are ignored and may have different
2496 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002497 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002498 tline = tline->next;
2499 skip_white_(tline);
2500 tline = expand_smacro(tline);
2501 if (tok_type_(tline, TOK_ID)) {
2502 if (!nasm_stricmp(tline->text, "preproc")) {
2503 /* Preprocessor pragma */
2504 do_pragma_preproc(tline);
2505 } else {
2506 /* Build the assembler directive */
2507 t = new_Token(NULL, TOK_OTHER, "[", 1);
2508 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2509 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2510 tline = t;
2511 for (t = tline; t->next; t = t->next)
2512 ;
2513 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002514 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002515 }
2516 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002517 free_tlist(origline);
2518 return DIRECTIVE_FOUND;
2519
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 case PP_STACKSIZE:
2521 /* Directive to tell NASM what the default stack size is. The
2522 * default is for a 16-bit stack, and this can be overriden with
2523 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002524 */
2525 tline = tline->next;
2526 if (tline && tline->type == TOK_WHITESPACE)
2527 tline = tline->next;
2528 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002529 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 free_tlist(origline);
2531 return DIRECTIVE_FOUND;
2532 }
2533 if (nasm_stricmp(tline->text, "flat") == 0) {
2534 /* All subsequent ARG directives are for a 32-bit stack */
2535 StackSize = 4;
2536 StackPointer = "ebp";
2537 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002538 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002539 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2540 /* All subsequent ARG directives are for a 64-bit stack */
2541 StackSize = 8;
2542 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002543 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002544 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 } else if (nasm_stricmp(tline->text, "large") == 0) {
2546 /* All subsequent ARG directives are for a 16-bit stack,
2547 * far function call.
2548 */
2549 StackSize = 2;
2550 StackPointer = "bp";
2551 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002552 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002553 } else if (nasm_stricmp(tline->text, "small") == 0) {
2554 /* All subsequent ARG directives are for a 16-bit stack,
2555 * far function call. We don't support near functions.
2556 */
2557 StackSize = 2;
2558 StackPointer = "bp";
2559 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002560 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002562 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 free_tlist(origline);
2564 return DIRECTIVE_FOUND;
2565 }
2566 free_tlist(origline);
2567 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002568
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 case PP_ARG:
2570 /* TASM like ARG directive to define arguments to functions, in
2571 * the following form:
2572 *
2573 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2574 */
2575 offset = ArgOffset;
2576 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002577 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002579
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 /* Find the argument name */
2581 tline = tline->next;
2582 if (tline && tline->type == TOK_WHITESPACE)
2583 tline = tline->next;
2584 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002585 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002586 free_tlist(origline);
2587 return DIRECTIVE_FOUND;
2588 }
2589 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002590
H. Peter Anvine2c80182005-01-15 22:15:51 +00002591 /* Find the argument size type */
2592 tline = tline->next;
2593 if (!tline || tline->type != TOK_OTHER
2594 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002595 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002596 free_tlist(origline);
2597 return DIRECTIVE_FOUND;
2598 }
2599 tline = tline->next;
2600 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002601 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002602 free_tlist(origline);
2603 return DIRECTIVE_FOUND;
2604 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002605
H. Peter Anvine2c80182005-01-15 22:15:51 +00002606 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002607 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002608 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002609 size = parse_size(tt->text);
2610 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002611 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002612 free_tlist(tt);
2613 free_tlist(origline);
2614 return DIRECTIVE_FOUND;
2615 }
2616 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002617
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002618 /* Round up to even stack slots */
2619 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002620
H. Peter Anvine2c80182005-01-15 22:15:51 +00002621 /* Now define the macro for the argument */
2622 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2623 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002624 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002625 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002626
H. Peter Anvine2c80182005-01-15 22:15:51 +00002627 /* Move to the next argument in the list */
2628 tline = tline->next;
2629 if (tline && tline->type == TOK_WHITESPACE)
2630 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002631 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002632 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 free_tlist(origline);
2634 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002635
H. Peter Anvine2c80182005-01-15 22:15:51 +00002636 case PP_LOCAL:
2637 /* TASM like LOCAL directive to define local variables for a
2638 * function, in the following form:
2639 *
2640 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2641 *
2642 * The '= LocalSize' at the end is ignored by NASM, but is
2643 * required by TASM to define the local parameter size (and used
2644 * by the TASM macro package).
2645 */
2646 offset = LocalOffset;
2647 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002648 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002650
H. Peter Anvine2c80182005-01-15 22:15:51 +00002651 /* Find the argument name */
2652 tline = tline->next;
2653 if (tline && tline->type == TOK_WHITESPACE)
2654 tline = tline->next;
2655 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002656 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 free_tlist(origline);
2658 return DIRECTIVE_FOUND;
2659 }
2660 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002661
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 /* Find the argument size type */
2663 tline = tline->next;
2664 if (!tline || tline->type != TOK_OTHER
2665 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002666 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 free_tlist(origline);
2668 return DIRECTIVE_FOUND;
2669 }
2670 tline = tline->next;
2671 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002672 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002673 free_tlist(origline);
2674 return DIRECTIVE_FOUND;
2675 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002676
H. Peter Anvine2c80182005-01-15 22:15:51 +00002677 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002678 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002680 size = parse_size(tt->text);
2681 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002682 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 free_tlist(tt);
2684 free_tlist(origline);
2685 return DIRECTIVE_FOUND;
2686 }
2687 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002688
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002689 /* Round up to even stack slots */
2690 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002691
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002693
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002694 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002695 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2696 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002697 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002698
H. Peter Anvine2c80182005-01-15 22:15:51 +00002699 /* Now define the assign to setup the enter_c macro correctly */
2700 snprintf(directive, sizeof(directive),
2701 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002702 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002703
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 /* Move to the next argument in the list */
2705 tline = tline->next;
2706 if (tline && tline->type == TOK_WHITESPACE)
2707 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002708 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002709 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002710 free_tlist(origline);
2711 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002712
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 case PP_CLEAR:
2714 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002715 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002716 free_macros();
2717 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 free_tlist(origline);
2719 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002720
H. Peter Anvin418ca702008-05-30 10:42:30 -07002721 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002722 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002723 skip_white_(t);
2724 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002725 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002726 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002727 free_tlist(origline);
2728 return DIRECTIVE_FOUND; /* but we did _something_ */
2729 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002730 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002731 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002733 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002734 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002735 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002737 return DIRECTIVE_FOUND;
2738
2739 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002740 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002741 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002742
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002743 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002744 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002745 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 free_tlist(origline);
2747 return DIRECTIVE_FOUND; /* but we did _something_ */
2748 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002749 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002750 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002751 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002752 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002753 nasm_unquote_cstr(p, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002754 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002755 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002756 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002757 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002758 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002759 (pp_mode == PP_DEPS)
2760 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002761 if (!inc->fp) {
2762 /* -MG given but file not found */
2763 nasm_free(inc);
2764 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002765 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002766 inc->lineno = src_set_linnum(0);
2767 inc->lineinc = 1;
2768 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002769 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002770 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002771 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002772 }
2773 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002775
H. Peter Anvind2456592008-06-19 15:04:18 -07002776 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002777 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002778 static macros_t *use_pkg;
2779 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002780
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002781 tline = tline->next;
2782 skip_white_(tline);
2783 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002784
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002785 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002786 tline->type != TOK_INTERNAL_STRING &&
2787 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002788 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002789 free_tlist(origline);
2790 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002791 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002792 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002793 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002795 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002796 use_pkg = nasm_stdmac_find_package(tline->text);
2797 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002798 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002799 else
2800 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002801 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002802 /* Not already included, go ahead and include it */
2803 stdmacpos = use_pkg;
2804 }
2805 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002806 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002807 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002810 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 tline = tline->next;
2812 skip_white_(tline);
2813 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002814 if (tline) {
2815 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002816 nasm_nonfatal("`%s' expects a context identifier",
2817 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002818 free_tlist(origline);
2819 return DIRECTIVE_FOUND; /* but we did _something_ */
2820 }
2821 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002822 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002823 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002824 p = nasm_strdup(tline->text);
2825 } else {
2826 p = NULL; /* Anonymous */
2827 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002828
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002829 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002830 nasm_new(ctx);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07002831 ctx->depth = cstk ? cstk->depth + 1 : 1;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002832 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002833 ctx->name = p;
2834 ctx->number = unique++;
2835 cstk = ctx;
2836 } else {
2837 /* %pop or %repl */
2838 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002839 nasm_nonfatal("`%s': context stack is empty",
2840 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002841 } else if (i == PP_POP) {
2842 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002843 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002844 "expected %s",
2845 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002846 else
2847 ctx_pop();
2848 } else {
2849 /* i == PP_REPL */
2850 nasm_free(cstk->name);
2851 cstk->name = p;
2852 p = NULL;
2853 }
2854 nasm_free(p);
2855 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002857 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002858 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002859 severity = ERR_FATAL;
2860 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002861 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002862 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002863 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002864 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002865 /*!
2866 *!user [on] %warning directives
2867 *! controls output of \c{%warning} directives (see \k{pperror}).
2868 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002869 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002870 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002871
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002872issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002873 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002874 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002875 tline->next = expand_smacro(tline->next);
2876 tline = tline->next;
2877 skip_white_(tline);
2878 t = tline ? tline->next : NULL;
2879 skip_white_(t);
2880 if (tok_type_(tline, TOK_STRING) && !t) {
2881 /* The line contains only a quoted string */
2882 p = tline->text;
2883 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002884 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002885 } else {
2886 /* Not a quoted string, or more than a quoted string */
2887 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002888 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002889 nasm_free(p);
2890 }
2891 free_tlist(origline);
2892 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002893 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002894
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002895 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002896 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002897 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002898 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 j = if_condition(tline->next, i);
2900 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002901 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002902 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002903 cond = nasm_malloc(sizeof(Cond));
2904 cond->next = istk->conds;
2905 cond->state = j;
2906 istk->conds = cond;
2907 if(istk->mstk)
2908 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002909 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002910 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002911
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002912 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002913 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002914 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002915 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002917 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002918 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002919
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002920 case COND_DONE:
2921 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002922 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002923
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002924 case COND_ELSE_TRUE:
2925 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002926 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002927 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002928 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002929 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002930
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002931 case COND_IF_FALSE:
2932 /*
2933 * IMPORTANT: In the case of %if, we will already have
2934 * called expand_mmac_params(); however, if we're
2935 * processing an %elif we must have been in a
2936 * non-emitting mode, which would have inhibited
2937 * the normal invocation of expand_mmac_params().
2938 * Therefore, we have to do it explicitly here.
2939 */
2940 j = if_condition(expand_mmac_params(tline->next), i);
2941 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002942 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002943 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2944 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002946 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002947 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002948
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 case PP_ELSE:
2950 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002951 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002952 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002953 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002954 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002955 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002956 case COND_IF_TRUE:
2957 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002958 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002959 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002960
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002961 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002962 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002963
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002964 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002965 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002966 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002967
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002968 case COND_ELSE_TRUE:
2969 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002970 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002971 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002972 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002973 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002974 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 free_tlist(origline);
2976 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002977
H. Peter Anvine2c80182005-01-15 22:15:51 +00002978 case PP_ENDIF:
2979 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002980 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002981 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002982 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002983 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002984 cond = istk->conds;
2985 istk->conds = cond->next;
2986 nasm_free(cond);
2987 if(istk->mstk)
2988 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 free_tlist(origline);
2990 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002991
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002992 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002994 casesense = false;
2995 /* fall through */
2996 case PP_RMACRO:
2997 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002998 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002999 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003000 return DIRECTIVE_FOUND;
3001 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003002 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003003 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
3004 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003005 defining->casesense = casesense;
3006 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003007 nasm_free(defining);
3008 defining = NULL;
3009 return DIRECTIVE_FOUND;
3010 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07003011
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003012 src_get(&defining->xline, &defining->fname);
3013
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003014 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
3015 while (mmac) {
3016 if (!strcmp(mmac->name, defining->name) &&
3017 (mmac->nparam_min <= defining->nparam_max
3018 || defining->plus)
3019 && (defining->nparam_min <= mmac->nparam_max
3020 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003021 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003022 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003023 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003024 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003025 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003026 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003027 free_tlist(origline);
3028 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003029
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 case PP_ENDM:
3031 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003032 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003033 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003034 return DIRECTIVE_FOUND;
3035 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003036 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
3037 defining->next = *mmhead;
3038 *mmhead = defining;
3039 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 free_tlist(origline);
3041 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003042
H. Peter Anvin89cee572009-07-15 09:16:54 -04003043 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003044 /*
3045 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003046 * macro-end marker for a macro with a name. Then we
3047 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003048 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003049 list_for_each(l, istk->expansion)
3050 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003051 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003052
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003053 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003054 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003055 * Remove all conditional entries relative to this
3056 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003057 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003058 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
3059 cond = istk->conds;
3060 istk->conds = cond->next;
3061 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003062 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003063 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003064 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003065 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003066 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05003067 free_tlist(origline);
3068 return DIRECTIVE_FOUND;
3069
H. Peter Anvina26433d2008-07-16 14:40:01 -07003070 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003071 casesense = false;
3072 /* fall through */
3073 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07003074 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003075 MMacro **mmac_p;
3076 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003077
H. Peter Anvin8b262472019-02-26 14:00:54 -08003078 spec.casesense = casesense;
3079 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003080 return DIRECTIVE_FOUND;
3081 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003082 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
3083 while (mmac_p && *mmac_p) {
3084 mmac = *mmac_p;
3085 if (mmac->casesense == spec.casesense &&
3086 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
3087 mmac->nparam_min == spec.nparam_min &&
3088 mmac->nparam_max == spec.nparam_max &&
3089 mmac->plus == spec.plus) {
3090 *mmac_p = mmac->next;
3091 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003092 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003093 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003094 }
3095 }
3096 free_tlist(origline);
3097 free_tlist(spec.dlist);
3098 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07003099 }
3100
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 case PP_ROTATE:
3102 if (tline->next && tline->next->type == TOK_WHITESPACE)
3103 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04003104 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003106 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003107 return DIRECTIVE_FOUND;
3108 }
3109 t = expand_smacro(tline->next);
3110 tline->next = NULL;
3111 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003112 pps.tptr = tline = t;
3113 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 tokval.t_type = TOKEN_INVALID;
3115 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003116 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 free_tlist(tline);
3118 if (!evalresult)
3119 return DIRECTIVE_FOUND;
3120 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003121 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003122 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003123 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 return DIRECTIVE_FOUND;
3125 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003126 mmac = istk->mstk;
3127 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3128 mmac = mmac->next_active;
3129 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003130 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003131 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003132 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003133 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003134 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003135
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003136 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003137 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003138 rotate += mmac->nparam;
3139
3140 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003141 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003143
H. Peter Anvine2c80182005-01-15 22:15:51 +00003144 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003145 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003146 do {
3147 tline = tline->next;
3148 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003149
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 if (tok_type_(tline, TOK_ID) &&
3151 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003152 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003153 do {
3154 tline = tline->next;
3155 } while (tok_type_(tline, TOK_WHITESPACE));
3156 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003157
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003159 pps.tptr = expand_smacro(tline);
3160 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003161 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003162 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003163 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003164 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003165 if (!evalresult) {
3166 free_tlist(origline);
3167 return DIRECTIVE_FOUND;
3168 }
3169 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003170 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003172 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003173 return DIRECTIVE_FOUND;
3174 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003175 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003176 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003177 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3178 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003179 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003180 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003181 /*!
3182 *!negative-rep [on] regative %rep count
3183 *! warns about negative counts given to the \c{%rep}
3184 *! preprocessor directive.
3185 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003186 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003187 "negative `%%rep' count: %"PRId64, count);
3188 count = 0;
3189 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003190 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003191 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003192 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003193 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003194 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 }
3196 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003197
3198 tmp_defining = defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003199 nasm_new(defining);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003200 defining->nolist = nolist;
3201 defining->in_progress = count;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003202 defining->next_active = istk->mstk;
3203 defining->rep_nest = tmp_defining;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07003204 src_get(&defining->xline, &defining->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003205 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003206
H. Peter Anvine2c80182005-01-15 22:15:51 +00003207 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003208 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003209 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003210 return DIRECTIVE_FOUND;
3211 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003212
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 /*
3214 * Now we have a "macro" defined - although it has no name
3215 * and we won't be entering it in the hash tables - we must
3216 * push a macro-end marker for it on to istk->expansion.
3217 * After that, it will take care of propagating itself (a
3218 * macro-end marker line for a macro which is really a %rep
3219 * block will cause the macro to be re-expanded, complete
3220 * with another macro-end marker to ensure the process
3221 * continues) until the whole expansion is forcibly removed
3222 * from istk->expansion by a %exitrep.
3223 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003224 l = nasm_malloc(sizeof(Line));
3225 l->next = istk->expansion;
3226 l->finishes = defining;
3227 l->first = NULL;
3228 istk->expansion = l;
3229
3230 istk->mstk = defining;
3231
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003232 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003233 tmp_defining = defining;
3234 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003235 free_tlist(origline);
3236 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003237
H. Peter Anvine2c80182005-01-15 22:15:51 +00003238 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003239 /*
3240 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003241 * macro-end marker for a macro with no name. Then we set
3242 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003243 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003244 list_for_each(l, istk->expansion)
3245 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003246 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003247
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003248 if (l)
3249 l->finishes->in_progress = 1;
3250 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003251 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003252 free_tlist(origline);
3253 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003254
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003256 case PP_IXDEFINE:
3257 casesense = false;
3258 /* fall through */
3259 case PP_DEFINE:
3260 case PP_XDEFINE:
3261 {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003262 bool have_eval_params = false;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003263 bool *eval_params = NULL;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003264
H. Peter Anvine2c80182005-01-15 22:15:51 +00003265 tline = tline->next;
3266 skip_white_(tline);
3267 tline = expand_id(tline);
3268 if (!tline || (tline->type != TOK_ID &&
3269 (tline->type != TOK_PREPROC_ID ||
3270 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003271 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003272 free_tlist(origline);
3273 return DIRECTIVE_FOUND;
3274 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003275
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003276 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003277 last = tline;
3278 param_start = tline = tline->next;
3279 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003280
H. Peter Anvine2c80182005-01-15 22:15:51 +00003281 if (tok_is_(tline, "(")) {
3282 /*
3283 * This macro has parameters.
3284 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003285
H. Peter Anvine2c80182005-01-15 22:15:51 +00003286 tline = tline->next;
3287 while (1) {
3288 skip_white_(tline);
3289 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003290 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003291 free_tlist(origline);
3292 return DIRECTIVE_FOUND;
3293 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003294 if (tok_is_(tline, "=")) {
3295 have_eval_params = true;
3296 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003297 skip_white_(tline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003298 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003299
3300 /*
3301 * "*" means an unnamed, ignored argument; it can never
3302 * match anything because "*" is not TOK_ID, and so
3303 * none of the expansion entries will be converted
3304 * to parameters.
3305 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003306 if (tline->type != TOK_ID) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003307 if (tok_is_(tline, ",") || tok_is_(tline, ")")) {
3308 /*
3309 * Empty name; duplicate the termination
3310 * token and use the first copy as a placeholder.
3311 * It will never match anything, since the
3312 * associated text doesn't correspond to a TOK_ID.
3313 */
3314 tline = dup_Token(tline, tline);
3315 } else {
3316 nasm_nonfatal("`%s': parameter identifier expected",
3317 tline->text);
3318 free_tlist(origline);
3319 return DIRECTIVE_FOUND;
3320 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003321 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003322 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003323 tline = tline->next;
3324 skip_white_(tline);
3325 if (tok_is_(tline, ",")) {
3326 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003327 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003329 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003330 free_tlist(origline);
3331 return DIRECTIVE_FOUND;
3332 }
3333 break;
3334 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003335 }
3336 last = tline;
3337 tline = tline->next;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003338
3339 if (have_eval_params) {
3340 /* Create evaluated parameters table */
3341 bool is_eval = false;
3342
3343 nasm_newn(eval_params, nparam);
3344 list_for_each(tt, param_start) {
3345 if (is_smac_param(tt->type))
3346 eval_params[smac_nparam(tt->type)] = is_eval;
3347 is_eval = tok_is_(tt, "=");
3348 }
3349 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003350 }
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003351
H. Peter Anvine2c80182005-01-15 22:15:51 +00003352 if (tok_type_(tline, TOK_WHITESPACE))
3353 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003354 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003355
3356 /* Expand the macro definition now for %xdefine and %ixdefine */
3357 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3358 tline = expand_smacro(tline);
3359
3360 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003361 t = tline;
3362 while (t) {
3363 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003364 list_for_each(tt, param_start)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003365 if (is_smac_param(tt->type) &&
3366 tt->len == t->len &&
3367 !memcmp(tt->text, t->text, tt->len))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003368 t->type = tt->type;
3369 }
3370 tt = t->next;
3371 t->next = macro_start;
3372 macro_start = t;
3373 t = tt;
3374 }
3375 /*
3376 * Good. We now have a macro name, a parameter count, and a
3377 * token list (in reverse order) for an expansion. We ought
3378 * to be OK just to create an SMacro, store it, and let
3379 * free_tlist have the rest of the line (which we have
3380 * carefully re-terminated after chopping off the expansion
3381 * from the end).
3382 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003383 define_smacro(ctx, mname, casesense, nparam, eval_params, macro_start);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003384
H. Peter Anvine2c80182005-01-15 22:15:51 +00003385 free_tlist(origline);
3386 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003387 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003388
H. Peter Anvine2c80182005-01-15 22:15:51 +00003389 case PP_UNDEF:
3390 tline = tline->next;
3391 skip_white_(tline);
3392 tline = expand_id(tline);
3393 if (!tline || (tline->type != TOK_ID &&
3394 (tline->type != TOK_PREPROC_ID ||
3395 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003396 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003397 free_tlist(origline);
3398 return DIRECTIVE_FOUND;
3399 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003400 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003401 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003402
H. Peter Anvine2c80182005-01-15 22:15:51 +00003403 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003404 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003405 undef_smacro(ctx, mname);
3406 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003408
H. Peter Anvin9e200162008-06-04 17:23:14 -07003409 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003410 casesense = false;
3411 /* fall through */
3412 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003413 tline = tline->next;
3414 skip_white_(tline);
3415 tline = expand_id(tline);
3416 if (!tline || (tline->type != TOK_ID &&
3417 (tline->type != TOK_PREPROC_ID ||
3418 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003419 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003420 free_tlist(origline);
3421 return DIRECTIVE_FOUND;
3422 }
3423
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003424 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003425 last = tline;
3426 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003427 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003428
3429 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003430 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003431
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003432 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003433 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003434 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003435
3436 /*
3437 * We now have a macro name, an implicit parameter count of
3438 * zero, and a string token to use as an expansion. Create
3439 * and store an SMacro.
3440 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003441 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003442 free_tlist(origline);
3443 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003444
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003445 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003446 casesense = false;
3447 /* fall through */
3448 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003449 tline = tline->next;
3450 skip_white_(tline);
3451 tline = expand_id(tline);
3452 if (!tline || (tline->type != TOK_ID &&
3453 (tline->type != TOK_PREPROC_ID ||
3454 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003455 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003456 free_tlist(origline);
3457 return DIRECTIVE_FOUND;
3458 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003459 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003460 last = tline;
3461 tline = expand_smacro(tline->next);
3462 last->next = NULL;
3463
3464 t = tline;
3465 while (tok_type_(t, TOK_WHITESPACE))
3466 t = t->next;
3467 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003468 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003469 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003470 free_tlist(tline);
3471 free_tlist(origline);
3472 return DIRECTIVE_FOUND;
3473 }
3474
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003475 /*
3476 * Convert the string to a token stream. Note that smacros
3477 * are stored with the token stream reversed, so we have to
3478 * reverse the output of tokenize().
3479 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003480 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003481 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003483 /*
3484 * We now have a macro name, an implicit parameter count of
3485 * zero, and a numeric token to use as an expansion. Create
3486 * and store an SMacro.
3487 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003488 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003489 free_tlist(tline);
3490 free_tlist(origline);
3491 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003492
H. Peter Anvin8b262472019-02-26 14:00:54 -08003493 case PP_IPATHSEARCH:
3494 casesense = false;
3495 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003496 case PP_PATHSEARCH:
3497 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003498 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003499
H. Peter Anvin418ca702008-05-30 10:42:30 -07003500 tline = tline->next;
3501 skip_white_(tline);
3502 tline = expand_id(tline);
3503 if (!tline || (tline->type != TOK_ID &&
3504 (tline->type != TOK_PREPROC_ID ||
3505 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003506 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003507 free_tlist(origline);
3508 return DIRECTIVE_FOUND;
3509 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003510 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003511 last = tline;
3512 tline = expand_smacro(tline->next);
3513 last->next = NULL;
3514
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003515 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003516 while (tok_type_(t, TOK_WHITESPACE))
3517 t = t->next;
3518
3519 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003520 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003521 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003522 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003523 free_tlist(origline);
3524 return DIRECTIVE_FOUND; /* but we did _something_ */
3525 }
3526 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003527 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003528 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003529 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003530 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003531
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003532 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003533 if (!found_path)
3534 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003535 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003536
3537 /*
3538 * We now have a macro name, an implicit parameter count of
3539 * zero, and a string token to use as an expansion. Create
3540 * and store an SMacro.
3541 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003542 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003543 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003544 free_tlist(origline);
3545 return DIRECTIVE_FOUND;
3546 }
3547
H. Peter Anvin8b262472019-02-26 14:00:54 -08003548 case PP_ISTRLEN:
3549 casesense = false;
3550 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003551 case PP_STRLEN:
3552 tline = tline->next;
3553 skip_white_(tline);
3554 tline = expand_id(tline);
3555 if (!tline || (tline->type != TOK_ID &&
3556 (tline->type != TOK_PREPROC_ID ||
3557 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003558 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 free_tlist(origline);
3560 return DIRECTIVE_FOUND;
3561 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003562 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003563 last = tline;
3564 tline = expand_smacro(tline->next);
3565 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003566
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 t = tline;
3568 while (tok_type_(t, TOK_WHITESPACE))
3569 t = t->next;
3570 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003571 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003572 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003573 free_tlist(tline);
3574 free_tlist(origline);
3575 return DIRECTIVE_FOUND;
3576 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003577
H. Peter Anvin8b262472019-02-26 14:00:54 -08003578 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003579
H. Peter Anvine2c80182005-01-15 22:15:51 +00003580 /*
3581 * We now have a macro name, an implicit parameter count of
3582 * zero, and a numeric token to use as an expansion. Create
3583 * and store an SMacro.
3584 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003585 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 free_tlist(tline);
3587 free_tlist(origline);
3588 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003589
H. Peter Anvin8b262472019-02-26 14:00:54 -08003590 case PP_ISTRCAT:
3591 casesense = false;
3592 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003593 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003594 tline = tline->next;
3595 skip_white_(tline);
3596 tline = expand_id(tline);
3597 if (!tline || (tline->type != TOK_ID &&
3598 (tline->type != TOK_PREPROC_ID ||
3599 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003600 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003601 free_tlist(origline);
3602 return DIRECTIVE_FOUND;
3603 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003604 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003605 last = tline;
3606 tline = expand_smacro(tline->next);
3607 last->next = NULL;
3608
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003609 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003610 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003611 switch (t->type) {
3612 case TOK_WHITESPACE:
3613 break;
3614 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003615 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003616 break;
3617 case TOK_OTHER:
3618 if (!strcmp(t->text, ",")) /* permit comma separators */
3619 break;
3620 /* else fall through */
3621 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003622 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003623 free_tlist(tline);
3624 free_tlist(origline);
3625 return DIRECTIVE_FOUND;
3626 }
3627 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003628
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003629 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003630 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003631 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003632 memcpy(p, t->text, t->len);
3633 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003634 }
3635 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003636
3637 /*
3638 * We now have a macro name, an implicit parameter count of
3639 * zero, and a numeric token to use as an expansion. Create
3640 * and store an SMacro.
3641 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003642 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003643 nasm_free(pp);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003644 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003645 free_tlist(tline);
3646 free_tlist(origline);
3647 return DIRECTIVE_FOUND;
3648
H. Peter Anvin8b262472019-02-26 14:00:54 -08003649 case PP_ISUBSTR:
3650 casesense = false;
3651 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003652 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003653 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003654 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003655 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003656
H. Peter Anvine2c80182005-01-15 22:15:51 +00003657 tline = tline->next;
3658 skip_white_(tline);
3659 tline = expand_id(tline);
3660 if (!tline || (tline->type != TOK_ID &&
3661 (tline->type != TOK_PREPROC_ID ||
3662 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003663 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003664 free_tlist(origline);
3665 return DIRECTIVE_FOUND;
3666 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003667 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 last = tline;
3669 tline = expand_smacro(tline->next);
3670 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003671
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003672 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003673 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 while (tok_type_(t, TOK_WHITESPACE))
3675 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003676
H. Peter Anvine2c80182005-01-15 22:15:51 +00003677 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003678 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003679 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003680 free_tlist(tline);
3681 free_tlist(origline);
3682 return DIRECTIVE_FOUND;
3683 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003684
H. Peter Anvin8b262472019-02-26 14:00:54 -08003685 pps.tptr = t->next;
3686 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003687 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003688 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003689 if (!evalresult) {
3690 free_tlist(tline);
3691 free_tlist(origline);
3692 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003693 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003694 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003695 free_tlist(tline);
3696 free_tlist(origline);
3697 return DIRECTIVE_FOUND;
3698 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003699 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003700
H. Peter Anvin8b262472019-02-26 14:00:54 -08003701 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3702 pps.tptr = pps.tptr->next;
3703 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003704 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003705 } else {
3706 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003707 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003708 if (!evalresult) {
3709 free_tlist(tline);
3710 free_tlist(origline);
3711 return DIRECTIVE_FOUND;
3712 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003713 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003714 free_tlist(tline);
3715 free_tlist(origline);
3716 return DIRECTIVE_FOUND;
3717 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003718 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003719 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003720
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003721 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003722
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003723 /* make start and count being in range */
3724 if (start < 0)
3725 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003726 if (count < 0)
3727 count = len + count + 1 - start;
3728 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003729 count = len - start;
3730 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003731 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003732
H. Peter Anvin8b262472019-02-26 14:00:54 -08003733 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003734 macro_start->len = count;
3735 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3736 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003737
H. Peter Anvine2c80182005-01-15 22:15:51 +00003738 /*
3739 * We now have a macro name, an implicit parameter count of
3740 * zero, and a numeric token to use as an expansion. Create
3741 * and store an SMacro.
3742 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003743 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003744 free_tlist(tline);
3745 free_tlist(origline);
3746 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003747 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003748
H. Peter Anvine2c80182005-01-15 22:15:51 +00003749 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003750 casesense = false;
3751 /* fall through */
3752 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003753 tline = tline->next;
3754 skip_white_(tline);
3755 tline = expand_id(tline);
3756 if (!tline || (tline->type != TOK_ID &&
3757 (tline->type != TOK_PREPROC_ID ||
3758 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003759 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003760 free_tlist(origline);
3761 return DIRECTIVE_FOUND;
3762 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003763 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003764 last = tline;
3765 tline = expand_smacro(tline->next);
3766 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003767
H. Peter Anvin8b262472019-02-26 14:00:54 -08003768 pps.tptr = tline;
3769 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003770 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003771 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003772 free_tlist(tline);
3773 if (!evalresult) {
3774 free_tlist(origline);
3775 return DIRECTIVE_FOUND;
3776 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003777
H. Peter Anvine2c80182005-01-15 22:15:51 +00003778 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003779 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003780
H. Peter Anvine2c80182005-01-15 22:15:51 +00003781 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003782 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003783 free_tlist(origline);
3784 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003785 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003786
H. Peter Anvin8b262472019-02-26 14:00:54 -08003787 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003788
H. Peter Anvine2c80182005-01-15 22:15:51 +00003789 /*
3790 * We now have a macro name, an implicit parameter count of
3791 * zero, and a numeric token to use as an expansion. Create
3792 * and store an SMacro.
3793 */
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07003794 define_smacro(ctx, mname, casesense, 0, NULL, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003795 free_tlist(origline);
3796 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003797
H. Peter Anvine2c80182005-01-15 22:15:51 +00003798 case PP_LINE:
3799 /*
3800 * Syntax is `%line nnn[+mmm] [filename]'
3801 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003802 if (unlikely(pp_noline)) {
3803 free_tlist(origline);
3804 return DIRECTIVE_FOUND;
3805 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003806 tline = tline->next;
3807 skip_white_(tline);
3808 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003809 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003810 free_tlist(origline);
3811 return DIRECTIVE_FOUND;
3812 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003813 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003814 m = 1;
3815 tline = tline->next;
3816 if (tok_is_(tline, "+")) {
3817 tline = tline->next;
3818 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003819 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003820 free_tlist(origline);
3821 return DIRECTIVE_FOUND;
3822 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003823 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003824 tline = tline->next;
3825 }
3826 skip_white_(tline);
3827 src_set_linnum(k);
3828 istk->lineinc = m;
3829 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003830 char *fname = detoken(tline, false);
3831 src_set_fname(fname);
3832 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003833 }
3834 free_tlist(origline);
3835 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003836
H. Peter Anvine2c80182005-01-15 22:15:51 +00003837 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003838 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003839 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003840 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003841}
3842
3843/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003844 * Ensure that a macro parameter contains a condition code and
3845 * nothing else. Return the condition code index if so, or -1
3846 * otherwise.
3847 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003848static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003849{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003850 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003851
H. Peter Anvin25a99342007-09-22 17:45:45 -07003852 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003853 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003854
H. Peter Anvineba20a72002-04-30 20:53:55 +00003855 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003856 if (!t)
3857 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003858 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003859 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003860 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003861 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003862 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003863 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003864
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003865 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003866}
3867
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003868/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003869 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003870 * pasting, if @handle_explicit passed then explicit pasting
3871 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003872 * The @m array can contain a series of token types which are
3873 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003874 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003875static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003876 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003877{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003878 Token *tok, *next, **prev_next, **prev_nonspace;
3879 bool pasted = false;
3880 char *buf, *p;
3881 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003882
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003883 /*
3884 * The last token before pasting. We need it
3885 * to be able to connect new handled tokens.
3886 * In other words if there were a tokens stream
3887 *
3888 * A -> B -> C -> D
3889 *
3890 * and we've joined tokens B and C, the resulting
3891 * stream should be
3892 *
3893 * A -> BC -> D
3894 */
3895 tok = *head;
3896 prev_next = NULL;
3897
3898 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3899 prev_nonspace = head;
3900 else
3901 prev_nonspace = NULL;
3902
3903 while (tok && (next = tok->next)) {
3904
3905 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003906 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003907 /* Zap redundant whitespaces */
3908 while (tok_type_(next, TOK_WHITESPACE))
3909 next = delete_Token(next);
3910 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003911 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003912
3913 case TOK_PASTE:
3914 /* Explicit pasting */
3915 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003916 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003917 next = delete_Token(tok);
3918
3919 while (tok_type_(next, TOK_WHITESPACE))
3920 next = delete_Token(next);
3921
3922 if (!pasted)
3923 pasted = true;
3924
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003925 /* Left pasting token is start of line */
3926 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003927 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003928
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003929 /*
3930 * No ending token, this might happen in two
3931 * cases
3932 *
3933 * 1) There indeed no right token at all
3934 * 2) There is a bare "%define ID" statement,
3935 * and @ID does expand to whitespace.
3936 *
3937 * So technically we need to do a grammar analysis
3938 * in another stage of parsing, but for now lets don't
3939 * change the behaviour people used to. Simply allow
3940 * whitespace after paste token.
3941 */
3942 if (!next) {
3943 /*
3944 * Zap ending space tokens and that's all.
3945 */
3946 tok = (*prev_nonspace)->next;
3947 while (tok_type_(tok, TOK_WHITESPACE))
3948 tok = delete_Token(tok);
3949 tok = *prev_nonspace;
3950 tok->next = NULL;
3951 break;
3952 }
3953
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003954 tok = *prev_nonspace;
3955 while (tok_type_(tok, TOK_WHITESPACE))
3956 tok = delete_Token(tok);
3957 len = strlen(tok->text);
3958 len += strlen(next->text);
3959
3960 p = buf = nasm_malloc(len + 1);
3961 strcpy(p, tok->text);
3962 p = strchr(p, '\0');
3963 strcpy(p, next->text);
3964
3965 delete_Token(tok);
3966
3967 tok = tokenize(buf);
3968 nasm_free(buf);
3969
3970 *prev_nonspace = tok;
3971 while (tok && tok->next)
3972 tok = tok->next;
3973
3974 tok->next = delete_Token(next);
3975
3976 /* Restart from pasted tokens head */
3977 tok = *prev_nonspace;
3978 break;
3979
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003980 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003981 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003982 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003983 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3984 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003985
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003986 len = 0;
3987 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3988 len += strlen(next->text);
3989 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003990 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003991
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003992 /* No match or no text to process */
3993 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003994 break;
3995
3996 len += strlen(tok->text);
3997 p = buf = nasm_malloc(len + 1);
3998
Adam Majer1a069432017-07-25 11:12:35 +02003999 strcpy(p, tok->text);
4000 p = strchr(p, '\0');
4001 tok = delete_Token(tok);
4002
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004003 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02004004 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
4005 strcpy(p, tok->text);
4006 p = strchr(p, '\0');
4007 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004008 tok = delete_Token(tok);
4009 }
4010
4011 tok = tokenize(buf);
4012 nasm_free(buf);
4013
4014 if (prev_next)
4015 *prev_next = tok;
4016 else
4017 *head = tok;
4018
4019 /*
4020 * Connect pasted into original stream,
4021 * ie A -> new-tokens -> B
4022 */
4023 while (tok && tok->next)
4024 tok = tok->next;
4025 tok->next = next;
4026
4027 if (!pasted)
4028 pasted = true;
4029
4030 /* Restart from pasted tokens head */
4031 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04004032 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004033
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004034 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07004035 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004036
4037 prev_next = &tok->next;
4038
4039 if (tok->next &&
4040 !tok_type_(tok->next, TOK_WHITESPACE) &&
4041 !tok_type_(tok->next, TOK_PASTE))
4042 prev_nonspace = prev_next;
4043
4044 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07004045 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04004046
4047 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07004048}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004049
4050/*
4051 * expands to a list of tokens from %{x:y}
4052 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004053static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004054{
4055 Token *t = tline, **tt, *tm, *head;
4056 char *pos;
4057 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004058
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004059 pos = strchr(tline->text, ':');
4060 nasm_assert(pos);
4061
4062 lst = atoi(pos + 1);
4063 fst = atoi(tline->text + 1);
4064
4065 /*
4066 * only macros params are accounted so
4067 * if someone passes %0 -- we reject such
4068 * value(s)
4069 */
4070 if (lst == 0 || fst == 0)
4071 goto err;
4072
4073 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004074 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
4075 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004076 goto err;
4077
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004078 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
4079 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004080
4081 /* counted from zero */
4082 fst--, lst--;
4083
4084 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004085 * It will be at least one token. Note we
4086 * need to scan params until separator, otherwise
4087 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004088 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004089 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03004090 if (!tm)
4091 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004092 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004093 tt = &head->next, tm = tm->next;
4094 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004095 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004096 *tt = t, tt = &t->next, tm = tm->next;
4097 }
4098
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004099 if (fst < lst) {
4100 for (i = fst + 1; i <= lst; i++) {
4101 t = new_Token(NULL, TOK_OTHER, ",", 0);
4102 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004103 j = (i + mac->rotate) % mac->nparam;
4104 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004105 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004106 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004107 *tt = t, tt = &t->next, tm = tm->next;
4108 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004109 }
4110 } else {
4111 for (i = fst - 1; i >= lst; i--) {
4112 t = new_Token(NULL, TOK_OTHER, ",", 0);
4113 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004114 j = (i + mac->rotate) % mac->nparam;
4115 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004116 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004117 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04004118 *tt = t, tt = &t->next, tm = tm->next;
4119 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004120 }
4121 }
4122
4123 *last = tt;
4124 return head;
4125
4126err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004127 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004128 &tline->text[1]);
4129 return tline;
4130}
4131
H. Peter Anvin76690a12002-04-30 20:52:49 +00004132/*
4133 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004134 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004135 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004136 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004137static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004138{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004139 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004140 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004141 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004142
4143 tail = &thead;
4144 thead = NULL;
4145
H. Peter Anvine2c80182005-01-15 22:15:51 +00004146 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004147 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004148 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4149 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4150 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004151 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004152 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004153 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004154 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004155 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004156 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004157
H. Peter Anvine2c80182005-01-15 22:15:51 +00004158 t = tline;
4159 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004160
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004161 mac = istk->mstk;
4162 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4163 mac = mac->next_active;
4164 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004165 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004166 } else {
4167 pos = strchr(t->text, ':');
4168 if (!pos) {
4169 switch (t->text[1]) {
4170 /*
4171 * We have to make a substitution of one of the
4172 * forms %1, %-1, %+1, %%foo, %0.
4173 */
4174 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004175 type = TOK_NUMBER;
4176 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4177 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004178 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004179 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004180 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004181 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004182 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004183 text = nasm_strcat(tmpbuf, t->text + 2);
4184 break;
4185 case '-':
4186 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004187 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004188 tt = NULL;
4189 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004190 if (mac->nparam > 1)
4191 n = (n + mac->rotate) % mac->nparam;
4192 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004193 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004194 cc = find_cc(tt);
4195 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004196 nasm_nonfatal("macro parameter %d is not a condition code",
4197 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004198 text = NULL;
4199 } else {
4200 type = TOK_ID;
4201 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004202 nasm_nonfatal("condition code `%s' is not invertible",
4203 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004204 text = NULL;
4205 } else
4206 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4207 }
4208 break;
4209 case '+':
4210 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004211 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004212 tt = NULL;
4213 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004214 if (mac->nparam > 1)
4215 n = (n + mac->rotate) % mac->nparam;
4216 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004217 }
4218 cc = find_cc(tt);
4219 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004220 nasm_nonfatal("macro parameter %d is not a condition code",
4221 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004222 text = NULL;
4223 } else {
4224 type = TOK_ID;
4225 text = nasm_strdup(conditions[cc]);
4226 }
4227 break;
4228 default:
4229 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004230 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004231 tt = NULL;
4232 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004233 if (mac->nparam > 1)
4234 n = (n + mac->rotate) % mac->nparam;
4235 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004236 }
4237 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004238 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004239 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004240 tail = &(*tail)->next;
4241 tt = tt->next;
4242 }
4243 }
4244 text = NULL; /* we've done it here */
4245 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004246 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004247 } else {
4248 /*
4249 * seems we have a parameters range here
4250 */
4251 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004252 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004253 if (head != t) {
4254 *tail = head;
4255 *last = tline;
4256 tline = head;
4257 text = NULL;
4258 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004259 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004260 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004261 if (!text) {
4262 delete_Token(t);
4263 } else {
4264 *tail = t;
4265 tail = &t->next;
4266 t->type = type;
4267 nasm_free(t->text);
4268 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004269 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004270 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004271 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004272 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004273 } else if (tline->type == TOK_INDIRECT) {
4274 t = tline;
4275 tline = tline->next;
4276 tt = tokenize(t->text);
4277 tt = expand_mmac_params(tt);
4278 tt = expand_smacro(tt);
4279 *tail = tt;
4280 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004281 tail = &tt->next;
4282 tt = tt->next;
4283 }
4284 delete_Token(t);
4285 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004286 } else {
4287 t = *tail = tline;
4288 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004289 tail = &t->next;
4290 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004291 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004292 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004293
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004294 if (changed) {
4295 const struct tokseq_match t[] = {
4296 {
4297 PP_CONCAT_MASK(TOK_ID) |
4298 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4299 PP_CONCAT_MASK(TOK_ID) |
4300 PP_CONCAT_MASK(TOK_NUMBER) |
4301 PP_CONCAT_MASK(TOK_FLOAT) |
4302 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4303 },
4304 {
4305 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4306 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4307 }
4308 };
4309 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4310 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004311
H. Peter Anvin76690a12002-04-30 20:52:49 +00004312 return thead;
4313}
4314
4315/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004316 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004317 * a macro at all, it is simply copied to the output and the pointer
4318 * advanced. tpp should be a pointer to a pointer (usually the next
4319 * pointer of the previous token) to the first token. **tpp is updated
4320 * to point to the last token of the expansion, and *tpp updated to
4321 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004322 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004323 * If the expansion is empty, *tpp will be unchanged but **tpp will
4324 * be advanced past the macro call.
4325 *
4326 * The return value equals **tpp.
4327 *
4328 * Return false if no expansion took place; true if it did.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004329 *
4330 */
4331static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004332static int64_t smacro_deadman;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004333
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004334static bool expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004335{
4336 Token **params = NULL;
4337 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004338 Token *tline = **tpp;
4339 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004340 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004341 unsigned int i;
4342 Token *t, *tup, *ttail;
4343 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004344
4345 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004346 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004347
4348 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004349
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004350 if (--smacro_deadman <= 0) {
4351 if (smacro_deadman == 0)
4352 nasm_nonfatal("interminable macro recursion");
4353 goto not_a_macro;
4354 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004355 head = (SMacro *)hash_findix(&smacros, mname);
4356 } else if (tline->type == TOK_PREPROC_ID) {
4357 Context *ctx = get_ctx(mname, &mname);
4358 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4359 } else {
4360 goto not_a_macro;
4361 }
4362
4363 /*
4364 * We've hit an identifier of some sort. First check whether the
4365 * identifier is a single-line macro at all, then think about
4366 * checking for parameters if necessary.
4367 */
4368 list_for_each(m, head) {
4369 if (!mstrcmp(m->name, mname, m->casesense))
4370 break;
4371 }
4372
4373 if (!m) {
4374 goto not_a_macro;
4375 }
4376
4377 /* Parse parameters, if applicable */
4378
4379 params = NULL;
4380 nparam = 0;
4381
4382 if (m->nparam == 0) {
4383 /*
4384 * Simple case: the macro is parameterless.
4385 * Nothing to parse; the expansion code will
4386 * drop the macro name token.
4387 */
4388 } else {
4389 /*
4390 * Complicated case: at least one macro with this name
4391 * exists and takes parameters. We must find the
4392 * parameters in the call, count them, find the SMacro
4393 * that corresponds to that form of the macro call, and
4394 * substitute for the parameters when we expand. What a
4395 * pain.
4396 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004397 Token **phead, **pep;
4398 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004399 int white = 0;
4400 int brackets = 0;
4401 bool bracketed = false;
4402 bool bad_bracket = false;
4403 unsigned int sparam = PARAM_DELTA;
4404
4405 tline = tline->next;
4406
4407 while (tok_type_(tline, TOK_WHITESPACE)) {
4408 tline = tline->next;
4409 }
4410 if (!tok_is_(tline, "(")) {
4411 /*
4412 * This macro wasn't called with parameters: ignore
4413 * the call. (Behaviour borrowed from gnu cpp.)
4414 */
4415 goto not_a_macro;
4416 }
4417
4418 paren = 1;
4419 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004420 nasm_newn(params, sparam);
4421 phead = pep = &params[0];
4422 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004423
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004424 while (paren) {
4425 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004426 char ch;
4427
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004428 tline = tline->next;
4429
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004430 if (!tline) {
4431 nasm_nonfatal("macro call expects terminating `)'");
4432 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004433 }
4434
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004435 ch = 0;
4436 skip = false;
4437
4438 switch (tline->type) {
4439 case TOK_OTHER:
4440 if (!tline->text[1])
4441 ch = tline->text[0];
4442 break;
4443
4444 case TOK_WHITESPACE:
4445 if (brackets || *phead)
4446 white++; /* Keep interior whitespace */
4447 skip = true;
4448 break;
4449
4450 default:
4451 break;
4452 }
4453
4454 switch (ch) {
4455 case ',':
4456 if (!brackets) {
4457 if (++nparam >= sparam) {
4458 sparam += PARAM_DELTA;
4459 params = nasm_realloc(params, sparam * sizeof *params);
4460 }
4461 pep = &params[nparam];
4462 *pep = NULL;
4463 bracketed = false;
4464 skip = true;
4465 }
4466 break;
4467
4468 case '{':
4469 if (!bracketed) {
4470 bracketed = !*phead;
4471 skip = true;
4472 }
4473 brackets++;
4474 break;
4475
4476 case '}':
4477 if (brackets > 0) {
4478 if (!--brackets)
4479 skip = bracketed;
4480 }
4481 break;
4482
4483 case '(':
4484 if (!brackets)
4485 paren++;
4486 break;
4487
4488 case ')':
4489 if (!brackets) {
4490 paren--;
4491 if (!paren) {
4492 skip = true;
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07004493 /* Count the final argument */
4494 nparam++;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004495 }
4496 }
4497 break;
4498
4499 default:
4500 break; /* Normal token */
4501 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004502
4503 if (!skip) {
4504 Token *t;
4505
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004506 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004507
4508 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004509 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004510 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004511 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004512 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004513 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004514 pep = &t->next;
4515 white = 0;
4516 }
4517 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004518
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004519 /*
4520 * Look for a macro matching in both name and parameter count.
4521 * We already know any matches cannot be anywhere before the
4522 * current position of "m", so there is no reason to
4523 * backtrack.
4524 */
4525 while (1) {
4526 if (!m) {
4527 /*!
4528 *!macro-params-single [on] single-line macro calls with wrong parameter count
4529 *! warns about \i{single-line macros} being invoked
4530 *! with the wrong number of parameters.
4531 */
4532 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4533 "single-line macro `%s' exists, "
4534 "but not taking %d parameter%s",
4535 mname, nparam, (nparam == 1) ? "" : "s");
4536 goto not_a_macro;
4537 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004538
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004539 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4540 break; /* It's good */
4541
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004542 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004543 }
4544 }
4545
4546 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004547 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004548
4549 /* Expand each parameter */
4550 m->in_progress = true;
4551
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004552 if (m->eval_param) {
4553 for (i = 0; i < nparam; i++) {
4554 if (m->eval_param[i]) {
4555 /* Evaluate this parameter as a number */
4556 struct ppscan pps;
4557 struct tokenval tokval;
4558 expr *evalresult;
4559 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004560
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004561 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4562 pps.ntokens = -1;
4563 tokval.t_type = TOKEN_INVALID;
4564 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004565
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004566 free_tlist(eval_param);
4567 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004568
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004569 if (!evalresult) {
4570 /* Nothing meaningful to do */
4571 } else if (tokval.t_type) {
4572 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4573 } else if (!is_simple(evalresult)) {
4574 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4575 } else {
4576 params[i] = make_tok_num(reloc_value(evalresult));
4577 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004578 }
4579 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004580 }
4581
4582 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004583 tline = tline->next; /* Remove the macro call from the input */
4584 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004585
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004586 /* Note: we own the expansion this returns. */
4587 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004588
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004589 tup = NULL;
4590 ttail = NULL; /* Pointer to the last token of the expansion */
4591 while (t) {
4592 enum pp_token_type type = t->type;
4593 Token *tnext = t->next;
4594 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004595
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004596 switch (type) {
4597 case TOK_PREPROC_Q:
4598 case TOK_PREPROC_QQ:
4599 t->type = TOK_ID;
4600 nasm_free(t->text);
4601 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4602 t->len = nasm_last_string_len();
4603 t->next = tline;
4604 break;
4605
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004606 case TOK_ID:
4607 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004608 /*
4609 * Chain this into the target line *before* expanding,
4610 * that way we pick up any arguments to the new macro call,
4611 * if applicable.
4612 */
4613 t->next = tline;
4614 tp = &t;
4615 expand_one_smacro(&tp);
4616 if (t == tline)
4617 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004618 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004619
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004620 default:
4621 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004622 unsigned int param = smac_nparam(t->type);
4623 nasm_assert(!tup && param < nparam);
4624 delete_Token(t);
4625 t = NULL;
4626 tup = tnext;
4627 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004628 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004629 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004630 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004631 }
4632
4633 if (t) {
4634 tline = t;
4635 if (!ttail)
4636 ttail = t;
4637 }
4638
4639 if (tnext) {
4640 t = tnext;
4641 } else {
4642 t = tup;
4643 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004644 }
4645 }
4646
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004647 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004648 if (ttail)
4649 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004650
4651 m->in_progress = false;
4652
4653 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004654 free_tlist(mstart);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004655 free_tlist_array(params, nparam);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004656 return true;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004657
4658 /*
4659 * No macro expansion needed; roll back to mstart (if necessary)
4660 * and then advance to the next input token.
4661 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004662not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004663 *tpp = &mstart->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004664 free_tlist_array(params, nparam);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004665 return false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004666}
4667
4668/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004669 * Expand all single-line macro calls made in the given line.
4670 * Return the expanded version of the line. The original is deemed
4671 * to be destroyed in the process. (In reality we'll just move
4672 * Tokens from input to output a lot of the time, rather than
4673 * actually bothering to destroy and replicate.)
4674 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004675static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004676{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004677 smacro_deadman = nasm_limit[LIMIT_MACROS];
4678 return expand_smacro_noreset(tline);
4679}
4680
4681static Token *expand_smacro_noreset(Token * tline)
4682{
4683 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004684 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004685 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004686
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004687 /*
4688 * Trick: we should avoid changing the start token pointer since it can
4689 * be contained in "next" field of other token. Because of this
4690 * we allocate a copy of first token and work with it; at the end of
4691 * routine we copy it back
4692 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004693 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004694 tline = new_Token(org_tline->next, org_tline->type,
4695 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004696 nasm_free(org_tline->text);
4697 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004698 }
4699
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004700
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004701 /*
4702 * Pretend that we always end up doing expansion on the first pass;
4703 * that way %+ get processed. However, if we process %+ before the
4704 * first pass, we end up with things like MACRO %+ TAIL trying to
4705 * look up the macro "MACROTAIL", which we don't want.
4706 */
4707 expanded = true;
4708 thead = tline;
4709 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004710 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004711 {
4712 PP_CONCAT_MASK(TOK_ID) |
4713 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4714 PP_CONCAT_MASK(TOK_ID) |
4715 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4716 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4717 }
4718 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004719
4720 tail = &thead;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004721 while ((t = *tail)) { /* main token loop */
4722
4723 expanded |= expand_one_smacro(&tail);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004724 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004725
4726 if (!expanded) {
4727 tline = thead;
4728 break; /* Done! */
4729 }
4730
4731 /*
4732 * Now scan the entire line and look for successive TOK_IDs
4733 * that resulted after expansion (they can't be produced by
4734 * tokenize()). The successive TOK_IDs should be concatenated.
4735 * Also we look for %+ tokens and concatenate the tokens
4736 * before and after them (without white spaces in between).
4737 */
4738 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004739
4740 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004741 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004742
H. Peter Anvine2c80182005-01-15 22:15:51 +00004743 if (org_tline) {
4744 if (thead) {
4745 *org_tline = *thead;
4746 /* since we just gave text to org_line, don't free it */
4747 thead->text = NULL;
4748 delete_Token(thead);
4749 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004750 /*
4751 * The expression expanded to empty line;
4752 * we can't return NULL because of the "trick" above.
4753 * Just set the line to a single WHITESPACE token.
4754 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004755 memset(org_tline, 0, sizeof(*org_tline));
4756 org_tline->text = NULL;
4757 org_tline->type = TOK_WHITESPACE;
4758 }
4759 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004760 }
4761
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004762 return thead;
4763}
4764
4765/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004766 * Similar to expand_smacro but used exclusively with macro identifiers
4767 * right before they are fetched in. The reason is that there can be
4768 * identifiers consisting of several subparts. We consider that if there
4769 * are more than one element forming the name, user wants a expansion,
4770 * otherwise it will be left as-is. Example:
4771 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004772 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004773 *
4774 * the identifier %$abc will be left as-is so that the handler for %define
4775 * will suck it and define the corresponding value. Other case:
4776 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004777 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004778 *
4779 * In this case user wants name to be expanded *before* %define starts
4780 * working, so we'll expand %$abc into something (if it has a value;
4781 * otherwise it will be left as-is) then concatenate all successive
4782 * PP_IDs into one.
4783 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004784static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004785{
4786 Token *cur, *oldnext = NULL;
4787
H. Peter Anvin734b1882002-04-30 21:01:08 +00004788 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004789 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004790
4791 cur = tline;
4792 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004793 (cur->next->type == TOK_ID ||
4794 cur->next->type == TOK_PREPROC_ID
4795 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004797
4798 /* If identifier consists of just one token, don't expand */
4799 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004800 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004801
H. Peter Anvine2c80182005-01-15 22:15:51 +00004802 if (cur) {
4803 oldnext = cur->next; /* Detach the tail past identifier */
4804 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004805 }
4806
H. Peter Anvin734b1882002-04-30 21:01:08 +00004807 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004808
H. Peter Anvine2c80182005-01-15 22:15:51 +00004809 if (cur) {
4810 /* expand_smacro possibly changhed tline; re-scan for EOL */
4811 cur = tline;
4812 while (cur && cur->next)
4813 cur = cur->next;
4814 if (cur)
4815 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004816 }
4817
4818 return tline;
4819}
4820
4821/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004822 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004823 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004824 * to check for an initial label - that's taken care of in
4825 * expand_mmacro - but must check numbers of parameters. Guaranteed
4826 * to be called with tline->type == TOK_ID, so the putative macro
4827 * name is easy to find.
4828 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004829static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004830{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004831 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004832 Token **params;
4833 int nparam;
4834
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004835 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004836
4837 /*
4838 * Efficiency: first we see if any macro exists with the given
4839 * name. If not, we can return NULL immediately. _Then_ we
4840 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004841 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004842 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004843 list_for_each(m, head)
4844 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004845 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004846 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004847 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004848
4849 /*
4850 * OK, we have a potential macro. Count and demarcate the
4851 * parameters.
4852 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004853 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854
4855 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004856 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004857 * structure that handles this number.
4858 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004859 while (m) {
4860 if (m->nparam_min <= nparam
4861 && (m->plus || nparam <= m->nparam_max)) {
4862 /*
4863 * This one is right. Just check if cycle removal
4864 * prohibits us using it before we actually celebrate...
4865 */
4866 if (m->in_progress > m->max_depth) {
4867 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004868 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004869 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004870 }
4871 nasm_free(params);
4872 return NULL;
4873 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004874 /*
4875 * It's right, and we can use it. Add its default
4876 * parameters to the end of our list if necessary.
4877 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004878 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004879 params =
4880 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004881 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004882 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004883 while (nparam < m->nparam_min + m->ndefs) {
4884 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004885 nparam++;
4886 }
4887 }
4888 /*
4889 * If we've gone over the maximum parameter count (and
4890 * we're in Plus mode), ignore parameters beyond
4891 * nparam_max.
4892 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004893 if (m->plus && nparam > m->nparam_max)
4894 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004895 /*
4896 * Then terminate the parameter list, and leave.
4897 */
4898 if (!params) { /* need this special case */
4899 params = nasm_malloc(sizeof(*params));
4900 nparam = 0;
4901 }
4902 params[nparam] = NULL;
4903 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004904 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004905 }
4906 /*
4907 * This one wasn't right: look for the next one with the
4908 * same name.
4909 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004910 list_for_each(m, m->next)
4911 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004912 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004913 }
4914
4915 /*
4916 * After all that, we didn't find one with the right number of
4917 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004918 *!
4919 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4920 *! warns about \i{multi-line macros} being invoked
4921 *! with the wrong number of parameters. See \k{mlmacover} for an
4922 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004923 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004924 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4925 "multi-line macro `%s' exists, but not taking %d parameter%s",
4926 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004927 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004928 return NULL;
4929}
4930
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004931
4932/*
4933 * Save MMacro invocation specific fields in
4934 * preparation for a recursive macro expansion
4935 */
4936static void push_mmacro(MMacro *m)
4937{
4938 MMacroInvocation *i;
4939
4940 i = nasm_malloc(sizeof(MMacroInvocation));
4941 i->prev = m->prev;
4942 i->params = m->params;
4943 i->iline = m->iline;
4944 i->nparam = m->nparam;
4945 i->rotate = m->rotate;
4946 i->paramlen = m->paramlen;
4947 i->unique = m->unique;
4948 i->condcnt = m->condcnt;
4949 m->prev = i;
4950}
4951
4952
4953/*
4954 * Restore MMacro invocation specific fields that were
4955 * saved during a previous recursive macro expansion
4956 */
4957static void pop_mmacro(MMacro *m)
4958{
4959 MMacroInvocation *i;
4960
4961 if (m->prev) {
4962 i = m->prev;
4963 m->prev = i->prev;
4964 m->params = i->params;
4965 m->iline = i->iline;
4966 m->nparam = i->nparam;
4967 m->rotate = i->rotate;
4968 m->paramlen = i->paramlen;
4969 m->unique = i->unique;
4970 m->condcnt = i->condcnt;
4971 nasm_free(i);
4972 }
4973}
4974
4975
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004976/*
4977 * Expand the multi-line macro call made by the given line, if
4978 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004979 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004980 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004981static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004982{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004983 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004984 Token *label = NULL;
4985 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004986 Token **params, *t, *tt;
4987 MMacro *m;
4988 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004989 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004990 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004991
4992 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004993 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004994 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004995 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004996 return 0;
4997 m = is_mmacro(t, &params);
4998 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004999 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07005000 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005001 Token *last;
5002 /*
5003 * We have an id which isn't a macro call. We'll assume
5004 * it might be a label; we'll also check to see if a
5005 * colon follows it. Then, if there's another id after
5006 * that lot, we'll check it again for macro-hood.
5007 */
5008 label = last = t;
5009 t = t->next;
5010 if (tok_type_(t, TOK_WHITESPACE))
5011 last = t, t = t->next;
5012 if (tok_is_(t, ":")) {
5013 dont_prepend = 1;
5014 last = t, t = t->next;
5015 if (tok_type_(t, TOK_WHITESPACE))
5016 last = t, t = t->next;
5017 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005018 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
5019 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005020 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05005021 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005022 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005023 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005024
5025 /*
5026 * Fix up the parameters: this involves stripping leading and
5027 * trailing whitespace, then stripping braces if they are
5028 * present.
5029 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005030 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00005031 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005032
H. Peter Anvine2c80182005-01-15 22:15:51 +00005033 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005034 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005035 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005036
H. Peter Anvine2c80182005-01-15 22:15:51 +00005037 t = params[i];
5038 skip_white_(t);
5039 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005040 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005041 params[i] = t;
5042 paramlen[i] = 0;
5043 while (t) {
5044 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
5045 break; /* ... because we have hit a comma */
5046 if (comma && t->type == TOK_WHITESPACE
5047 && tok_is_(t->next, ","))
5048 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005049 if (brace && t->type == TOK_OTHER) {
5050 if (t->text[0] == '{')
5051 brace++; /* ... or a nested opening brace */
5052 else if (t->text[0] == '}')
5053 if (!--brace)
5054 break; /* ... or a brace */
5055 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005056 t = t->next;
5057 paramlen[i]++;
5058 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08005059 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005060 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005061 }
5062
5063 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005064 * OK, we have a MMacro structure together with a set of
5065 * parameters. We must now go through the expansion and push
5066 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00005067 * parameter tokens and macro-local tokens doesn't get done
5068 * until the single-line macro substitution process; this is
5069 * because delaying them allows us to change the semantics
5070 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005071 *
5072 * First, push an end marker on to istk->expansion, mark this
5073 * macro as in progress, and set up its invocation-specific
5074 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005075 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005076 ll = nasm_malloc(sizeof(Line));
5077 ll->next = istk->expansion;
5078 ll->finishes = m;
5079 ll->first = NULL;
5080 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005081
5082 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005083 * Save the previous MMacro expansion in the case of
5084 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005085 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005086 if (m->max_depth && m->in_progress)
5087 push_mmacro(m);
5088
5089 m->in_progress ++;
5090 m->params = params;
5091 m->iline = tline;
5092 m->nparam = nparam;
5093 m->rotate = 0;
5094 m->paramlen = paramlen;
5095 m->unique = unique++;
5096 m->lineno = 0;
5097 m->condcnt = 0;
5098
5099 m->next_active = istk->mstk;
5100 istk->mstk = m;
5101
5102 list_for_each(l, m->expansion) {
5103 Token **tail;
5104
5105 ll = nasm_malloc(sizeof(Line));
5106 ll->finishes = NULL;
5107 ll->next = istk->expansion;
5108 istk->expansion = ll;
5109 tail = &ll->first;
5110
5111 list_for_each(t, l->first) {
5112 Token *x = t;
5113 switch (t->type) {
5114 case TOK_PREPROC_Q:
5115 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005116 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005117 case TOK_PREPROC_QQ:
5118 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
5119 break;
5120 case TOK_PREPROC_ID:
5121 if (t->text[1] == '0' && t->text[2] == '0') {
5122 dont_prepend = -1;
5123 x = label;
5124 if (!x)
5125 continue;
5126 }
5127 /* fall through */
5128 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005129 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005130 break;
5131 }
5132 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005133 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005134 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005135 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005136
5137 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005138 * If we had a label, push it on as the first line of
5139 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005140 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005141 if (label) {
5142 if (dont_prepend < 0)
5143 free_tlist(startline);
5144 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005145 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005146 ll->finishes = NULL;
5147 ll->next = istk->expansion;
5148 istk->expansion = ll;
5149 ll->first = startline;
5150 if (!dont_prepend) {
5151 while (label->next)
5152 label = label->next;
5153 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005154 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005155 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005156 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005157
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005158 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005159
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005160 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005161}
5162
H. Peter Anvin130736c2016-02-17 20:27:41 -08005163/*
5164 * This function adds macro names to error messages, and suppresses
5165 * them if necessary.
5166 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005167static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005168{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005169 /*
5170 * If we're in a dead branch of IF or something like it, ignore the error.
5171 * However, because %else etc are evaluated in the state context
5172 * of the previous branch, errors might get lost:
5173 * %if 0 ... %else trailing garbage ... %endif
5174 * So %else etc should set the ERR_PP_PRECOND flag.
5175 */
5176 if ((severity & ERR_MASK) < ERR_FATAL &&
5177 istk && istk->conds &&
5178 ((severity & ERR_PP_PRECOND) ?
5179 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005180 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005181 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005182
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005183 /* This doesn't make sense with the macro stack unwinding */
5184 if (0) {
5185 MMacro *mmac = NULL;
5186 int32_t delta = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005187
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005188 /* get %macro name */
5189 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
5190 mmac = istk->mstk;
5191 /* but %rep blocks should be skipped */
5192 while (mmac && !mmac->name)
5193 mmac = mmac->next_active, delta++;
5194 }
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005195
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005196 if (mmac) {
5197 char *buf;
5198 nasm_set_verror(real_verror);
5199 buf = nasm_vasprintf(fmt, arg);
5200 nasm_error(severity, "(%s:%"PRId32") %s",
5201 mmac->name, mmac->lineno - delta, buf);
5202 nasm_set_verror(pp_verror);
5203 nasm_free(buf);
5204 return;
5205 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005206 }
H. Peter Anvin (Intel)98031bf2019-08-09 16:11:28 -07005207 real_verror(severity, fmt, arg);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005208}
5209
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005210static Token *
5211stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005212{
5213 (void)s;
5214 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005215 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005216
5217 return make_tok_qstr(src_get_fname());
5218}
5219
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005220static Token *
5221stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005222{
5223 (void)s;
5224 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005225 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005226
5227 return make_tok_num(src_get_linnum());
5228}
5229
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005230static Token *
5231stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005232{
5233 (void)s;
5234 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005235 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005236
5237 return make_tok_num(globalbits);
5238}
5239
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005240static Token *
5241stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005242{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005243 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005244 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5245 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5246 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005247
5248 (void)s;
5249 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005250 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005251
5252 switch (globalbits) {
5253 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005254 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005255 break;
5256 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005257 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005258 break;
5259 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005260 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005261 break;
5262 default:
5263 panic();
5264 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005265
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005266 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005267}
5268
H. Peter Anvin8b262472019-02-26 14:00:54 -08005269/* Add magic standard macros */
5270struct magic_macros {
5271 const char *name;
5272 int nparams;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005273 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005274};
5275static const struct magic_macros magic_macros[] =
5276{
5277 { "__FILE__", 0, stdmac_file },
5278 { "__LINE__", 0, stdmac_line },
5279 { "__BITS__", 0, stdmac_bits },
5280 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005281 { NULL, 0, NULL }
5282};
5283
5284static void pp_add_magic_stdmac(void)
5285{
5286 const struct magic_macros *m;
5287 SMacro *s;
5288
5289 for (m = magic_macros; m->name; m++) {
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005290 s = define_smacro(NULL, m->name, true, m->nparams, NULL, NULL);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005291 s->expand = m->func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005292 }
5293}
5294
H. Peter Anvin734b1882002-04-30 21:01:08 +00005295static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005296pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005297{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005298 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005299
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005300 cstk = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005301 nasm_new(istk);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07005302 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin274cda82016-05-10 02:56:29 -07005303 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005304 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005305 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03005306 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005307 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005308 nested_mac_count = 0;
5309 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005310 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005311 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005312 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005313 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005314
H. Peter Anvin8b262472019-02-26 14:00:54 -08005315 pp_add_magic_stdmac();
5316
H. Peter Anvinf7606612016-07-13 14:23:48 -07005317 if (tasm_compatible_mode)
5318 pp_add_stdmac(nasm_stdmac_tasm);
5319
5320 pp_add_stdmac(nasm_stdmac_nasm);
5321 pp_add_stdmac(nasm_stdmac_version);
5322
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005323 if (extrastdmac)
5324 pp_add_stdmac(extrastdmac);
5325
H. Peter Anvinf7606612016-07-13 14:23:48 -07005326 stdmacpos = stdmacros[0];
5327 stdmacnext = &stdmacros[1];
5328
H. Peter Anvind2456592008-06-19 15:04:18 -07005329 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005330
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03005331 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005332
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005333 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005334 * Define the __PASS__ macro. This is defined here unlike all the
5335 * other builtins, because it is special -- it varies between
5336 * passes -- but there is really no particular reason to make it
5337 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005338 *
5339 * 0 = dependencies only
5340 * 1 = preparatory passes
5341 * 2 = final pass
5342 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005343 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005344 switch (mode) {
5345 case PP_NORMAL:
5346 apass = pass_final() ? 2 : 1;
5347 break;
5348 case PP_DEPS:
5349 apass = 0;
5350 break;
5351 case PP_PREPROC:
5352 apass = 3;
5353 break;
5354 default:
5355 panic();
5356 }
5357
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005358 define_smacro(NULL, "__PASS__", true, 0, NULL, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005359}
5360
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005361static void pp_init(void)
5362{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005363}
5364
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005365/*
5366 * Get a line of tokens. If we popped the macro expansion/include stack,
5367 * we return a pointer to the dummy token tok_pop; at that point if
5368 * istk is NULL then we have reached end of input;
5369 */
5370static Token tok_pop; /* Dummy token placeholder */
5371
5372static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005373{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005374 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005375
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005376 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005377 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005378 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005379 * buffer or from the input file.
5380 */
5381 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005382 while (istk->expansion && istk->expansion->finishes) {
5383 Line *l = istk->expansion;
5384 if (!l->finishes->name && l->finishes->in_progress > 1) {
5385 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005386
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005387 /*
5388 * This is a macro-end marker for a macro with no
5389 * name, which means it's not really a macro at all
5390 * but a %rep block, and the `in_progress' field is
5391 * more than 1, meaning that we still need to
5392 * repeat. (1 means the natural last repetition; 0
5393 * means termination by %exitrep.) We have
5394 * therefore expanded up to the %endrep, and must
5395 * push the whole block on to the expansion buffer
5396 * again. We don't bother to remove the macro-end
5397 * marker: we'd only have to generate another one
5398 * if we did.
5399 */
5400 l->finishes->in_progress--;
5401 list_for_each(l, l->finishes->expansion) {
5402 Token *t, *tt, **tail;
5403
5404 ll = nasm_malloc(sizeof(Line));
5405 ll->next = istk->expansion;
5406 ll->finishes = NULL;
5407 ll->first = NULL;
5408 tail = &ll->first;
5409
5410 list_for_each(t, l->first) {
5411 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005412 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005413 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005414 }
5415 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005416 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005417 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005418 } else {
5419 /*
5420 * Check whether a `%rep' was started and not ended
5421 * within this macro expansion. This can happen and
5422 * should be detected. It's a fatal error because
5423 * I'm too confused to work out how to recover
5424 * sensibly from it.
5425 */
5426 if (defining) {
5427 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005428 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005429 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005430 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005431 " expansion of macro `%s'",
5432 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005433 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005434
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005435 /*
5436 * FIXME: investigate the relationship at this point between
5437 * istk->mstk and l->finishes
5438 */
5439 {
5440 MMacro *m = istk->mstk;
5441 istk->mstk = m->next_active;
5442 if (m->name) {
5443 /*
5444 * This was a real macro call, not a %rep, and
5445 * therefore the parameter information needs to
5446 * be freed.
5447 */
5448 if (m->prev) {
5449 pop_mmacro(m);
5450 l->finishes->in_progress --;
5451 } else {
5452 nasm_free(m->params);
5453 free_tlist(m->iline);
5454 nasm_free(m->paramlen);
5455 l->finishes->in_progress = 0;
5456 }
Adam Majer91e72402017-07-25 10:42:01 +02005457 }
5458
5459 /*
5460 * FIXME It is incorrect to always free_mmacro here.
5461 * It leads to usage-after-free.
5462 *
5463 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5464 */
5465#if 0
5466 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005467 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005468#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005469 }
5470 istk->expansion = l->next;
5471 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005472 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005473 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005474 }
5475 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005476 do { /* until we get a line we can use */
5477 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005478
5479 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005480 Line *l = istk->expansion;
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005481 int32_t lineno;
5482
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005483 if (istk->mstk)
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005484 lineno = ++istk->mstk->lineno + istk->mstk->xline;
5485 else
5486 lineno = src_get_linnum();
5487
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005488 tline = l->first;
5489 istk->expansion = l->next;
5490 nasm_free(l);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005491
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005492 line = detoken(tline, false);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005493 lfmt->line(LIST_MACRO, lineno, line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005494 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);
H. Peter Anvinab6f8312019-08-09 22:31:45 -07005605 lfmt->line(LIST_MACRO, -1, buf);
H. Peter Anvin (Intel)d6e81772019-08-09 08:06:39 -07005606 nasm_free(buf);
5607 }
5608
H. Peter Anvin130736c2016-02-17 20:27:41 -08005609 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005610 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005611}
5612
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005613static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005614{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005615 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005616
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005617 if (defining) {
5618 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005619 nasm_nonfatal("end of file while still defining macro `%s'",
5620 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005621 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005622 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005623 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005624
5625 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005626 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005627 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005628
5629 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005630
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005631 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005632 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005633 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005634 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005635 Include *i = istk;
5636 istk = istk->next;
5637 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005638 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005639 }
5640 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005641 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005642 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005643}
5644
5645static void pp_cleanup_session(void)
5646{
5647 free_llist(predef);
5648 predef = NULL;
5649 delete_Blocks();
5650 freeTokens = NULL;
5651 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005652}
5653
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005654static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005655{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005656 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005657}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005658
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005659static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005660{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005661 Token *inc, *space, *name;
5662 Line *l;
5663
H. Peter Anvin734b1882002-04-30 21:01:08 +00005664 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5665 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5666 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005667
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005668 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005669 l->next = predef;
5670 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005671 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005672 predef = l;
5673}
5674
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005675static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005676{
5677 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005678 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005679 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005680
H. Peter Anvin130736c2016-02-17 20:27:41 -08005681 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005682
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005683 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005684 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5685 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005686 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005687 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005688 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005689 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005690 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005691
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005692 if (space->next->type != TOK_PREPROC_ID &&
5693 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005694 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005695
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005696 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005697 l->next = predef;
5698 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005699 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005700 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005701
5702 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005703}
5704
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005705static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005706{
5707 Token *def, *space;
5708 Line *l;
5709
H. Peter Anvin734b1882002-04-30 21:01:08 +00005710 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5711 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005712 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005713
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005714 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005715 l->next = predef;
5716 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005717 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005718 predef = l;
5719}
5720
H. Peter Anvin05990342018-06-11 13:32:42 -07005721/* Insert an early preprocessor command that doesn't need special handling */
5722static void pp_pre_command(const char *what, char *string)
5723{
5724 char *cmd;
5725 Token *def, *space;
5726 Line *l;
5727
5728 def = tokenize(string);
5729 if (what) {
5730 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5731 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5732 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5733 }
5734
5735 l = nasm_malloc(sizeof(Line));
5736 l->next = predef;
5737 l->first = def;
5738 l->finishes = NULL;
5739 predef = l;
5740}
5741
H. Peter Anvinf7606612016-07-13 14:23:48 -07005742static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005743{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005744 macros_t **mp;
5745
5746 /* Find the end of the list and avoid duplicates */
5747 for (mp = stdmacros; *mp; mp++) {
5748 if (*mp == macros)
5749 return; /* Nothing to do */
5750 }
5751
5752 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5753
5754 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005755}
5756
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005757static void pp_extra_stdmac(macros_t *macros)
5758{
5759 extrastdmac = macros;
5760}
5761
H. Peter Anvin8b262472019-02-26 14:00:54 -08005762static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005763{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005764 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005765 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5766 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5767}
5768
5769static Token *make_tok_qstr(const char *str)
5770{
5771 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005772 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005773 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005774}
5775
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005776static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005777{
5778 if (!m)
5779 return;
5780
5781 /* We need to print the next_active list in reverse order */
5782 pp_list_one_macro(m->next_active, severity);
5783
5784 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005785 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005786 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005787 }
5788}
5789
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005790static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005791{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005792 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005793
H. Peter Anvinddb29062018-12-11 00:06:29 -08005794 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5795 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005796
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005797 if (istk)
5798 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005799
H. Peter Anvinddb29062018-12-11 00:06:29 -08005800 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005801}
5802
H. Peter Anvine7469712016-02-18 02:20:59 -08005803const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005804 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005805 pp_reset,
5806 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005807 pp_cleanup_pass,
5808 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005809 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005810 pp_pre_define,
5811 pp_pre_undefine,
5812 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005813 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005814 pp_include_path,
5815 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005816};