blob: 2dcf06b58af973db85481e1fe7fabda69ed8f8b6 [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)41e96822019-04-25 18:00:32 -070099 * Function call for a magic smacro
100 */
101typedef Token *(*MagicSMacro)(const SMacro *s, Token **params,
102 unsigned int nparams, bool *free_expansion);
103
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;
111 MagicSMacro magic;
112 intorptr magicpvt;
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 Anvin8b262472019-02-26 14:00:54 -0800115 bool casesense;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800116 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;
187 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188};
189
190/*
191 * This is the internal form which we break input lines up into.
192 * Typically stored in linked lists.
193 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800194 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
195 * not necessarily used as-is, but is also used to encode the number
196 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800198 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700199 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000200 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800201 * tok_smac_param(0) but the one representing `y' will be
202 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000203 *
204 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
205 * which doesn't need quotes around it. Used in the pre-include
206 * mechanism as an alternative to trying to find a sensible type of
207 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000208 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000209enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800210 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
211 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800212 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700213 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800214 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300215 TOK_PASTE, /* %+ */
216 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800217 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300218 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000219};
220
H. Peter Anvin8b262472019-02-26 14:00:54 -0800221static inline enum pp_token_type tok_smac_param(int param)
222{
223 return TOK_SMAC_START_PARAMS + param;
224}
225static int smac_nparam(enum pp_token_type toktype)
226{
227 return toktype - TOK_SMAC_START_PARAMS;
228}
229static bool is_smac_param(enum pp_token_type toktype)
230{
231 return toktype >= TOK_SMAC_START_PARAMS;
232}
233
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400234#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400235#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400236
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400237struct tokseq_match {
238 int mask_head;
239 int mask_tail;
240};
241
H. Peter Anvine2c80182005-01-15 22:15:51 +0000242struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 Token *next;
244 char *text;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700245 size_t len;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800246 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000247};
248
249/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800250 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000251 * these, which is essentially a container to allow several linked
252 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700253 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000254 * Note that in this module, linked lists are treated as stacks
255 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800256 * `expansion' field in MMacro structures, so that the linked list,
257 * if walked, would give the macro lines in reverse order; this
258 * means that we can walk the list when expanding a macro, and thus
259 * push the lines on to the `expansion' field in _istk_ in reverse
260 * order (so that when popped back off they are in the right
261 * order). It may seem cockeyed, and it relies on my design having
262 * an even number of steps in, but it works...
263 *
264 * Some of these structures, rather than being actual lines, are
265 * markers delimiting the end of the expansion of a given macro.
266 * This is for use in the cycle-tracking and %rep-handling code.
267 * Such structures have `finishes' non-NULL, and `first' NULL. All
268 * others have `finishes' NULL, but `first' may still be NULL if
269 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800272 Line *next;
273 MMacro *finishes;
274 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * To handle an arbitrary level of file inclusion, we maintain a
279 * stack (ie linked list) of these things.
280 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800282 Include *next;
283 FILE *fp;
284 Cond *conds;
285 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700286 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800287 int lineno, lineinc;
288 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289};
290
291/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700292 * File real name hash, so we don't have to re-search the include
293 * path for every pass (and potentially more than that if a file
294 * is used more than once.)
295 */
296struct hash_table FileHash;
297
298/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000299 * Conditional assembly: we maintain a separate stack of these for
300 * each level of file inclusion. (The only reason we keep the
301 * stacks separate is to ensure that a stray `%endif' in a file
302 * included from within the true branch of a `%if' won't terminate
303 * it and cause confusion: instead, rightly, it'll cause an error.)
304 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305struct Cond {
306 Cond *next;
307 int state;
308};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000309enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310 /*
311 * These states are for use just after %if or %elif: IF_TRUE
312 * means the condition has evaluated to truth so we are
313 * currently emitting, whereas IF_FALSE means we are not
314 * currently emitting but will start doing so if a %else comes
315 * up. In these states, all directives are admissible: %elif,
316 * %else and %endif. (And of course %if.)
317 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800318 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000319 /*
320 * These states come up after a %else: ELSE_TRUE means we're
321 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
322 * any %elif or %else will cause an error.
323 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800324 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200326 * These states mean that we're not emitting now, and also that
327 * nothing until %endif will be emitted at all. COND_DONE is
328 * used when we've had our moment of emission
329 * and have now started seeing %elifs. COND_NEVER is used when
330 * the condition construct in question is contained within a
331 * non-emitting branch of a larger condition construct,
332 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800334 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800336#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
H. Peter Anvin70653092007-10-19 14:42:29 -0700338/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000339 * These defines are used as the possible return values for do_directive
340 */
341#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300342#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000343
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400344/* max reps */
345#define REP_LIMIT ((INT64_C(1) << 62))
346
Keith Kanios852f1ee2009-07-12 00:19:55 -0500347/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 * Condition codes. Note that we use c_ prefix not C_ because C_ is
349 * used in nasm.h for the "real" condition codes. At _this_ level,
350 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
351 * ones, so we need a different enum...
352 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700353static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
355 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000356 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700358enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
360 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700361 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
362 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700364static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
366 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000367 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368};
369
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800370/*
371 * Directive names.
372 */
373/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
374static int is_condition(enum preproc_token arg)
375{
376 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
377}
378
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379/* For TASM compatibility we need to be able to recognise TASM compatible
380 * conditional compilation directives. Using the NASM pre-processor does
381 * not work, so we look for them specifically from the following list and
382 * then jam in the equivalent NASM directive into the input stream.
383 */
384
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000386 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
387 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
388};
389
H. Peter Anvin476d2862007-10-02 22:04:15 -0700390static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000391 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
392 "ifndef", "include", "local"
393};
394
395static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700396static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000397static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800398static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000399
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400static Context *cstk;
401static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300402static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300404static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300406static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800408static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700409static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800410static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000411
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800415static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000416
417/*
418 * The current set of single-line macros we have defined.
419 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700420static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421
422/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800423 * The multi-line macro we are currently defining, or the %rep
424 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427
Charles Crayned4200be2008-07-12 16:42:33 -0700428static uint64_t nested_mac_count;
429static uint64_t nested_rep_count;
430
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000431/*
432 * The number of macro parameters to allocate space for at a time.
433 */
434#define PARAM_DELTA 16
435
436/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700437 * The standard macro set: defined in macros.c in a set of arrays.
438 * This gives our position in any macro set, while we are processing it.
439 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000440 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700441static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700442static macros_t **stdmacnext;
443static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300444static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000445
446/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447 * Tokens are allocated in blocks to improve speed
448 */
449#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800450static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000451struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 Blocks *next;
453 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000454};
455
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800456static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000457
458/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459 * Forward declarations.
460 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700461static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000462static Token *expand_mmac_params(Token * tline);
463static Token *expand_smacro(Token * tline);
464static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400465static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800466static Token *make_tok_num(int64_t val);
467static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800468static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800469static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000470static void *new_Block(size_t size);
471static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700472static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700473 const char *text, size_t txtlen);
474static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000475static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476
477/*
478 * Macros for safe checking of token pointers, avoid *(NULL)
479 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480#define tok_type_(x,t) ((x) && (x)->type == (t))
481#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
482#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
483#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400485/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700486 * nasm_unquote with error if the string contains control characters.
487 * If the string contains control characters, issue an error and return
488 * the C len, i.e. truncate at the control character.
H. Peter Anvin077fb932010-07-20 14:56:30 -0700489 */
490static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
491{
492 size_t len = nasm_unquote(qstr, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700493 size_t i;
H. Peter Anvin077fb932010-07-20 14:56:30 -0700494
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700495 for (i = 0; i < len; i++) {
496 if (qstr[i] < ' ') {
497 nasm_nonfatal("control character in `%s' directive string",
498 pp_directives[directive]);
499 break;
500 }
501 }
502
503 qstr[i] = '\0';
504 return i;
H. Peter Anvin077fb932010-07-20 14:56:30 -0700505}
506
507/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700508 * In-place reverse a list of tokens.
509 */
510static Token *reverse_tokens(Token *t)
511{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800512 Token *prev = NULL;
513 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700514
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800515 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400516 next = t->next;
517 t->next = prev;
518 prev = t;
519 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800520 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700521
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800522 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700523}
524
525/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300526 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527 * front of them. We do it here because I could not find any other
528 * place to do it for the moment, and it is a hack (ideally it would
529 * be nice to be able to use the NASM pre-processor to do it).
530 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000531static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000532{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000533 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400534 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000535
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400536 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000537
538 /* Binary search for the directive name */
539 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400540 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400541 q = nasm_skip_word(p);
542 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 if (len) {
544 oldchar = p[len];
545 p[len] = 0;
546 while (j - i > 1) {
547 k = (j + i) / 2;
548 m = nasm_stricmp(p, tasm_directives[k]);
549 if (m == 0) {
550 /* We have found a directive, so jam a % in front of it
551 * so that NASM will then recognise it as one if it's own.
552 */
553 p[len] = oldchar;
554 len = strlen(p);
555 oldline = line;
556 line = nasm_malloc(len + 2);
557 line[0] = '%';
558 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700559 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300560 * NASM does not recognise IFDIFI, so we convert
561 * it to %if 0. This is not used in NASM
562 * compatible code, but does need to parse for the
563 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700565 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 } else {
567 memcpy(line + 1, p, len + 1);
568 }
569 nasm_free(oldline);
570 return line;
571 } else if (m < 0) {
572 j = k;
573 } else
574 i = k;
575 }
576 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000577 }
578 return line;
579}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000580
H. Peter Anvin76690a12002-04-30 20:52:49 +0000581/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000582 * The pre-preprocessing stage... This function translates line
583 * number indications as they emerge from GNU cpp (`# lineno "file"
584 * flags') into NASM preprocessor line number indications (`%line
585 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000587static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000588{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000590 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000591
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 if (line[0] == '#' && line[1] == ' ') {
593 oldline = line;
594 fname = oldline + 2;
595 lineno = atoi(fname);
596 fname += strspn(fname, "0123456789 ");
597 if (*fname == '"')
598 fname++;
599 fnlen = strcspn(fname, "\"");
600 line = nasm_malloc(20 + fnlen);
601 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
602 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000603 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000604 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000606 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607}
608
609/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000610 * Free a linked list of tokens.
611 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400614 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000616}
617
618/*
619 * Free a linked list of lines.
620 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000622{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400623 Line *l, *tmp;
624 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 free_tlist(l->first);
626 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000627 }
628}
629
630/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800631 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000632 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800633static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000634{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800635 nasm_free(m->name);
636 free_tlist(m->dlist);
637 nasm_free(m->defaults);
638 free_llist(m->expansion);
639 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000640}
641
642/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700643 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800644 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700645static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800646{
647 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700648 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800649 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700650}
651
652static void clear_smacro(SMacro *s)
653{
654 free_smacro_members(s);
655 /* Wipe everything except the next pointer */
656 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
657}
658
659/*
660 * Free an SMacro
661 */
662static void free_smacro(SMacro *s)
663{
664 free_smacro_members(s);
665 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800666}
667
668/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700669 * Free all currently defined macros, and free the hash tables
670 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700671static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700672{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800673 struct hash_iterator it;
674 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700675
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800676 hash_for_each(smt, it, np) {
677 SMacro *tmp;
678 SMacro *s = np->data;
679 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800680 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700681 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700682 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700683 hash_free(smt);
684}
685
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800686static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700687{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800688 struct hash_iterator it;
689 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700690
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800691 hash_for_each(mmt, it, np) {
692 MMacro *tmp;
693 MMacro *m = np->data;
694 nasm_free((void *)np->key);
695 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800696 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700697 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800698 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700699}
700
701static void free_macros(void)
702{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700703 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800704 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700705}
706
707/*
708 * Initialize the hash tables
709 */
710static void init_macros(void)
711{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700712}
713
714/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000715 * Pop the context stack.
716 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000718{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000719 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000720
721 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700722 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000723 nasm_free(c->name);
724 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000725}
726
H. Peter Anvin072771e2008-05-22 13:17:51 -0700727/*
728 * Search for a key in the hash index; adding it if necessary
729 * (in which case we initialize the data pointer to NULL.)
730 */
731static void **
732hash_findi_add(struct hash_table *hash, const char *str)
733{
734 struct hash_insert hi;
735 void **r;
736 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800737 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700738
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800739 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700740 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300741 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700742
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800743 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
744 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700745 return hash_add(&hi, strx, NULL);
746}
747
748/*
749 * Like hash_findi, but returns the data element rather than a pointer
750 * to it. Used only when not adding a new element, hence no third
751 * argument.
752 */
753static void *
754hash_findix(struct hash_table *hash, const char *str)
755{
756 void **p;
757
758 p = hash_findi(hash, str, NULL);
759 return p ? *p : NULL;
760}
761
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400762/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800763 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400764 * if there no more left -- return NULL
765 */
766static char *line_from_stdmac(void)
767{
768 unsigned char c;
769 const unsigned char *p = stdmacpos;
770 char *line, *q;
771 size_t len = 0;
772
773 if (!stdmacpos)
774 return NULL;
775
776 while ((c = *p++)) {
777 if (c >= 0x80)
778 len += pp_directives_len[c - 0x80] + 1;
779 else
780 len++;
781 }
782
783 line = nasm_malloc(len + 1);
784 q = line;
785 while ((c = *stdmacpos++)) {
786 if (c >= 0x80) {
787 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
788 q += pp_directives_len[c - 0x80];
789 *q++ = ' ';
790 } else {
791 *q++ = c;
792 }
793 }
794 stdmacpos = p;
795 *q = '\0';
796
797 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700798 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400799 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700800 if (*stdmacnext) {
801 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400802 } else if (do_predef) {
803 Line *pd, *l;
804 Token *head, **tail, *t;
805
806 /*
807 * Nasty hack: here we push the contents of
808 * `predef' on to the top-level expansion stack,
809 * since this is the most convenient way to
810 * implement the pre-include and pre-define
811 * features.
812 */
813 list_for_each(pd, predef) {
814 head = NULL;
815 tail = &head;
816 list_for_each(t, pd->first) {
817 *tail = new_Token(NULL, t->type, t->text, 0);
818 tail = &(*tail)->next;
819 }
820
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800821 l = nasm_malloc(sizeof(Line));
822 l->next = istk->expansion;
823 l->first = head;
824 l->finishes = NULL;
825
826 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400827 }
828 do_predef = false;
829 }
830 }
831
832 return line;
833}
834
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000835static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000836{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700837 int c;
838 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400839 const unsigned int delta = 512;
840 const unsigned int pad = 8;
841 unsigned int nr_cont = 0;
842 bool cont = false;
843 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000844
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400845 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400846 p = line_from_stdmac();
847 if (p)
848 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700849
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400850 size = delta;
851 p = buffer = nasm_malloc(size);
852
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700853 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400854 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400855
856 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700857 case EOF:
858 if (p == buffer) {
859 nasm_free(buffer);
860 return NULL;
861 }
862 c = 0;
863 break;
864
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400865 case '\r':
866 next = fgetc(istk->fp);
867 if (next != '\n')
868 ungetc(next, istk->fp);
869 if (cont) {
870 cont = false;
871 continue;
872 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700873 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400874 break;
875
876 case '\n':
877 if (cont) {
878 cont = false;
879 continue;
880 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700881 c = 0;
882 break;
883
884 case 032: /* ^Z = legacy MS-DOS end of file mark */
885 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400886 break;
887
888 case '\\':
889 next = fgetc(istk->fp);
890 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400891 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400892 cont = true;
893 nr_cont++;
894 continue;
895 }
896 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000897 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400898
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400899 if (p >= (buffer + size - pad)) {
900 buffer = nasm_realloc(buffer, size + delta);
901 p = buffer + size - pad;
902 size += delta;
903 }
904
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700905 *p++ = c;
906 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000907
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300908 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400909 (nr_cont * istk->lineinc));
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800910 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000911
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000912 return buffer;
913}
914
915/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000916 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000917 * don't need to parse the value out of e.g. numeric tokens: we
918 * simply split one string into many.
919 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000920static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000921{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700922 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000923 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000924 Token *list = NULL;
925 Token *t, **tail = &list;
926
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 while (*line) {
928 p = line;
929 if (*p == '%') {
930 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300931 if (*p == '+' && !nasm_isdigit(p[1])) {
932 p++;
933 type = TOK_PASTE;
934 } else if (nasm_isdigit(*p) ||
935 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 do {
937 p++;
938 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700939 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 type = TOK_PREPROC_ID;
941 } else if (*p == '{') {
942 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800943 while (*p) {
944 if (*p == '}')
945 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 p[-1] = *p;
947 p++;
948 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800949 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800950 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 p[-1] = '\0';
952 if (*p)
953 p++;
954 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300955 } else if (*p == '[') {
956 int lvl = 1;
957 line += 2; /* Skip the leading %[ */
958 p++;
959 while (lvl && (c = *p++)) {
960 switch (c) {
961 case ']':
962 lvl--;
963 break;
964 case '%':
965 if (*p == '[')
966 lvl++;
967 break;
968 case '\'':
969 case '\"':
970 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300971 p = nasm_skip_string(p - 1);
972 if (*p)
973 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300974 break;
975 default:
976 break;
977 }
978 }
979 p--;
980 if (*p)
981 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800982 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300983 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300984 type = TOK_INDIRECT;
985 } else if (*p == '?') {
986 type = TOK_PREPROC_Q; /* %? */
987 p++;
988 if (*p == '?') {
989 type = TOK_PREPROC_QQ; /* %?? */
990 p++;
991 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400992 } else if (*p == '!') {
993 type = TOK_PREPROC_ID;
994 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -0800995 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400996 do {
997 p++;
998 }
H. Peter Anvin13506202018-11-28 14:55:58 -0800999 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001000 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001001 p = nasm_skip_string(p);
1002 if (*p)
1003 p++;
1004 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001005 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001006 } else {
1007 /* %! without string or identifier */
1008 type = TOK_OTHER; /* Legacy behavior... */
1009 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001010 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001012 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 do {
1014 p++;
1015 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001016 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 type = TOK_PREPROC_ID;
1018 } else {
1019 type = TOK_OTHER;
1020 if (*p == '%')
1021 p++;
1022 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001023 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001024 type = TOK_ID;
1025 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001026 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001028 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 /*
1030 * A string token.
1031 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001033 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001034
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 if (*p) {
1036 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001037 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001038 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 /* Handling unterminated strings by UNV */
1040 /* type = -1; */
1041 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001042 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001043 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001044 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001045 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001046 bool is_hex = false;
1047 bool is_float = false;
1048 bool has_e = false;
1049 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001050
H. Peter Anvine2c80182005-01-15 22:15:51 +00001051 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001052 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001053 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001054
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001055 if (*p == '$') {
1056 p++;
1057 is_hex = true;
1058 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001059
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001060 for (;;) {
1061 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001062
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001063 if (!is_hex && (c == 'e' || c == 'E')) {
1064 has_e = true;
1065 if (*p == '+' || *p == '-') {
1066 /*
1067 * e can only be followed by +/- if it is either a
1068 * prefixed hex number or a floating-point number
1069 */
1070 p++;
1071 is_float = true;
1072 }
1073 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1074 is_hex = true;
1075 } else if (c == 'P' || c == 'p') {
1076 is_float = true;
1077 if (*p == '+' || *p == '-')
1078 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001079 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001080 ; /* just advance */
1081 else if (c == '.') {
1082 /*
1083 * we need to deal with consequences of the legacy
1084 * parser, like "1.nolist" being two tokens
1085 * (TOK_NUMBER, TOK_ID) here; at least give it
1086 * a shot for now. In the future, we probably need
1087 * a flex-based scanner with proper pattern matching
1088 * to do it as well as it can be done. Nothing in
1089 * the world is going to help the person who wants
1090 * 0x123.p16 interpreted as two tokens, though.
1091 */
1092 r = p;
1093 while (*r == '_')
1094 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001095
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001096 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1097 (!is_hex && (*r == 'e' || *r == 'E')) ||
1098 (*r == 'p' || *r == 'P')) {
1099 p = r;
1100 is_float = true;
1101 } else
1102 break; /* Terminate the token */
1103 } else
1104 break;
1105 }
1106 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001107
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001108 if (p == line+1 && *line == '$') {
1109 type = TOK_OTHER; /* TOKEN_HERE */
1110 } else {
1111 if (has_e && !is_hex) {
1112 /* 1e13 is floating-point, but 1e13h is not */
1113 is_float = true;
1114 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001115
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001116 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1117 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001118 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001120 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 /*
1122 * Whitespace just before end-of-line is discarded by
1123 * pretending it's a comment; whitespace just before a
1124 * comment gets lumped into the comment.
1125 */
1126 if (!*p || *p == ';') {
1127 type = TOK_COMMENT;
1128 while (*p)
1129 p++;
1130 }
1131 } else if (*p == ';') {
1132 type = TOK_COMMENT;
1133 while (*p)
1134 p++;
1135 } else {
1136 /*
1137 * Anything else is an operator of some kind. We check
1138 * for all the double-character operators (>>, <<, //,
1139 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001140 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 */
1142 type = TOK_OTHER;
1143 if ((p[0] == '>' && p[1] == '>') ||
1144 (p[0] == '<' && p[1] == '<') ||
1145 (p[0] == '/' && p[1] == '/') ||
1146 (p[0] == '<' && p[1] == '=') ||
1147 (p[0] == '>' && p[1] == '=') ||
1148 (p[0] == '=' && p[1] == '=') ||
1149 (p[0] == '!' && p[1] == '=') ||
1150 (p[0] == '<' && p[1] == '>') ||
1151 (p[0] == '&' && p[1] == '&') ||
1152 (p[0] == '|' && p[1] == '|') ||
1153 (p[0] == '^' && p[1] == '^')) {
1154 p++;
1155 }
1156 p++;
1157 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001158
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 /* Handling unterminated string by UNV */
1160 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001161 {
1162 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1163 t->text[p-line] = *line;
1164 tail = &t->next;
1165 }
1166 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 if (type != TOK_COMMENT) {
1168 *tail = t = new_Token(NULL, type, line, p - line);
1169 tail = &t->next;
1170 }
1171 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001172 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001173 return list;
1174}
1175
H. Peter Anvince616072002-04-30 21:02:23 +00001176/*
1177 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001178 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001179 * deleted only all at once by the delete_Blocks function.
1180 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001182{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001183 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001185 /* first, get to the end of the linked list */
1186 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001188 /* now allocate the requested chunk */
1189 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001191 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001192 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001193 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001194}
1195
1196/*
1197 * this function deletes all managed blocks of memory
1198 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001200{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001202
H. Peter Anvin70653092007-10-19 14:42:29 -07001203 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001204 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001205 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001206 * free it.
1207 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001209 if (b->chunk)
1210 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 a = b;
1212 b = b->next;
1213 if (a != &blocks)
1214 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001215 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001216 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001218
1219/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001220 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001221 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001222 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001223static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001224 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001225{
1226 Token *t;
1227 int i;
1228
H. Peter Anvin89cee572009-07-15 09:16:54 -04001229 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1231 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1232 freeTokens[i].next = &freeTokens[i + 1];
1233 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001234 }
1235 t = freeTokens;
1236 freeTokens = t->next;
1237 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001238 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001239 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001240 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 t->text = NULL;
1242 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001243 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001245 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001246 t->text = nasm_malloc(txtlen+1);
1247 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001249 }
1250 return t;
1251}
1252
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001253static Token *dup_Token(Token *next, const Token *src)
1254{
1255 return new_Token(next, src->type, src->text, src->len);
1256}
1257
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001259{
1260 Token *next = t->next;
1261 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001262 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001263 freeTokens = t;
1264 return next;
1265}
1266
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001267/*
1268 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001269 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1270 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001271 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001272static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001273{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001274 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001275 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001276 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001277 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001278
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001279 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001280 if (t->type == TOK_PREPROC_ID && t->text &&
1281 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001282 char *v;
1283 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001284
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001285 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001286 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001287 size_t len = nasm_unquote(v, NULL);
1288 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001289
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001290 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001291 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001292 v = NULL;
1293 }
1294 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001295
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001296 if (v) {
1297 char *p = getenv(v);
1298 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001299 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001300 /*
1301 * FIXME We better should investigate if accessing
1302 * ->text[1] without ->text[0] is safe enough.
1303 */
1304 t->text = nasm_zalloc(2);
1305 } else
1306 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001307 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001308 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001310
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 /* Expand local macros here and not during preprocessing */
1312 if (expand_locals &&
1313 t->type == TOK_PREPROC_ID && t->text &&
1314 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001315 const char *q;
1316 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001317 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001319 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001320 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001321 p = nasm_strcat(buffer, q);
1322 nasm_free(t->text);
1323 t->text = p;
1324 }
1325 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001326 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001328 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001329 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001330 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001331
H. Peter Anvin734b1882002-04-30 21:01:08 +00001332 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001333
1334 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001336 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001337 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001338 q = t->text;
1339 while (*q)
1340 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001342 }
1343 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001344
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001345 return line;
1346}
1347
1348/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001349 * A scanner, suitable for use by the expression evaluator, which
1350 * operates on a line of Tokens. Expects a pointer to a pointer to
1351 * the first token in the line to be passed in as its private_data
1352 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001353 *
1354 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001355 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001356struct ppscan {
1357 Token *tptr;
1358 int ntokens;
1359};
1360
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001362{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001363 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001364 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001365 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001366
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001368 if (pps->ntokens && (tline = pps->tptr)) {
1369 pps->ntokens--;
1370 pps->tptr = tline->next;
1371 } else {
1372 pps->tptr = NULL;
1373 pps->ntokens = 0;
1374 return tokval->t_type = TOKEN_EOS;
1375 }
1376 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001377
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001378 tokval->t_charptr = tline->text;
1379
H. Peter Anvin76690a12002-04-30 20:52:49 +00001380 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001382 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001384
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001386 p = tokval->t_charptr = tline->text;
1387 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 tokval->t_charptr++;
1389 return tokval->t_type = TOKEN_ID;
1390 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001391
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001392 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001393 if (r >= p+MAX_KEYWORD)
1394 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001395 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001396 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001397 *s = '\0';
1398 /* right, so we have an identifier sitting in temp storage. now,
1399 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001400 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001401 }
1402
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001404 bool rn_error;
1405 tokval->t_integer = readnum(tline->text, &rn_error);
1406 tokval->t_charptr = tline->text;
1407 if (rn_error)
1408 return tokval->t_type = TOKEN_ERRNUM;
1409 else
1410 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001411 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001412
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001413 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001414 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001415 }
1416
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001418 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001419
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001420 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001421 tokval->t_charptr = tline->text;
1422 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001423
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001424 if (ep[0] != bq || ep[1] != '\0')
1425 return tokval->t_type = TOKEN_ERRSTR;
1426 else
1427 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001428 }
1429
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 if (tline->type == TOK_OTHER) {
1431 if (!strcmp(tline->text, "<<"))
1432 return tokval->t_type = TOKEN_SHL;
1433 if (!strcmp(tline->text, ">>"))
1434 return tokval->t_type = TOKEN_SHR;
1435 if (!strcmp(tline->text, "//"))
1436 return tokval->t_type = TOKEN_SDIV;
1437 if (!strcmp(tline->text, "%%"))
1438 return tokval->t_type = TOKEN_SMOD;
1439 if (!strcmp(tline->text, "=="))
1440 return tokval->t_type = TOKEN_EQ;
1441 if (!strcmp(tline->text, "<>"))
1442 return tokval->t_type = TOKEN_NE;
1443 if (!strcmp(tline->text, "!="))
1444 return tokval->t_type = TOKEN_NE;
1445 if (!strcmp(tline->text, "<="))
1446 return tokval->t_type = TOKEN_LE;
1447 if (!strcmp(tline->text, ">="))
1448 return tokval->t_type = TOKEN_GE;
1449 if (!strcmp(tline->text, "&&"))
1450 return tokval->t_type = TOKEN_DBL_AND;
1451 if (!strcmp(tline->text, "^^"))
1452 return tokval->t_type = TOKEN_DBL_XOR;
1453 if (!strcmp(tline->text, "||"))
1454 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001455 }
1456
1457 /*
1458 * We have no other options: just return the first character of
1459 * the token text.
1460 */
1461 return tokval->t_type = tline->text[0];
1462}
1463
1464/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001465 * Compare a string to the name of an existing macro; this is a
1466 * simple wrapper which calls either strcmp or nasm_stricmp
1467 * depending on the value of the `casesense' parameter.
1468 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001469static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001470{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001471 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001472}
1473
1474/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001475 * Compare a string to the name of an existing macro; this is a
1476 * simple wrapper which calls either strcmp or nasm_stricmp
1477 * depending on the value of the `casesense' parameter.
1478 */
1479static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1480{
1481 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1482}
1483
1484/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001485 * Return the Context structure associated with a %$ token. Return
1486 * NULL, having _already_ reported an error condition, if the
1487 * context stack isn't deep enough for the supplied number of $
1488 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001489 *
1490 * If "namep" is non-NULL, set it to the pointer to the macro name
1491 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001492 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001493static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001494{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001495 Context *ctx;
1496 int i;
1497
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001498 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001499 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001500
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001501 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001503
H. Peter Anvine2c80182005-01-15 22:15:51 +00001504 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001505 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001506 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001507 }
1508
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001509 name += 2;
1510 ctx = cstk;
1511 i = 0;
1512 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001513 name++;
1514 i++;
1515 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001516 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001517 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001518 nasm_nonfatal("`%s': context stack is only"
1519 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001521 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001522
1523 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001524 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001525
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001526 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001527}
1528
1529/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001530 * Open an include file. This routine must always return a valid
1531 * file pointer if it returns - it's responsible for throwing an
1532 * ERR_FATAL and bombing out completely if not. It should also try
1533 * the include path one by one until it finds the file or reaches
1534 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001535 *
1536 * Note: for INC_PROBE the function returns NULL at all times;
1537 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001538 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001539enum incopen_mode {
1540 INC_NEEDED, /* File must exist */
1541 INC_OPTIONAL, /* Missing is OK */
1542 INC_PROBE /* Only an existence probe */
1543};
1544
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001545/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001546static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001547 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001548{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001549 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001550 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001551 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001552 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001553 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001554
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001556 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001557 if (omode == INC_PROBE) {
1558 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001559 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001560 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001561 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001562 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001563 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001564 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001565 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001567 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001568
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001569 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001570
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001571 if (!ip) {
1572 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001573 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001574 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001575
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001576 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001577 ip = ip->next;
1578 }
1579}
1580
1581/*
1582 * Open a file, or test for the presence of one (depending on omode),
1583 * considering the include path.
1584 */
1585static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001586 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001587 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001588 enum incopen_mode omode,
1589 enum file_flags fmode)
1590{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001591 struct hash_insert hi;
1592 void **hp;
1593 char *path;
1594 FILE *fp = NULL;
1595
1596 hp = hash_find(&FileHash, file, &hi);
1597 if (hp) {
1598 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001599 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001600 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001601 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001602 } else {
1603 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001604 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001605
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001606 /* Positive or negative result */
1607 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001608
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001609 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001610 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001611 */
1612 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001613 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001614 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001615
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001616 if (!path) {
1617 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001618 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001619 } else {
1620 if (!fp && omode != INC_PROBE)
1621 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001622 }
1623
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001624 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001625 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001626
1627 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001628}
1629
1630/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001631 * Opens an include or input file. Public version, for use by modules
1632 * that get a file:lineno pair and need to look at the file again
1633 * (e.g. the CodeView debug backend). Returns NULL on failure.
1634 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001635FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001636{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001637 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001638}
1639
1640/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001641 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001642 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001643 * return true if _any_ single-line macro of that name is defined.
1644 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001645 * `nparam' or no parameters is defined.
1646 *
1647 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001648 * defined, or nparam is -1, the address of the definition structure
1649 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001650 * is NULL, no action will be taken regarding its contents, and no
1651 * error will occur.
1652 *
1653 * Note that this is also called with nparam zero to resolve
1654 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001655 *
1656 * If you already know which context macro belongs to, you can pass
1657 * the context pointer as first parameter; if you won't but name begins
1658 * with %$ the context will be automatically computed. If all_contexts
1659 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001660 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001661static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001662smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001663 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001664{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001665 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001666 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001667
H. Peter Anvin97a23472007-09-16 17:57:25 -07001668 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001669 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001670 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001671 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001672 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001673 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001674 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001675 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001676 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001677 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001678 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001679 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001680
H. Peter Anvine2c80182005-01-15 22:15:51 +00001681 while (m) {
1682 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001683 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001684 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001685 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001686 *defn = m;
1687 else
1688 *defn = NULL;
1689 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001690 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001691 }
1692 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001693 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001694
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001695 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001696}
1697
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001698/* param should be a natural number [0; INT_MAX] */
1699static int read_param_count(const char *str)
1700{
1701 int result;
1702 bool err;
1703
1704 result = readnum(str, &err);
1705 if (result < 0 || result > INT_MAX) {
1706 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001707 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1708 str, 0, INT_MAX);
1709 } else if (err)
1710 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001711 return result;
1712}
1713
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001714/*
1715 * Count and mark off the parameters in a multi-line macro call.
1716 * This is called both from within the multi-line macro expansion
1717 * code, and also to mark off the default parameters when provided
1718 * in a %macro definition line.
1719 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001721{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001722 int paramsize, brace;
1723
1724 *nparam = paramsize = 0;
1725 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001727 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001728 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 paramsize += PARAM_DELTA;
1730 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1731 }
1732 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001733 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001734 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001735 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001736 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001737 if (brace) {
1738 while (brace && (t = t->next) != NULL) {
1739 if (tok_is_(t, "{"))
1740 brace++;
1741 else if (tok_is_(t, "}"))
1742 brace--;
1743 }
1744
1745 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 /*
1747 * Now we've found the closing brace, look further
1748 * for the comma.
1749 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001750 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 skip_white_(t);
1752 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001753 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 while (tok_isnt_(t, ","))
1755 t = t->next;
1756 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001758 } else {
1759 while (tok_isnt_(t, ","))
1760 t = t->next;
1761 }
1762 if (t) { /* got a comma/brace */
1763 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001765 }
1766}
1767
1768/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001769 * Determine whether one of the various `if' conditions is true or
1770 * not.
1771 *
1772 * We must free the tline we get passed.
1773 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001774static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001775{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001776 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001777 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001778 Token *t, *tt, *origline;
1779 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001780 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001781 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001782 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001783 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001784
1785 origline = tline;
1786
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001788 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001789 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001790 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001792 if (!tline)
1793 break;
1794 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001795 nasm_nonfatal("`%s' expects context identifiers",
1796 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 free_tlist(origline);
1798 return -1;
1799 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001800 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001801 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 tline = tline->next;
1803 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001804 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001805
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001806 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001807 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001808 while (tline) {
1809 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 if (!tline || (tline->type != TOK_ID &&
1811 (tline->type != TOK_PREPROC_ID ||
1812 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001813 nasm_nonfatal("`%s' expects macro identifiers",
1814 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001815 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001817 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001818 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001820 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001821 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001822
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001823 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001824 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001825 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001826 while (tline) {
1827 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001828 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001829 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001830 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001831 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001832 nasm_nonfatal("`%s' expects environment variable names",
1833 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001834 goto fail;
1835 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001836 p = tline->text;
1837 if (tline->type == TOK_PREPROC_ID)
1838 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001839 if (nasm_isquote(*p))
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001840 nasm_unquote_cstr(p, ct);
1841 if (getenv(p))
1842 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001843 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001844 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001845 break;
1846
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001847 case PPC_IFIDN:
1848 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 tline = expand_smacro(tline);
1850 t = tt = tline;
1851 while (tok_isnt_(tt, ","))
1852 tt = tt->next;
1853 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001854 nasm_nonfatal("`%s' expects two comma-separated arguments",
1855 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001856 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 }
1858 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001859 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1861 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001862 nasm_nonfatal("`%s': more than one comma on line",
1863 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001864 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 }
1866 if (t->type == TOK_WHITESPACE) {
1867 t = t->next;
1868 continue;
1869 }
1870 if (tt->type == TOK_WHITESPACE) {
1871 tt = tt->next;
1872 continue;
1873 }
1874 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001875 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 break;
1877 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001878 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001880 size_t l1 = nasm_unquote(t->text, NULL);
1881 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001882
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001883 if (l1 != l2) {
1884 j = false;
1885 break;
1886 }
1887 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1888 j = false;
1889 break;
1890 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001891 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001892 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001893 break;
1894 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001895
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 t = t->next;
1897 tt = tt->next;
1898 }
1899 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001900 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001901 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001902
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001903 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001904 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001905 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001906 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001907
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001908 skip_white_(tline);
1909 tline = expand_id(tline);
1910 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001911 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001912 goto fail;
1913 }
1914 searching.name = nasm_strdup(tline->text);
1915 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001916 searching.plus = false;
1917 searching.nolist = false;
1918 searching.in_progress = 0;
1919 searching.max_depth = 0;
1920 searching.rep_nest = NULL;
1921 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 searching.nparam_max = INT_MAX;
1923 tline = expand_smacro(tline->next);
1924 skip_white_(tline);
1925 if (!tline) {
1926 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001927 nasm_nonfatal("`%s' expects a parameter count or nothing",
1928 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001929 } else {
1930 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001931 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001932 }
1933 if (tline && tok_is_(tline->next, "-")) {
1934 tline = tline->next->next;
1935 if (tok_is_(tline, "*"))
1936 searching.nparam_max = INT_MAX;
1937 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001938 nasm_nonfatal("`%s' expects a parameter count after `-'",
1939 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001940 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001941 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001942 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001943 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001944 searching.nparam_max = searching.nparam_min;
1945 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001946 }
1947 }
1948 if (tline && tok_is_(tline->next, "+")) {
1949 tline = tline->next;
1950 searching.plus = true;
1951 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001952 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1953 while (mmac) {
1954 if (!strcmp(mmac->name, searching.name) &&
1955 (mmac->nparam_min <= searching.nparam_max
1956 || searching.plus)
1957 && (searching.nparam_min <= mmac->nparam_max
1958 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001959 found = true;
1960 break;
1961 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001962 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 }
1964 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001965 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001966 nasm_free(searching.name);
1967 j = found;
1968 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001969 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001970
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001971 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 needtype = TOK_ID;
1973 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001974 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001975 needtype = TOK_NUMBER;
1976 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001977 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 needtype = TOK_STRING;
1979 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001980
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981iftype:
1982 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001983
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 while (tok_type_(t, TOK_WHITESPACE) ||
1985 (needtype == TOK_NUMBER &&
1986 tok_type_(t, TOK_OTHER) &&
1987 (t->text[0] == '-' || t->text[0] == '+') &&
1988 !t->text[1]))
1989 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001990
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001991 j = tok_type_(t, needtype);
1992 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001993
1994 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 t = tline = expand_smacro(tline);
1996 while (tok_type_(t, TOK_WHITESPACE))
1997 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001998
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001999 j = false;
2000 if (t) {
2001 t = t->next; /* Skip the actual token */
2002 while (tok_type_(t, TOK_WHITESPACE))
2003 t = t->next;
2004 j = !t; /* Should be nothing left */
2005 }
2006 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002007
H. Peter Anvin134b9462008-02-16 17:01:40 -08002008 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002009 t = tline = expand_smacro(tline);
2010 while (tok_type_(t, TOK_WHITESPACE))
2011 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002012
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002013 j = !t; /* Should be empty */
2014 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002015
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002016 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002017 pps.tptr = tline = expand_smacro(tline);
2018 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002020 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 if (!evalresult)
2022 return -1;
2023 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002024 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002026 nasm_nonfatal("non-constant value given to `%s'",
2027 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002028 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002030 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002031 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002032
H. Peter Anvine2c80182005-01-15 22:15:51 +00002033 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002034 nasm_fatal("preprocessor directive `%s' not yet implemented",
2035 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002036 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002037 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002038
2039 free_tlist(origline);
2040 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002041
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002042fail:
2043 free_tlist(origline);
2044 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002045}
2046
2047/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002048 * Common code for defining an smacro
2049 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002050static SMacro *define_smacro(Context *ctx, const char *mname,
2051 bool casesense, int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002052{
2053 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002054 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002055
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002056 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002057 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002058 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002059 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002060 /*
2061 * Some instances of the old code considered this a failure,
2062 * some others didn't. What is the right thing to do here?
2063 */
2064 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002065 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002066 } else {
2067 /*
2068 * We're redefining, so we have to take over an
2069 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002070 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002071 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002072 clear_smacro(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002073 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002074 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002075 smtbl = ctx ? &ctx->localmac : &smacros;
2076 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002077 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002078 smac->next = *smhead;
2079 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002080 }
2081 smac->name = nasm_strdup(mname);
2082 smac->casesense = casesense;
2083 smac->nparam = nparam;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002084 smac->expansion = expansion;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002085 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002086}
2087
2088/*
2089 * Undefine an smacro
2090 */
2091static void undef_smacro(Context *ctx, const char *mname)
2092{
2093 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002094 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002095
H. Peter Anvin166c2472008-05-28 12:28:58 -07002096 smtbl = ctx ? &ctx->localmac : &smacros;
2097 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002098
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002099 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002100 /*
2101 * We now have a macro name... go hunt for it.
2102 */
2103 sp = smhead;
2104 while ((s = *sp) != NULL) {
2105 if (!mstrcmp(s->name, mname, s->casesense)) {
2106 *sp = s->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002107 free_smacro(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 } else {
2109 sp = &s->next;
2110 }
2111 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002112 }
2113}
2114
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002115/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002116 * Parse a mmacro specification.
2117 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002118static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002119{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002120 tline = tline->next;
2121 skip_white_(tline);
2122 tline = expand_id(tline);
2123 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002124 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002125 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002126 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002127
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002128 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002129 def->name = nasm_strdup(tline->text);
2130 def->plus = false;
2131 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002132 def->in_progress = 0;
2133 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002134 def->nparam_min = 0;
2135 def->nparam_max = 0;
2136
H. Peter Anvina26433d2008-07-16 14:40:01 -07002137 tline = expand_smacro(tline->next);
2138 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002139 if (!tok_type_(tline, TOK_NUMBER))
2140 nasm_nonfatal("`%s' expects a parameter count", directive);
2141 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002142 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002143 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002144 tline = tline->next->next;
2145 if (tok_is_(tline, "*")) {
2146 def->nparam_max = INT_MAX;
2147 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002148 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002149 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002150 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002151 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002152 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002153 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002154 }
2155 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002156 }
2157 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002158 tline = tline->next;
2159 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002160 }
2161 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002162 !nasm_stricmp(tline->next->text, ".nolist")) {
2163 tline = tline->next;
2164 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002165 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002166
H. Peter Anvina26433d2008-07-16 14:40:01 -07002167 /*
2168 * Handle default parameters.
2169 */
2170 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002171 def->dlist = tline->next;
2172 tline->next = NULL;
2173 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002174 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002175 def->dlist = NULL;
2176 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002177 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002178 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002179
H. Peter Anvin89cee572009-07-15 09:16:54 -04002180 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002181 !def->plus) {
2182 /*
2183 *!macro-defaults [on] macros with more default than optional parameters
2184 *! warns when a macro has more default parameters than optional parameters.
2185 *! See \k{mlmacdef} for why might want to disable this warning.
2186 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002187 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002188 "too many default macro parameters in macro `%s'", def->name);
2189 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002190
H. Peter Anvina26433d2008-07-16 14:40:01 -07002191 return true;
2192}
2193
2194
2195/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002196 * Decode a size directive
2197 */
2198static int parse_size(const char *str) {
2199 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002200 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002201 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002202 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002203 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002204}
2205
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002206/*
2207 * Process a preprocessor %pragma directive. Currently there are none.
2208 * Gets passed the token list starting with the "preproc" token from
2209 * "%pragma preproc".
2210 */
2211static void do_pragma_preproc(Token *tline)
2212{
2213 /* Skip to the real stuff */
2214 tline = tline->next;
2215 skip_white_(tline);
2216 if (!tline)
2217 return;
2218
2219 (void)tline; /* Nothing else to do at present */
2220}
2221
Ed Beroset3ab3f412002-06-11 03:31:49 +00002222/**
2223 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002224 * Find out if a line contains a preprocessor directive, and deal
2225 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002226 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002227 * If a directive _is_ found, it is the responsibility of this routine
2228 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002229 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002230 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002231 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002232 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002233 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002234 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002235static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002236{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002237 enum preproc_token i;
2238 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002239 bool err;
2240 int nparam;
2241 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002242 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002243 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002244 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002245 char *p, *pp;
2246 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002247 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002248 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002249 Include *inc;
2250 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002251 Cond *cond;
2252 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002253 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002254 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002255 struct tokenval tokval;
2256 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002257 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002258 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002259 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002260 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002261 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002262
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002263 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002264 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002265
H. Peter Anvineba20a72002-04-30 20:53:55 +00002266 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002267 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002268 (tline->text[0] && (tline->text[1] == '%' ||
2269 tline->text[1] == '$' ||
2270 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002271 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002272
H. Peter Anvin4169a472007-09-12 01:29:43 +00002273 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002274
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002275 /*
2276 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2277 * since they are known to be buggy at moment, we need to fix them
2278 * in future release (2.09-2.10)
2279 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002280 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002281 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2282 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002283 }
2284
2285 /*
2286 * If we're in a non-emitting branch of a condition construct,
2287 * or walking to the end of an already terminated %rep block,
2288 * we should ignore all directives except for condition
2289 * directives.
2290 */
2291 if (((istk->conds && !emitting(istk->conds->state)) ||
2292 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2293 return NO_DIRECTIVE_FOUND;
2294 }
2295
2296 /*
2297 * If we're defining a macro or reading a %rep block, we should
2298 * ignore all directives except for %macro/%imacro (which nest),
2299 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2300 * If we're in a %rep block, another %rep nests, so should be let through.
2301 */
2302 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2303 i != PP_RMACRO && i != PP_IRMACRO &&
2304 i != PP_ENDMACRO && i != PP_ENDM &&
2305 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2306 return NO_DIRECTIVE_FOUND;
2307 }
2308
2309 if (defining) {
2310 if (i == PP_MACRO || i == PP_IMACRO ||
2311 i == PP_RMACRO || i == PP_IRMACRO) {
2312 nested_mac_count++;
2313 return NO_DIRECTIVE_FOUND;
2314 } else if (nested_mac_count > 0) {
2315 if (i == PP_ENDMACRO) {
2316 nested_mac_count--;
2317 return NO_DIRECTIVE_FOUND;
2318 }
2319 }
2320 if (!defining->name) {
2321 if (i == PP_REP) {
2322 nested_rep_count++;
2323 return NO_DIRECTIVE_FOUND;
2324 } else if (nested_rep_count > 0) {
2325 if (i == PP_ENDREP) {
2326 nested_rep_count--;
2327 return NO_DIRECTIVE_FOUND;
2328 }
2329 }
2330 }
2331 }
2332
H. Peter Anvin8b262472019-02-26 14:00:54 -08002333 dname = pp_directives[i]; /* Directive name, for error messages */
2334 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002335 switch (i) {
2336 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002337 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002338 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002339
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002340 case PP_PRAGMA:
2341 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002342 * %pragma namespace options...
2343 *
2344 * The namespace "preproc" is reserved for the preprocessor;
2345 * all other namespaces generate a [pragma] assembly directive.
2346 *
2347 * Invalid %pragmas are ignored and may have different
2348 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002349 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002350 tline = tline->next;
2351 skip_white_(tline);
2352 tline = expand_smacro(tline);
2353 if (tok_type_(tline, TOK_ID)) {
2354 if (!nasm_stricmp(tline->text, "preproc")) {
2355 /* Preprocessor pragma */
2356 do_pragma_preproc(tline);
2357 } else {
2358 /* Build the assembler directive */
2359 t = new_Token(NULL, TOK_OTHER, "[", 1);
2360 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2361 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2362 tline = t;
2363 for (t = tline; t->next; t = t->next)
2364 ;
2365 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002366 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002367 }
2368 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002369 free_tlist(origline);
2370 return DIRECTIVE_FOUND;
2371
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 case PP_STACKSIZE:
2373 /* Directive to tell NASM what the default stack size is. The
2374 * default is for a 16-bit stack, and this can be overriden with
2375 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 */
2377 tline = tline->next;
2378 if (tline && tline->type == TOK_WHITESPACE)
2379 tline = tline->next;
2380 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002381 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 free_tlist(origline);
2383 return DIRECTIVE_FOUND;
2384 }
2385 if (nasm_stricmp(tline->text, "flat") == 0) {
2386 /* All subsequent ARG directives are for a 32-bit stack */
2387 StackSize = 4;
2388 StackPointer = "ebp";
2389 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002390 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002391 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2392 /* All subsequent ARG directives are for a 64-bit stack */
2393 StackSize = 8;
2394 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002395 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002396 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002397 } else if (nasm_stricmp(tline->text, "large") == 0) {
2398 /* All subsequent ARG directives are for a 16-bit stack,
2399 * far function call.
2400 */
2401 StackSize = 2;
2402 StackPointer = "bp";
2403 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002404 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405 } else if (nasm_stricmp(tline->text, "small") == 0) {
2406 /* All subsequent ARG directives are for a 16-bit stack,
2407 * far function call. We don't support near functions.
2408 */
2409 StackSize = 2;
2410 StackPointer = "bp";
2411 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002412 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002414 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415 free_tlist(origline);
2416 return DIRECTIVE_FOUND;
2417 }
2418 free_tlist(origline);
2419 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002420
H. Peter Anvine2c80182005-01-15 22:15:51 +00002421 case PP_ARG:
2422 /* TASM like ARG directive to define arguments to functions, in
2423 * the following form:
2424 *
2425 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2426 */
2427 offset = ArgOffset;
2428 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002429 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002430 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002431
H. Peter Anvine2c80182005-01-15 22:15:51 +00002432 /* Find the argument name */
2433 tline = tline->next;
2434 if (tline && tline->type == TOK_WHITESPACE)
2435 tline = tline->next;
2436 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002437 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002438 free_tlist(origline);
2439 return DIRECTIVE_FOUND;
2440 }
2441 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002442
H. Peter Anvine2c80182005-01-15 22:15:51 +00002443 /* Find the argument size type */
2444 tline = tline->next;
2445 if (!tline || tline->type != TOK_OTHER
2446 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002447 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 free_tlist(origline);
2449 return DIRECTIVE_FOUND;
2450 }
2451 tline = tline->next;
2452 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002453 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 free_tlist(origline);
2455 return DIRECTIVE_FOUND;
2456 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002457
H. Peter Anvine2c80182005-01-15 22:15:51 +00002458 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002459 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002461 size = parse_size(tt->text);
2462 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002463 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002464 free_tlist(tt);
2465 free_tlist(origline);
2466 return DIRECTIVE_FOUND;
2467 }
2468 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002469
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002470 /* Round up to even stack slots */
2471 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002472
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 /* Now define the macro for the argument */
2474 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2475 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002476 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002478
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 /* Move to the next argument in the list */
2480 tline = tline->next;
2481 if (tline && tline->type == TOK_WHITESPACE)
2482 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002483 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002484 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 free_tlist(origline);
2486 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002487
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 case PP_LOCAL:
2489 /* TASM like LOCAL directive to define local variables for a
2490 * function, in the following form:
2491 *
2492 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2493 *
2494 * The '= LocalSize' at the end is ignored by NASM, but is
2495 * required by TASM to define the local parameter size (and used
2496 * by the TASM macro package).
2497 */
2498 offset = LocalOffset;
2499 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002500 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002501 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002502
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 /* Find the argument name */
2504 tline = tline->next;
2505 if (tline && tline->type == TOK_WHITESPACE)
2506 tline = tline->next;
2507 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002508 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002509 free_tlist(origline);
2510 return DIRECTIVE_FOUND;
2511 }
2512 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002513
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 /* Find the argument size type */
2515 tline = tline->next;
2516 if (!tline || tline->type != TOK_OTHER
2517 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002518 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 free_tlist(origline);
2520 return DIRECTIVE_FOUND;
2521 }
2522 tline = tline->next;
2523 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002524 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 free_tlist(origline);
2526 return DIRECTIVE_FOUND;
2527 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002528
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002530 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002532 size = parse_size(tt->text);
2533 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002534 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 free_tlist(tt);
2536 free_tlist(origline);
2537 return DIRECTIVE_FOUND;
2538 }
2539 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002540
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002541 /* Round up to even stack slots */
2542 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002543
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002544 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002545
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002546 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002547 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2548 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002549 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002550
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 /* Now define the assign to setup the enter_c macro correctly */
2552 snprintf(directive, sizeof(directive),
2553 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002554 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002555
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 /* Move to the next argument in the list */
2557 tline = tline->next;
2558 if (tline && tline->type == TOK_WHITESPACE)
2559 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002560 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002561 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 free_tlist(origline);
2563 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002564
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 case PP_CLEAR:
2566 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002567 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 free_macros();
2569 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002570 free_tlist(origline);
2571 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002572
H. Peter Anvin418ca702008-05-30 10:42:30 -07002573 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002574 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002575 skip_white_(t);
2576 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002577 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002578 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002579 free_tlist(origline);
2580 return DIRECTIVE_FOUND; /* but we did _something_ */
2581 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002582 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002583 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002584 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002585 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002586 nasm_unquote_cstr(p, i);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002587 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002588 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002589 return DIRECTIVE_FOUND;
2590
2591 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002593 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002594
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002595 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002596 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002597 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002598 free_tlist(origline);
2599 return DIRECTIVE_FOUND; /* but we did _something_ */
2600 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002601 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002602 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002604 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002606 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002607 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002608 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002609 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002610 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002611 (pp_mode == PP_DEPS)
2612 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002613 if (!inc->fp) {
2614 /* -MG given but file not found */
2615 nasm_free(inc);
2616 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002617 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002618 inc->lineno = src_set_linnum(0);
2619 inc->lineinc = 1;
2620 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002621 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002622 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002623 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002624 }
2625 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002626 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002627
H. Peter Anvind2456592008-06-19 15:04:18 -07002628 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002629 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002630 static macros_t *use_pkg;
2631 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002632
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002633 tline = tline->next;
2634 skip_white_(tline);
2635 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002636
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002637 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002638 tline->type != TOK_INTERNAL_STRING &&
2639 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002640 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002641 free_tlist(origline);
2642 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002643 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002644 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002645 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002646 if (tline->type == TOK_STRING)
2647 nasm_unquote_cstr(tline->text, i);
2648 use_pkg = nasm_stdmac_find_package(tline->text);
2649 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002650 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002651 else
2652 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002653 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002654 /* Not already included, go ahead and include it */
2655 stdmacpos = use_pkg;
2656 }
2657 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002658 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002659 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002661 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002662 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002663 tline = tline->next;
2664 skip_white_(tline);
2665 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002666 if (tline) {
2667 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002668 nasm_nonfatal("`%s' expects a context identifier",
2669 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002670 free_tlist(origline);
2671 return DIRECTIVE_FOUND; /* but we did _something_ */
2672 }
2673 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002674 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002675 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002676 p = nasm_strdup(tline->text);
2677 } else {
2678 p = NULL; /* Anonymous */
2679 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002680
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002681 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002682 nasm_new(ctx);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002684 ctx->name = p;
2685 ctx->number = unique++;
2686 cstk = ctx;
2687 } else {
2688 /* %pop or %repl */
2689 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002690 nasm_nonfatal("`%s': context stack is empty",
2691 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002692 } else if (i == PP_POP) {
2693 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002694 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002695 "expected %s",
2696 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 else
2698 ctx_pop();
2699 } else {
2700 /* i == PP_REPL */
2701 nasm_free(cstk->name);
2702 cstk->name = p;
2703 p = NULL;
2704 }
2705 nasm_free(p);
2706 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002707 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002708 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002709 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002710 severity = ERR_FATAL;
2711 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002713 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002715 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002716 /*!
2717 *!user [on] %warning directives
2718 *! controls output of \c{%warning} directives (see \k{pperror}).
2719 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002720 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002722
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002724 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002725 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002726 tline->next = expand_smacro(tline->next);
2727 tline = tline->next;
2728 skip_white_(tline);
2729 t = tline ? tline->next : NULL;
2730 skip_white_(t);
2731 if (tok_type_(tline, TOK_STRING) && !t) {
2732 /* The line contains only a quoted string */
2733 p = tline->text;
2734 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002735 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002736 } else {
2737 /* Not a quoted string, or more than a quoted string */
2738 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002739 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002740 nasm_free(p);
2741 }
2742 free_tlist(origline);
2743 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002744 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002745
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002746 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002747 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002748 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002749 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002750 j = if_condition(tline->next, i);
2751 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002752 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002753 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002754 cond = nasm_malloc(sizeof(Cond));
2755 cond->next = istk->conds;
2756 cond->state = j;
2757 istk->conds = cond;
2758 if(istk->mstk)
2759 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002760 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002761 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002762
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002763 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002764 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002765 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002766 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002767 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002768 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002769 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002770
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002771 case COND_DONE:
2772 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002773 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002774
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002775 case COND_ELSE_TRUE:
2776 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002777 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002778 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002779 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002780 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002781
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002782 case COND_IF_FALSE:
2783 /*
2784 * IMPORTANT: In the case of %if, we will already have
2785 * called expand_mmac_params(); however, if we're
2786 * processing an %elif we must have been in a
2787 * non-emitting mode, which would have inhibited
2788 * the normal invocation of expand_mmac_params().
2789 * Therefore, we have to do it explicitly here.
2790 */
2791 j = if_condition(expand_mmac_params(tline->next), i);
2792 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002793 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002794 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2795 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002797 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002798 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002799
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 case PP_ELSE:
2801 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002802 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002803 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002804 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002805 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002806 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002807 case COND_IF_TRUE:
2808 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002809 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002810 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002811
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002812 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002813 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002814
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002815 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002816 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002817 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002818
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002819 case COND_ELSE_TRUE:
2820 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002821 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002822 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002824 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002825 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002826 free_tlist(origline);
2827 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002828
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 case PP_ENDIF:
2830 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002831 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002832 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002833 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002834 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002835 cond = istk->conds;
2836 istk->conds = cond->next;
2837 nasm_free(cond);
2838 if(istk->mstk)
2839 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 free_tlist(origline);
2841 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002842
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002843 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002845 casesense = false;
2846 /* fall through */
2847 case PP_RMACRO:
2848 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002849 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002850 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002851 return DIRECTIVE_FOUND;
2852 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002853 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002854 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2855 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002856 defining->casesense = casesense;
2857 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002858 nasm_free(defining);
2859 defining = NULL;
2860 return DIRECTIVE_FOUND;
2861 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002862
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002863 src_get(&defining->xline, &defining->fname);
2864
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002865 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2866 while (mmac) {
2867 if (!strcmp(mmac->name, defining->name) &&
2868 (mmac->nparam_min <= defining->nparam_max
2869 || defining->plus)
2870 && (defining->nparam_min <= mmac->nparam_max
2871 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002872 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002873 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002874 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002875 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002876 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002877 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 free_tlist(origline);
2879 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002880
H. Peter Anvine2c80182005-01-15 22:15:51 +00002881 case PP_ENDM:
2882 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002883 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002884 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002885 return DIRECTIVE_FOUND;
2886 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002887 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2888 defining->next = *mmhead;
2889 *mmhead = defining;
2890 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002891 free_tlist(origline);
2892 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002893
H. Peter Anvin89cee572009-07-15 09:16:54 -04002894 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002895 /*
2896 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002897 * macro-end marker for a macro with a name. Then we
2898 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002899 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002900 list_for_each(l, istk->expansion)
2901 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002902 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002903
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002904 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002905 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002906 * Remove all conditional entries relative to this
2907 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002908 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002909 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2910 cond = istk->conds;
2911 istk->conds = cond->next;
2912 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002913 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002914 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002915 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002916 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002917 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002918 free_tlist(origline);
2919 return DIRECTIVE_FOUND;
2920
H. Peter Anvina26433d2008-07-16 14:40:01 -07002921 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002922 casesense = false;
2923 /* fall through */
2924 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002925 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002926 MMacro **mmac_p;
2927 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002928
H. Peter Anvin8b262472019-02-26 14:00:54 -08002929 spec.casesense = casesense;
2930 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002931 return DIRECTIVE_FOUND;
2932 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002933 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2934 while (mmac_p && *mmac_p) {
2935 mmac = *mmac_p;
2936 if (mmac->casesense == spec.casesense &&
2937 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2938 mmac->nparam_min == spec.nparam_min &&
2939 mmac->nparam_max == spec.nparam_max &&
2940 mmac->plus == spec.plus) {
2941 *mmac_p = mmac->next;
2942 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002943 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002944 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002945 }
2946 }
2947 free_tlist(origline);
2948 free_tlist(spec.dlist);
2949 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002950 }
2951
H. Peter Anvine2c80182005-01-15 22:15:51 +00002952 case PP_ROTATE:
2953 if (tline->next && tline->next->type == TOK_WHITESPACE)
2954 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002955 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002956 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002957 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002958 return DIRECTIVE_FOUND;
2959 }
2960 t = expand_smacro(tline->next);
2961 tline->next = NULL;
2962 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002963 pps.tptr = tline = t;
2964 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002965 tokval.t_type = TOKEN_INVALID;
2966 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002967 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002968 free_tlist(tline);
2969 if (!evalresult)
2970 return DIRECTIVE_FOUND;
2971 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002972 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002973 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002974 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 return DIRECTIVE_FOUND;
2976 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002977 mmac = istk->mstk;
2978 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2979 mmac = mmac->next_active;
2980 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002981 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002982 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002983 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002984 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002985 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002986
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002987 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002988 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002989 rotate += mmac->nparam;
2990
2991 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002992 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002993 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00002994
H. Peter Anvine2c80182005-01-15 22:15:51 +00002995 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002996 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002997 do {
2998 tline = tline->next;
2999 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003000
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 if (tok_type_(tline, TOK_ID) &&
3002 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003003 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 do {
3005 tline = tline->next;
3006 } while (tok_type_(tline, TOK_WHITESPACE));
3007 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003008
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003010 pps.tptr = expand_smacro(tline);
3011 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003012 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003013 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003015 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 if (!evalresult) {
3017 free_tlist(origline);
3018 return DIRECTIVE_FOUND;
3019 }
3020 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003021 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003023 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 return DIRECTIVE_FOUND;
3025 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003026 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003027 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003028 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3029 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003030 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003031 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003032 /*!
3033 *!negative-rep [on] regative %rep count
3034 *! warns about negative counts given to the \c{%rep}
3035 *! preprocessor directive.
3036 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003037 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003038 "negative `%%rep' count: %"PRId64, count);
3039 count = 0;
3040 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003041 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003042 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003044 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003045 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 }
3047 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003048
3049 tmp_defining = defining;
3050 defining = nasm_malloc(sizeof(MMacro));
3051 defining->prev = NULL;
3052 defining->name = NULL; /* flags this macro as a %rep block */
3053 defining->casesense = false;
3054 defining->plus = false;
3055 defining->nolist = nolist;
3056 defining->in_progress = count;
3057 defining->max_depth = 0;
3058 defining->nparam_min = defining->nparam_max = 0;
3059 defining->defaults = NULL;
3060 defining->dlist = NULL;
3061 defining->expansion = NULL;
3062 defining->next_active = istk->mstk;
3063 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003065
H. Peter Anvine2c80182005-01-15 22:15:51 +00003066 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003067 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003068 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003069 return DIRECTIVE_FOUND;
3070 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003071
H. Peter Anvine2c80182005-01-15 22:15:51 +00003072 /*
3073 * Now we have a "macro" defined - although it has no name
3074 * and we won't be entering it in the hash tables - we must
3075 * push a macro-end marker for it on to istk->expansion.
3076 * After that, it will take care of propagating itself (a
3077 * macro-end marker line for a macro which is really a %rep
3078 * block will cause the macro to be re-expanded, complete
3079 * with another macro-end marker to ensure the process
3080 * continues) until the whole expansion is forcibly removed
3081 * from istk->expansion by a %exitrep.
3082 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003083 l = nasm_malloc(sizeof(Line));
3084 l->next = istk->expansion;
3085 l->finishes = defining;
3086 l->first = NULL;
3087 istk->expansion = l;
3088
3089 istk->mstk = defining;
3090
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003091 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003092 tmp_defining = defining;
3093 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003094 free_tlist(origline);
3095 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003096
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003098 /*
3099 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003100 * macro-end marker for a macro with no name. Then we set
3101 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003102 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003103 list_for_each(l, istk->expansion)
3104 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003105 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003106
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003107 if (l)
3108 l->finishes->in_progress = 1;
3109 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003110 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003111 free_tlist(origline);
3112 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003113
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003115 case PP_IXDEFINE:
3116 casesense = false;
3117 /* fall through */
3118 case PP_DEFINE:
3119 case PP_XDEFINE:
3120 {
3121 SMacro *s;
3122 bool have_eval_params = false;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003123
H. Peter Anvine2c80182005-01-15 22:15:51 +00003124 tline = tline->next;
3125 skip_white_(tline);
3126 tline = expand_id(tline);
3127 if (!tline || (tline->type != TOK_ID &&
3128 (tline->type != TOK_PREPROC_ID ||
3129 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003130 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003131 free_tlist(origline);
3132 return DIRECTIVE_FOUND;
3133 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003134
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003135 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003136 last = tline;
3137 param_start = tline = tline->next;
3138 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003139
H. Peter Anvine2c80182005-01-15 22:15:51 +00003140 if (tok_is_(tline, "(")) {
3141 /*
3142 * This macro has parameters.
3143 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003144
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 tline = tline->next;
3146 while (1) {
3147 skip_white_(tline);
3148 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003149 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003150 free_tlist(origline);
3151 return DIRECTIVE_FOUND;
3152 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003153 if (tok_is_(tline, "=")) {
3154 have_eval_params = true;
3155 tline = tline->next;
3156 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003158 nasm_nonfatal("`%s': parameter identifier expected",
3159 tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 free_tlist(origline);
3161 return DIRECTIVE_FOUND;
3162 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003163 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 tline = tline->next;
3165 skip_white_(tline);
3166 if (tok_is_(tline, ",")) {
3167 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003168 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003169 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003170 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003171 free_tlist(origline);
3172 return DIRECTIVE_FOUND;
3173 }
3174 break;
3175 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003176 }
3177 last = tline;
3178 tline = tline->next;
3179 }
3180 if (tok_type_(tline, TOK_WHITESPACE))
3181 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003182 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003183
3184 /* Expand the macro definition now for %xdefine and %ixdefine */
3185 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3186 tline = expand_smacro(tline);
3187
3188 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 t = tline;
3190 while (t) {
3191 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003192 list_for_each(tt, param_start)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003193 if (is_smac_param(tt->type) &&
3194 tt->len == t->len &&
3195 !memcmp(tt->text, t->text, tt->len))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 t->type = tt->type;
3197 }
3198 tt = t->next;
3199 t->next = macro_start;
3200 macro_start = t;
3201 t = tt;
3202 }
3203 /*
3204 * Good. We now have a macro name, a parameter count, and a
3205 * token list (in reverse order) for an expansion. We ought
3206 * to be OK just to create an SMacro, store it, and let
3207 * free_tlist have the rest of the line (which we have
3208 * carefully re-terminated after chopping off the expansion
3209 * from the end).
3210 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003211 s = define_smacro(ctx, mname, casesense, nparam, macro_start);
3212
3213 if (have_eval_params) {
3214 /* Create evaluated parameters table */
3215 bool is_eval = false;
3216
3217 nasm_newn(s->eval_param, nparam);
3218 list_for_each(tt, param_start) {
3219 if (is_smac_param(tt->type))
3220 s->eval_param[smac_nparam(tt->type)] = is_eval;
3221 is_eval = tok_is_(tt, "=");
3222 }
3223 }
3224
3225
H. Peter Anvine2c80182005-01-15 22:15:51 +00003226 free_tlist(origline);
3227 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003228 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003229
H. Peter Anvine2c80182005-01-15 22:15:51 +00003230 case PP_UNDEF:
3231 tline = tline->next;
3232 skip_white_(tline);
3233 tline = expand_id(tline);
3234 if (!tline || (tline->type != TOK_ID &&
3235 (tline->type != TOK_PREPROC_ID ||
3236 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003237 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003238 free_tlist(origline);
3239 return DIRECTIVE_FOUND;
3240 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003241 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003242 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003243
H. Peter Anvine2c80182005-01-15 22:15:51 +00003244 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003245 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003246 undef_smacro(ctx, mname);
3247 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003248 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003249
H. Peter Anvin9e200162008-06-04 17:23:14 -07003250 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003251 casesense = false;
3252 /* fall through */
3253 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003254 tline = tline->next;
3255 skip_white_(tline);
3256 tline = expand_id(tline);
3257 if (!tline || (tline->type != TOK_ID &&
3258 (tline->type != TOK_PREPROC_ID ||
3259 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003260 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003261 free_tlist(origline);
3262 return DIRECTIVE_FOUND;
3263 }
3264
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003265 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003266 last = tline;
3267 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003268 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003269
3270 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003271 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003272
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003273 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003274 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003276
3277 /*
3278 * We now have a macro name, an implicit parameter count of
3279 * zero, and a string token to use as an expansion. Create
3280 * and store an SMacro.
3281 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003282 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003283 free_tlist(origline);
3284 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003285
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003286 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003287 casesense = false;
3288 /* fall through */
3289 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003290 tline = tline->next;
3291 skip_white_(tline);
3292 tline = expand_id(tline);
3293 if (!tline || (tline->type != TOK_ID &&
3294 (tline->type != TOK_PREPROC_ID ||
3295 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003296 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003297 free_tlist(origline);
3298 return DIRECTIVE_FOUND;
3299 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003300 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003301 last = tline;
3302 tline = expand_smacro(tline->next);
3303 last->next = NULL;
3304
3305 t = tline;
3306 while (tok_type_(t, TOK_WHITESPACE))
3307 t = t->next;
3308 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003309 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003310 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003311 free_tlist(tline);
3312 free_tlist(origline);
3313 return DIRECTIVE_FOUND;
3314 }
3315
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003316 /*
3317 * Convert the string to a token stream. Note that smacros
3318 * are stored with the token stream reversed, so we have to
3319 * reverse the output of tokenize().
3320 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003321 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003322 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003323
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003324 /*
3325 * We now have a macro name, an implicit parameter count of
3326 * zero, and a numeric token to use as an expansion. Create
3327 * and store an SMacro.
3328 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003329 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003330 free_tlist(tline);
3331 free_tlist(origline);
3332 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003333
H. Peter Anvin8b262472019-02-26 14:00:54 -08003334 case PP_IPATHSEARCH:
3335 casesense = false;
3336 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003337 case PP_PATHSEARCH:
3338 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003339 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003340
H. Peter Anvin418ca702008-05-30 10:42:30 -07003341 tline = tline->next;
3342 skip_white_(tline);
3343 tline = expand_id(tline);
3344 if (!tline || (tline->type != TOK_ID &&
3345 (tline->type != TOK_PREPROC_ID ||
3346 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003347 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003348 free_tlist(origline);
3349 return DIRECTIVE_FOUND;
3350 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003351 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003352 last = tline;
3353 tline = expand_smacro(tline->next);
3354 last->next = NULL;
3355
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003356 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003357 while (tok_type_(t, TOK_WHITESPACE))
3358 t = t->next;
3359
3360 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003361 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003362 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003363 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003364 free_tlist(origline);
3365 return DIRECTIVE_FOUND; /* but we did _something_ */
3366 }
3367 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003368 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003369 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003370 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003371 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003372
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003373 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003374 if (!found_path)
3375 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003376 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003377
3378 /*
3379 * We now have a macro name, an implicit parameter count of
3380 * zero, and a string token to use as an expansion. Create
3381 * and store an SMacro.
3382 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003383 define_smacro(ctx, mname, casesense, 0, macro_start);
3384 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003385 free_tlist(origline);
3386 return DIRECTIVE_FOUND;
3387 }
3388
H. Peter Anvin8b262472019-02-26 14:00:54 -08003389 case PP_ISTRLEN:
3390 casesense = false;
3391 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003392 case PP_STRLEN:
3393 tline = tline->next;
3394 skip_white_(tline);
3395 tline = expand_id(tline);
3396 if (!tline || (tline->type != TOK_ID &&
3397 (tline->type != TOK_PREPROC_ID ||
3398 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003399 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003400 free_tlist(origline);
3401 return DIRECTIVE_FOUND;
3402 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003403 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003404 last = tline;
3405 tline = expand_smacro(tline->next);
3406 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003407
H. Peter Anvine2c80182005-01-15 22:15:51 +00003408 t = tline;
3409 while (tok_type_(t, TOK_WHITESPACE))
3410 t = t->next;
3411 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003412 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003413 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003414 free_tlist(tline);
3415 free_tlist(origline);
3416 return DIRECTIVE_FOUND;
3417 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003418
H. Peter Anvin8b262472019-02-26 14:00:54 -08003419 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003420
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 /*
3422 * We now have a macro name, an implicit parameter count of
3423 * zero, and a numeric token to use as an expansion. Create
3424 * and store an SMacro.
3425 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003426 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003427 free_tlist(tline);
3428 free_tlist(origline);
3429 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003430
H. Peter Anvin8b262472019-02-26 14:00:54 -08003431 case PP_ISTRCAT:
3432 casesense = false;
3433 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003434 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003435 tline = tline->next;
3436 skip_white_(tline);
3437 tline = expand_id(tline);
3438 if (!tline || (tline->type != TOK_ID &&
3439 (tline->type != TOK_PREPROC_ID ||
3440 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003441 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003442 free_tlist(origline);
3443 return DIRECTIVE_FOUND;
3444 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003445 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003446 last = tline;
3447 tline = expand_smacro(tline->next);
3448 last->next = NULL;
3449
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003450 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003451 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003452 switch (t->type) {
3453 case TOK_WHITESPACE:
3454 break;
3455 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003456 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 break;
3458 case TOK_OTHER:
3459 if (!strcmp(t->text, ",")) /* permit comma separators */
3460 break;
3461 /* else fall through */
3462 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003463 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 free_tlist(tline);
3465 free_tlist(origline);
3466 return DIRECTIVE_FOUND;
3467 }
3468 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003469
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003470 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003471 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003472 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003473 memcpy(p, t->text, t->len);
3474 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003475 }
3476 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003477
3478 /*
3479 * We now have a macro name, an implicit parameter count of
3480 * zero, and a numeric token to use as an expansion. Create
3481 * and store an SMacro.
3482 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003483 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003484 nasm_free(pp);
3485 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003486 free_tlist(tline);
3487 free_tlist(origline);
3488 return DIRECTIVE_FOUND;
3489
H. Peter Anvin8b262472019-02-26 14:00:54 -08003490 case PP_ISUBSTR:
3491 casesense = false;
3492 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003493 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003494 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003495 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003496 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003497
H. Peter Anvine2c80182005-01-15 22:15:51 +00003498 tline = tline->next;
3499 skip_white_(tline);
3500 tline = expand_id(tline);
3501 if (!tline || (tline->type != TOK_ID &&
3502 (tline->type != TOK_PREPROC_ID ||
3503 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003504 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 free_tlist(origline);
3506 return DIRECTIVE_FOUND;
3507 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003508 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003509 last = tline;
3510 tline = expand_smacro(tline->next);
3511 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003512
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003513 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003514 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003515 while (tok_type_(t, TOK_WHITESPACE))
3516 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003517
H. Peter Anvine2c80182005-01-15 22:15:51 +00003518 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003519 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003520 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003521 free_tlist(tline);
3522 free_tlist(origline);
3523 return DIRECTIVE_FOUND;
3524 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003525
H. Peter Anvin8b262472019-02-26 14:00:54 -08003526 pps.tptr = t->next;
3527 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003529 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003530 if (!evalresult) {
3531 free_tlist(tline);
3532 free_tlist(origline);
3533 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003534 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003535 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 free_tlist(tline);
3537 free_tlist(origline);
3538 return DIRECTIVE_FOUND;
3539 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003540 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003541
H. Peter Anvin8b262472019-02-26 14:00:54 -08003542 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3543 pps.tptr = pps.tptr->next;
3544 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003545 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003546 } else {
3547 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003548 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003549 if (!evalresult) {
3550 free_tlist(tline);
3551 free_tlist(origline);
3552 return DIRECTIVE_FOUND;
3553 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003554 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003555 free_tlist(tline);
3556 free_tlist(origline);
3557 return DIRECTIVE_FOUND;
3558 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003559 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003560 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003561
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003562 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003563
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003564 /* make start and count being in range */
3565 if (start < 0)
3566 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003567 if (count < 0)
3568 count = len + count + 1 - start;
3569 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003570 count = len - start;
3571 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003572 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003573
H. Peter Anvin8b262472019-02-26 14:00:54 -08003574 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003575 macro_start->len = count;
3576 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3577 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003578
H. Peter Anvine2c80182005-01-15 22:15:51 +00003579 /*
3580 * We now have a macro name, an implicit parameter count of
3581 * zero, and a numeric token to use as an expansion. Create
3582 * and store an SMacro.
3583 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003584 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003585 free_tlist(tline);
3586 free_tlist(origline);
3587 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003588 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003589
H. Peter Anvine2c80182005-01-15 22:15:51 +00003590 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003591 casesense = false;
3592 /* fall through */
3593 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003594 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", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 free_tlist(origline);
3602 return DIRECTIVE_FOUND;
3603 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003604 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003605 last = tline;
3606 tline = expand_smacro(tline->next);
3607 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003608
H. Peter Anvin8b262472019-02-26 14:00:54 -08003609 pps.tptr = tline;
3610 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003611 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003612 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003613 free_tlist(tline);
3614 if (!evalresult) {
3615 free_tlist(origline);
3616 return DIRECTIVE_FOUND;
3617 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003618
H. Peter Anvine2c80182005-01-15 22:15:51 +00003619 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003620 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003621
H. Peter Anvine2c80182005-01-15 22:15:51 +00003622 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003623 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003624 free_tlist(origline);
3625 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003626 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003627
H. Peter Anvin8b262472019-02-26 14:00:54 -08003628 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003629
H. Peter Anvine2c80182005-01-15 22:15:51 +00003630 /*
3631 * We now have a macro name, an implicit parameter count of
3632 * zero, and a numeric token to use as an expansion. Create
3633 * and store an SMacro.
3634 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003635 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 free_tlist(origline);
3637 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003638
H. Peter Anvine2c80182005-01-15 22:15:51 +00003639 case PP_LINE:
3640 /*
3641 * Syntax is `%line nnn[+mmm] [filename]'
3642 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003643 if (unlikely(pp_noline)) {
3644 free_tlist(origline);
3645 return DIRECTIVE_FOUND;
3646 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003647 tline = tline->next;
3648 skip_white_(tline);
3649 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003650 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003651 free_tlist(origline);
3652 return DIRECTIVE_FOUND;
3653 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003654 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 m = 1;
3656 tline = tline->next;
3657 if (tok_is_(tline, "+")) {
3658 tline = tline->next;
3659 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003660 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 free_tlist(origline);
3662 return DIRECTIVE_FOUND;
3663 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003664 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003665 tline = tline->next;
3666 }
3667 skip_white_(tline);
3668 src_set_linnum(k);
3669 istk->lineinc = m;
3670 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003671 char *fname = detoken(tline, false);
3672 src_set_fname(fname);
3673 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003674 }
3675 free_tlist(origline);
3676 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003677
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003679 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003680 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003681 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003682}
3683
3684/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003685 * Ensure that a macro parameter contains a condition code and
3686 * nothing else. Return the condition code index if so, or -1
3687 * otherwise.
3688 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003689static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003690{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003691 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003692
H. Peter Anvin25a99342007-09-22 17:45:45 -07003693 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003694 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003695
H. Peter Anvineba20a72002-04-30 20:53:55 +00003696 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003697 if (!t)
3698 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003699 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003700 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003701 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003702 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003703 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003704 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003705
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003706 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003707}
3708
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003709/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003710 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003711 * pasting, if @handle_explicit passed then explicit pasting
3712 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003713 * The @m array can contain a series of token types which are
3714 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003715 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003716static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003717 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003718{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003719 Token *tok, *next, **prev_next, **prev_nonspace;
3720 bool pasted = false;
3721 char *buf, *p;
3722 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003723
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003724 /*
3725 * The last token before pasting. We need it
3726 * to be able to connect new handled tokens.
3727 * In other words if there were a tokens stream
3728 *
3729 * A -> B -> C -> D
3730 *
3731 * and we've joined tokens B and C, the resulting
3732 * stream should be
3733 *
3734 * A -> BC -> D
3735 */
3736 tok = *head;
3737 prev_next = NULL;
3738
3739 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3740 prev_nonspace = head;
3741 else
3742 prev_nonspace = NULL;
3743
3744 while (tok && (next = tok->next)) {
3745
3746 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003747 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003748 /* Zap redundant whitespaces */
3749 while (tok_type_(next, TOK_WHITESPACE))
3750 next = delete_Token(next);
3751 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003752 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003753
3754 case TOK_PASTE:
3755 /* Explicit pasting */
3756 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003757 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003758 next = delete_Token(tok);
3759
3760 while (tok_type_(next, TOK_WHITESPACE))
3761 next = delete_Token(next);
3762
3763 if (!pasted)
3764 pasted = true;
3765
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003766 /* Left pasting token is start of line */
3767 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003768 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003769
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003770 /*
3771 * No ending token, this might happen in two
3772 * cases
3773 *
3774 * 1) There indeed no right token at all
3775 * 2) There is a bare "%define ID" statement,
3776 * and @ID does expand to whitespace.
3777 *
3778 * So technically we need to do a grammar analysis
3779 * in another stage of parsing, but for now lets don't
3780 * change the behaviour people used to. Simply allow
3781 * whitespace after paste token.
3782 */
3783 if (!next) {
3784 /*
3785 * Zap ending space tokens and that's all.
3786 */
3787 tok = (*prev_nonspace)->next;
3788 while (tok_type_(tok, TOK_WHITESPACE))
3789 tok = delete_Token(tok);
3790 tok = *prev_nonspace;
3791 tok->next = NULL;
3792 break;
3793 }
3794
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003795 tok = *prev_nonspace;
3796 while (tok_type_(tok, TOK_WHITESPACE))
3797 tok = delete_Token(tok);
3798 len = strlen(tok->text);
3799 len += strlen(next->text);
3800
3801 p = buf = nasm_malloc(len + 1);
3802 strcpy(p, tok->text);
3803 p = strchr(p, '\0');
3804 strcpy(p, next->text);
3805
3806 delete_Token(tok);
3807
3808 tok = tokenize(buf);
3809 nasm_free(buf);
3810
3811 *prev_nonspace = tok;
3812 while (tok && tok->next)
3813 tok = tok->next;
3814
3815 tok->next = delete_Token(next);
3816
3817 /* Restart from pasted tokens head */
3818 tok = *prev_nonspace;
3819 break;
3820
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003821 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003822 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003823 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003824 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3825 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003826
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003827 len = 0;
3828 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3829 len += strlen(next->text);
3830 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003831 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003832
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003833 /* No match or no text to process */
3834 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003835 break;
3836
3837 len += strlen(tok->text);
3838 p = buf = nasm_malloc(len + 1);
3839
Adam Majer1a069432017-07-25 11:12:35 +02003840 strcpy(p, tok->text);
3841 p = strchr(p, '\0');
3842 tok = delete_Token(tok);
3843
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003844 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003845 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3846 strcpy(p, tok->text);
3847 p = strchr(p, '\0');
3848 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003849 tok = delete_Token(tok);
3850 }
3851
3852 tok = tokenize(buf);
3853 nasm_free(buf);
3854
3855 if (prev_next)
3856 *prev_next = tok;
3857 else
3858 *head = tok;
3859
3860 /*
3861 * Connect pasted into original stream,
3862 * ie A -> new-tokens -> B
3863 */
3864 while (tok && tok->next)
3865 tok = tok->next;
3866 tok->next = next;
3867
3868 if (!pasted)
3869 pasted = true;
3870
3871 /* Restart from pasted tokens head */
3872 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003873 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003874
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003875 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003876 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003877
3878 prev_next = &tok->next;
3879
3880 if (tok->next &&
3881 !tok_type_(tok->next, TOK_WHITESPACE) &&
3882 !tok_type_(tok->next, TOK_PASTE))
3883 prev_nonspace = prev_next;
3884
3885 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003886 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003887
3888 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003889}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003890
3891/*
3892 * expands to a list of tokens from %{x:y}
3893 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003894static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003895{
3896 Token *t = tline, **tt, *tm, *head;
3897 char *pos;
3898 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003899
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003900 pos = strchr(tline->text, ':');
3901 nasm_assert(pos);
3902
3903 lst = atoi(pos + 1);
3904 fst = atoi(tline->text + 1);
3905
3906 /*
3907 * only macros params are accounted so
3908 * if someone passes %0 -- we reject such
3909 * value(s)
3910 */
3911 if (lst == 0 || fst == 0)
3912 goto err;
3913
3914 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003915 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3916 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003917 goto err;
3918
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003919 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3920 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003921
3922 /* counted from zero */
3923 fst--, lst--;
3924
3925 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003926 * It will be at least one token. Note we
3927 * need to scan params until separator, otherwise
3928 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003929 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003930 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003931 if (!tm)
3932 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003933 head = new_Token(NULL, tm->type, tm->text, 0);
3934 tt = &head->next, tm = tm->next;
3935 while (tok_isnt_(tm, ",")) {
3936 t = new_Token(NULL, tm->type, tm->text, 0);
3937 *tt = t, tt = &t->next, tm = tm->next;
3938 }
3939
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003940 if (fst < lst) {
3941 for (i = fst + 1; i <= lst; i++) {
3942 t = new_Token(NULL, TOK_OTHER, ",", 0);
3943 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003944 j = (i + mac->rotate) % mac->nparam;
3945 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003946 while (tok_isnt_(tm, ",")) {
3947 t = new_Token(NULL, tm->type, tm->text, 0);
3948 *tt = t, tt = &t->next, tm = tm->next;
3949 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003950 }
3951 } else {
3952 for (i = fst - 1; i >= lst; i--) {
3953 t = new_Token(NULL, TOK_OTHER, ",", 0);
3954 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003955 j = (i + mac->rotate) % mac->nparam;
3956 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003957 while (tok_isnt_(tm, ",")) {
3958 t = new_Token(NULL, tm->type, tm->text, 0);
3959 *tt = t, tt = &t->next, tm = tm->next;
3960 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003961 }
3962 }
3963
3964 *last = tt;
3965 return head;
3966
3967err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003968 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003969 &tline->text[1]);
3970 return tline;
3971}
3972
H. Peter Anvin76690a12002-04-30 20:52:49 +00003973/*
3974 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003975 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003976 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003977 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003978static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003979{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003980 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003981 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003982 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003983
3984 tail = &thead;
3985 thead = NULL;
3986
H. Peter Anvine2c80182005-01-15 22:15:51 +00003987 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003988 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003989 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3990 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3991 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003992 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003993 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003994 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07003995 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003996 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003997 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003998
H. Peter Anvine2c80182005-01-15 22:15:51 +00003999 t = tline;
4000 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004001
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004002 mac = istk->mstk;
4003 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4004 mac = mac->next_active;
4005 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004006 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004007 } else {
4008 pos = strchr(t->text, ':');
4009 if (!pos) {
4010 switch (t->text[1]) {
4011 /*
4012 * We have to make a substitution of one of the
4013 * forms %1, %-1, %+1, %%foo, %0.
4014 */
4015 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004016 type = TOK_NUMBER;
4017 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4018 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004019 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004020 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004021 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004022 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004023 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004024 text = nasm_strcat(tmpbuf, t->text + 2);
4025 break;
4026 case '-':
4027 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004028 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004029 tt = NULL;
4030 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004031 if (mac->nparam > 1)
4032 n = (n + mac->rotate) % mac->nparam;
4033 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004034 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004035 cc = find_cc(tt);
4036 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004037 nasm_nonfatal("macro parameter %d is not a condition code",
4038 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004039 text = NULL;
4040 } else {
4041 type = TOK_ID;
4042 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004043 nasm_nonfatal("condition code `%s' is not invertible",
4044 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004045 text = NULL;
4046 } else
4047 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4048 }
4049 break;
4050 case '+':
4051 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004052 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004053 tt = NULL;
4054 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004055 if (mac->nparam > 1)
4056 n = (n + mac->rotate) % mac->nparam;
4057 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004058 }
4059 cc = find_cc(tt);
4060 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004061 nasm_nonfatal("macro parameter %d is not a condition code",
4062 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004063 text = NULL;
4064 } else {
4065 type = TOK_ID;
4066 text = nasm_strdup(conditions[cc]);
4067 }
4068 break;
4069 default:
4070 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004071 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004072 tt = NULL;
4073 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004074 if (mac->nparam > 1)
4075 n = (n + mac->rotate) % mac->nparam;
4076 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004077 }
4078 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004079 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004080 *tail = new_Token(NULL, tt->type, tt->text, 0);
4081 tail = &(*tail)->next;
4082 tt = tt->next;
4083 }
4084 }
4085 text = NULL; /* we've done it here */
4086 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004087 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004088 } else {
4089 /*
4090 * seems we have a parameters range here
4091 */
4092 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004093 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004094 if (head != t) {
4095 *tail = head;
4096 *last = tline;
4097 tline = head;
4098 text = NULL;
4099 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004100 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004101 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004102 if (!text) {
4103 delete_Token(t);
4104 } else {
4105 *tail = t;
4106 tail = &t->next;
4107 t->type = type;
4108 nasm_free(t->text);
4109 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004110 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004111 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004112 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004113 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004114 } else if (tline->type == TOK_INDIRECT) {
4115 t = tline;
4116 tline = tline->next;
4117 tt = tokenize(t->text);
4118 tt = expand_mmac_params(tt);
4119 tt = expand_smacro(tt);
4120 *tail = tt;
4121 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004122 tail = &tt->next;
4123 tt = tt->next;
4124 }
4125 delete_Token(t);
4126 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004127 } else {
4128 t = *tail = tline;
4129 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004130 tail = &t->next;
4131 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004132 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004133 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004134
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004135 if (changed) {
4136 const struct tokseq_match t[] = {
4137 {
4138 PP_CONCAT_MASK(TOK_ID) |
4139 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4140 PP_CONCAT_MASK(TOK_ID) |
4141 PP_CONCAT_MASK(TOK_NUMBER) |
4142 PP_CONCAT_MASK(TOK_FLOAT) |
4143 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4144 },
4145 {
4146 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4147 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4148 }
4149 };
4150 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4151 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004152
H. Peter Anvin76690a12002-04-30 20:52:49 +00004153 return thead;
4154}
4155
4156/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004157 * Expand *one* single-line macro instance. If the first token is not
4158 * a macro at all, it is simply copied to the output. tpp should be
4159 * a pointer to a pointer (usually the next pointer of the previous token)
4160 * to the first token. **tpp is updated to point to the pointer to first token
4161 * of the expansion, which is also the return value, and then *tpp will point to
4162 * the pointer to the first input token after the expansion.
4163 *
4164 * If the expansion is empty, then these two values point to the same token.
4165 *
4166 */
4167static Token *expand_smacro_noreset(Token * tline);
4168
4169static Token *expand_one_smacro(Token ***tpp)
4170{
4171 Token **params = NULL;
4172 const char *mname;
4173 Token *tline = **tpp;
4174 Token **tnextp;
4175 SMacro *head, *m;
4176 unsigned int nparam, i;
4177 Token *expansion;
4178 Token *t, *mstart, **ttail;
4179 bool free_expansion;
4180
4181 if (!tline)
4182 return NULL;
4183
4184 mname = tline->text;
4185 tnextp = &tline->next; /* By default we shift out one token */
4186
4187 if (tline->type == TOK_ID) {
4188 head = (SMacro *)hash_findix(&smacros, mname);
4189 } else if (tline->type == TOK_PREPROC_ID) {
4190 Context *ctx = get_ctx(mname, &mname);
4191 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4192 } else {
4193 goto not_a_macro;
4194 }
4195
4196 /*
4197 * We've hit an identifier of some sort. First check whether the
4198 * identifier is a single-line macro at all, then think about
4199 * checking for parameters if necessary.
4200 */
4201 list_for_each(m, head) {
4202 if (!mstrcmp(m->name, mname, m->casesense))
4203 break;
4204 }
4205
4206 if (!m) {
4207 goto not_a_macro;
4208 }
4209
4210 /* Parse parameters, if applicable */
4211
4212 params = NULL;
4213 nparam = 0;
4214
4215 if (m->nparam == 0) {
4216 /*
4217 * Simple case: the macro is parameterless.
4218 * Nothing to parse; the expansion code will
4219 * drop the macro name token.
4220 */
4221 } else {
4222 /*
4223 * Complicated case: at least one macro with this name
4224 * exists and takes parameters. We must find the
4225 * parameters in the call, count them, find the SMacro
4226 * that corresponds to that form of the macro call, and
4227 * substitute for the parameters when we expand. What a
4228 * pain.
4229 */
4230 Token **pep;
4231 int paren;
4232 int white = 0;
4233 int brackets = 0;
4234 bool bracketed = false;
4235 bool bad_bracket = false;
4236 unsigned int sparam = PARAM_DELTA;
4237
4238 tline = tline->next;
4239
4240 while (tok_type_(tline, TOK_WHITESPACE)) {
4241 tline = tline->next;
4242 }
4243 if (!tok_is_(tline, "(")) {
4244 /*
4245 * This macro wasn't called with parameters: ignore
4246 * the call. (Behaviour borrowed from gnu cpp.)
4247 */
4248 goto not_a_macro;
4249 }
4250
4251 paren = 1;
4252 nparam = 0;
4253 params = nasm_malloc(sparam * sizeof *params);
4254 params[0] = NULL;
4255 pep = &params[0];
4256
4257 while (true) {
4258 bool skip = false;
4259 char ch;
4260
4261 if (!tline->next) {
4262 nasm_nonfatal("macro call expects terminating `)'");
4263 break;
4264 }
4265 tline = tline->next;
4266
4267 ch = (tline->type == TOK_OTHER && !tline->text[1])
4268 ? tline->text[0] : 0;
4269
4270 if (!brackets) {
4271 if (tline->type == TOK_WHITESPACE) {
4272 if (params[nparam]) {
4273 /* Keep interior whitespace */
4274 white++;
4275 }
4276 skip = true;
4277 }
4278
4279 switch (ch) {
4280 case ',':
4281 if (!paren) {
4282 if (++nparam >= sparam) {
4283 sparam += PARAM_DELTA;
4284 params = nasm_realloc(params, sparam * sizeof *params);
4285 }
4286 params[nparam] = NULL;
4287 pep = &params[nparam];
4288 white = 0;
4289 bracketed = false;
4290 }
4291 break;
4292
4293 case '{':
4294 brackets++;
4295 bracketed = skip = !params[nparam];
4296 break;
4297
4298 case '(':
4299 paren++;
4300 break;
4301
4302 case ')':
4303 paren--;
4304 break;
4305
4306 default:
4307 break; /* Just advance to the next token */
4308 }
4309 } else {
4310 if (ch == '}')
4311 brackets--;
4312
4313 if (brackets == 0)
4314 skip = bracketed;
4315 }
4316
4317 if (!paren)
4318 break; /* We are done */
4319
4320 if (!skip) {
4321 Token *t;
4322
4323 if (bracketed && !brackets)
4324 bad_bracket = true;
4325
4326 if (white) {
4327 t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4328 *pep = t;
4329 pep = &t->next;
4330 }
4331
4332 t = new_Token(NULL, tline->type, tline->text, 0);
4333 *pep = t;
4334 pep = &t->next;
4335 white = 0;
4336 }
4337 }
4338 nparam++; /* Count the last parameter */
4339
4340 if (bad_bracket)
4341 nasm_nonfatal("braces do not enclose all of macro parameter");
4342
4343 /* Look for a macro matching in both name and parameter count */
4344 while (m && (m->nparam != nparam ||
4345 mstrcmp(m->name, mname, m->casesense)))
4346 m = m->next;
4347
4348 if (!m) {
4349 /*!
4350 *!macro-params [on] macro calls with wrong parameter count
4351 *! covers warnings about \i{multi-line macros} being invoked
4352 *! with the wrong number of parameters. See \k{mlmacover} for an
4353 *! example of why you might want to disable this warning.
4354 */
4355 nasm_warn(WARN_MACRO_PARAMS,
4356 "macro `%s' exists, "
4357 "but not taking %d parameters",
4358 mname, nparam);
4359 goto not_a_macro_cleanup;
4360 }
4361 }
4362
4363 if (m->in_progress)
4364 goto not_a_macro_cleanup;
4365
4366 /* Expand each parameter */
4367 m->in_progress = true;
4368
4369 for (i = 0; i < nparam; i++) {
4370 params[i] = expand_smacro_noreset(params[i]);
4371
4372 if (m->eval_param && m->eval_param[i]) {
4373 /* Evaluate this parameter as a number */
4374 struct ppscan pps;
4375 struct tokenval tokval;
4376 expr *evalresult;
4377
4378 pps.tptr = params[i];
4379 pps.ntokens = -1;
4380 tokval.t_type = TOKEN_INVALID;
4381 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
4382
4383 if (!evalresult) {
4384 /* Nothing meaningful to do */
4385 } else if (tokval.t_type) {
4386 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4387 } else if (!is_simple(evalresult)) {
4388 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4389 } else {
4390 free_tlist(params[i]);
4391 params[i] = make_tok_num(reloc_value(evalresult));
4392 }
4393 }
4394
4395 /* Put tokens in reverse order like the expansion list */
4396 params[i] = reverse_tokens(params[i]);
4397 }
4398
4399 t = tline;
4400 tline = tline->next;
4401 t->next = NULL; /* Remove the macro call from the input */
4402
4403 if (unlikely(m->magic)) {
4404 expansion = m->magic(m, params, nparam, &free_expansion);
4405 } else {
4406 expansion = m->expansion;
4407 free_expansion = false;
4408 }
4409
4410 ttail = NULL;
4411
4412 list_for_each(t, expansion) {
4413 if (is_smac_param(t->type)) {
4414 Token *ttt;
4415 list_for_each(ttt, params[smac_nparam(t->type)]) {
4416 tline = dup_Token(tline, ttt);
4417 if (!ttail)
4418 ttail = &tline->next;
4419 }
4420 } else {
4421 if (t->type == TOK_PREPROC_Q) {
4422 tline = new_Token(tline, TOK_ID, mname, 0);
4423 } else if (t->type == TOK_PREPROC_QQ) {
4424 tline = new_Token(tline, TOK_ID, m->name, 0);
4425 } else {
4426 tline = dup_Token(tline, t);
4427 }
4428 if (!ttail)
4429 ttail = &tline->next;
4430 }
4431 }
4432
4433 if (free_expansion)
4434 free_tlist(expansion);
4435
4436 m->in_progress = false;
4437
4438 /* Don't do this until after expansion or we will clobber mname */
4439 mstart = **tpp;
4440 tnextp = ttail ? ttail : *tpp;
4441 **tpp = tline;
4442 free_tlist(mstart);
4443
4444 /*
4445 * No macro expansion needed; roll back to mstart (if necessary)
4446 * and then advance to the next input token.
4447 */
4448not_a_macro_cleanup:
4449 nasm_free(params);
4450
4451not_a_macro:
4452 t = **tpp;
4453 *tpp = tnextp;
4454 return t;
4455}
4456
4457/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004458 * Expand all single-line macro calls made in the given line.
4459 * Return the expanded version of the line. The original is deemed
4460 * to be destroyed in the process. (In reality we'll just move
4461 * Tokens from input to output a lot of the time, rather than
4462 * actually bothering to destroy and replicate.)
4463 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004464static int64_t smacro_deadman;
4465static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004466{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004467 smacro_deadman = nasm_limit[LIMIT_MACROS];
4468 return expand_smacro_noreset(tline);
4469}
4470
4471static Token *expand_smacro_noreset(Token * tline)
4472{
4473 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004474 Token *org_tline = tline;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004475 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004476
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004477 /*
4478 * Trick: we should avoid changing the start token pointer since it can
4479 * be contained in "next" field of other token. Because of this
4480 * we allocate a copy of first token and work with it; at the end of
4481 * routine we copy it back
4482 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004483 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004484 tline = new_Token(org_tline->next, org_tline->type,
4485 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004486 nasm_free(org_tline->text);
4487 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004488 }
4489
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004490 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004491
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004492again:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004493 thead = tline;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004494 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004495
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004496 while ((t = *tail)) { /* main token loop */
4497 if (!--smacro_deadman) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004498 nasm_nonfatal("interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004499 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004500 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004501
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004502 /*
4503 * An expansion happened if and only if the first token of the
4504 * expansion was the first token of the original simply moved over.
4505 */
4506 expanded |= (expand_one_smacro(&tail) != t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004507 }
4508
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004509 /*
4510 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004511 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004512 * TOK_IDs should be concatenated.
4513 * Also we look for %+ tokens and concatenate the tokens before and after
4514 * them (without white spaces in between).
4515 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004516 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004517 const struct tokseq_match t[] = {
4518 {
4519 PP_CONCAT_MASK(TOK_ID) |
4520 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4521 PP_CONCAT_MASK(TOK_ID) |
4522 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4523 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4524 }
4525 };
4526 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004527 /*
4528 * If we concatenated something, *and* we had previously expanded
4529 * an actual macro, scan the lines again for macros...
4530 */
4531 tline = thead;
4532 expanded = false;
4533 goto again;
4534 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004535 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004536
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004537err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004538 if (org_tline) {
4539 if (thead) {
4540 *org_tline = *thead;
4541 /* since we just gave text to org_line, don't free it */
4542 thead->text = NULL;
4543 delete_Token(thead);
4544 } else {
4545 /* the expression expanded to empty line;
4546 we can't return NULL for some reasons
4547 we just set the line to a single WHITESPACE token. */
4548 memset(org_tline, 0, sizeof(*org_tline));
4549 org_tline->text = NULL;
4550 org_tline->type = TOK_WHITESPACE;
4551 }
4552 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004553 }
4554
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004555 return thead;
4556}
4557
4558/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004559 * Similar to expand_smacro but used exclusively with macro identifiers
4560 * right before they are fetched in. The reason is that there can be
4561 * identifiers consisting of several subparts. We consider that if there
4562 * are more than one element forming the name, user wants a expansion,
4563 * otherwise it will be left as-is. Example:
4564 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004565 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004566 *
4567 * the identifier %$abc will be left as-is so that the handler for %define
4568 * will suck it and define the corresponding value. Other case:
4569 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004570 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004571 *
4572 * In this case user wants name to be expanded *before* %define starts
4573 * working, so we'll expand %$abc into something (if it has a value;
4574 * otherwise it will be left as-is) then concatenate all successive
4575 * PP_IDs into one.
4576 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004577static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004578{
4579 Token *cur, *oldnext = NULL;
4580
H. Peter Anvin734b1882002-04-30 21:01:08 +00004581 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004582 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004583
4584 cur = tline;
4585 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004586 (cur->next->type == TOK_ID ||
4587 cur->next->type == TOK_PREPROC_ID
4588 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004590
4591 /* If identifier consists of just one token, don't expand */
4592 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004593 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004594
H. Peter Anvine2c80182005-01-15 22:15:51 +00004595 if (cur) {
4596 oldnext = cur->next; /* Detach the tail past identifier */
4597 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004598 }
4599
H. Peter Anvin734b1882002-04-30 21:01:08 +00004600 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004601
H. Peter Anvine2c80182005-01-15 22:15:51 +00004602 if (cur) {
4603 /* expand_smacro possibly changhed tline; re-scan for EOL */
4604 cur = tline;
4605 while (cur && cur->next)
4606 cur = cur->next;
4607 if (cur)
4608 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004609 }
4610
4611 return tline;
4612}
4613
4614/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004615 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004616 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004617 * to check for an initial label - that's taken care of in
4618 * expand_mmacro - but must check numbers of parameters. Guaranteed
4619 * to be called with tline->type == TOK_ID, so the putative macro
4620 * name is easy to find.
4621 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004622static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004623{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004624 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004625 Token **params;
4626 int nparam;
4627
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004628 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004629
4630 /*
4631 * Efficiency: first we see if any macro exists with the given
4632 * name. If not, we can return NULL immediately. _Then_ we
4633 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004634 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004635 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004636 list_for_each(m, head)
4637 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004638 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004639 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004640 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004641
4642 /*
4643 * OK, we have a potential macro. Count and demarcate the
4644 * parameters.
4645 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004646 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004647
4648 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004649 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004650 * structure that handles this number.
4651 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004652 while (m) {
4653 if (m->nparam_min <= nparam
4654 && (m->plus || nparam <= m->nparam_max)) {
4655 /*
4656 * This one is right. Just check if cycle removal
4657 * prohibits us using it before we actually celebrate...
4658 */
4659 if (m->in_progress > m->max_depth) {
4660 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004661 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004662 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004663 }
4664 nasm_free(params);
4665 return NULL;
4666 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004667 /*
4668 * It's right, and we can use it. Add its default
4669 * parameters to the end of our list if necessary.
4670 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004671 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004672 params =
4673 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004674 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004675 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004676 while (nparam < m->nparam_min + m->ndefs) {
4677 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004678 nparam++;
4679 }
4680 }
4681 /*
4682 * If we've gone over the maximum parameter count (and
4683 * we're in Plus mode), ignore parameters beyond
4684 * nparam_max.
4685 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004686 if (m->plus && nparam > m->nparam_max)
4687 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004688 /*
4689 * Then terminate the parameter list, and leave.
4690 */
4691 if (!params) { /* need this special case */
4692 params = nasm_malloc(sizeof(*params));
4693 nparam = 0;
4694 }
4695 params[nparam] = NULL;
4696 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004697 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004698 }
4699 /*
4700 * This one wasn't right: look for the next one with the
4701 * same name.
4702 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004703 list_for_each(m, m->next)
4704 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004705 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004706 }
4707
4708 /*
4709 * After all that, we didn't find one with the right number of
4710 * parameters. Issue a warning, and fail to expand the macro.
4711 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004712 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004713 "macro `%s' exists, but not taking %d parameters",
4714 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004715 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004716 return NULL;
4717}
4718
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004719
4720/*
4721 * Save MMacro invocation specific fields in
4722 * preparation for a recursive macro expansion
4723 */
4724static void push_mmacro(MMacro *m)
4725{
4726 MMacroInvocation *i;
4727
4728 i = nasm_malloc(sizeof(MMacroInvocation));
4729 i->prev = m->prev;
4730 i->params = m->params;
4731 i->iline = m->iline;
4732 i->nparam = m->nparam;
4733 i->rotate = m->rotate;
4734 i->paramlen = m->paramlen;
4735 i->unique = m->unique;
4736 i->condcnt = m->condcnt;
4737 m->prev = i;
4738}
4739
4740
4741/*
4742 * Restore MMacro invocation specific fields that were
4743 * saved during a previous recursive macro expansion
4744 */
4745static void pop_mmacro(MMacro *m)
4746{
4747 MMacroInvocation *i;
4748
4749 if (m->prev) {
4750 i = m->prev;
4751 m->prev = i->prev;
4752 m->params = i->params;
4753 m->iline = i->iline;
4754 m->nparam = i->nparam;
4755 m->rotate = i->rotate;
4756 m->paramlen = i->paramlen;
4757 m->unique = i->unique;
4758 m->condcnt = i->condcnt;
4759 nasm_free(i);
4760 }
4761}
4762
4763
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004764/*
4765 * Expand the multi-line macro call made by the given line, if
4766 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004767 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004768 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004769static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004770{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004771 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004772 Token *label = NULL;
4773 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004774 Token **params, *t, *tt;
4775 MMacro *m;
4776 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004777 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004778 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004779
4780 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004781 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004782 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004783 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004784 return 0;
4785 m = is_mmacro(t, &params);
4786 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004787 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004788 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004789 Token *last;
4790 /*
4791 * We have an id which isn't a macro call. We'll assume
4792 * it might be a label; we'll also check to see if a
4793 * colon follows it. Then, if there's another id after
4794 * that lot, we'll check it again for macro-hood.
4795 */
4796 label = last = t;
4797 t = t->next;
4798 if (tok_type_(t, TOK_WHITESPACE))
4799 last = t, t = t->next;
4800 if (tok_is_(t, ":")) {
4801 dont_prepend = 1;
4802 last = t, t = t->next;
4803 if (tok_type_(t, TOK_WHITESPACE))
4804 last = t, t = t->next;
4805 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004806 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4807 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004808 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004809 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004810 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004811 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004812
4813 /*
4814 * Fix up the parameters: this involves stripping leading and
4815 * trailing whitespace, then stripping braces if they are
4816 * present.
4817 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004818 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004819 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004820
H. Peter Anvine2c80182005-01-15 22:15:51 +00004821 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004822 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004823 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004824
H. Peter Anvine2c80182005-01-15 22:15:51 +00004825 t = params[i];
4826 skip_white_(t);
4827 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004828 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004829 params[i] = t;
4830 paramlen[i] = 0;
4831 while (t) {
4832 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4833 break; /* ... because we have hit a comma */
4834 if (comma && t->type == TOK_WHITESPACE
4835 && tok_is_(t->next, ","))
4836 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004837 if (brace && t->type == TOK_OTHER) {
4838 if (t->text[0] == '{')
4839 brace++; /* ... or a nested opening brace */
4840 else if (t->text[0] == '}')
4841 if (!--brace)
4842 break; /* ... or a brace */
4843 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004844 t = t->next;
4845 paramlen[i]++;
4846 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004847 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004848 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004849 }
4850
4851 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004852 * OK, we have a MMacro structure together with a set of
4853 * parameters. We must now go through the expansion and push
4854 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004855 * parameter tokens and macro-local tokens doesn't get done
4856 * until the single-line macro substitution process; this is
4857 * because delaying them allows us to change the semantics
4858 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004859 *
4860 * First, push an end marker on to istk->expansion, mark this
4861 * macro as in progress, and set up its invocation-specific
4862 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004863 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004864 ll = nasm_malloc(sizeof(Line));
4865 ll->next = istk->expansion;
4866 ll->finishes = m;
4867 ll->first = NULL;
4868 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004869
4870 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 * Save the previous MMacro expansion in the case of
4872 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004873 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004874 if (m->max_depth && m->in_progress)
4875 push_mmacro(m);
4876
4877 m->in_progress ++;
4878 m->params = params;
4879 m->iline = tline;
4880 m->nparam = nparam;
4881 m->rotate = 0;
4882 m->paramlen = paramlen;
4883 m->unique = unique++;
4884 m->lineno = 0;
4885 m->condcnt = 0;
4886
4887 m->next_active = istk->mstk;
4888 istk->mstk = m;
4889
4890 list_for_each(l, m->expansion) {
4891 Token **tail;
4892
4893 ll = nasm_malloc(sizeof(Line));
4894 ll->finishes = NULL;
4895 ll->next = istk->expansion;
4896 istk->expansion = ll;
4897 tail = &ll->first;
4898
4899 list_for_each(t, l->first) {
4900 Token *x = t;
4901 switch (t->type) {
4902 case TOK_PREPROC_Q:
4903 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004904 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004905 case TOK_PREPROC_QQ:
4906 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4907 break;
4908 case TOK_PREPROC_ID:
4909 if (t->text[1] == '0' && t->text[2] == '0') {
4910 dont_prepend = -1;
4911 x = label;
4912 if (!x)
4913 continue;
4914 }
4915 /* fall through */
4916 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07004917 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004918 break;
4919 }
4920 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004921 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004922 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004923 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004924
4925 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004926 * If we had a label, push it on as the first line of
4927 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004928 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004929 if (label) {
4930 if (dont_prepend < 0)
4931 free_tlist(startline);
4932 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07004933 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004934 ll->finishes = NULL;
4935 ll->next = istk->expansion;
4936 istk->expansion = ll;
4937 ll->first = startline;
4938 if (!dont_prepend) {
4939 while (label->next)
4940 label = label->next;
4941 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004942 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004943 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004944 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004945
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004946 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004947
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004948 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949}
4950
H. Peter Anvin130736c2016-02-17 20:27:41 -08004951/*
4952 * This function adds macro names to error messages, and suppresses
4953 * them if necessary.
4954 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08004955static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004956{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004957 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004958 MMacro *mmac = NULL;
4959 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004960
H. Peter Anvin130736c2016-02-17 20:27:41 -08004961 /*
4962 * If we're in a dead branch of IF or something like it, ignore the error.
4963 * However, because %else etc are evaluated in the state context
4964 * of the previous branch, errors might get lost:
4965 * %if 0 ... %else trailing garbage ... %endif
4966 * So %else etc should set the ERR_PP_PRECOND flag.
4967 */
4968 if ((severity & ERR_MASK) < ERR_FATAL &&
4969 istk && istk->conds &&
4970 ((severity & ERR_PP_PRECOND) ?
4971 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004972 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004973 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004974
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004975 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004976 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004977 mmac = istk->mstk;
4978 /* but %rep blocks should be skipped */
4979 while (mmac && !mmac->name)
4980 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004981 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004982
H. Peter Anvin130736c2016-02-17 20:27:41 -08004983 if (mmac) {
4984 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004985
H. Peter Anvin130736c2016-02-17 20:27:41 -08004986 nasm_set_verror(real_verror);
4987 nasm_error(severity, "(%s:%d) %s",
4988 mmac->name, mmac->lineno - delta, buff);
4989 nasm_set_verror(pp_verror);
4990 } else {
4991 real_verror(severity, fmt, arg);
4992 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004993}
4994
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004995static Token *stdmac_file(const SMacro *s, Token **params,
4996 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08004997{
4998 (void)s;
4999 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005000 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005001
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005002 *free_expansion = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005003 return make_tok_qstr(src_get_fname());
5004}
5005
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005006static Token *stdmac_line(const SMacro *s, Token **params,
5007 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005008{
5009 (void)s;
5010 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005011 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005012
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005013 *free_expansion = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005014 return make_tok_num(src_get_linnum());
5015}
5016
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005017static Token *stdmac_bits(const SMacro *s, Token **params,
5018 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005019{
5020 (void)s;
5021 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005022 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005023
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005024 *free_expansion = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005025 return make_tok_num(globalbits);
5026}
5027
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005028static Token *stdmac_ptr(const SMacro *s, Token **params,
5029 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005030{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005031 const Token *t;
5032 static const Token ptr_word = { NULL, "word", 4, TOKEN_ID };
5033 static const Token ptr_dword = { NULL, "dword", 5, TOKEN_ID };
5034 static const Token ptr_qword = { NULL, "qword", 5, TOKEN_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005035
5036 (void)s;
5037 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005038 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005039
5040 switch (globalbits) {
5041 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005042 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005043 break;
5044 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005045 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005046 break;
5047 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005048 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005049 break;
5050 default:
5051 panic();
5052 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005053
5054 *free_expansion = false;
5055 return (Token *)t;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005056}
5057
H. Peter Anvin8b262472019-02-26 14:00:54 -08005058/* Add magic standard macros */
5059struct magic_macros {
5060 const char *name;
5061 int nparams;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005062 MagicSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005063};
5064static const struct magic_macros magic_macros[] =
5065{
5066 { "__FILE__", 0, stdmac_file },
5067 { "__LINE__", 0, stdmac_line },
5068 { "__BITS__", 0, stdmac_bits },
5069 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005070 { NULL, 0, NULL }
5071};
5072
5073static void pp_add_magic_stdmac(void)
5074{
5075 const struct magic_macros *m;
5076 SMacro *s;
5077
5078 for (m = magic_macros; m->name; m++) {
5079 s = define_smacro(NULL, m->name, true, m->nparams, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005080 s->magic = m->func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005081 }
5082}
5083
H. Peter Anvin734b1882002-04-30 21:01:08 +00005084static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005085pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005086{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005087 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005088
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005089 cstk = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005090 nasm_new(istk);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07005091 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin274cda82016-05-10 02:56:29 -07005092 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005093 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005094 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03005095 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005096 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005097 nested_mac_count = 0;
5098 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005099 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005100 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005101 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005102 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005103
H. Peter Anvin8b262472019-02-26 14:00:54 -08005104 pp_add_magic_stdmac();
5105
H. Peter Anvinf7606612016-07-13 14:23:48 -07005106 if (tasm_compatible_mode)
5107 pp_add_stdmac(nasm_stdmac_tasm);
5108
5109 pp_add_stdmac(nasm_stdmac_nasm);
5110 pp_add_stdmac(nasm_stdmac_version);
5111
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005112 if (extrastdmac)
5113 pp_add_stdmac(extrastdmac);
5114
H. Peter Anvinf7606612016-07-13 14:23:48 -07005115 stdmacpos = stdmacros[0];
5116 stdmacnext = &stdmacros[1];
5117
H. Peter Anvind2456592008-06-19 15:04:18 -07005118 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005119
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03005120 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005121
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005122 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005123 * Define the __PASS__ macro. This is defined here unlike all the
5124 * other builtins, because it is special -- it varies between
5125 * passes -- but there is really no particular reason to make it
5126 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005127 *
5128 * 0 = dependencies only
5129 * 1 = preparatory passes
5130 * 2 = final pass
5131 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005132 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005133 switch (mode) {
5134 case PP_NORMAL:
5135 apass = pass_final() ? 2 : 1;
5136 break;
5137 case PP_DEPS:
5138 apass = 0;
5139 break;
5140 case PP_PREPROC:
5141 apass = 3;
5142 break;
5143 default:
5144 panic();
5145 }
5146
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005147 define_smacro(NULL, "__PASS__", true, 0, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005148}
5149
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005150static void pp_init(void)
5151{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005152}
5153
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005154/*
5155 * Get a line of tokens. If we popped the macro expansion/include stack,
5156 * we return a pointer to the dummy token tok_pop; at that point if
5157 * istk is NULL then we have reached end of input;
5158 */
5159static Token tok_pop; /* Dummy token placeholder */
5160
5161static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005162{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005163 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005164
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005165 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005166 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005167 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005168 * buffer or from the input file.
5169 */
5170 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005171 while (istk->expansion && istk->expansion->finishes) {
5172 Line *l = istk->expansion;
5173 if (!l->finishes->name && l->finishes->in_progress > 1) {
5174 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005175
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005176 /*
5177 * This is a macro-end marker for a macro with no
5178 * name, which means it's not really a macro at all
5179 * but a %rep block, and the `in_progress' field is
5180 * more than 1, meaning that we still need to
5181 * repeat. (1 means the natural last repetition; 0
5182 * means termination by %exitrep.) We have
5183 * therefore expanded up to the %endrep, and must
5184 * push the whole block on to the expansion buffer
5185 * again. We don't bother to remove the macro-end
5186 * marker: we'd only have to generate another one
5187 * if we did.
5188 */
5189 l->finishes->in_progress--;
5190 list_for_each(l, l->finishes->expansion) {
5191 Token *t, *tt, **tail;
5192
5193 ll = nasm_malloc(sizeof(Line));
5194 ll->next = istk->expansion;
5195 ll->finishes = NULL;
5196 ll->first = NULL;
5197 tail = &ll->first;
5198
5199 list_for_each(t, l->first) {
5200 if (t->text || t->type == TOK_WHITESPACE) {
5201 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5202 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005203 }
5204 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005205 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005206 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005207 } else {
5208 /*
5209 * Check whether a `%rep' was started and not ended
5210 * within this macro expansion. This can happen and
5211 * should be detected. It's a fatal error because
5212 * I'm too confused to work out how to recover
5213 * sensibly from it.
5214 */
5215 if (defining) {
5216 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005217 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005218 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005219 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005220 " expansion of macro `%s'",
5221 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005222 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005223
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005224 /*
5225 * FIXME: investigate the relationship at this point between
5226 * istk->mstk and l->finishes
5227 */
5228 {
5229 MMacro *m = istk->mstk;
5230 istk->mstk = m->next_active;
5231 if (m->name) {
5232 /*
5233 * This was a real macro call, not a %rep, and
5234 * therefore the parameter information needs to
5235 * be freed.
5236 */
5237 if (m->prev) {
5238 pop_mmacro(m);
5239 l->finishes->in_progress --;
5240 } else {
5241 nasm_free(m->params);
5242 free_tlist(m->iline);
5243 nasm_free(m->paramlen);
5244 l->finishes->in_progress = 0;
5245 }
Adam Majer91e72402017-07-25 10:42:01 +02005246 }
5247
5248 /*
5249 * FIXME It is incorrect to always free_mmacro here.
5250 * It leads to usage-after-free.
5251 *
5252 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5253 */
5254#if 0
5255 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005256 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005257#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005258 }
5259 istk->expansion = l->next;
5260 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005261 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005262 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005263 }
5264 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005265 do { /* until we get a line we can use */
5266 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005267
5268 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005269 Line *l = istk->expansion;
5270 if (istk->mstk)
5271 istk->mstk->lineno++;
5272 tline = l->first;
5273 istk->expansion = l->next;
5274 nasm_free(l);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005275 line = detoken(tline, false);
5276 lfmt->line(LIST_MACRO, line);
5277 nasm_free(line);
5278 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005279 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005280 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005281 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005282 } else {
5283 /*
5284 * The current file has ended; work down the istk
5285 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005286 Include *i = istk;
5287 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005288 if (i->conds) {
5289 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005290 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005291 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005292 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005293 if (i->next)
5294 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005295 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005296 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005297 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005298 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005299 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005300 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005301
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005302 /*
5303 * We must expand MMacro parameters and MMacro-local labels
5304 * _before_ we plunge into directive processing, to cope
5305 * with things like `%define something %1' such as STRUC
5306 * uses. Unless we're _defining_ a MMacro, in which case
5307 * those tokens should be left alone to go into the
5308 * definition; and unless we're in a non-emitting
5309 * condition, in which case we don't want to meddle with
5310 * anything.
5311 */
5312 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5313 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005314 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005315 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005316
H. Peter Anvine2c80182005-01-15 22:15:51 +00005317 /*
5318 * Check the line to see if it's a preprocessor directive.
5319 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005320 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5321 if (dtline)
5322 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005323 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005324 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005325 * We're defining a multi-line macro. We emit nothing
5326 * at all, and just
5327 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005328 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005329 Line *l = nasm_malloc(sizeof(Line));
5330 l->next = defining->expansion;
5331 l->first = tline;
5332 l->finishes = NULL;
5333 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005334 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005335 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005336 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005337 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005338 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005339 * directive so we keep our place correctly.
5340 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005341 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005342 } else if (istk->mstk && !istk->mstk->in_progress) {
5343 /*
5344 * We're in a %rep block which has been terminated, so
5345 * we're walking through to the %endrep without
5346 * emitting anything. Emit nothing at all, not even a
5347 * blank line: when we emerge from the %rep block we'll
5348 * give a line-number directive so we keep our place
5349 * correctly.
5350 */
5351 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005352 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005353 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005354 if (!expand_mmacro(tline))
5355 return tline;
5356 }
5357 }
5358}
5359
5360static char *pp_getline(void)
5361{
5362 char *line = NULL;
5363 Token *tline;
5364
5365 real_verror = nasm_set_verror(pp_verror);
5366
5367 while (true) {
5368 tline = pp_tokline();
5369 if (tline == &tok_pop) {
5370 /*
5371 * We popped the macro/include stack. If istk is empty,
5372 * we are at end of input, otherwise just loop back.
5373 */
5374 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005375 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005376 } else {
5377 /*
5378 * De-tokenize the line and emit it.
5379 */
5380 line = detoken(tline, true);
5381 free_tlist(tline);
5382 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005383 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005384 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005385
H. Peter Anvin130736c2016-02-17 20:27:41 -08005386 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005387 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005388}
5389
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005390static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005391{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005392 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005393
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005394 if (defining) {
5395 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005396 nasm_nonfatal("end of file while still defining macro `%s'",
5397 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005398 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005399 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005400 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005401
5402 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005403 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005404 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005405
5406 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005407
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005408 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005409 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005410 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005411 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005412 Include *i = istk;
5413 istk = istk->next;
5414 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005415 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005416 }
5417 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005418 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005419 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005420}
5421
5422static void pp_cleanup_session(void)
5423{
5424 free_llist(predef);
5425 predef = NULL;
5426 delete_Blocks();
5427 freeTokens = NULL;
5428 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005429}
5430
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005431static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005432{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005433 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005434}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005435
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005436static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005437{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005438 Token *inc, *space, *name;
5439 Line *l;
5440
H. Peter Anvin734b1882002-04-30 21:01:08 +00005441 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5442 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5443 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005444
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005445 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005446 l->next = predef;
5447 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005448 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005449 predef = l;
5450}
5451
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005452static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005453{
5454 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005455 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005456 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005457
H. Peter Anvin130736c2016-02-17 20:27:41 -08005458 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005459
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005460 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005461 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5462 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005463 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005464 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005465 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005466 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005467 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005468
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005469 if (space->next->type != TOK_PREPROC_ID &&
5470 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005471 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005472
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005473 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005474 l->next = predef;
5475 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005476 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005477 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005478
5479 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005480}
5481
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005482static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005483{
5484 Token *def, *space;
5485 Line *l;
5486
H. Peter Anvin734b1882002-04-30 21:01:08 +00005487 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5488 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005489 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005490
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005491 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005492 l->next = predef;
5493 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005494 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005495 predef = l;
5496}
5497
H. Peter Anvin05990342018-06-11 13:32:42 -07005498/* Insert an early preprocessor command that doesn't need special handling */
5499static void pp_pre_command(const char *what, char *string)
5500{
5501 char *cmd;
5502 Token *def, *space;
5503 Line *l;
5504
5505 def = tokenize(string);
5506 if (what) {
5507 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5508 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5509 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5510 }
5511
5512 l = nasm_malloc(sizeof(Line));
5513 l->next = predef;
5514 l->first = def;
5515 l->finishes = NULL;
5516 predef = l;
5517}
5518
H. Peter Anvinf7606612016-07-13 14:23:48 -07005519static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005520{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005521 macros_t **mp;
5522
5523 /* Find the end of the list and avoid duplicates */
5524 for (mp = stdmacros; *mp; mp++) {
5525 if (*mp == macros)
5526 return; /* Nothing to do */
5527 }
5528
5529 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5530
5531 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005532}
5533
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005534static void pp_extra_stdmac(macros_t *macros)
5535{
5536 extrastdmac = macros;
5537}
5538
H. Peter Anvin8b262472019-02-26 14:00:54 -08005539static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005540{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005541 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005542 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5543 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5544}
5545
5546static Token *make_tok_qstr(const char *str)
5547{
5548 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005549 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005550 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005551}
5552
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005553static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005554{
5555 if (!m)
5556 return;
5557
5558 /* We need to print the next_active list in reverse order */
5559 pp_list_one_macro(m->next_active, severity);
5560
5561 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005562 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005563 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005564 }
5565}
5566
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005567static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005568{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005569 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005570
H. Peter Anvinddb29062018-12-11 00:06:29 -08005571 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5572 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005573
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005574 if (istk)
5575 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005576
H. Peter Anvinddb29062018-12-11 00:06:29 -08005577 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005578}
5579
H. Peter Anvine7469712016-02-18 02:20:59 -08005580const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005581 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005582 pp_reset,
5583 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005584 pp_cleanup_pass,
5585 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005586 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005587 pp_pre_define,
5588 pp_pre_undefine,
5589 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005590 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005591 pp_include_path,
5592 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005593};