blob: 7a5d11fedb147b90f7c11fe5ada76dd6f6fa3101 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000084typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000085typedef struct Line Line;
86typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080087typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088
89/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070090 * Note on the storage of both SMacro and MMacros: the hash table
91 * indexes them case-insensitively, and we then have to go through a
92 * linked list of potential case aliases (and, for MMacros, parameter
93 * ranges); this is to preserve the matching semantics of the earlier
94 * code. If the number of case aliases for a specific macro is a
95 * performance issue, you may want to reconsider your coding style.
96 */
97
98/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070099 * Function call tp obtain the expansion of an smacro
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700100 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700101typedef Token *(*ExpandSMacro)(const SMacro *s, Token **params,
102 unsigned int nparams);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800108 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800109 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700110 Token *expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700111 ExpandSMacro expand;
112 intorptr expandpvt;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800113 bool *eval_param;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800114 unsigned int nparam;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800115 bool casesense;
116 bool in_progress;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117};
118
119/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800120 * Store the definition of a multi-line macro. This is also used to
121 * store the interiors of `%rep...%endrep' blocks, which are
122 * effectively self-re-invoking multi-line macros which simply
123 * don't have a name or bother to appear in the hash tables. %rep
124 * blocks are signified by having a NULL `name' field.
125 *
126 * In a MMacro describing a `%rep' block, the `in_progress' field
127 * isn't merely boolean, but gives the number of repeats left to
128 * run.
129 *
130 * The `next' field is used for storing MMacros in hash tables; the
131 * `next_active' field is for stacking them on istk entries.
132 *
133 * When a MMacro is being expanded, `params', `iline', `nparam',
134 * `paramlen', `rotate' and `unique' are local to the invocation.
135 */
136struct MMacro {
137 MMacro *next;
138 MMacroInvocation *prev; /* previous invocation */
139 char *name;
140 int nparam_min, nparam_max;
141 bool casesense;
142 bool plus; /* is the last parameter greedy? */
143 bool nolist; /* is this macro listing-inhibited? */
144 int64_t in_progress; /* is this macro currently being expanded? */
145 int32_t max_depth; /* maximum number of recursive expansions allowed */
146 Token *dlist; /* All defaults as one list */
147 Token **defaults; /* Parameter default pointers */
148 int ndefs; /* number of default parameters */
149 Line *expansion;
150
151 MMacro *next_active;
152 MMacro *rep_nest; /* used for nesting %rep */
153 Token **params; /* actual parameters */
154 Token *iline; /* invocation line */
155 unsigned int nparam, rotate;
156 int *paramlen;
157 uint64_t unique;
158 int lineno; /* Current line number on expansion */
159 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700160
H. Peter Anvin274cda82016-05-10 02:56:29 -0700161 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700162 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800163};
164
165
166/* Store the definition of a multi-line macro, as defined in a
167 * previous recursive macro expansion.
168 */
169struct MMacroInvocation {
170 MMacroInvocation *prev; /* previous invocation */
171 Token **params; /* actual parameters */
172 Token *iline; /* invocation line */
173 unsigned int nparam, rotate;
174 int *paramlen;
175 uint64_t unique;
176 uint64_t condcnt;
177};
178
179
180/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000181 * The context stack is composed of a linked list of these.
182 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000183struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800184 Context *next;
185 char *name;
186 struct hash_table localmac;
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 Anvinb40992c2010-09-15 08:57:21 -0700486 * In-place reverse a list of tokens.
487 */
488static Token *reverse_tokens(Token *t)
489{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800490 Token *prev = NULL;
491 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700492
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800493 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400494 next = t->next;
495 t->next = prev;
496 prev = t;
497 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800498 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700499
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800500 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700501}
502
503/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300504 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000505 * front of them. We do it here because I could not find any other
506 * place to do it for the moment, and it is a hack (ideally it would
507 * be nice to be able to use the NASM pre-processor to do it).
508 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000509static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000510{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000511 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400512 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000513
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400514 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000515
516 /* Binary search for the directive name */
517 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400518 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400519 q = nasm_skip_word(p);
520 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 if (len) {
522 oldchar = p[len];
523 p[len] = 0;
524 while (j - i > 1) {
525 k = (j + i) / 2;
526 m = nasm_stricmp(p, tasm_directives[k]);
527 if (m == 0) {
528 /* We have found a directive, so jam a % in front of it
529 * so that NASM will then recognise it as one if it's own.
530 */
531 p[len] = oldchar;
532 len = strlen(p);
533 oldline = line;
534 line = nasm_malloc(len + 2);
535 line[0] = '%';
536 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700537 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300538 * NASM does not recognise IFDIFI, so we convert
539 * it to %if 0. This is not used in NASM
540 * compatible code, but does need to parse for the
541 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700543 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 } else {
545 memcpy(line + 1, p, len + 1);
546 }
547 nasm_free(oldline);
548 return line;
549 } else if (m < 0) {
550 j = k;
551 } else
552 i = k;
553 }
554 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000555 }
556 return line;
557}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000558
H. Peter Anvin76690a12002-04-30 20:52:49 +0000559/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000560 * The pre-preprocessing stage... This function translates line
561 * number indications as they emerge from GNU cpp (`# lineno "file"
562 * flags') into NASM preprocessor line number indications (`%line
563 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000564 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000565static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000566{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000567 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000568 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000569
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 if (line[0] == '#' && line[1] == ' ') {
571 oldline = line;
572 fname = oldline + 2;
573 lineno = atoi(fname);
574 fname += strspn(fname, "0123456789 ");
575 if (*fname == '"')
576 fname++;
577 fnlen = strcspn(fname, "\"");
578 line = nasm_malloc(20 + fnlen);
579 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
580 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000581 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000582 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000584 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000585}
586
587/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000588 * Free a linked list of tokens.
589 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000591{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400592 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000594}
595
596/*
597 * Free a linked list of lines.
598 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000600{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400601 Line *l, *tmp;
602 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 free_tlist(l->first);
604 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605 }
606}
607
608/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700609 * Free an array of linked lists of tokens
610 */
611static void free_tlist_array(Token **array, size_t nlists)
612{
613 Token **listp = array;
614
615 while (nlists--)
616 free_tlist(*listp++);
617
618 nasm_free(array);
619}
620
621/*
622 * Duplicate a linked list of tokens.
623 */
624static Token *dup_tlist(const Token *list, Token ***tailp)
625{
626 Token *newlist = NULL;
627 Token **tailpp = &newlist;
628 const Token *t;
629
630 list_for_each(t, list) {
631 Token *nt;
632 *tailpp = nt = dup_Token(NULL, t);
633 tailpp = &nt->next;
634 }
635
636 if (tailp)
637 *tailp = tailpp;
638
639 return newlist;
640}
641
642/*
643 * Duplicate a linked list of tokens in reverse order
644 */
645static Token *dup_tlist_reverse(const Token *list, Token *tail)
646{
647 const Token *t;
648
649 list_for_each(t, list)
650 tail = dup_Token(tail, t);
651
652 return tail;
653}
654
655/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800656 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000657 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800658static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000659{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800660 nasm_free(m->name);
661 free_tlist(m->dlist);
662 nasm_free(m->defaults);
663 free_llist(m->expansion);
664 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000665}
666
667/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700668 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800669 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700670static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800671{
672 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700673 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800674 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700675}
676
677static void clear_smacro(SMacro *s)
678{
679 free_smacro_members(s);
680 /* Wipe everything except the next pointer */
681 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
682}
683
684/*
685 * Free an SMacro
686 */
687static void free_smacro(SMacro *s)
688{
689 free_smacro_members(s);
690 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800691}
692
693/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700694 * Free all currently defined macros, and free the hash tables
695 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700696static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700697{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800698 struct hash_iterator it;
699 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700700
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800701 hash_for_each(smt, it, np) {
702 SMacro *tmp;
703 SMacro *s = np->data;
704 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800705 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700706 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700707 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700708 hash_free(smt);
709}
710
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800711static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700712{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800713 struct hash_iterator it;
714 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700715
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800716 hash_for_each(mmt, it, np) {
717 MMacro *tmp;
718 MMacro *m = np->data;
719 nasm_free((void *)np->key);
720 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800721 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700722 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800723 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700724}
725
726static void free_macros(void)
727{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700728 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800729 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700730}
731
732/*
733 * Initialize the hash tables
734 */
735static void init_macros(void)
736{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700737}
738
739/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000740 * Pop the context stack.
741 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000742static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000743{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000744 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000745
746 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700747 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000748 nasm_free(c->name);
749 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000750}
751
H. Peter Anvin072771e2008-05-22 13:17:51 -0700752/*
753 * Search for a key in the hash index; adding it if necessary
754 * (in which case we initialize the data pointer to NULL.)
755 */
756static void **
757hash_findi_add(struct hash_table *hash, const char *str)
758{
759 struct hash_insert hi;
760 void **r;
761 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800762 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700763
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800764 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700765 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300766 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700767
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800768 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
769 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700770 return hash_add(&hi, strx, NULL);
771}
772
773/*
774 * Like hash_findi, but returns the data element rather than a pointer
775 * to it. Used only when not adding a new element, hence no third
776 * argument.
777 */
778static void *
779hash_findix(struct hash_table *hash, const char *str)
780{
781 void **p;
782
783 p = hash_findi(hash, str, NULL);
784 return p ? *p : NULL;
785}
786
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400787/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800788 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400789 * if there no more left -- return NULL
790 */
791static char *line_from_stdmac(void)
792{
793 unsigned char c;
794 const unsigned char *p = stdmacpos;
795 char *line, *q;
796 size_t len = 0;
797
798 if (!stdmacpos)
799 return NULL;
800
801 while ((c = *p++)) {
802 if (c >= 0x80)
803 len += pp_directives_len[c - 0x80] + 1;
804 else
805 len++;
806 }
807
808 line = nasm_malloc(len + 1);
809 q = line;
810 while ((c = *stdmacpos++)) {
811 if (c >= 0x80) {
812 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
813 q += pp_directives_len[c - 0x80];
814 *q++ = ' ';
815 } else {
816 *q++ = c;
817 }
818 }
819 stdmacpos = p;
820 *q = '\0';
821
822 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700823 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400824 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700825 if (*stdmacnext) {
826 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400827 } else if (do_predef) {
828 Line *pd, *l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400829
830 /*
831 * Nasty hack: here we push the contents of
832 * `predef' on to the top-level expansion stack,
833 * since this is the most convenient way to
834 * implement the pre-include and pre-define
835 * features.
836 */
837 list_for_each(pd, predef) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800838 l = nasm_malloc(sizeof(Line));
839 l->next = istk->expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700840 l->first = dup_tlist(pd->first, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800841 l->finishes = NULL;
842
843 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400844 }
845 do_predef = false;
846 }
847 }
848
849 return line;
850}
851
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000852static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000853{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700854 int c;
855 unsigned int size, next;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400856 const unsigned int delta = 512;
857 const unsigned int pad = 8;
858 unsigned int nr_cont = 0;
859 bool cont = false;
860 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000861
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400862 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400863 p = line_from_stdmac();
864 if (p)
865 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700866
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400867 size = delta;
868 p = buffer = nasm_malloc(size);
869
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700870 do {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400871 c = fgetc(istk->fp);
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400872
873 switch (c) {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700874 case EOF:
875 if (p == buffer) {
876 nasm_free(buffer);
877 return NULL;
878 }
879 c = 0;
880 break;
881
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400882 case '\r':
883 next = fgetc(istk->fp);
884 if (next != '\n')
885 ungetc(next, istk->fp);
886 if (cont) {
887 cont = false;
888 continue;
889 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700890 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400891 break;
892
893 case '\n':
894 if (cont) {
895 cont = false;
896 continue;
897 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700898 c = 0;
899 break;
900
901 case 032: /* ^Z = legacy MS-DOS end of file mark */
902 c = 0;
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400903 break;
904
905 case '\\':
906 next = fgetc(istk->fp);
907 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400908 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400909 cont = true;
910 nr_cont++;
911 continue;
912 }
913 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000914 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400915
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400916 if (p >= (buffer + size - pad)) {
917 buffer = nasm_realloc(buffer, size + delta);
918 p = buffer + size - pad;
919 size += delta;
920 }
921
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -0700922 *p++ = c;
923 } while (c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000924
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300925 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400926 (nr_cont * istk->lineinc));
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800927 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000928
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000929 return buffer;
930}
931
932/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000933 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000934 * don't need to parse the value out of e.g. numeric tokens: we
935 * simply split one string into many.
936 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000937static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000938{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700939 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000940 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000941 Token *list = NULL;
942 Token *t, **tail = &list;
943
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 while (*line) {
945 p = line;
946 if (*p == '%') {
947 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300948 if (*p == '+' && !nasm_isdigit(p[1])) {
949 p++;
950 type = TOK_PASTE;
951 } else if (nasm_isdigit(*p) ||
952 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 do {
954 p++;
955 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700956 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 type = TOK_PREPROC_ID;
958 } else if (*p == '{') {
959 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800960 while (*p) {
961 if (*p == '}')
962 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 p[-1] = *p;
964 p++;
965 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800966 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800967 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 p[-1] = '\0';
969 if (*p)
970 p++;
971 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300972 } else if (*p == '[') {
973 int lvl = 1;
974 line += 2; /* Skip the leading %[ */
975 p++;
976 while (lvl && (c = *p++)) {
977 switch (c) {
978 case ']':
979 lvl--;
980 break;
981 case '%':
982 if (*p == '[')
983 lvl++;
984 break;
985 case '\'':
986 case '\"':
987 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300988 p = nasm_skip_string(p - 1);
989 if (*p)
990 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300991 break;
992 default:
993 break;
994 }
995 }
996 p--;
997 if (*p)
998 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800999 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001000 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001001 type = TOK_INDIRECT;
1002 } else if (*p == '?') {
1003 type = TOK_PREPROC_Q; /* %? */
1004 p++;
1005 if (*p == '?') {
1006 type = TOK_PREPROC_QQ; /* %?? */
1007 p++;
1008 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001009 } else if (*p == '!') {
1010 type = TOK_PREPROC_ID;
1011 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001012 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001013 do {
1014 p++;
1015 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001016 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001017 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001018 p = nasm_skip_string(p);
1019 if (*p)
1020 p++;
1021 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001022 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001023 } else {
1024 /* %! without string or identifier */
1025 type = TOK_OTHER; /* Legacy behavior... */
1026 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001027 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001029 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 do {
1031 p++;
1032 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001033 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 type = TOK_PREPROC_ID;
1035 } else {
1036 type = TOK_OTHER;
1037 if (*p == '%')
1038 p++;
1039 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001040 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 type = TOK_ID;
1042 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001043 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001045 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 /*
1047 * A string token.
1048 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001049 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001050 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001051
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 if (*p) {
1053 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001054 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001055 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 /* Handling unterminated strings by UNV */
1057 /* type = -1; */
1058 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001059 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001060 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001061 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001062 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001063 bool is_hex = false;
1064 bool is_float = false;
1065 bool has_e = false;
1066 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001067
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001069 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001071
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001072 if (*p == '$') {
1073 p++;
1074 is_hex = true;
1075 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001076
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001077 for (;;) {
1078 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001079
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001080 if (!is_hex && (c == 'e' || c == 'E')) {
1081 has_e = true;
1082 if (*p == '+' || *p == '-') {
1083 /*
1084 * e can only be followed by +/- if it is either a
1085 * prefixed hex number or a floating-point number
1086 */
1087 p++;
1088 is_float = true;
1089 }
1090 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1091 is_hex = true;
1092 } else if (c == 'P' || c == 'p') {
1093 is_float = true;
1094 if (*p == '+' || *p == '-')
1095 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001096 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001097 ; /* just advance */
1098 else if (c == '.') {
1099 /*
1100 * we need to deal with consequences of the legacy
1101 * parser, like "1.nolist" being two tokens
1102 * (TOK_NUMBER, TOK_ID) here; at least give it
1103 * a shot for now. In the future, we probably need
1104 * a flex-based scanner with proper pattern matching
1105 * to do it as well as it can be done. Nothing in
1106 * the world is going to help the person who wants
1107 * 0x123.p16 interpreted as two tokens, though.
1108 */
1109 r = p;
1110 while (*r == '_')
1111 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001112
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001113 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1114 (!is_hex && (*r == 'e' || *r == 'E')) ||
1115 (*r == 'p' || *r == 'P')) {
1116 p = r;
1117 is_float = true;
1118 } else
1119 break; /* Terminate the token */
1120 } else
1121 break;
1122 }
1123 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001124
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001125 if (p == line+1 && *line == '$') {
1126 type = TOK_OTHER; /* TOKEN_HERE */
1127 } else {
1128 if (has_e && !is_hex) {
1129 /* 1e13 is floating-point, but 1e13h is not */
1130 is_float = true;
1131 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001132
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001133 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1134 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001135 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001137 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001138 /*
1139 * Whitespace just before end-of-line is discarded by
1140 * pretending it's a comment; whitespace just before a
1141 * comment gets lumped into the comment.
1142 */
1143 if (!*p || *p == ';') {
1144 type = TOK_COMMENT;
1145 while (*p)
1146 p++;
1147 }
1148 } else if (*p == ';') {
1149 type = TOK_COMMENT;
1150 while (*p)
1151 p++;
1152 } else {
1153 /*
1154 * Anything else is an operator of some kind. We check
1155 * for all the double-character operators (>>, <<, //,
1156 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001157 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 */
1159 type = TOK_OTHER;
1160 if ((p[0] == '>' && p[1] == '>') ||
1161 (p[0] == '<' && p[1] == '<') ||
1162 (p[0] == '/' && p[1] == '/') ||
1163 (p[0] == '<' && p[1] == '=') ||
1164 (p[0] == '>' && p[1] == '=') ||
1165 (p[0] == '=' && p[1] == '=') ||
1166 (p[0] == '!' && p[1] == '=') ||
1167 (p[0] == '<' && p[1] == '>') ||
1168 (p[0] == '&' && p[1] == '&') ||
1169 (p[0] == '|' && p[1] == '|') ||
1170 (p[0] == '^' && p[1] == '^')) {
1171 p++;
1172 }
1173 p++;
1174 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001175
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 /* Handling unterminated string by UNV */
1177 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001178 {
1179 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1180 t->text[p-line] = *line;
1181 tail = &t->next;
1182 }
1183 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 if (type != TOK_COMMENT) {
1185 *tail = t = new_Token(NULL, type, line, p - line);
1186 tail = &t->next;
1187 }
1188 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001189 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001190 return list;
1191}
1192
H. Peter Anvince616072002-04-30 21:02:23 +00001193/*
1194 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001195 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001196 * deleted only all at once by the delete_Blocks function.
1197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001199{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001200 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001202 /* first, get to the end of the linked list */
1203 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001205 /* now allocate the requested chunk */
1206 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001208 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001209 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001210 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001211}
1212
1213/*
1214 * this function deletes all managed blocks of memory
1215 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001217{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001219
H. Peter Anvin70653092007-10-19 14:42:29 -07001220 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001221 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001222 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001223 * free it.
1224 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001225 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001226 if (b->chunk)
1227 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 a = b;
1229 b = b->next;
1230 if (a != &blocks)
1231 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001232 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001233 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001235
1236/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001237 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001238 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001239 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001240static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001241 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001242{
1243 Token *t;
1244 int i;
1245
H. Peter Anvin89cee572009-07-15 09:16:54 -04001246 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1248 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1249 freeTokens[i].next = &freeTokens[i + 1];
1250 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001251 }
1252 t = freeTokens;
1253 freeTokens = t->next;
1254 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001255 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001256 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001257 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 t->text = NULL;
1259 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001260 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001262 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001263 t->text = nasm_malloc(txtlen+1);
1264 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001266 }
1267 return t;
1268}
1269
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001270static Token *dup_Token(Token *next, const Token *src)
1271{
1272 return new_Token(next, src->type, src->text, src->len);
1273}
1274
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001276{
1277 Token *next = t->next;
1278 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001279 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001280 freeTokens = t;
1281 return next;
1282}
1283
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001284/*
1285 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001286 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1287 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001288 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001289static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001290{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001291 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001292 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001293 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001294 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001295
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001296 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001297 if (t->type == TOK_PREPROC_ID && t->text &&
1298 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001299 char *v;
1300 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001301
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001302 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001303 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001304 size_t len = nasm_unquote(v, NULL);
1305 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001306
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001307 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001308 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001309 v = NULL;
1310 }
1311 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001312
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001313 if (v) {
1314 char *p = getenv(v);
1315 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001316 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001317 /*
1318 * FIXME We better should investigate if accessing
1319 * ->text[1] without ->text[0] is safe enough.
1320 */
1321 t->text = nasm_zalloc(2);
1322 } else
1323 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001324 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001325 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001327
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 /* Expand local macros here and not during preprocessing */
1329 if (expand_locals &&
1330 t->type == TOK_PREPROC_ID && t->text &&
1331 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001332 const char *q;
1333 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001334 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001336 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001337 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001338 p = nasm_strcat(buffer, q);
1339 nasm_free(t->text);
1340 t->text = p;
1341 }
1342 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001343 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001345 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001347 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001348
H. Peter Anvin734b1882002-04-30 21:01:08 +00001349 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001350
1351 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001352 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001353 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001355 q = t->text;
1356 while (*q)
1357 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001359 }
1360 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001361
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001362 return line;
1363}
1364
1365/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001366 * A scanner, suitable for use by the expression evaluator, which
1367 * operates on a line of Tokens. Expects a pointer to a pointer to
1368 * the first token in the line to be passed in as its private_data
1369 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001370 *
1371 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001372 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001373struct ppscan {
1374 Token *tptr;
1375 int ntokens;
1376};
1377
H. Peter Anvine2c80182005-01-15 22:15:51 +00001378static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001379{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001380 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001381 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001382 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001383
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001385 if (pps->ntokens && (tline = pps->tptr)) {
1386 pps->ntokens--;
1387 pps->tptr = tline->next;
1388 } else {
1389 pps->tptr = NULL;
1390 pps->ntokens = 0;
1391 return tokval->t_type = TOKEN_EOS;
1392 }
1393 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001394
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001395 tokval->t_charptr = tline->text;
1396
H. Peter Anvin76690a12002-04-30 20:52:49 +00001397 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001399 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001400 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001401
H. Peter Anvine2c80182005-01-15 22:15:51 +00001402 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001403 p = tokval->t_charptr = tline->text;
1404 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 tokval->t_charptr++;
1406 return tokval->t_type = TOKEN_ID;
1407 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001408
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001409 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001410 if (r >= p+MAX_KEYWORD)
1411 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001412 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001413 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001414 *s = '\0';
1415 /* right, so we have an identifier sitting in temp storage. now,
1416 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001417 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001418 }
1419
H. Peter Anvine2c80182005-01-15 22:15:51 +00001420 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001421 bool rn_error;
1422 tokval->t_integer = readnum(tline->text, &rn_error);
1423 tokval->t_charptr = tline->text;
1424 if (rn_error)
1425 return tokval->t_type = TOKEN_ERRNUM;
1426 else
1427 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001428 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001429
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001430 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001431 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001432 }
1433
H. Peter Anvine2c80182005-01-15 22:15:51 +00001434 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001435 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001436
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001437 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001438 tokval->t_charptr = tline->text;
1439 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001440
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001441 if (ep[0] != bq || ep[1] != '\0')
1442 return tokval->t_type = TOKEN_ERRSTR;
1443 else
1444 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001445 }
1446
H. Peter Anvine2c80182005-01-15 22:15:51 +00001447 if (tline->type == TOK_OTHER) {
1448 if (!strcmp(tline->text, "<<"))
1449 return tokval->t_type = TOKEN_SHL;
1450 if (!strcmp(tline->text, ">>"))
1451 return tokval->t_type = TOKEN_SHR;
1452 if (!strcmp(tline->text, "//"))
1453 return tokval->t_type = TOKEN_SDIV;
1454 if (!strcmp(tline->text, "%%"))
1455 return tokval->t_type = TOKEN_SMOD;
1456 if (!strcmp(tline->text, "=="))
1457 return tokval->t_type = TOKEN_EQ;
1458 if (!strcmp(tline->text, "<>"))
1459 return tokval->t_type = TOKEN_NE;
1460 if (!strcmp(tline->text, "!="))
1461 return tokval->t_type = TOKEN_NE;
1462 if (!strcmp(tline->text, "<="))
1463 return tokval->t_type = TOKEN_LE;
1464 if (!strcmp(tline->text, ">="))
1465 return tokval->t_type = TOKEN_GE;
1466 if (!strcmp(tline->text, "&&"))
1467 return tokval->t_type = TOKEN_DBL_AND;
1468 if (!strcmp(tline->text, "^^"))
1469 return tokval->t_type = TOKEN_DBL_XOR;
1470 if (!strcmp(tline->text, "||"))
1471 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001472 }
1473
1474 /*
1475 * We have no other options: just return the first character of
1476 * the token text.
1477 */
1478 return tokval->t_type = tline->text[0];
1479}
1480
1481/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001482 * Compare a string to the name of an existing macro; this is a
1483 * simple wrapper which calls either strcmp or nasm_stricmp
1484 * depending on the value of the `casesense' parameter.
1485 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001486static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001487{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001488 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001489}
1490
1491/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001492 * Compare a string to the name of an existing macro; this is a
1493 * simple wrapper which calls either strcmp or nasm_stricmp
1494 * depending on the value of the `casesense' parameter.
1495 */
1496static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1497{
1498 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1499}
1500
1501/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001502 * Return the Context structure associated with a %$ token. Return
1503 * NULL, having _already_ reported an error condition, if the
1504 * context stack isn't deep enough for the supplied number of $
1505 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001506 *
1507 * If "namep" is non-NULL, set it to the pointer to the macro name
1508 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001509 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001510static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001511{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001512 Context *ctx;
1513 int i;
1514
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001515 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001516 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001517
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001518 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001520
H. Peter Anvine2c80182005-01-15 22:15:51 +00001521 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001522 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001524 }
1525
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001526 name += 2;
1527 ctx = cstk;
1528 i = 0;
1529 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001530 name++;
1531 i++;
1532 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001533 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001535 nasm_nonfatal("`%s': context stack is only"
1536 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001538 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001539
1540 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001541 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001542
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001543 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001544}
1545
1546/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001547 * Open an include file. This routine must always return a valid
1548 * file pointer if it returns - it's responsible for throwing an
1549 * ERR_FATAL and bombing out completely if not. It should also try
1550 * the include path one by one until it finds the file or reaches
1551 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001552 *
1553 * Note: for INC_PROBE the function returns NULL at all times;
1554 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001555 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001556enum incopen_mode {
1557 INC_NEEDED, /* File must exist */
1558 INC_OPTIONAL, /* Missing is OK */
1559 INC_PROBE /* Only an existence probe */
1560};
1561
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001562/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001563static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001564 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001565{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001566 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001567 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001568 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001569 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001570 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001571
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001573 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001574 if (omode == INC_PROBE) {
1575 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001576 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001577 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001578 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001579 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001580 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001581 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001582 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001584 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001585
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001586 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001587
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001588 if (!ip) {
1589 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001590 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001591 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001592
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001593 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001594 ip = ip->next;
1595 }
1596}
1597
1598/*
1599 * Open a file, or test for the presence of one (depending on omode),
1600 * considering the include path.
1601 */
1602static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001603 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001604 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001605 enum incopen_mode omode,
1606 enum file_flags fmode)
1607{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001608 struct hash_insert hi;
1609 void **hp;
1610 char *path;
1611 FILE *fp = NULL;
1612
1613 hp = hash_find(&FileHash, file, &hi);
1614 if (hp) {
1615 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001616 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001617 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001618 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001619 } else {
1620 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001621 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001622
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001623 /* Positive or negative result */
1624 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001625
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001626 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001627 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001628 */
1629 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001630 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001631 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001632
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001633 if (!path) {
1634 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001635 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001636 } else {
1637 if (!fp && omode != INC_PROBE)
1638 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001639 }
1640
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001641 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001642 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001643
1644 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001645}
1646
1647/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001648 * Opens an include or input file. Public version, for use by modules
1649 * that get a file:lineno pair and need to look at the file again
1650 * (e.g. the CodeView debug backend). Returns NULL on failure.
1651 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001652FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001653{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001654 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001655}
1656
1657/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001658 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001659 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001660 * return true if _any_ single-line macro of that name is defined.
1661 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001662 * `nparam' or no parameters is defined.
1663 *
1664 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001665 * defined, or nparam is -1, the address of the definition structure
1666 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001667 * is NULL, no action will be taken regarding its contents, and no
1668 * error will occur.
1669 *
1670 * Note that this is also called with nparam zero to resolve
1671 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001672 *
1673 * If you already know which context macro belongs to, you can pass
1674 * the context pointer as first parameter; if you won't but name begins
1675 * with %$ the context will be automatically computed. If all_contexts
1676 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001677 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001678static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001679smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001680 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001681{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001682 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001683 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001684
H. Peter Anvin97a23472007-09-16 17:57:25 -07001685 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001686 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001687 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001688 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001689 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001691 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001692 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001693 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001694 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001695 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001696 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001697
H. Peter Anvine2c80182005-01-15 22:15:51 +00001698 while (m) {
1699 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001700 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001701 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001702 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001703 *defn = m;
1704 else
1705 *defn = NULL;
1706 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001707 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001708 }
1709 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001710 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001711
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001712 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001713}
1714
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001715/* param should be a natural number [0; INT_MAX] */
1716static int read_param_count(const char *str)
1717{
1718 int result;
1719 bool err;
1720
1721 result = readnum(str, &err);
1722 if (result < 0 || result > INT_MAX) {
1723 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001724 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1725 str, 0, INT_MAX);
1726 } else if (err)
1727 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001728 return result;
1729}
1730
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001731/*
1732 * Count and mark off the parameters in a multi-line macro call.
1733 * This is called both from within the multi-line macro expansion
1734 * code, and also to mark off the default parameters when provided
1735 * in a %macro definition line.
1736 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001738{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001739 int paramsize, brace;
1740
1741 *nparam = paramsize = 0;
1742 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001744 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001745 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 paramsize += PARAM_DELTA;
1747 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1748 }
1749 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001750 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001752 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001754 if (brace) {
1755 while (brace && (t = t->next) != NULL) {
1756 if (tok_is_(t, "{"))
1757 brace++;
1758 else if (tok_is_(t, "}"))
1759 brace--;
1760 }
1761
1762 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 /*
1764 * Now we've found the closing brace, look further
1765 * for the comma.
1766 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001767 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 skip_white_(t);
1769 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001770 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 while (tok_isnt_(t, ","))
1772 t = t->next;
1773 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001775 } else {
1776 while (tok_isnt_(t, ","))
1777 t = t->next;
1778 }
1779 if (t) { /* got a comma/brace */
1780 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001782 }
1783}
1784
1785/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001786 * Determine whether one of the various `if' conditions is true or
1787 * not.
1788 *
1789 * We must free the tline we get passed.
1790 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001791static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001792{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001793 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001794 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001795 Token *t, *tt, *origline;
1796 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001797 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001798 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001799 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001800 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001801
1802 origline = tline;
1803
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001805 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001806 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001807 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001809 if (!tline)
1810 break;
1811 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001812 nasm_nonfatal("`%s' expects context identifiers",
1813 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 free_tlist(origline);
1815 return -1;
1816 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001817 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001818 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 tline = tline->next;
1820 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001821 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001822
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001823 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001824 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001825 while (tline) {
1826 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 if (!tline || (tline->type != TOK_ID &&
1828 (tline->type != TOK_PREPROC_ID ||
1829 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001830 nasm_nonfatal("`%s' expects macro identifiers",
1831 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001832 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001834 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001835 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001837 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001838 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001839
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001840 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001841 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001842 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001843 while (tline) {
1844 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001845 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001846 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001847 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001848 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001849 nasm_nonfatal("`%s' expects environment variable names",
1850 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001851 goto fail;
1852 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001853 p = tline->text;
1854 if (tline->type == TOK_PREPROC_ID)
1855 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001856 if (nasm_isquote(*p))
H. Peter Anvinbb42d302019-04-22 14:29:29 -07001857 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001858 if (getenv(p))
1859 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001860 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001861 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001862 break;
1863
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001864 case PPC_IFIDN:
1865 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 tline = expand_smacro(tline);
1867 t = tt = tline;
1868 while (tok_isnt_(tt, ","))
1869 tt = tt->next;
1870 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001871 nasm_nonfatal("`%s' expects two comma-separated arguments",
1872 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001873 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 }
1875 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001876 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1878 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001879 nasm_nonfatal("`%s': more than one comma on line",
1880 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001881 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 }
1883 if (t->type == TOK_WHITESPACE) {
1884 t = t->next;
1885 continue;
1886 }
1887 if (tt->type == TOK_WHITESPACE) {
1888 tt = tt->next;
1889 continue;
1890 }
1891 if (tt->type != t->type) {
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 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001895 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001896 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001897 size_t l1 = nasm_unquote(t->text, NULL);
1898 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001899
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001900 if (l1 != l2) {
1901 j = false;
1902 break;
1903 }
1904 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1905 j = false;
1906 break;
1907 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001908 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001909 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 break;
1911 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001912
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 t = t->next;
1914 tt = tt->next;
1915 }
1916 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001917 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001919
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001920 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001921 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001922 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001923 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001924
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001925 skip_white_(tline);
1926 tline = expand_id(tline);
1927 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001928 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001929 goto fail;
1930 }
1931 searching.name = nasm_strdup(tline->text);
1932 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001933 searching.plus = false;
1934 searching.nolist = false;
1935 searching.in_progress = 0;
1936 searching.max_depth = 0;
1937 searching.rep_nest = NULL;
1938 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001939 searching.nparam_max = INT_MAX;
1940 tline = expand_smacro(tline->next);
1941 skip_white_(tline);
1942 if (!tline) {
1943 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001944 nasm_nonfatal("`%s' expects a parameter count or nothing",
1945 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001946 } else {
1947 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001948 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001949 }
1950 if (tline && tok_is_(tline->next, "-")) {
1951 tline = tline->next->next;
1952 if (tok_is_(tline, "*"))
1953 searching.nparam_max = INT_MAX;
1954 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001955 nasm_nonfatal("`%s' expects a parameter count after `-'",
1956 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001957 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001958 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001959 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001960 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001961 searching.nparam_max = searching.nparam_min;
1962 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001963 }
1964 }
1965 if (tline && tok_is_(tline->next, "+")) {
1966 tline = tline->next;
1967 searching.plus = true;
1968 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001969 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1970 while (mmac) {
1971 if (!strcmp(mmac->name, searching.name) &&
1972 (mmac->nparam_min <= searching.nparam_max
1973 || searching.plus)
1974 && (searching.nparam_min <= mmac->nparam_max
1975 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001976 found = true;
1977 break;
1978 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001979 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001980 }
1981 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001982 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001983 nasm_free(searching.name);
1984 j = found;
1985 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001986 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001987
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001988 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001989 needtype = TOK_ID;
1990 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001991 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001992 needtype = TOK_NUMBER;
1993 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001994 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001995 needtype = TOK_STRING;
1996 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001997
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001998iftype:
1999 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07002000
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 while (tok_type_(t, TOK_WHITESPACE) ||
2002 (needtype == TOK_NUMBER &&
2003 tok_type_(t, TOK_OTHER) &&
2004 (t->text[0] == '-' || t->text[0] == '+') &&
2005 !t->text[1]))
2006 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002007
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002008 j = tok_type_(t, needtype);
2009 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002010
2011 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002012 t = tline = expand_smacro(tline);
2013 while (tok_type_(t, TOK_WHITESPACE))
2014 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002015
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002016 j = false;
2017 if (t) {
2018 t = t->next; /* Skip the actual token */
2019 while (tok_type_(t, TOK_WHITESPACE))
2020 t = t->next;
2021 j = !t; /* Should be nothing left */
2022 }
2023 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002024
H. Peter Anvin134b9462008-02-16 17:01:40 -08002025 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002026 t = tline = expand_smacro(tline);
2027 while (tok_type_(t, TOK_WHITESPACE))
2028 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002029
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002030 j = !t; /* Should be empty */
2031 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002032
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002033 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002034 pps.tptr = tline = expand_smacro(tline);
2035 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002036 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002037 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002038 if (!evalresult)
2039 return -1;
2040 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002041 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002042 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002043 nasm_nonfatal("non-constant value given to `%s'",
2044 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002045 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002046 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002047 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002048 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002049
H. Peter Anvine2c80182005-01-15 22:15:51 +00002050 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002051 nasm_fatal("preprocessor directive `%s' not yet implemented",
2052 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002053 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002054 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002055
2056 free_tlist(origline);
2057 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002058
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002059fail:
2060 free_tlist(origline);
2061 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002062}
2063
2064/*
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002065 * Default smacro expansion routine: just returns a copy of the
2066 * expansion list.
2067 */
2068static Token *
2069smacro_expand_default(const SMacro *s, Token **params, unsigned int nparams)
2070{
2071 (void)params;
2072 (void)nparams;
2073
2074 return dup_tlist(s->expansion, NULL);
2075}
2076
2077/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002078 * Common code for defining an smacro
2079 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002080static SMacro *define_smacro(Context *ctx, const char *mname,
2081 bool casesense, int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002082{
2083 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002084 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002085
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002086 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002087 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002088 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002089 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002090 /*
2091 * Some instances of the old code considered this a failure,
2092 * some others didn't. What is the right thing to do here?
2093 */
2094 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002095 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002096 } else {
2097 /*
2098 * We're redefining, so we have to take over an
2099 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002100 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002101 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002102 clear_smacro(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002103 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002104 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002105 smtbl = ctx ? &ctx->localmac : &smacros;
2106 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002107 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002108 smac->next = *smhead;
2109 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002110 }
2111 smac->name = nasm_strdup(mname);
2112 smac->casesense = casesense;
2113 smac->nparam = nparam;
2114 smac->expansion = expansion;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07002115 smac->expand = smacro_expand_default;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002116 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002117}
2118
2119/*
2120 * Undefine an smacro
2121 */
2122static void undef_smacro(Context *ctx, const char *mname)
2123{
2124 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002125 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002126
H. Peter Anvin166c2472008-05-28 12:28:58 -07002127 smtbl = ctx ? &ctx->localmac : &smacros;
2128 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002129
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002130 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002131 /*
2132 * We now have a macro name... go hunt for it.
2133 */
2134 sp = smhead;
2135 while ((s = *sp) != NULL) {
2136 if (!mstrcmp(s->name, mname, s->casesense)) {
2137 *sp = s->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002138 free_smacro(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002139 } else {
2140 sp = &s->next;
2141 }
2142 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002143 }
2144}
2145
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002146/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002147 * Parse a mmacro specification.
2148 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002149static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002150{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002151 tline = tline->next;
2152 skip_white_(tline);
2153 tline = expand_id(tline);
2154 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002155 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002156 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002157 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002158
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002159 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002160 def->name = nasm_strdup(tline->text);
2161 def->plus = false;
2162 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002163 def->in_progress = 0;
2164 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002165 def->nparam_min = 0;
2166 def->nparam_max = 0;
2167
H. Peter Anvina26433d2008-07-16 14:40:01 -07002168 tline = expand_smacro(tline->next);
2169 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002170 if (!tok_type_(tline, TOK_NUMBER))
2171 nasm_nonfatal("`%s' expects a parameter count", directive);
2172 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002173 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002174 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002175 tline = tline->next->next;
2176 if (tok_is_(tline, "*")) {
2177 def->nparam_max = INT_MAX;
2178 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002179 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002180 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002181 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002182 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002183 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002184 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002185 }
2186 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002187 }
2188 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002189 tline = tline->next;
2190 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002191 }
2192 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002193 !nasm_stricmp(tline->next->text, ".nolist")) {
2194 tline = tline->next;
2195 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002196 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002197
H. Peter Anvina26433d2008-07-16 14:40:01 -07002198 /*
2199 * Handle default parameters.
2200 */
2201 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002202 def->dlist = tline->next;
2203 tline->next = NULL;
2204 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002205 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002206 def->dlist = NULL;
2207 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002208 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002209 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002210
H. Peter Anvin89cee572009-07-15 09:16:54 -04002211 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002212 !def->plus) {
2213 /*
2214 *!macro-defaults [on] macros with more default than optional parameters
2215 *! warns when a macro has more default parameters than optional parameters.
2216 *! See \k{mlmacdef} for why might want to disable this warning.
2217 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002218 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002219 "too many default macro parameters in macro `%s'", def->name);
2220 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002221
H. Peter Anvina26433d2008-07-16 14:40:01 -07002222 return true;
2223}
2224
2225
2226/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002227 * Decode a size directive
2228 */
2229static int parse_size(const char *str) {
2230 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002231 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002232 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002233 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002234 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002235}
2236
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002237/*
2238 * Process a preprocessor %pragma directive. Currently there are none.
2239 * Gets passed the token list starting with the "preproc" token from
2240 * "%pragma preproc".
2241 */
2242static void do_pragma_preproc(Token *tline)
2243{
2244 /* Skip to the real stuff */
2245 tline = tline->next;
2246 skip_white_(tline);
2247 if (!tline)
2248 return;
2249
2250 (void)tline; /* Nothing else to do at present */
2251}
2252
Ed Beroset3ab3f412002-06-11 03:31:49 +00002253/**
2254 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002255 * Find out if a line contains a preprocessor directive, and deal
2256 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002257 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002258 * If a directive _is_ found, it is the responsibility of this routine
2259 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002260 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002261 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002262 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002263 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002264 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002265 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002266static int do_directive(Token *tline, Token **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002267{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002268 enum preproc_token i;
2269 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002270 bool err;
2271 int nparam;
2272 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002273 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002274 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002275 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002276 char *p, *pp;
2277 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002278 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002279 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002280 Include *inc;
2281 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002282 Cond *cond;
2283 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002284 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002285 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002286 struct tokenval tokval;
2287 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002288 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002289 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002290 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002291 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002292 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002293
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002294 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002295 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002296
H. Peter Anvineba20a72002-04-30 20:53:55 +00002297 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002298 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002299 (tline->text[0] && (tline->text[1] == '%' ||
2300 tline->text[1] == '$' ||
2301 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002303
H. Peter Anvin4169a472007-09-12 01:29:43 +00002304 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002305
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002306 /*
2307 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2308 * since they are known to be buggy at moment, we need to fix them
2309 * in future release (2.09-2.10)
2310 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002311 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002312 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2313 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002314 }
2315
2316 /*
2317 * If we're in a non-emitting branch of a condition construct,
2318 * or walking to the end of an already terminated %rep block,
2319 * we should ignore all directives except for condition
2320 * directives.
2321 */
2322 if (((istk->conds && !emitting(istk->conds->state)) ||
2323 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2324 return NO_DIRECTIVE_FOUND;
2325 }
2326
2327 /*
2328 * If we're defining a macro or reading a %rep block, we should
2329 * ignore all directives except for %macro/%imacro (which nest),
2330 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2331 * If we're in a %rep block, another %rep nests, so should be let through.
2332 */
2333 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2334 i != PP_RMACRO && i != PP_IRMACRO &&
2335 i != PP_ENDMACRO && i != PP_ENDM &&
2336 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2337 return NO_DIRECTIVE_FOUND;
2338 }
2339
2340 if (defining) {
2341 if (i == PP_MACRO || i == PP_IMACRO ||
2342 i == PP_RMACRO || i == PP_IRMACRO) {
2343 nested_mac_count++;
2344 return NO_DIRECTIVE_FOUND;
2345 } else if (nested_mac_count > 0) {
2346 if (i == PP_ENDMACRO) {
2347 nested_mac_count--;
2348 return NO_DIRECTIVE_FOUND;
2349 }
2350 }
2351 if (!defining->name) {
2352 if (i == PP_REP) {
2353 nested_rep_count++;
2354 return NO_DIRECTIVE_FOUND;
2355 } else if (nested_rep_count > 0) {
2356 if (i == PP_ENDREP) {
2357 nested_rep_count--;
2358 return NO_DIRECTIVE_FOUND;
2359 }
2360 }
2361 }
2362 }
2363
H. Peter Anvin8b262472019-02-26 14:00:54 -08002364 dname = pp_directives[i]; /* Directive name, for error messages */
2365 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002366 switch (i) {
2367 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002368 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002370
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002371 case PP_PRAGMA:
2372 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002373 * %pragma namespace options...
2374 *
2375 * The namespace "preproc" is reserved for the preprocessor;
2376 * all other namespaces generate a [pragma] assembly directive.
2377 *
2378 * Invalid %pragmas are ignored and may have different
2379 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002380 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002381 tline = tline->next;
2382 skip_white_(tline);
2383 tline = expand_smacro(tline);
2384 if (tok_type_(tline, TOK_ID)) {
2385 if (!nasm_stricmp(tline->text, "preproc")) {
2386 /* Preprocessor pragma */
2387 do_pragma_preproc(tline);
2388 } else {
2389 /* Build the assembler directive */
2390 t = new_Token(NULL, TOK_OTHER, "[", 1);
2391 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2392 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2393 tline = t;
2394 for (t = tline; t->next; t = t->next)
2395 ;
2396 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07002397 *output = tline;
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002398 }
2399 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002400 free_tlist(origline);
2401 return DIRECTIVE_FOUND;
2402
H. Peter Anvine2c80182005-01-15 22:15:51 +00002403 case PP_STACKSIZE:
2404 /* Directive to tell NASM what the default stack size is. The
2405 * default is for a 16-bit stack, and this can be overriden with
2406 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407 */
2408 tline = tline->next;
2409 if (tline && tline->type == TOK_WHITESPACE)
2410 tline = tline->next;
2411 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002412 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002413 free_tlist(origline);
2414 return DIRECTIVE_FOUND;
2415 }
2416 if (nasm_stricmp(tline->text, "flat") == 0) {
2417 /* All subsequent ARG directives are for a 32-bit stack */
2418 StackSize = 4;
2419 StackPointer = "ebp";
2420 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002421 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002422 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2423 /* All subsequent ARG directives are for a 64-bit stack */
2424 StackSize = 8;
2425 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002426 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002427 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 } else if (nasm_stricmp(tline->text, "large") == 0) {
2429 /* All subsequent ARG directives are for a 16-bit stack,
2430 * far function call.
2431 */
2432 StackSize = 2;
2433 StackPointer = "bp";
2434 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002435 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002436 } else if (nasm_stricmp(tline->text, "small") == 0) {
2437 /* All subsequent ARG directives are for a 16-bit stack,
2438 * far function call. We don't support near functions.
2439 */
2440 StackSize = 2;
2441 StackPointer = "bp";
2442 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002443 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002444 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002445 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002446 free_tlist(origline);
2447 return DIRECTIVE_FOUND;
2448 }
2449 free_tlist(origline);
2450 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002451
H. Peter Anvine2c80182005-01-15 22:15:51 +00002452 case PP_ARG:
2453 /* TASM like ARG directive to define arguments to functions, in
2454 * the following form:
2455 *
2456 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2457 */
2458 offset = ArgOffset;
2459 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002460 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002461 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002462
H. Peter Anvine2c80182005-01-15 22:15:51 +00002463 /* Find the argument name */
2464 tline = tline->next;
2465 if (tline && tline->type == TOK_WHITESPACE)
2466 tline = tline->next;
2467 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002468 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 free_tlist(origline);
2470 return DIRECTIVE_FOUND;
2471 }
2472 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002473
H. Peter Anvine2c80182005-01-15 22:15:51 +00002474 /* Find the argument size type */
2475 tline = tline->next;
2476 if (!tline || tline->type != TOK_OTHER
2477 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002478 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 free_tlist(origline);
2480 return DIRECTIVE_FOUND;
2481 }
2482 tline = tline->next;
2483 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002484 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 free_tlist(origline);
2486 return DIRECTIVE_FOUND;
2487 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002488
H. Peter Anvine2c80182005-01-15 22:15:51 +00002489 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002490 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002491 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002492 size = parse_size(tt->text);
2493 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002494 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 free_tlist(tt);
2496 free_tlist(origline);
2497 return DIRECTIVE_FOUND;
2498 }
2499 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002500
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002501 /* Round up to even stack slots */
2502 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002503
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 /* Now define the macro for the argument */
2505 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2506 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002507 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002509
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 /* Move to the next argument in the list */
2511 tline = tline->next;
2512 if (tline && tline->type == TOK_WHITESPACE)
2513 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002514 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002515 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 free_tlist(origline);
2517 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002518
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 case PP_LOCAL:
2520 /* TASM like LOCAL directive to define local variables for a
2521 * function, in the following form:
2522 *
2523 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2524 *
2525 * The '= LocalSize' at the end is ignored by NASM, but is
2526 * required by TASM to define the local parameter size (and used
2527 * by the TASM macro package).
2528 */
2529 offset = LocalOffset;
2530 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002531 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002533
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 /* Find the argument name */
2535 tline = tline->next;
2536 if (tline && tline->type == TOK_WHITESPACE)
2537 tline = tline->next;
2538 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002539 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 free_tlist(origline);
2541 return DIRECTIVE_FOUND;
2542 }
2543 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002544
H. Peter Anvine2c80182005-01-15 22:15:51 +00002545 /* Find the argument size type */
2546 tline = tline->next;
2547 if (!tline || tline->type != TOK_OTHER
2548 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002549 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 free_tlist(origline);
2551 return DIRECTIVE_FOUND;
2552 }
2553 tline = tline->next;
2554 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002555 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 free_tlist(origline);
2557 return DIRECTIVE_FOUND;
2558 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002559
H. Peter Anvine2c80182005-01-15 22:15:51 +00002560 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002561 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002562 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002563 size = parse_size(tt->text);
2564 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002565 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002566 free_tlist(tt);
2567 free_tlist(origline);
2568 return DIRECTIVE_FOUND;
2569 }
2570 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002571
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002572 /* Round up to even stack slots */
2573 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002574
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002576
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002577 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002578 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2579 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002580 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002581
H. Peter Anvine2c80182005-01-15 22:15:51 +00002582 /* Now define the assign to setup the enter_c macro correctly */
2583 snprintf(directive, sizeof(directive),
2584 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002585 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002586
H. Peter Anvine2c80182005-01-15 22:15:51 +00002587 /* Move to the next argument in the list */
2588 tline = tline->next;
2589 if (tline && tline->type == TOK_WHITESPACE)
2590 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002591 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002592 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002593 free_tlist(origline);
2594 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002595
H. Peter Anvine2c80182005-01-15 22:15:51 +00002596 case PP_CLEAR:
2597 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002598 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 free_macros();
2600 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002601 free_tlist(origline);
2602 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002603
H. Peter Anvin418ca702008-05-30 10:42:30 -07002604 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002605 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002606 skip_white_(t);
2607 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002608 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002609 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002610 free_tlist(origline);
2611 return DIRECTIVE_FOUND; /* but we did _something_ */
2612 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002613 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002614 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002615 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002616 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002617 nasm_unquote_cstr(p, NULL);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002618 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002619 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002620 return DIRECTIVE_FOUND;
2621
2622 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002623 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002624 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002625
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002626 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002627 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002628 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002629 free_tlist(origline);
2630 return DIRECTIVE_FOUND; /* but we did _something_ */
2631 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002632 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002633 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002634 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002635 if (t->type != TOK_INTERNAL_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002636 nasm_unquote_cstr(p, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002637 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002638 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002639 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002640 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002641 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002642 (pp_mode == PP_DEPS)
2643 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002644 if (!inc->fp) {
2645 /* -MG given but file not found */
2646 nasm_free(inc);
2647 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002648 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002649 inc->lineno = src_set_linnum(0);
2650 inc->lineinc = 1;
2651 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002652 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 istk = inc;
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07002654 lfmt->uplevel(LIST_INCLUDE, 0);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002655 }
2656 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002658
H. Peter Anvind2456592008-06-19 15:04:18 -07002659 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002660 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 static macros_t *use_pkg;
2662 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002663
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002664 tline = tline->next;
2665 skip_white_(tline);
2666 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002667
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002668 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002669 tline->type != TOK_INTERNAL_STRING &&
2670 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002671 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002672 free_tlist(origline);
2673 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002674 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002675 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002676 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 if (tline->type == TOK_STRING)
H. Peter Anvinbb42d302019-04-22 14:29:29 -07002678 nasm_unquote_cstr(tline->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002679 use_pkg = nasm_stdmac_find_package(tline->text);
2680 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002681 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002682 else
2683 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002684 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002685 /* Not already included, go ahead and include it */
2686 stdmacpos = use_pkg;
2687 }
2688 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002689 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002690 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002691 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002693 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002694 tline = tline->next;
2695 skip_white_(tline);
2696 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002697 if (tline) {
2698 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002699 nasm_nonfatal("`%s' expects a context identifier",
2700 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002701 free_tlist(origline);
2702 return DIRECTIVE_FOUND; /* but we did _something_ */
2703 }
2704 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002705 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002706 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002707 p = nasm_strdup(tline->text);
2708 } else {
2709 p = NULL; /* Anonymous */
2710 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002711
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002712 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002713 nasm_new(ctx);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002714 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 ctx->name = p;
2716 ctx->number = unique++;
2717 cstk = ctx;
2718 } else {
2719 /* %pop or %repl */
2720 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002721 nasm_nonfatal("`%s': context stack is empty",
2722 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002723 } else if (i == PP_POP) {
2724 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002725 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002726 "expected %s",
2727 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002728 else
2729 ctx_pop();
2730 } else {
2731 /* i == PP_REPL */
2732 nasm_free(cstk->name);
2733 cstk->name = p;
2734 p = NULL;
2735 }
2736 nasm_free(p);
2737 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002739 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002740 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002741 severity = ERR_FATAL;
2742 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002744 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002745 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002746 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002747 /*!
2748 *!user [on] %warning directives
2749 *! controls output of \c{%warning} directives (see \k{pperror}).
2750 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002751 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002752 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002753
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002754issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002755 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002756 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002757 tline->next = expand_smacro(tline->next);
2758 tline = tline->next;
2759 skip_white_(tline);
2760 t = tline ? tline->next : NULL;
2761 skip_white_(t);
2762 if (tok_type_(tline, TOK_STRING) && !t) {
2763 /* The line contains only a quoted string */
2764 p = tline->text;
2765 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002766 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002767 } else {
2768 /* Not a quoted string, or more than a quoted string */
2769 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002770 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002771 nasm_free(p);
2772 }
2773 free_tlist(origline);
2774 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002775 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002776
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002777 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002778 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002779 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002780 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002781 j = if_condition(tline->next, i);
2782 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002783 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002784 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002785 cond = nasm_malloc(sizeof(Cond));
2786 cond->next = istk->conds;
2787 cond->state = j;
2788 istk->conds = cond;
2789 if(istk->mstk)
2790 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002791 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002793
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002794 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002795 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002796 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002797 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002798 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002799 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002800 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002801
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002802 case COND_DONE:
2803 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002804 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002805
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002806 case COND_ELSE_TRUE:
2807 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002808 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002809 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002810 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002811 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002812
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002813 case COND_IF_FALSE:
2814 /*
2815 * IMPORTANT: In the case of %if, we will already have
2816 * called expand_mmac_params(); however, if we're
2817 * processing an %elif we must have been in a
2818 * non-emitting mode, which would have inhibited
2819 * the normal invocation of expand_mmac_params().
2820 * Therefore, we have to do it explicitly here.
2821 */
2822 j = if_condition(expand_mmac_params(tline->next), i);
2823 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002824 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002825 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2826 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002828 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002830
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 case PP_ELSE:
2832 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002833 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002834 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002835 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002836 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002837 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002838 case COND_IF_TRUE:
2839 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002840 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002841 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002842
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002843 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002844 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002845
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002846 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002847 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002848 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002849
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002850 case COND_ELSE_TRUE:
2851 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002852 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002853 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002854 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002855 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002856 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 free_tlist(origline);
2858 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002859
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 case PP_ENDIF:
2861 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002862 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002863 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002864 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002865 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002866 cond = istk->conds;
2867 istk->conds = cond->next;
2868 nasm_free(cond);
2869 if(istk->mstk)
2870 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 free_tlist(origline);
2872 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002873
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002874 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002876 casesense = false;
2877 /* fall through */
2878 case PP_RMACRO:
2879 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002880 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002881 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002882 return DIRECTIVE_FOUND;
2883 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002884 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002885 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2886 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002887 defining->casesense = casesense;
2888 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002889 nasm_free(defining);
2890 defining = NULL;
2891 return DIRECTIVE_FOUND;
2892 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002893
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002894 src_get(&defining->xline, &defining->fname);
2895
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002896 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2897 while (mmac) {
2898 if (!strcmp(mmac->name, defining->name) &&
2899 (mmac->nparam_min <= defining->nparam_max
2900 || defining->plus)
2901 && (defining->nparam_min <= mmac->nparam_max
2902 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002903 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002904 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002905 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002906 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002907 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002908 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002909 free_tlist(origline);
2910 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002911
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 case PP_ENDM:
2913 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002914 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002915 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002916 return DIRECTIVE_FOUND;
2917 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002918 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2919 defining->next = *mmhead;
2920 *mmhead = defining;
2921 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 free_tlist(origline);
2923 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002924
H. Peter Anvin89cee572009-07-15 09:16:54 -04002925 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002926 /*
2927 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002928 * macro-end marker for a macro with a name. Then we
2929 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002930 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002931 list_for_each(l, istk->expansion)
2932 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002933 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002934
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002935 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002936 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002937 * Remove all conditional entries relative to this
2938 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002939 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002940 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2941 cond = istk->conds;
2942 istk->conds = cond->next;
2943 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002944 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002945 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002946 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002947 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002948 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002949 free_tlist(origline);
2950 return DIRECTIVE_FOUND;
2951
H. Peter Anvina26433d2008-07-16 14:40:01 -07002952 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002953 casesense = false;
2954 /* fall through */
2955 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002956 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002957 MMacro **mmac_p;
2958 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002959
H. Peter Anvin8b262472019-02-26 14:00:54 -08002960 spec.casesense = casesense;
2961 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002962 return DIRECTIVE_FOUND;
2963 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002964 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2965 while (mmac_p && *mmac_p) {
2966 mmac = *mmac_p;
2967 if (mmac->casesense == spec.casesense &&
2968 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2969 mmac->nparam_min == spec.nparam_min &&
2970 mmac->nparam_max == spec.nparam_max &&
2971 mmac->plus == spec.plus) {
2972 *mmac_p = mmac->next;
2973 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002974 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002975 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002976 }
2977 }
2978 free_tlist(origline);
2979 free_tlist(spec.dlist);
2980 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002981 }
2982
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 case PP_ROTATE:
2984 if (tline->next && tline->next->type == TOK_WHITESPACE)
2985 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002986 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002987 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002988 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 return DIRECTIVE_FOUND;
2990 }
2991 t = expand_smacro(tline->next);
2992 tline->next = NULL;
2993 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002994 pps.tptr = tline = t;
2995 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002996 tokval.t_type = TOKEN_INVALID;
2997 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002998 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 free_tlist(tline);
3000 if (!evalresult)
3001 return DIRECTIVE_FOUND;
3002 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003003 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003005 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003006 return DIRECTIVE_FOUND;
3007 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003008 mmac = istk->mstk;
3009 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
3010 mmac = mmac->next_active;
3011 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003012 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003013 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003014 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003015 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003016 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003017
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003018 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003019 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003020 rotate += mmac->nparam;
3021
3022 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003023 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003025
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003027 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 do {
3029 tline = tline->next;
3030 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003031
H. Peter Anvine2c80182005-01-15 22:15:51 +00003032 if (tok_type_(tline, TOK_ID) &&
3033 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003034 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 do {
3036 tline = tline->next;
3037 } while (tok_type_(tline, TOK_WHITESPACE));
3038 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003039
H. Peter Anvine2c80182005-01-15 22:15:51 +00003040 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003041 pps.tptr = expand_smacro(tline);
3042 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003044 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003045 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003046 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003047 if (!evalresult) {
3048 free_tlist(origline);
3049 return DIRECTIVE_FOUND;
3050 }
3051 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003052 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003054 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 return DIRECTIVE_FOUND;
3056 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003057 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003058 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003059 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3060 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003061 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003062 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003063 /*!
3064 *!negative-rep [on] regative %rep count
3065 *! warns about negative counts given to the \c{%rep}
3066 *! preprocessor directive.
3067 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003068 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003069 "negative `%%rep' count: %"PRId64, count);
3070 count = 0;
3071 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003072 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003073 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003075 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003076 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003077 }
3078 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003079
3080 tmp_defining = defining;
3081 defining = nasm_malloc(sizeof(MMacro));
3082 defining->prev = NULL;
3083 defining->name = NULL; /* flags this macro as a %rep block */
3084 defining->casesense = false;
3085 defining->plus = false;
3086 defining->nolist = nolist;
3087 defining->in_progress = count;
3088 defining->max_depth = 0;
3089 defining->nparam_min = defining->nparam_max = 0;
3090 defining->defaults = NULL;
3091 defining->dlist = NULL;
3092 defining->expansion = NULL;
3093 defining->next_active = istk->mstk;
3094 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003095 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003096
H. Peter Anvine2c80182005-01-15 22:15:51 +00003097 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003098 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003099 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 return DIRECTIVE_FOUND;
3101 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003102
H. Peter Anvine2c80182005-01-15 22:15:51 +00003103 /*
3104 * Now we have a "macro" defined - although it has no name
3105 * and we won't be entering it in the hash tables - we must
3106 * push a macro-end marker for it on to istk->expansion.
3107 * After that, it will take care of propagating itself (a
3108 * macro-end marker line for a macro which is really a %rep
3109 * block will cause the macro to be re-expanded, complete
3110 * with another macro-end marker to ensure the process
3111 * continues) until the whole expansion is forcibly removed
3112 * from istk->expansion by a %exitrep.
3113 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003114 l = nasm_malloc(sizeof(Line));
3115 l->next = istk->expansion;
3116 l->finishes = defining;
3117 l->first = NULL;
3118 istk->expansion = l;
3119
3120 istk->mstk = defining;
3121
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07003122 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003123 tmp_defining = defining;
3124 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003125 free_tlist(origline);
3126 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003127
H. Peter Anvine2c80182005-01-15 22:15:51 +00003128 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003129 /*
3130 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003131 * macro-end marker for a macro with no name. Then we set
3132 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003133 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003134 list_for_each(l, istk->expansion)
3135 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003136 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003137
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003138 if (l)
3139 l->finishes->in_progress = 1;
3140 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003141 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003142 free_tlist(origline);
3143 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003144
H. Peter Anvine2c80182005-01-15 22:15:51 +00003145 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003146 case PP_IXDEFINE:
3147 casesense = false;
3148 /* fall through */
3149 case PP_DEFINE:
3150 case PP_XDEFINE:
3151 {
3152 SMacro *s;
3153 bool have_eval_params = false;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003154
H. Peter Anvine2c80182005-01-15 22:15:51 +00003155 tline = tline->next;
3156 skip_white_(tline);
3157 tline = expand_id(tline);
3158 if (!tline || (tline->type != TOK_ID &&
3159 (tline->type != TOK_PREPROC_ID ||
3160 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003161 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003162 free_tlist(origline);
3163 return DIRECTIVE_FOUND;
3164 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003165
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003166 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 last = tline;
3168 param_start = tline = tline->next;
3169 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003170
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 if (tok_is_(tline, "(")) {
3172 /*
3173 * This macro has parameters.
3174 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003175
H. Peter Anvine2c80182005-01-15 22:15:51 +00003176 tline = tline->next;
3177 while (1) {
3178 skip_white_(tline);
3179 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003180 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003181 free_tlist(origline);
3182 return DIRECTIVE_FOUND;
3183 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003184 if (tok_is_(tline, "=")) {
3185 have_eval_params = true;
3186 tline = tline->next;
3187 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003188 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003189 nasm_nonfatal("`%s': parameter identifier expected",
3190 tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003191 free_tlist(origline);
3192 return DIRECTIVE_FOUND;
3193 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003194 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003195 tline = tline->next;
3196 skip_white_(tline);
3197 if (tok_is_(tline, ",")) {
3198 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003199 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003200 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003201 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003202 free_tlist(origline);
3203 return DIRECTIVE_FOUND;
3204 }
3205 break;
3206 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003207 }
3208 last = tline;
3209 tline = tline->next;
3210 }
3211 if (tok_type_(tline, TOK_WHITESPACE))
3212 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003213 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003214
3215 /* Expand the macro definition now for %xdefine and %ixdefine */
3216 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3217 tline = expand_smacro(tline);
3218
3219 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003220 t = tline;
3221 while (t) {
3222 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003223 list_for_each(tt, param_start)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003224 if (is_smac_param(tt->type) &&
3225 tt->len == t->len &&
3226 !memcmp(tt->text, t->text, tt->len))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003227 t->type = tt->type;
3228 }
3229 tt = t->next;
3230 t->next = macro_start;
3231 macro_start = t;
3232 t = tt;
3233 }
3234 /*
3235 * Good. We now have a macro name, a parameter count, and a
3236 * token list (in reverse order) for an expansion. We ought
3237 * to be OK just to create an SMacro, store it, and let
3238 * free_tlist have the rest of the line (which we have
3239 * carefully re-terminated after chopping off the expansion
3240 * from the end).
3241 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003242 s = define_smacro(ctx, mname, casesense, nparam, macro_start);
3243
3244 if (have_eval_params) {
3245 /* Create evaluated parameters table */
3246 bool is_eval = false;
3247
3248 nasm_newn(s->eval_param, nparam);
3249 list_for_each(tt, param_start) {
3250 if (is_smac_param(tt->type))
3251 s->eval_param[smac_nparam(tt->type)] = is_eval;
3252 is_eval = tok_is_(tt, "=");
3253 }
3254 }
3255
3256
H. Peter Anvine2c80182005-01-15 22:15:51 +00003257 free_tlist(origline);
3258 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003259 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003260
H. Peter Anvine2c80182005-01-15 22:15:51 +00003261 case PP_UNDEF:
3262 tline = tline->next;
3263 skip_white_(tline);
3264 tline = expand_id(tline);
3265 if (!tline || (tline->type != TOK_ID &&
3266 (tline->type != TOK_PREPROC_ID ||
3267 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003268 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003269 free_tlist(origline);
3270 return DIRECTIVE_FOUND;
3271 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003272 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003273 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003274
H. Peter Anvine2c80182005-01-15 22:15:51 +00003275 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003276 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003277 undef_smacro(ctx, mname);
3278 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003279 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003280
H. Peter Anvin9e200162008-06-04 17:23:14 -07003281 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003282 casesense = false;
3283 /* fall through */
3284 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003285 tline = tline->next;
3286 skip_white_(tline);
3287 tline = expand_id(tline);
3288 if (!tline || (tline->type != TOK_ID &&
3289 (tline->type != TOK_PREPROC_ID ||
3290 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003291 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003292 free_tlist(origline);
3293 return DIRECTIVE_FOUND;
3294 }
3295
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003296 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003297 last = tline;
3298 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003299 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003300
3301 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003302 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003303
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003304 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003305 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003306 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003307
3308 /*
3309 * We now have a macro name, an implicit parameter count of
3310 * zero, and a string token to use as an expansion. Create
3311 * and store an SMacro.
3312 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003313 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003314 free_tlist(origline);
3315 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003316
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003317 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003318 casesense = false;
3319 /* fall through */
3320 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003321 tline = tline->next;
3322 skip_white_(tline);
3323 tline = expand_id(tline);
3324 if (!tline || (tline->type != TOK_ID &&
3325 (tline->type != TOK_PREPROC_ID ||
3326 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003327 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003328 free_tlist(origline);
3329 return DIRECTIVE_FOUND;
3330 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003331 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003332 last = tline;
3333 tline = expand_smacro(tline->next);
3334 last->next = NULL;
3335
3336 t = tline;
3337 while (tok_type_(t, TOK_WHITESPACE))
3338 t = t->next;
3339 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003340 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003341 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003342 free_tlist(tline);
3343 free_tlist(origline);
3344 return DIRECTIVE_FOUND;
3345 }
3346
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003347 /*
3348 * Convert the string to a token stream. Note that smacros
3349 * are stored with the token stream reversed, so we have to
3350 * reverse the output of tokenize().
3351 */
H. Peter Anvinbb42d302019-04-22 14:29:29 -07003352 nasm_unquote_cstr(t->text, NULL);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003353 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003354
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003355 /*
3356 * We now have a macro name, an implicit parameter count of
3357 * zero, and a numeric token to use as an expansion. Create
3358 * and store an SMacro.
3359 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003360 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003361 free_tlist(tline);
3362 free_tlist(origline);
3363 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003364
H. Peter Anvin8b262472019-02-26 14:00:54 -08003365 case PP_IPATHSEARCH:
3366 casesense = false;
3367 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003368 case PP_PATHSEARCH:
3369 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003370 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003371
H. Peter Anvin418ca702008-05-30 10:42:30 -07003372 tline = tline->next;
3373 skip_white_(tline);
3374 tline = expand_id(tline);
3375 if (!tline || (tline->type != TOK_ID &&
3376 (tline->type != TOK_PREPROC_ID ||
3377 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003378 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003379 free_tlist(origline);
3380 return DIRECTIVE_FOUND;
3381 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003382 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003383 last = tline;
3384 tline = expand_smacro(tline->next);
3385 last->next = NULL;
3386
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003387 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003388 while (tok_type_(t, TOK_WHITESPACE))
3389 t = t->next;
3390
3391 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003392 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003393 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003394 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003395 free_tlist(origline);
3396 return DIRECTIVE_FOUND; /* but we did _something_ */
3397 }
3398 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003399 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003400 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003401 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003402 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003403
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003404 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003405 if (!found_path)
3406 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003407 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003408
3409 /*
3410 * We now have a macro name, an implicit parameter count of
3411 * zero, and a string token to use as an expansion. Create
3412 * and store an SMacro.
3413 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003414 define_smacro(ctx, mname, casesense, 0, macro_start);
3415 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003416 free_tlist(origline);
3417 return DIRECTIVE_FOUND;
3418 }
3419
H. Peter Anvin8b262472019-02-26 14:00:54 -08003420 case PP_ISTRLEN:
3421 casesense = false;
3422 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003423 case PP_STRLEN:
3424 tline = tline->next;
3425 skip_white_(tline);
3426 tline = expand_id(tline);
3427 if (!tline || (tline->type != TOK_ID &&
3428 (tline->type != TOK_PREPROC_ID ||
3429 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003430 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003431 free_tlist(origline);
3432 return DIRECTIVE_FOUND;
3433 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003434 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003435 last = tline;
3436 tline = expand_smacro(tline->next);
3437 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003438
H. Peter Anvine2c80182005-01-15 22:15:51 +00003439 t = tline;
3440 while (tok_type_(t, TOK_WHITESPACE))
3441 t = t->next;
3442 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003443 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003444 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003445 free_tlist(tline);
3446 free_tlist(origline);
3447 return DIRECTIVE_FOUND;
3448 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003449
H. Peter Anvin8b262472019-02-26 14:00:54 -08003450 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003451
H. Peter Anvine2c80182005-01-15 22:15:51 +00003452 /*
3453 * We now have a macro name, an implicit parameter count of
3454 * zero, and a numeric token to use as an expansion. Create
3455 * and store an SMacro.
3456 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003458 free_tlist(tline);
3459 free_tlist(origline);
3460 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003461
H. Peter Anvin8b262472019-02-26 14:00:54 -08003462 case PP_ISTRCAT:
3463 casesense = false;
3464 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003465 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003466 tline = tline->next;
3467 skip_white_(tline);
3468 tline = expand_id(tline);
3469 if (!tline || (tline->type != TOK_ID &&
3470 (tline->type != TOK_PREPROC_ID ||
3471 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003472 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003473 free_tlist(origline);
3474 return DIRECTIVE_FOUND;
3475 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003476 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003477 last = tline;
3478 tline = expand_smacro(tline->next);
3479 last->next = NULL;
3480
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003481 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003482 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003483 switch (t->type) {
3484 case TOK_WHITESPACE:
3485 break;
3486 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003487 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003488 break;
3489 case TOK_OTHER:
3490 if (!strcmp(t->text, ",")) /* permit comma separators */
3491 break;
3492 /* else fall through */
3493 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003494 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003495 free_tlist(tline);
3496 free_tlist(origline);
3497 return DIRECTIVE_FOUND;
3498 }
3499 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003500
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003501 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003502 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003503 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003504 memcpy(p, t->text, t->len);
3505 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003506 }
3507 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003508
3509 /*
3510 * We now have a macro name, an implicit parameter count of
3511 * zero, and a numeric token to use as an expansion. Create
3512 * and store an SMacro.
3513 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003514 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003515 nasm_free(pp);
3516 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003517 free_tlist(tline);
3518 free_tlist(origline);
3519 return DIRECTIVE_FOUND;
3520
H. Peter Anvin8b262472019-02-26 14:00:54 -08003521 case PP_ISUBSTR:
3522 casesense = false;
3523 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003524 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003525 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003526 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003527 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003528
H. Peter Anvine2c80182005-01-15 22:15:51 +00003529 tline = tline->next;
3530 skip_white_(tline);
3531 tline = expand_id(tline);
3532 if (!tline || (tline->type != TOK_ID &&
3533 (tline->type != TOK_PREPROC_ID ||
3534 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003535 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003536 free_tlist(origline);
3537 return DIRECTIVE_FOUND;
3538 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003539 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003540 last = tline;
3541 tline = expand_smacro(tline->next);
3542 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003543
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003544 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003545 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003546 while (tok_type_(t, TOK_WHITESPACE))
3547 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003548
H. Peter Anvine2c80182005-01-15 22:15:51 +00003549 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003550 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003551 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003552 free_tlist(tline);
3553 free_tlist(origline);
3554 return DIRECTIVE_FOUND;
3555 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003556
H. Peter Anvin8b262472019-02-26 14:00:54 -08003557 pps.tptr = t->next;
3558 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003559 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003560 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003561 if (!evalresult) {
3562 free_tlist(tline);
3563 free_tlist(origline);
3564 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003565 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003566 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003567 free_tlist(tline);
3568 free_tlist(origline);
3569 return DIRECTIVE_FOUND;
3570 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003571 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003572
H. Peter Anvin8b262472019-02-26 14:00:54 -08003573 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3574 pps.tptr = pps.tptr->next;
3575 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003576 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003577 } else {
3578 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003579 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003580 if (!evalresult) {
3581 free_tlist(tline);
3582 free_tlist(origline);
3583 return DIRECTIVE_FOUND;
3584 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003585 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003586 free_tlist(tline);
3587 free_tlist(origline);
3588 return DIRECTIVE_FOUND;
3589 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003590 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003591 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003592
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003593 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003594
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003595 /* make start and count being in range */
3596 if (start < 0)
3597 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003598 if (count < 0)
3599 count = len + count + 1 - start;
3600 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003601 count = len - start;
3602 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003603 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003604
H. Peter Anvin8b262472019-02-26 14:00:54 -08003605 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003606 macro_start->len = count;
3607 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3608 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003609
H. Peter Anvine2c80182005-01-15 22:15:51 +00003610 /*
3611 * We now have a macro name, an implicit parameter count of
3612 * zero, and a numeric token to use as an expansion. Create
3613 * and store an SMacro.
3614 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003615 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003616 free_tlist(tline);
3617 free_tlist(origline);
3618 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003619 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003620
H. Peter Anvine2c80182005-01-15 22:15:51 +00003621 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003622 casesense = false;
3623 /* fall through */
3624 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003625 tline = tline->next;
3626 skip_white_(tline);
3627 tline = expand_id(tline);
3628 if (!tline || (tline->type != TOK_ID &&
3629 (tline->type != TOK_PREPROC_ID ||
3630 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003631 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003632 free_tlist(origline);
3633 return DIRECTIVE_FOUND;
3634 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003635 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003636 last = tline;
3637 tline = expand_smacro(tline->next);
3638 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003639
H. Peter Anvin8b262472019-02-26 14:00:54 -08003640 pps.tptr = tline;
3641 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003642 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003643 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003644 free_tlist(tline);
3645 if (!evalresult) {
3646 free_tlist(origline);
3647 return DIRECTIVE_FOUND;
3648 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003649
H. Peter Anvine2c80182005-01-15 22:15:51 +00003650 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003651 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003652
H. Peter Anvine2c80182005-01-15 22:15:51 +00003653 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003654 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003655 free_tlist(origline);
3656 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003657 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003658
H. Peter Anvin8b262472019-02-26 14:00:54 -08003659 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003660
H. Peter Anvine2c80182005-01-15 22:15:51 +00003661 /*
3662 * We now have a macro name, an implicit parameter count of
3663 * zero, and a numeric token to use as an expansion. Create
3664 * and store an SMacro.
3665 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003666 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003667 free_tlist(origline);
3668 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003669
H. Peter Anvine2c80182005-01-15 22:15:51 +00003670 case PP_LINE:
3671 /*
3672 * Syntax is `%line nnn[+mmm] [filename]'
3673 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003674 if (unlikely(pp_noline)) {
3675 free_tlist(origline);
3676 return DIRECTIVE_FOUND;
3677 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003678 tline = tline->next;
3679 skip_white_(tline);
3680 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003681 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003682 free_tlist(origline);
3683 return DIRECTIVE_FOUND;
3684 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003685 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003686 m = 1;
3687 tline = tline->next;
3688 if (tok_is_(tline, "+")) {
3689 tline = tline->next;
3690 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003691 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003692 free_tlist(origline);
3693 return DIRECTIVE_FOUND;
3694 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003695 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003696 tline = tline->next;
3697 }
3698 skip_white_(tline);
3699 src_set_linnum(k);
3700 istk->lineinc = m;
3701 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003702 char *fname = detoken(tline, false);
3703 src_set_fname(fname);
3704 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003705 }
3706 free_tlist(origline);
3707 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003708
H. Peter Anvine2c80182005-01-15 22:15:51 +00003709 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003710 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003711 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003712 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003713}
3714
3715/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003716 * Ensure that a macro parameter contains a condition code and
3717 * nothing else. Return the condition code index if so, or -1
3718 * otherwise.
3719 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003720static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003721{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003722 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003723
H. Peter Anvin25a99342007-09-22 17:45:45 -07003724 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003725 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003726
H. Peter Anvineba20a72002-04-30 20:53:55 +00003727 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003728 if (!t)
3729 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003730 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003731 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003732 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003733 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003734 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003735 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003736
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003737 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003738}
3739
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003740/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003741 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003742 * pasting, if @handle_explicit passed then explicit pasting
3743 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003744 * The @m array can contain a series of token types which are
3745 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003746 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003747static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003748 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003749{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003750 Token *tok, *next, **prev_next, **prev_nonspace;
3751 bool pasted = false;
3752 char *buf, *p;
3753 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003754
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003755 /*
3756 * The last token before pasting. We need it
3757 * to be able to connect new handled tokens.
3758 * In other words if there were a tokens stream
3759 *
3760 * A -> B -> C -> D
3761 *
3762 * and we've joined tokens B and C, the resulting
3763 * stream should be
3764 *
3765 * A -> BC -> D
3766 */
3767 tok = *head;
3768 prev_next = NULL;
3769
3770 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3771 prev_nonspace = head;
3772 else
3773 prev_nonspace = NULL;
3774
3775 while (tok && (next = tok->next)) {
3776
3777 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003778 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003779 /* Zap redundant whitespaces */
3780 while (tok_type_(next, TOK_WHITESPACE))
3781 next = delete_Token(next);
3782 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003783 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003784
3785 case TOK_PASTE:
3786 /* Explicit pasting */
3787 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003788 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003789 next = delete_Token(tok);
3790
3791 while (tok_type_(next, TOK_WHITESPACE))
3792 next = delete_Token(next);
3793
3794 if (!pasted)
3795 pasted = true;
3796
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003797 /* Left pasting token is start of line */
3798 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003799 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003800
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003801 /*
3802 * No ending token, this might happen in two
3803 * cases
3804 *
3805 * 1) There indeed no right token at all
3806 * 2) There is a bare "%define ID" statement,
3807 * and @ID does expand to whitespace.
3808 *
3809 * So technically we need to do a grammar analysis
3810 * in another stage of parsing, but for now lets don't
3811 * change the behaviour people used to. Simply allow
3812 * whitespace after paste token.
3813 */
3814 if (!next) {
3815 /*
3816 * Zap ending space tokens and that's all.
3817 */
3818 tok = (*prev_nonspace)->next;
3819 while (tok_type_(tok, TOK_WHITESPACE))
3820 tok = delete_Token(tok);
3821 tok = *prev_nonspace;
3822 tok->next = NULL;
3823 break;
3824 }
3825
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003826 tok = *prev_nonspace;
3827 while (tok_type_(tok, TOK_WHITESPACE))
3828 tok = delete_Token(tok);
3829 len = strlen(tok->text);
3830 len += strlen(next->text);
3831
3832 p = buf = nasm_malloc(len + 1);
3833 strcpy(p, tok->text);
3834 p = strchr(p, '\0');
3835 strcpy(p, next->text);
3836
3837 delete_Token(tok);
3838
3839 tok = tokenize(buf);
3840 nasm_free(buf);
3841
3842 *prev_nonspace = tok;
3843 while (tok && tok->next)
3844 tok = tok->next;
3845
3846 tok->next = delete_Token(next);
3847
3848 /* Restart from pasted tokens head */
3849 tok = *prev_nonspace;
3850 break;
3851
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003852 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003853 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003854 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003855 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3856 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003857
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003858 len = 0;
3859 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3860 len += strlen(next->text);
3861 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003862 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003863
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003864 /* No match or no text to process */
3865 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003866 break;
3867
3868 len += strlen(tok->text);
3869 p = buf = nasm_malloc(len + 1);
3870
Adam Majer1a069432017-07-25 11:12:35 +02003871 strcpy(p, tok->text);
3872 p = strchr(p, '\0');
3873 tok = delete_Token(tok);
3874
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003875 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003876 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3877 strcpy(p, tok->text);
3878 p = strchr(p, '\0');
3879 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003880 tok = delete_Token(tok);
3881 }
3882
3883 tok = tokenize(buf);
3884 nasm_free(buf);
3885
3886 if (prev_next)
3887 *prev_next = tok;
3888 else
3889 *head = tok;
3890
3891 /*
3892 * Connect pasted into original stream,
3893 * ie A -> new-tokens -> B
3894 */
3895 while (tok && tok->next)
3896 tok = tok->next;
3897 tok->next = next;
3898
3899 if (!pasted)
3900 pasted = true;
3901
3902 /* Restart from pasted tokens head */
3903 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003904 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003905
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003906 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003907 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003908
3909 prev_next = &tok->next;
3910
3911 if (tok->next &&
3912 !tok_type_(tok->next, TOK_WHITESPACE) &&
3913 !tok_type_(tok->next, TOK_PASTE))
3914 prev_nonspace = prev_next;
3915
3916 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003917 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003918
3919 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003920}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003921
3922/*
3923 * expands to a list of tokens from %{x:y}
3924 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003925static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003926{
3927 Token *t = tline, **tt, *tm, *head;
3928 char *pos;
3929 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003930
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003931 pos = strchr(tline->text, ':');
3932 nasm_assert(pos);
3933
3934 lst = atoi(pos + 1);
3935 fst = atoi(tline->text + 1);
3936
3937 /*
3938 * only macros params are accounted so
3939 * if someone passes %0 -- we reject such
3940 * value(s)
3941 */
3942 if (lst == 0 || fst == 0)
3943 goto err;
3944
3945 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003946 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3947 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003948 goto err;
3949
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003950 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3951 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003952
3953 /* counted from zero */
3954 fst--, lst--;
3955
3956 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003957 * It will be at least one token. Note we
3958 * need to scan params until separator, otherwise
3959 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003960 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003961 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003962 if (!tm)
3963 goto err;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07003964 head = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003965 tt = &head->next, tm = tm->next;
3966 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07003967 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003968 *tt = t, tt = &t->next, tm = tm->next;
3969 }
3970
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003971 if (fst < lst) {
3972 for (i = fst + 1; i <= lst; i++) {
3973 t = new_Token(NULL, TOK_OTHER, ",", 0);
3974 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003975 j = (i + mac->rotate) % mac->nparam;
3976 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003977 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07003978 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003979 *tt = t, tt = &t->next, tm = tm->next;
3980 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003981 }
3982 } else {
3983 for (i = fst - 1; i >= lst; i--) {
3984 t = new_Token(NULL, TOK_OTHER, ",", 0);
3985 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003986 j = (i + mac->rotate) % mac->nparam;
3987 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003988 while (tok_isnt_(tm, ",")) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07003989 t = dup_Token(NULL, tm);
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003990 *tt = t, tt = &t->next, tm = tm->next;
3991 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003992 }
3993 }
3994
3995 *last = tt;
3996 return head;
3997
3998err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003999 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004000 &tline->text[1]);
4001 return tline;
4002}
4003
H. Peter Anvin76690a12002-04-30 20:52:49 +00004004/*
4005 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07004006 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004007 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00004008 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004009static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004010{
H. Peter Anvin734b1882002-04-30 21:01:08 +00004011 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07004012 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004013 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004014
4015 tail = &thead;
4016 thead = NULL;
4017
H. Peter Anvine2c80182005-01-15 22:15:51 +00004018 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03004019 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04004020 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
4021 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
4022 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004023 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004024 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004025 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004026 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004027 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004028 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004029
H. Peter Anvine2c80182005-01-15 22:15:51 +00004030 t = tline;
4031 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004032
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004033 mac = istk->mstk;
4034 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4035 mac = mac->next_active;
4036 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004037 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004038 } else {
4039 pos = strchr(t->text, ':');
4040 if (!pos) {
4041 switch (t->text[1]) {
4042 /*
4043 * We have to make a substitution of one of the
4044 * forms %1, %-1, %+1, %%foo, %0.
4045 */
4046 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004047 type = TOK_NUMBER;
4048 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4049 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004050 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004051 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004052 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004053 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004054 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004055 text = nasm_strcat(tmpbuf, t->text + 2);
4056 break;
4057 case '-':
4058 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004059 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004060 tt = NULL;
4061 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004062 if (mac->nparam > 1)
4063 n = (n + mac->rotate) % mac->nparam;
4064 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004065 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004066 cc = find_cc(tt);
4067 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004068 nasm_nonfatal("macro parameter %d is not a condition code",
4069 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004070 text = NULL;
4071 } else {
4072 type = TOK_ID;
4073 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004074 nasm_nonfatal("condition code `%s' is not invertible",
4075 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004076 text = NULL;
4077 } else
4078 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4079 }
4080 break;
4081 case '+':
4082 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004083 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004084 tt = NULL;
4085 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004086 if (mac->nparam > 1)
4087 n = (n + mac->rotate) % mac->nparam;
4088 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004089 }
4090 cc = find_cc(tt);
4091 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004092 nasm_nonfatal("macro parameter %d is not a condition code",
4093 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004094 text = NULL;
4095 } else {
4096 type = TOK_ID;
4097 text = nasm_strdup(conditions[cc]);
4098 }
4099 break;
4100 default:
4101 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004102 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004103 tt = NULL;
4104 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004105 if (mac->nparam > 1)
4106 n = (n + mac->rotate) % mac->nparam;
4107 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004108 }
4109 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004110 for (i = 0; i < mac->paramlen[n]; i++) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004111 *tail = dup_Token(NULL, tt);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004112 tail = &(*tail)->next;
4113 tt = tt->next;
4114 }
4115 }
4116 text = NULL; /* we've done it here */
4117 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004118 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004119 } else {
4120 /*
4121 * seems we have a parameters range here
4122 */
4123 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004124 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004125 if (head != t) {
4126 *tail = head;
4127 *last = tline;
4128 tline = head;
4129 text = NULL;
4130 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004131 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004132 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004133 if (!text) {
4134 delete_Token(t);
4135 } else {
4136 *tail = t;
4137 tail = &t->next;
4138 t->type = type;
4139 nasm_free(t->text);
4140 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004141 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004142 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004143 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004144 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004145 } else if (tline->type == TOK_INDIRECT) {
4146 t = tline;
4147 tline = tline->next;
4148 tt = tokenize(t->text);
4149 tt = expand_mmac_params(tt);
4150 tt = expand_smacro(tt);
4151 *tail = tt;
4152 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004153 tail = &tt->next;
4154 tt = tt->next;
4155 }
4156 delete_Token(t);
4157 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004158 } else {
4159 t = *tail = tline;
4160 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004161 tail = &t->next;
4162 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004163 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004164 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004165
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004166 if (changed) {
4167 const struct tokseq_match t[] = {
4168 {
4169 PP_CONCAT_MASK(TOK_ID) |
4170 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4171 PP_CONCAT_MASK(TOK_ID) |
4172 PP_CONCAT_MASK(TOK_NUMBER) |
4173 PP_CONCAT_MASK(TOK_FLOAT) |
4174 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4175 },
4176 {
4177 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4178 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4179 }
4180 };
4181 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4182 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004183
H. Peter Anvin76690a12002-04-30 20:52:49 +00004184 return thead;
4185}
4186
4187/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004188 * Expand *one* single-line macro instance. If the first token is not
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004189 * a macro at all, it is simply copied to the output and the pointer
4190 * advanced. tpp should be a pointer to a pointer (usually the next
4191 * pointer of the previous token) to the first token. **tpp is updated
4192 * to point to the last token of the expansion, and *tpp updated to
4193 * point to the next pointer of the first token of the expansion.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004194 *
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004195 * If the expansion is empty, *tpp will be unchanged but **tpp will
4196 * be advanced past the macro call.
4197 *
4198 * The return value equals **tpp.
4199 *
4200 * Return false if no expansion took place; true if it did.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004201 *
4202 */
4203static Token *expand_smacro_noreset(Token * tline);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004204static int64_t smacro_deadman;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004205
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004206static bool expand_one_smacro(Token ***tpp)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004207{
4208 Token **params = NULL;
4209 const char *mname;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004210 Token *tline = **tpp;
4211 Token *mstart = **tpp;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004212 SMacro *head, *m;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004213 unsigned int i;
4214 Token *t, *tup, *ttail;
4215 unsigned int nparam = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004216
4217 if (!tline)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004218 return false; /* Empty line, nothing to do */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004219
4220 mname = tline->text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004221
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004222 if (--smacro_deadman <= 0) {
4223 if (smacro_deadman == 0)
4224 nasm_nonfatal("interminable macro recursion");
4225 goto not_a_macro;
4226 } else if (tline->type == TOK_ID) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004227 head = (SMacro *)hash_findix(&smacros, mname);
4228 } else if (tline->type == TOK_PREPROC_ID) {
4229 Context *ctx = get_ctx(mname, &mname);
4230 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4231 } else {
4232 goto not_a_macro;
4233 }
4234
4235 /*
4236 * We've hit an identifier of some sort. First check whether the
4237 * identifier is a single-line macro at all, then think about
4238 * checking for parameters if necessary.
4239 */
4240 list_for_each(m, head) {
4241 if (!mstrcmp(m->name, mname, m->casesense))
4242 break;
4243 }
4244
4245 if (!m) {
4246 goto not_a_macro;
4247 }
4248
4249 /* Parse parameters, if applicable */
4250
4251 params = NULL;
4252 nparam = 0;
4253
4254 if (m->nparam == 0) {
4255 /*
4256 * Simple case: the macro is parameterless.
4257 * Nothing to parse; the expansion code will
4258 * drop the macro name token.
4259 */
4260 } else {
4261 /*
4262 * Complicated case: at least one macro with this name
4263 * exists and takes parameters. We must find the
4264 * parameters in the call, count them, find the SMacro
4265 * that corresponds to that form of the macro call, and
4266 * substitute for the parameters when we expand. What a
4267 * pain.
4268 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004269 Token **phead, **pep;
4270 int paren = 1;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004271 int white = 0;
4272 int brackets = 0;
4273 bool bracketed = false;
4274 bool bad_bracket = false;
4275 unsigned int sparam = PARAM_DELTA;
4276
4277 tline = tline->next;
4278
4279 while (tok_type_(tline, TOK_WHITESPACE)) {
4280 tline = tline->next;
4281 }
4282 if (!tok_is_(tline, "(")) {
4283 /*
4284 * This macro wasn't called with parameters: ignore
4285 * the call. (Behaviour borrowed from gnu cpp.)
4286 */
4287 goto not_a_macro;
4288 }
4289
4290 paren = 1;
4291 nparam = 0;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004292 nasm_newn(params, sparam);
4293 phead = pep = &params[0];
4294 *pep = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004295
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004296 while (paren) {
4297 bool skip;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004298 char ch;
4299
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004300 tline = tline->next;
4301
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004302 if (!tline) {
4303 nasm_nonfatal("macro call expects terminating `)'");
4304 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004305 }
4306
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004307 ch = 0;
4308 skip = false;
4309
4310 switch (tline->type) {
4311 case TOK_OTHER:
4312 if (!tline->text[1])
4313 ch = tline->text[0];
4314 break;
4315
4316 case TOK_WHITESPACE:
4317 if (brackets || *phead)
4318 white++; /* Keep interior whitespace */
4319 skip = true;
4320 break;
4321
4322 default:
4323 break;
4324 }
4325
4326 switch (ch) {
4327 case ',':
4328 if (!brackets) {
4329 if (++nparam >= sparam) {
4330 sparam += PARAM_DELTA;
4331 params = nasm_realloc(params, sparam * sizeof *params);
4332 }
4333 pep = &params[nparam];
4334 *pep = NULL;
4335 bracketed = false;
4336 skip = true;
4337 }
4338 break;
4339
4340 case '{':
4341 if (!bracketed) {
4342 bracketed = !*phead;
4343 skip = true;
4344 }
4345 brackets++;
4346 break;
4347
4348 case '}':
4349 if (brackets > 0) {
4350 if (!--brackets)
4351 skip = bracketed;
4352 }
4353 break;
4354
4355 case '(':
4356 if (!brackets)
4357 paren++;
4358 break;
4359
4360 case ')':
4361 if (!brackets) {
4362 paren--;
4363 if (!paren) {
4364 skip = true;
4365 if (nparam > 0 || *phead) {
4366 /* Count the final argument unless just () */
4367 nparam++;
4368 }
4369 }
4370 }
4371 break;
4372
4373 default:
4374 break; /* Normal token */
4375 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004376
4377 if (!skip) {
4378 Token *t;
4379
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004380 bad_bracket |= bracketed && !brackets;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004381
4382 if (white) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004383 *pep = t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004384 pep = &t->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004385 white = 0;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004386 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004387 *pep = t = dup_Token(NULL, tline);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004388 pep = &t->next;
4389 white = 0;
4390 }
4391 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004392
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004393 /*
4394 * Look for a macro matching in both name and parameter count.
4395 * We already know any matches cannot be anywhere before the
4396 * current position of "m", so there is no reason to
4397 * backtrack.
4398 */
4399 while (1) {
4400 if (!m) {
4401 /*!
4402 *!macro-params-single [on] single-line macro calls with wrong parameter count
4403 *! warns about \i{single-line macros} being invoked
4404 *! with the wrong number of parameters.
4405 */
4406 nasm_warn(WARN_MACRO_PARAMS_SINGLE,
4407 "single-line macro `%s' exists, "
4408 "but not taking %d parameter%s",
4409 mname, nparam, (nparam == 1) ? "" : "s");
4410 goto not_a_macro;
4411 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004412
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004413 if (m->nparam == nparam && !mstrcmp(m->name, mname, m->casesense))
4414 break; /* It's good */
4415
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004416 m = m->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004417 }
4418 }
4419
4420 if (m->in_progress)
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004421 goto not_a_macro;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004422
4423 /* Expand each parameter */
4424 m->in_progress = true;
4425
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004426 if (m->eval_param) {
4427 for (i = 0; i < nparam; i++) {
4428 if (m->eval_param[i]) {
4429 /* Evaluate this parameter as a number */
4430 struct ppscan pps;
4431 struct tokenval tokval;
4432 expr *evalresult;
4433 Token *eval_param;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004434
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004435 pps.tptr = eval_param = expand_smacro_noreset(params[i]);
4436 pps.ntokens = -1;
4437 tokval.t_type = TOKEN_INVALID;
4438 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004439
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004440 free_tlist(eval_param);
4441 params[i] = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004442
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004443 if (!evalresult) {
4444 /* Nothing meaningful to do */
4445 } else if (tokval.t_type) {
4446 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4447 } else if (!is_simple(evalresult)) {
4448 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4449 } else {
4450 params[i] = make_tok_num(reloc_value(evalresult));
4451 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004452 }
4453 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004454 }
4455
4456 t = tline;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004457 tline = tline->next; /* Remove the macro call from the input */
4458 t->next = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004459
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004460 /* Note: we own the expansion this returns. */
4461 t = m->expand(m, params, nparam);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004462
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004463 tup = NULL;
4464 ttail = NULL; /* Pointer to the last token of the expansion */
4465 while (t) {
4466 enum pp_token_type type = t->type;
4467 Token *tnext = t->next;
4468 Token **tp;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004469
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004470 switch (type) {
4471 case TOK_PREPROC_Q:
4472 case TOK_PREPROC_QQ:
4473 t->type = TOK_ID;
4474 nasm_free(t->text);
4475 t->text = nasm_strdup(type == TOK_PREPROC_QQ ? m->name : mname);
4476 t->len = nasm_last_string_len();
4477 t->next = tline;
4478 break;
4479
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004480 case TOK_ID:
4481 case TOK_PREPROC_ID:
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004482 /*
4483 * Chain this into the target line *before* expanding,
4484 * that way we pick up any arguments to the new macro call,
4485 * if applicable.
4486 */
4487 t->next = tline;
4488 tp = &t;
4489 expand_one_smacro(&tp);
4490 if (t == tline)
4491 t = NULL; /* Null expansion */
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004492 break;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004493
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004494 default:
4495 if (is_smac_param(t->type)) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004496 unsigned int param = smac_nparam(t->type);
4497 nasm_assert(!tup && param < nparam);
4498 delete_Token(t);
4499 t = NULL;
4500 tup = tnext;
4501 tnext = dup_tlist_reverse(params[param], NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004502 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004503 t->next = tline;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004504 }
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004505 }
4506
4507 if (t) {
4508 tline = t;
4509 if (!ttail)
4510 ttail = t;
4511 }
4512
4513 if (tnext) {
4514 t = tnext;
4515 } else {
4516 t = tup;
4517 tup = NULL;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004518 }
4519 }
4520
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004521 **tpp = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004522 if (ttail)
4523 *tpp = &ttail->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004524
4525 m->in_progress = false;
4526
4527 /* Don't do this until after expansion or we will clobber mname */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004528 free_tlist(mstart);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004529 free_tlist_array(params, nparam);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004530 return true;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004531
4532 /*
4533 * No macro expansion needed; roll back to mstart (if necessary)
4534 * and then advance to the next input token.
4535 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004536not_a_macro:
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004537 *tpp = &mstart->next;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004538 free_tlist_array(params, nparam);
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004539 return false;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004540}
4541
4542/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004543 * Expand all single-line macro calls made in the given line.
4544 * Return the expanded version of the line. The original is deemed
4545 * to be destroyed in the process. (In reality we'll just move
4546 * Tokens from input to output a lot of the time, rather than
4547 * actually bothering to destroy and replicate.)
4548 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004549static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004550{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004551 smacro_deadman = nasm_limit[LIMIT_MACROS];
4552 return expand_smacro_noreset(tline);
4553}
4554
4555static Token *expand_smacro_noreset(Token * tline)
4556{
4557 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004558 Token *org_tline = tline;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004559 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004560
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004561 /*
4562 * Trick: we should avoid changing the start token pointer since it can
4563 * be contained in "next" field of other token. Because of this
4564 * we allocate a copy of first token and work with it; at the end of
4565 * routine we copy it back
4566 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004567 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004568 tline = new_Token(org_tline->next, org_tline->type,
4569 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004570 nasm_free(org_tline->text);
4571 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004572 }
4573
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004574
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004575 /*
4576 * Pretend that we always end up doing expansion on the first pass;
4577 * that way %+ get processed. However, if we process %+ before the
4578 * first pass, we end up with things like MACRO %+ TAIL trying to
4579 * look up the macro "MACROTAIL", which we don't want.
4580 */
4581 expanded = true;
4582 thead = tline;
4583 while (true) {
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004584 static const struct tokseq_match tmatch[] = {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004585 {
4586 PP_CONCAT_MASK(TOK_ID) |
4587 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4588 PP_CONCAT_MASK(TOK_ID) |
4589 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4590 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4591 }
4592 };
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004593
4594 tail = &thead;
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004595 while ((t = *tail)) { /* main token loop */
4596
4597 expanded |= expand_one_smacro(&tail);
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004598 }
H. Peter Anvin (Intel)875eb242019-08-07 17:12:24 -07004599
4600 if (!expanded) {
4601 tline = thead;
4602 break; /* Done! */
4603 }
4604
4605 /*
4606 * Now scan the entire line and look for successive TOK_IDs
4607 * that resulted after expansion (they can't be produced by
4608 * tokenize()). The successive TOK_IDs should be concatenated.
4609 * Also we look for %+ tokens and concatenate the tokens
4610 * before and after them (without white spaces in between).
4611 */
4612 paste_tokens(&thead, tmatch, ARRAY_SIZE(tmatch), true);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004613
4614 expanded = false;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004615 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004616
H. Peter Anvine2c80182005-01-15 22:15:51 +00004617 if (org_tline) {
4618 if (thead) {
4619 *org_tline = *thead;
4620 /* since we just gave text to org_line, don't free it */
4621 thead->text = NULL;
4622 delete_Token(thead);
4623 } else {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004624 /*
4625 * The expression expanded to empty line;
4626 * we can't return NULL because of the "trick" above.
4627 * Just set the line to a single WHITESPACE token.
4628 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004629 memset(org_tline, 0, sizeof(*org_tline));
4630 org_tline->text = NULL;
4631 org_tline->type = TOK_WHITESPACE;
4632 }
4633 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004634 }
4635
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004636 return thead;
4637}
4638
4639/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004640 * Similar to expand_smacro but used exclusively with macro identifiers
4641 * right before they are fetched in. The reason is that there can be
4642 * identifiers consisting of several subparts. We consider that if there
4643 * are more than one element forming the name, user wants a expansion,
4644 * otherwise it will be left as-is. Example:
4645 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004646 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004647 *
4648 * the identifier %$abc will be left as-is so that the handler for %define
4649 * will suck it and define the corresponding value. Other case:
4650 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004651 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004652 *
4653 * In this case user wants name to be expanded *before* %define starts
4654 * working, so we'll expand %$abc into something (if it has a value;
4655 * otherwise it will be left as-is) then concatenate all successive
4656 * PP_IDs into one.
4657 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004658static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004659{
4660 Token *cur, *oldnext = NULL;
4661
H. Peter Anvin734b1882002-04-30 21:01:08 +00004662 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004663 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004664
4665 cur = tline;
4666 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004667 (cur->next->type == TOK_ID ||
4668 cur->next->type == TOK_PREPROC_ID
4669 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004670 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004671
4672 /* If identifier consists of just one token, don't expand */
4673 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004675
H. Peter Anvine2c80182005-01-15 22:15:51 +00004676 if (cur) {
4677 oldnext = cur->next; /* Detach the tail past identifier */
4678 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004679 }
4680
H. Peter Anvin734b1882002-04-30 21:01:08 +00004681 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004682
H. Peter Anvine2c80182005-01-15 22:15:51 +00004683 if (cur) {
4684 /* expand_smacro possibly changhed tline; re-scan for EOL */
4685 cur = tline;
4686 while (cur && cur->next)
4687 cur = cur->next;
4688 if (cur)
4689 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004690 }
4691
4692 return tline;
4693}
4694
4695/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004696 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004697 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004698 * to check for an initial label - that's taken care of in
4699 * expand_mmacro - but must check numbers of parameters. Guaranteed
4700 * to be called with tline->type == TOK_ID, so the putative macro
4701 * name is easy to find.
4702 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004703static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004704{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004705 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004706 Token **params;
4707 int nparam;
4708
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004709 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004710
4711 /*
4712 * Efficiency: first we see if any macro exists with the given
4713 * name. If not, we can return NULL immediately. _Then_ we
4714 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004715 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004716 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004717 list_for_each(m, head)
4718 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004719 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004720 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004721 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004722
4723 /*
4724 * OK, we have a potential macro. Count and demarcate the
4725 * parameters.
4726 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004727 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004728
4729 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004730 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004731 * structure that handles this number.
4732 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004733 while (m) {
4734 if (m->nparam_min <= nparam
4735 && (m->plus || nparam <= m->nparam_max)) {
4736 /*
4737 * This one is right. Just check if cycle removal
4738 * prohibits us using it before we actually celebrate...
4739 */
4740 if (m->in_progress > m->max_depth) {
4741 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004742 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004743 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004744 }
4745 nasm_free(params);
4746 return NULL;
4747 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004748 /*
4749 * It's right, and we can use it. Add its default
4750 * parameters to the end of our list if necessary.
4751 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004752 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004753 params =
4754 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004755 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004756 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004757 while (nparam < m->nparam_min + m->ndefs) {
4758 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004759 nparam++;
4760 }
4761 }
4762 /*
4763 * If we've gone over the maximum parameter count (and
4764 * we're in Plus mode), ignore parameters beyond
4765 * nparam_max.
4766 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004767 if (m->plus && nparam > m->nparam_max)
4768 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004769 /*
4770 * Then terminate the parameter list, and leave.
4771 */
4772 if (!params) { /* need this special case */
4773 params = nasm_malloc(sizeof(*params));
4774 nparam = 0;
4775 }
4776 params[nparam] = NULL;
4777 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004778 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004779 }
4780 /*
4781 * This one wasn't right: look for the next one with the
4782 * same name.
4783 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004784 list_for_each(m, m->next)
4785 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004786 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004787 }
4788
4789 /*
4790 * After all that, we didn't find one with the right number of
4791 * parameters. Issue a warning, and fail to expand the macro.
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004792 *!
4793 *!macro-params-multi [on] multi-line macro calls with wrong parameter count
4794 *! warns about \i{multi-line macros} being invoked
4795 *! with the wrong number of parameters. See \k{mlmacover} for an
4796 *! example of why you might want to disable this warning.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004797 */
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07004798 nasm_warn(WARN_MACRO_PARAMS_MULTI,
4799 "multi-line macro `%s' exists, but not taking %d parameter%s",
4800 tline->text, nparam, (nparam == 1) ? "" : "s");
H. Peter Anvin734b1882002-04-30 21:01:08 +00004801 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004802 return NULL;
4803}
4804
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004805
4806/*
4807 * Save MMacro invocation specific fields in
4808 * preparation for a recursive macro expansion
4809 */
4810static void push_mmacro(MMacro *m)
4811{
4812 MMacroInvocation *i;
4813
4814 i = nasm_malloc(sizeof(MMacroInvocation));
4815 i->prev = m->prev;
4816 i->params = m->params;
4817 i->iline = m->iline;
4818 i->nparam = m->nparam;
4819 i->rotate = m->rotate;
4820 i->paramlen = m->paramlen;
4821 i->unique = m->unique;
4822 i->condcnt = m->condcnt;
4823 m->prev = i;
4824}
4825
4826
4827/*
4828 * Restore MMacro invocation specific fields that were
4829 * saved during a previous recursive macro expansion
4830 */
4831static void pop_mmacro(MMacro *m)
4832{
4833 MMacroInvocation *i;
4834
4835 if (m->prev) {
4836 i = m->prev;
4837 m->prev = i->prev;
4838 m->params = i->params;
4839 m->iline = i->iline;
4840 m->nparam = i->nparam;
4841 m->rotate = i->rotate;
4842 m->paramlen = i->paramlen;
4843 m->unique = i->unique;
4844 m->condcnt = i->condcnt;
4845 nasm_free(i);
4846 }
4847}
4848
4849
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004850/*
4851 * Expand the multi-line macro call made by the given line, if
4852 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004853 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004854 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004855static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004856{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004857 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004858 Token *label = NULL;
4859 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004860 Token **params, *t, *tt;
4861 MMacro *m;
4862 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004863 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004864 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004865
4866 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004867 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004868 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004869 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004870 return 0;
4871 m = is_mmacro(t, &params);
4872 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004873 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004874 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004875 Token *last;
4876 /*
4877 * We have an id which isn't a macro call. We'll assume
4878 * it might be a label; we'll also check to see if a
4879 * colon follows it. Then, if there's another id after
4880 * that lot, we'll check it again for macro-hood.
4881 */
4882 label = last = t;
4883 t = t->next;
4884 if (tok_type_(t, TOK_WHITESPACE))
4885 last = t, t = t->next;
4886 if (tok_is_(t, ":")) {
4887 dont_prepend = 1;
4888 last = t, t = t->next;
4889 if (tok_type_(t, TOK_WHITESPACE))
4890 last = t, t = t->next;
4891 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004892 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4893 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004894 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004895 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004896 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004897 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004898
4899 /*
4900 * Fix up the parameters: this involves stripping leading and
4901 * trailing whitespace, then stripping braces if they are
4902 * present.
4903 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004904 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004905 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004906
H. Peter Anvine2c80182005-01-15 22:15:51 +00004907 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004908 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004909 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004910
H. Peter Anvine2c80182005-01-15 22:15:51 +00004911 t = params[i];
4912 skip_white_(t);
4913 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004914 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004915 params[i] = t;
4916 paramlen[i] = 0;
4917 while (t) {
4918 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4919 break; /* ... because we have hit a comma */
4920 if (comma && t->type == TOK_WHITESPACE
4921 && tok_is_(t->next, ","))
4922 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004923 if (brace && t->type == TOK_OTHER) {
4924 if (t->text[0] == '{')
4925 brace++; /* ... or a nested opening brace */
4926 else if (t->text[0] == '}')
4927 if (!--brace)
4928 break; /* ... or a brace */
4929 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004930 t = t->next;
4931 paramlen[i]++;
4932 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004933 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004934 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004935 }
4936
4937 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004938 * OK, we have a MMacro structure together with a set of
4939 * parameters. We must now go through the expansion and push
4940 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004941 * parameter tokens and macro-local tokens doesn't get done
4942 * until the single-line macro substitution process; this is
4943 * because delaying them allows us to change the semantics
4944 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004945 *
4946 * First, push an end marker on to istk->expansion, mark this
4947 * macro as in progress, and set up its invocation-specific
4948 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004949 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004950 ll = nasm_malloc(sizeof(Line));
4951 ll->next = istk->expansion;
4952 ll->finishes = m;
4953 ll->first = NULL;
4954 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004955
4956 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004957 * Save the previous MMacro expansion in the case of
4958 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004959 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004960 if (m->max_depth && m->in_progress)
4961 push_mmacro(m);
4962
4963 m->in_progress ++;
4964 m->params = params;
4965 m->iline = tline;
4966 m->nparam = nparam;
4967 m->rotate = 0;
4968 m->paramlen = paramlen;
4969 m->unique = unique++;
4970 m->lineno = 0;
4971 m->condcnt = 0;
4972
4973 m->next_active = istk->mstk;
4974 istk->mstk = m;
4975
4976 list_for_each(l, m->expansion) {
4977 Token **tail;
4978
4979 ll = nasm_malloc(sizeof(Line));
4980 ll->finishes = NULL;
4981 ll->next = istk->expansion;
4982 istk->expansion = ll;
4983 tail = &ll->first;
4984
4985 list_for_each(t, l->first) {
4986 Token *x = t;
4987 switch (t->type) {
4988 case TOK_PREPROC_Q:
4989 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004990 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004991 case TOK_PREPROC_QQ:
4992 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4993 break;
4994 case TOK_PREPROC_ID:
4995 if (t->text[1] == '0' && t->text[2] == '0') {
4996 dont_prepend = -1;
4997 x = label;
4998 if (!x)
4999 continue;
5000 }
5001 /* fall through */
5002 default:
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005003 tt = *tail = dup_Token(NULL, x);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005004 break;
5005 }
5006 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005007 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005008 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005009 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005010
5011 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00005012 * If we had a label, push it on as the first line of
5013 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005014 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005015 if (label) {
5016 if (dont_prepend < 0)
5017 free_tlist(startline);
5018 else {
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005019 nasm_new(ll);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005020 ll->finishes = NULL;
5021 ll->next = istk->expansion;
5022 istk->expansion = ll;
5023 ll->first = startline;
5024 if (!dont_prepend) {
5025 while (label->next)
5026 label = label->next;
5027 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005028 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005029 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005030 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005031
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07005032 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO, 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005033
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005034 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005035}
5036
H. Peter Anvin130736c2016-02-17 20:27:41 -08005037/*
5038 * This function adds macro names to error messages, and suppresses
5039 * them if necessary.
5040 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005041static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005042{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005043 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005044 MMacro *mmac = NULL;
5045 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005046
H. Peter Anvin130736c2016-02-17 20:27:41 -08005047 /*
5048 * If we're in a dead branch of IF or something like it, ignore the error.
5049 * However, because %else etc are evaluated in the state context
5050 * of the previous branch, errors might get lost:
5051 * %if 0 ... %else trailing garbage ... %endif
5052 * So %else etc should set the ERR_PP_PRECOND flag.
5053 */
5054 if ((severity & ERR_MASK) < ERR_FATAL &&
5055 istk && istk->conds &&
5056 ((severity & ERR_PP_PRECOND) ?
5057 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07005058 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08005059 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005060
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005061 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08005062 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005063 mmac = istk->mstk;
5064 /* but %rep blocks should be skipped */
5065 while (mmac && !mmac->name)
5066 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04005067 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005068
H. Peter Anvin130736c2016-02-17 20:27:41 -08005069 if (mmac) {
5070 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02005071
H. Peter Anvin130736c2016-02-17 20:27:41 -08005072 nasm_set_verror(real_verror);
5073 nasm_error(severity, "(%s:%d) %s",
5074 mmac->name, mmac->lineno - delta, buff);
5075 nasm_set_verror(pp_verror);
5076 } else {
5077 real_verror(severity, fmt, arg);
5078 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005079}
5080
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005081static Token *
5082stdmac_file(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005083{
5084 (void)s;
5085 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005086 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005087
5088 return make_tok_qstr(src_get_fname());
5089}
5090
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005091static Token *
5092stdmac_line(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005093{
5094 (void)s;
5095 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005096 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005097
5098 return make_tok_num(src_get_linnum());
5099}
5100
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005101static Token *
5102stdmac_bits(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005103{
5104 (void)s;
5105 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005106 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005107
5108 return make_tok_num(globalbits);
5109}
5110
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005111static Token *
5112stdmac_ptr(const SMacro *s, Token **params, unsigned int nparams)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005113{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005114 const Token *t;
Chang S. Baefea22692019-05-28 12:25:16 -07005115 static const Token ptr_word = { NULL, "word", 4, TOK_ID };
5116 static const Token ptr_dword = { NULL, "dword", 5, TOK_ID };
5117 static const Token ptr_qword = { NULL, "qword", 5, TOK_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005118
5119 (void)s;
5120 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005121 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005122
5123 switch (globalbits) {
5124 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005125 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005126 break;
5127 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005128 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005129 break;
5130 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005131 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005132 break;
5133 default:
5134 panic();
5135 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005136
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005137 return dup_Token(NULL, t);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005138}
5139
H. Peter Anvin8b262472019-02-26 14:00:54 -08005140/* Add magic standard macros */
5141struct magic_macros {
5142 const char *name;
5143 int nparams;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005144 ExpandSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005145};
5146static const struct magic_macros magic_macros[] =
5147{
5148 { "__FILE__", 0, stdmac_file },
5149 { "__LINE__", 0, stdmac_line },
5150 { "__BITS__", 0, stdmac_bits },
5151 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005152 { NULL, 0, NULL }
5153};
5154
5155static void pp_add_magic_stdmac(void)
5156{
5157 const struct magic_macros *m;
5158 SMacro *s;
5159
5160 for (m = magic_macros; m->name; m++) {
5161 s = define_smacro(NULL, m->name, true, m->nparams, NULL);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005162 s->expand = m->func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005163 }
5164}
5165
H. Peter Anvin734b1882002-04-30 21:01:08 +00005166static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005167pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005168{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005169 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005170
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005171 cstk = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005172 nasm_new(istk);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07005173 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin274cda82016-05-10 02:56:29 -07005174 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005175 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005176 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03005177 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005178 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005179 nested_mac_count = 0;
5180 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005181 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005182 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005183 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005184 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005185
H. Peter Anvin8b262472019-02-26 14:00:54 -08005186 pp_add_magic_stdmac();
5187
H. Peter Anvinf7606612016-07-13 14:23:48 -07005188 if (tasm_compatible_mode)
5189 pp_add_stdmac(nasm_stdmac_tasm);
5190
5191 pp_add_stdmac(nasm_stdmac_nasm);
5192 pp_add_stdmac(nasm_stdmac_version);
5193
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005194 if (extrastdmac)
5195 pp_add_stdmac(extrastdmac);
5196
H. Peter Anvinf7606612016-07-13 14:23:48 -07005197 stdmacpos = stdmacros[0];
5198 stdmacnext = &stdmacros[1];
5199
H. Peter Anvind2456592008-06-19 15:04:18 -07005200 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005201
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03005202 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005203
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005204 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005205 * Define the __PASS__ macro. This is defined here unlike all the
5206 * other builtins, because it is special -- it varies between
5207 * passes -- but there is really no particular reason to make it
5208 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005209 *
5210 * 0 = dependencies only
5211 * 1 = preparatory passes
5212 * 2 = final pass
5213 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005214 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005215 switch (mode) {
5216 case PP_NORMAL:
5217 apass = pass_final() ? 2 : 1;
5218 break;
5219 case PP_DEPS:
5220 apass = 0;
5221 break;
5222 case PP_PREPROC:
5223 apass = 3;
5224 break;
5225 default:
5226 panic();
5227 }
5228
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005229 define_smacro(NULL, "__PASS__", true, 0, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005230}
5231
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005232static void pp_init(void)
5233{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005234}
5235
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005236/*
5237 * Get a line of tokens. If we popped the macro expansion/include stack,
5238 * we return a pointer to the dummy token tok_pop; at that point if
5239 * istk is NULL then we have reached end of input;
5240 */
5241static Token tok_pop; /* Dummy token placeholder */
5242
5243static Token *pp_tokline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005244{
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005245 Token *tline, *dtline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005246
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005247 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005248 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005249 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005250 * buffer or from the input file.
5251 */
5252 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005253 while (istk->expansion && istk->expansion->finishes) {
5254 Line *l = istk->expansion;
5255 if (!l->finishes->name && l->finishes->in_progress > 1) {
5256 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005257
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005258 /*
5259 * This is a macro-end marker for a macro with no
5260 * name, which means it's not really a macro at all
5261 * but a %rep block, and the `in_progress' field is
5262 * more than 1, meaning that we still need to
5263 * repeat. (1 means the natural last repetition; 0
5264 * means termination by %exitrep.) We have
5265 * therefore expanded up to the %endrep, and must
5266 * push the whole block on to the expansion buffer
5267 * again. We don't bother to remove the macro-end
5268 * marker: we'd only have to generate another one
5269 * if we did.
5270 */
5271 l->finishes->in_progress--;
5272 list_for_each(l, l->finishes->expansion) {
5273 Token *t, *tt, **tail;
5274
5275 ll = nasm_malloc(sizeof(Line));
5276 ll->next = istk->expansion;
5277 ll->finishes = NULL;
5278 ll->first = NULL;
5279 tail = &ll->first;
5280
5281 list_for_each(t, l->first) {
5282 if (t->text || t->type == TOK_WHITESPACE) {
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -07005283 tt = *tail = dup_Token(NULL, t);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005284 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005285 }
5286 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005287 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005288 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005289 } else {
5290 /*
5291 * Check whether a `%rep' was started and not ended
5292 * within this macro expansion. This can happen and
5293 * should be detected. It's a fatal error because
5294 * I'm too confused to work out how to recover
5295 * sensibly from it.
5296 */
5297 if (defining) {
5298 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005299 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005300 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005301 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005302 " expansion of macro `%s'",
5303 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005304 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005305
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005306 /*
5307 * FIXME: investigate the relationship at this point between
5308 * istk->mstk and l->finishes
5309 */
5310 {
5311 MMacro *m = istk->mstk;
5312 istk->mstk = m->next_active;
5313 if (m->name) {
5314 /*
5315 * This was a real macro call, not a %rep, and
5316 * therefore the parameter information needs to
5317 * be freed.
5318 */
5319 if (m->prev) {
5320 pop_mmacro(m);
5321 l->finishes->in_progress --;
5322 } else {
5323 nasm_free(m->params);
5324 free_tlist(m->iline);
5325 nasm_free(m->paramlen);
5326 l->finishes->in_progress = 0;
5327 }
Adam Majer91e72402017-07-25 10:42:01 +02005328 }
5329
5330 /*
5331 * FIXME It is incorrect to always free_mmacro here.
5332 * It leads to usage-after-free.
5333 *
5334 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5335 */
5336#if 0
5337 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005338 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005339#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005340 }
5341 istk->expansion = l->next;
5342 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005343 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005344 return &tok_pop;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005345 }
5346 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005347 do { /* until we get a line we can use */
5348 char *line;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005349
5350 if (istk->expansion) { /* from a macro expansion */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005351 Line *l = istk->expansion;
5352 if (istk->mstk)
5353 istk->mstk->lineno++;
5354 tline = l->first;
5355 istk->expansion = l->next;
5356 nasm_free(l);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005357 line = detoken(tline, false);
5358 lfmt->line(LIST_MACRO, line);
5359 nasm_free(line);
5360 } else if ((line = read_line())) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005361 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005362 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005363 nasm_free(line);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005364 } else {
5365 /*
5366 * The current file has ended; work down the istk
5367 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00005368 Include *i = istk;
5369 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005370 if (i->conds) {
5371 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005372 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005373 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005374 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005375 if (i->next)
5376 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005377 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005378 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005379 nasm_free(i);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005380 return &tok_pop;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005381 }
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005382 } while (0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005383
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005384 /*
5385 * We must expand MMacro parameters and MMacro-local labels
5386 * _before_ we plunge into directive processing, to cope
5387 * with things like `%define something %1' such as STRUC
5388 * uses. Unless we're _defining_ a MMacro, in which case
5389 * those tokens should be left alone to go into the
5390 * definition; and unless we're in a non-emitting
5391 * condition, in which case we don't want to meddle with
5392 * anything.
5393 */
5394 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5395 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005396 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005397 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005398
H. Peter Anvine2c80182005-01-15 22:15:51 +00005399 /*
5400 * Check the line to see if it's a preprocessor directive.
5401 */
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005402 if (do_directive(tline, &dtline) == DIRECTIVE_FOUND) {
5403 if (dtline)
5404 return dtline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005405 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005406 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005407 * We're defining a multi-line macro. We emit nothing
5408 * at all, and just
5409 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005410 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005411 Line *l = nasm_malloc(sizeof(Line));
5412 l->next = defining->expansion;
5413 l->first = tline;
5414 l->finishes = NULL;
5415 defining->expansion = l;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005416 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005417 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005418 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005419 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005420 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005421 * directive so we keep our place correctly.
5422 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005423 free_tlist(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005424 } else if (istk->mstk && !istk->mstk->in_progress) {
5425 /*
5426 * We're in a %rep block which has been terminated, so
5427 * we're walking through to the %endrep without
5428 * emitting anything. Emit nothing at all, not even a
5429 * blank line: when we emerge from the %rep block we'll
5430 * give a line-number directive so we keep our place
5431 * correctly.
5432 */
5433 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005434 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005435 tline = expand_smacro(tline);
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005436 if (!expand_mmacro(tline))
5437 return tline;
5438 }
5439 }
5440}
5441
5442static char *pp_getline(void)
5443{
5444 char *line = NULL;
5445 Token *tline;
5446
5447 real_verror = nasm_set_verror(pp_verror);
5448
5449 while (true) {
5450 tline = pp_tokline();
5451 if (tline == &tok_pop) {
5452 /*
5453 * We popped the macro/include stack. If istk is empty,
5454 * we are at end of input, otherwise just loop back.
5455 */
5456 if (!istk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005457 break;
H. Peter Anvin (Intel)a7afe272019-04-26 00:34:04 -07005458 } else {
5459 /*
5460 * De-tokenize the line and emit it.
5461 */
5462 line = detoken(tline, true);
5463 free_tlist(tline);
5464 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005465 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005466 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005467
H. Peter Anvin130736c2016-02-17 20:27:41 -08005468 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005469 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005470}
5471
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005472static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005473{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005474 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005475
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005476 if (defining) {
5477 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005478 nasm_nonfatal("end of file while still defining macro `%s'",
5479 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005480 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005481 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005482 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005483
5484 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005485 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005486 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005487
5488 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005489
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005490 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005491 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005492 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005493 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005494 Include *i = istk;
5495 istk = istk->next;
5496 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005497 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005498 }
5499 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005500 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005501 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005502}
5503
5504static void pp_cleanup_session(void)
5505{
5506 free_llist(predef);
5507 predef = NULL;
5508 delete_Blocks();
5509 freeTokens = NULL;
5510 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005511}
5512
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005513static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005514{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005515 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005516}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005517
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005518static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005519{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005520 Token *inc, *space, *name;
5521 Line *l;
5522
H. Peter Anvin734b1882002-04-30 21:01:08 +00005523 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5524 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5525 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005526
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005527 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005528 l->next = predef;
5529 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005530 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005531 predef = l;
5532}
5533
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005534static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005535{
5536 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005537 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005538 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005539
H. Peter Anvin130736c2016-02-17 20:27:41 -08005540 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005541
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005542 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005543 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5544 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005545 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005546 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005547 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005548 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005549 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005550
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005551 if (space->next->type != TOK_PREPROC_ID &&
5552 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005553 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005554
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005555 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005556 l->next = predef;
5557 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005558 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005559 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005560
5561 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005562}
5563
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005564static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005565{
5566 Token *def, *space;
5567 Line *l;
5568
H. Peter Anvin734b1882002-04-30 21:01:08 +00005569 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5570 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005571 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005572
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005573 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005574 l->next = predef;
5575 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005576 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005577 predef = l;
5578}
5579
H. Peter Anvin05990342018-06-11 13:32:42 -07005580/* Insert an early preprocessor command that doesn't need special handling */
5581static void pp_pre_command(const char *what, char *string)
5582{
5583 char *cmd;
5584 Token *def, *space;
5585 Line *l;
5586
5587 def = tokenize(string);
5588 if (what) {
5589 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5590 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5591 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5592 }
5593
5594 l = nasm_malloc(sizeof(Line));
5595 l->next = predef;
5596 l->first = def;
5597 l->finishes = NULL;
5598 predef = l;
5599}
5600
H. Peter Anvinf7606612016-07-13 14:23:48 -07005601static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005602{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005603 macros_t **mp;
5604
5605 /* Find the end of the list and avoid duplicates */
5606 for (mp = stdmacros; *mp; mp++) {
5607 if (*mp == macros)
5608 return; /* Nothing to do */
5609 }
5610
5611 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5612
5613 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005614}
5615
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005616static void pp_extra_stdmac(macros_t *macros)
5617{
5618 extrastdmac = macros;
5619}
5620
H. Peter Anvin8b262472019-02-26 14:00:54 -08005621static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005622{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005623 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005624 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5625 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5626}
5627
5628static Token *make_tok_qstr(const char *str)
5629{
5630 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005631 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005632 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005633}
5634
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005635static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005636{
5637 if (!m)
5638 return;
5639
5640 /* We need to print the next_active list in reverse order */
5641 pp_list_one_macro(m->next_active, severity);
5642
5643 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005644 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005645 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005646 }
5647}
5648
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005649static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005650{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005651 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005652
H. Peter Anvinddb29062018-12-11 00:06:29 -08005653 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5654 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005655
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005656 if (istk)
5657 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005658
H. Peter Anvinddb29062018-12-11 00:06:29 -08005659 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005660}
5661
H. Peter Anvine7469712016-02-18 02:20:59 -08005662const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005663 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005664 pp_reset,
5665 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005666 pp_cleanup_pass,
5667 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005668 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005669 pp_pre_define,
5670 pp_pre_undefine,
5671 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005672 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005673 pp_include_path,
5674 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005675};