blob: 804ff455817e88e6ba2b65d5f2b22d5d6e92782b [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002 *
H. Peter Anvin05990342018-06-11 13:32:42 -07003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * preproc.c macro preprocessor for the Netwide Assembler
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000036 */
37
H. Peter Anvin4836e332002-04-30 20:56:43 +000038/* Typical flow of text through preproc
39 *
Keith Kaniosb7a89542007-04-12 02:40:54 +000040 * pp_getline gets tokenized lines, either
H. Peter Anvin4836e332002-04-30 20:56:43 +000041 *
42 * from a macro expansion
43 *
44 * or
45 * {
46 * read_line gets raw text from stdmacpos, or predef, or current input file
Keith Kaniosb7a89542007-04-12 02:40:54 +000047 * tokenize converts to tokens
H. Peter Anvin4836e332002-04-30 20:56:43 +000048 * }
49 *
50 * expand_mmac_params is used to expand %1 etc., unless a macro is being
51 * defined or a false conditional is being processed
52 * (%0, %1, %+1, %-1, %%foo
53 *
54 * do_directive checks for directives
55 *
56 * expand_smacro is used to expand single line macros
57 *
58 * expand_mmacro is used to expand multi-line macros
59 *
60 * detoken is used to convert the line back to text
61 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000062
H. Peter Anvinfe501952007-10-02 21:53:51 -070063#include "compiler.h"
64
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080065#include "nctype.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000066
67#include "nasm.h"
68#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080069#include "error.h"
H. Peter Anvin4169a472007-09-12 01:29:43 +000070#include "preproc.h"
H. Peter Anvin97a23472007-09-16 17:57:25 -070071#include "hashtbl.h"
H. Peter Anvin8cad14b2008-06-01 17:23:51 -070072#include "quote.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070073#include "stdscan.h"
H. Peter Anvindbb640b2009-07-18 18:57:16 -070074#include "eval.h"
H. Peter Anvinc2df2822007-10-24 15:29:28 -070075#include "tokens.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -070076#include "tables.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080077#include "listing.h"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000078
79typedef struct SMacro SMacro;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080080typedef struct MMacro MMacro;
81typedef struct MMacroInvocation MMacroInvocation;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000082typedef struct Context Context;
83typedef struct Token Token;
H. Peter Anvince616072002-04-30 21:02:23 +000084typedef struct Blocks Blocks;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000085typedef struct Line Line;
86typedef struct Include Include;
H. Peter Anvin36206cd2012-03-03 16:14:51 -080087typedef struct Cond Cond;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000088
89/*
H. Peter Anvin97a23472007-09-16 17:57:25 -070090 * Note on the storage of both SMacro and MMacros: the hash table
91 * indexes them case-insensitively, and we then have to go through a
92 * linked list of potential case aliases (and, for MMacros, parameter
93 * ranges); this is to preserve the matching semantics of the earlier
94 * code. If the number of case aliases for a specific macro is a
95 * performance issue, you may want to reconsider your coding style.
96 */
97
98/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -070099 * Function call for a magic smacro
100 */
101typedef Token *(*MagicSMacro)(const SMacro *s, Token **params,
102 unsigned int nparams, bool *free_expansion);
103
104/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000105 * Store the definition of a single-line macro.
106 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107struct SMacro {
H. Peter Anvin8b262472019-02-26 14:00:54 -0800108 SMacro *next; /* MUST BE FIRST - see free_smacro() */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800109 char *name;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700110 Token *expansion;
111 MagicSMacro magic;
112 intorptr magicpvt;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800113 bool *eval_param;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800114 unsigned int nparam;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800115 bool casesense;
H. Peter Anvin8b262472019-02-26 14:00:54 -0800116 bool in_progress;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000117};
118
119/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800120 * Store the definition of a multi-line macro. This is also used to
121 * store the interiors of `%rep...%endrep' blocks, which are
122 * effectively self-re-invoking multi-line macros which simply
123 * don't have a name or bother to appear in the hash tables. %rep
124 * blocks are signified by having a NULL `name' field.
125 *
126 * In a MMacro describing a `%rep' block, the `in_progress' field
127 * isn't merely boolean, but gives the number of repeats left to
128 * run.
129 *
130 * The `next' field is used for storing MMacros in hash tables; the
131 * `next_active' field is for stacking them on istk entries.
132 *
133 * When a MMacro is being expanded, `params', `iline', `nparam',
134 * `paramlen', `rotate' and `unique' are local to the invocation.
135 */
136struct MMacro {
137 MMacro *next;
138 MMacroInvocation *prev; /* previous invocation */
139 char *name;
140 int nparam_min, nparam_max;
141 bool casesense;
142 bool plus; /* is the last parameter greedy? */
143 bool nolist; /* is this macro listing-inhibited? */
144 int64_t in_progress; /* is this macro currently being expanded? */
145 int32_t max_depth; /* maximum number of recursive expansions allowed */
146 Token *dlist; /* All defaults as one list */
147 Token **defaults; /* Parameter default pointers */
148 int ndefs; /* number of default parameters */
149 Line *expansion;
150
151 MMacro *next_active;
152 MMacro *rep_nest; /* used for nesting %rep */
153 Token **params; /* actual parameters */
154 Token *iline; /* invocation line */
155 unsigned int nparam, rotate;
156 int *paramlen;
157 uint64_t unique;
158 int lineno; /* Current line number on expansion */
159 uint64_t condcnt; /* number of if blocks... */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700160
H. Peter Anvin274cda82016-05-10 02:56:29 -0700161 const char *fname; /* File where defined */
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700162 int32_t xline; /* First line in macro */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800163};
164
165
166/* Store the definition of a multi-line macro, as defined in a
167 * previous recursive macro expansion.
168 */
169struct MMacroInvocation {
170 MMacroInvocation *prev; /* previous invocation */
171 Token **params; /* actual parameters */
172 Token *iline; /* invocation line */
173 unsigned int nparam, rotate;
174 int *paramlen;
175 uint64_t unique;
176 uint64_t condcnt;
177};
178
179
180/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000181 * The context stack is composed of a linked list of these.
182 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000183struct Context {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800184 Context *next;
185 char *name;
186 struct hash_table localmac;
187 uint32_t number;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000188};
189
190/*
191 * This is the internal form which we break input lines up into.
192 * Typically stored in linked lists.
193 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800194 * Note that `type' serves a double meaning: TOK_SMAC_START_PARAMS is
195 * not necessarily used as-is, but is also used to encode the number
196 * and expansion type of substituted parameter. So in the definition
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000197 *
H. Peter Anvin8b262472019-02-26 14:00:54 -0800198 * %define a(x,=y) ( (x) & ~(y) )
H. Peter Anvin70653092007-10-19 14:42:29 -0700199 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000200 * the token representing `x' will have its type changed to
H. Peter Anvin8b262472019-02-26 14:00:54 -0800201 * tok_smac_param(0) but the one representing `y' will be
202 * tok_smac_param(1); see the accessor functions below.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000203 *
204 * TOK_INTERNAL_STRING is a dirty hack: it's a single string token
205 * which doesn't need quotes around it. Used in the pre-include
206 * mechanism as an alternative to trying to find a sensible type of
207 * quote to use on the filename we were passed.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000208 */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000209enum pp_token_type {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800210 TOK_NONE = 0, TOK_WHITESPACE, TOK_COMMENT, TOK_ID,
211 TOK_PREPROC_ID, TOK_STRING,
H. Peter Anvin8b262472019-02-26 14:00:54 -0800212 TOK_NUMBER, TOK_FLOAT, TOK_OTHER,
H. Peter Anvin6c81f0a2008-05-25 21:46:17 -0700213 TOK_INTERNAL_STRING,
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800214 TOK_PREPROC_Q, TOK_PREPROC_QQ,
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300215 TOK_PASTE, /* %+ */
216 TOK_INDIRECT, /* %[...] */
H. Peter Anvin8b262472019-02-26 14:00:54 -0800217 TOK_SMAC_START_PARAMS, /* MUST BE LAST IN THE LIST!!! */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300218 TOK_MAX = INT_MAX /* Keep compiler from reducing the range */
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000219};
220
H. Peter Anvin8b262472019-02-26 14:00:54 -0800221static inline enum pp_token_type tok_smac_param(int param)
222{
223 return TOK_SMAC_START_PARAMS + param;
224}
225static int smac_nparam(enum pp_token_type toktype)
226{
227 return toktype - TOK_SMAC_START_PARAMS;
228}
229static bool is_smac_param(enum pp_token_type toktype)
230{
231 return toktype >= TOK_SMAC_START_PARAMS;
232}
233
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400234#define PP_CONCAT_MASK(x) (1 << (x))
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +0400235#define PP_CONCAT_MATCH(t, mask) (PP_CONCAT_MASK((t)->type) & mask)
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +0400236
Cyrill Gorcunov575d4282010-10-06 00:25:55 +0400237struct tokseq_match {
238 int mask_head;
239 int mask_tail;
240};
241
H. Peter Anvine2c80182005-01-15 22:15:51 +0000242struct Token {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800243 Token *next;
244 char *text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700245 size_t len; /* scratch length field */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800246 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000247};
248
249/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800250 * Multi-line macro definitions are stored as a linked list of
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000251 * these, which is essentially a container to allow several linked
252 * lists of Tokens.
H. Peter Anvin70653092007-10-19 14:42:29 -0700253 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000254 * Note that in this module, linked lists are treated as stacks
255 * wherever possible. For this reason, Lines are _pushed_ on to the
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800256 * `expansion' field in MMacro structures, so that the linked list,
257 * if walked, would give the macro lines in reverse order; this
258 * means that we can walk the list when expanding a macro, and thus
259 * push the lines on to the `expansion' field in _istk_ in reverse
260 * order (so that when popped back off they are in the right
261 * order). It may seem cockeyed, and it relies on my design having
262 * an even number of steps in, but it works...
263 *
264 * Some of these structures, rather than being actual lines, are
265 * markers delimiting the end of the expansion of a given macro.
266 * This is for use in the cycle-tracking and %rep-handling code.
267 * Such structures have `finishes' non-NULL, and `first' NULL. All
268 * others have `finishes' NULL, but `first' may still be NULL if
269 * the line is blank.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000270 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000271struct Line {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800272 Line *next;
273 MMacro *finishes;
274 Token *first;
Keith Kaniosb307a4f2010-11-06 17:41:51 -0500275};
276
277/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000278 * To handle an arbitrary level of file inclusion, we maintain a
279 * stack (ie linked list) of these things.
280 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000281struct Include {
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800282 Include *next;
283 FILE *fp;
284 Cond *conds;
285 Line *expansion;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700286 const char *fname;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800287 int lineno, lineinc;
288 MMacro *mstk; /* stack of active macros/reps */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000289};
290
291/*
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700292 * File real name hash, so we don't have to re-search the include
293 * path for every pass (and potentially more than that if a file
294 * is used more than once.)
295 */
296struct hash_table FileHash;
297
298/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000299 * Conditional assembly: we maintain a separate stack of these for
300 * each level of file inclusion. (The only reason we keep the
301 * stacks separate is to ensure that a stray `%endif' in a file
302 * included from within the true branch of a `%if' won't terminate
303 * it and cause confusion: instead, rightly, it'll cause an error.)
304 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800305struct Cond {
306 Cond *next;
307 int state;
308};
H. Peter Anvine2c80182005-01-15 22:15:51 +0000309enum {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000310 /*
311 * These states are for use just after %if or %elif: IF_TRUE
312 * means the condition has evaluated to truth so we are
313 * currently emitting, whereas IF_FALSE means we are not
314 * currently emitting but will start doing so if a %else comes
315 * up. In these states, all directives are admissible: %elif,
316 * %else and %endif. (And of course %if.)
317 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800318 COND_IF_TRUE, COND_IF_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000319 /*
320 * These states come up after a %else: ELSE_TRUE means we're
321 * emitting, and ELSE_FALSE means we're not. In ELSE_* states,
322 * any %elif or %else will cause an error.
323 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800324 COND_ELSE_TRUE, COND_ELSE_FALSE,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000325 /*
Victor van den Elzen3b404c02008-09-18 13:51:36 +0200326 * These states mean that we're not emitting now, and also that
327 * nothing until %endif will be emitted at all. COND_DONE is
328 * used when we've had our moment of emission
329 * and have now started seeing %elifs. COND_NEVER is used when
330 * the condition construct in question is contained within a
331 * non-emitting branch of a larger condition construct,
332 * or if there is an error.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000333 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800334 COND_DONE, COND_NEVER
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000335};
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800336#define emitting(x) ( (x) == COND_IF_TRUE || (x) == COND_ELSE_TRUE )
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000337
H. Peter Anvin70653092007-10-19 14:42:29 -0700338/*
Ed Beroset3ab3f412002-06-11 03:31:49 +0000339 * These defines are used as the possible return values for do_directive
340 */
341#define NO_DIRECTIVE_FOUND 0
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300342#define DIRECTIVE_FOUND 1
Ed Beroset3ab3f412002-06-11 03:31:49 +0000343
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +0400344/* max reps */
345#define REP_LIMIT ((INT64_C(1) << 62))
346
Keith Kanios852f1ee2009-07-12 00:19:55 -0500347/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000348 * Condition codes. Note that we use c_ prefix not C_ because C_ is
349 * used in nasm.h for the "real" condition codes. At _this_ level,
350 * we treat CXZ and ECXZ as condition codes, albeit non-invertible
351 * ones, so we need a different enum...
352 */
H. Peter Anvin476d2862007-10-02 22:04:15 -0700353static const char * const conditions[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000354 "a", "ae", "b", "be", "c", "cxz", "e", "ecxz", "g", "ge", "l", "le",
355 "na", "nae", "nb", "nbe", "nc", "ne", "ng", "nge", "nl", "nle", "no",
H. Peter Anvince9be342007-09-12 00:22:29 +0000356 "np", "ns", "nz", "o", "p", "pe", "po", "rcxz", "s", "z"
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000357};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700358enum pp_conds {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000359 c_A, c_AE, c_B, c_BE, c_C, c_CXZ, c_E, c_ECXZ, c_G, c_GE, c_L, c_LE,
360 c_NA, c_NAE, c_NB, c_NBE, c_NC, c_NE, c_NG, c_NGE, c_NL, c_NLE, c_NO,
H. Peter Anvin476d2862007-10-02 22:04:15 -0700361 c_NP, c_NS, c_NZ, c_O, c_P, c_PE, c_PO, c_RCXZ, c_S, c_Z,
362 c_none = -1
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000363};
H. Peter Anvin476d2862007-10-02 22:04:15 -0700364static const enum pp_conds inverse_ccs[] = {
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000365 c_NA, c_NAE, c_NB, c_NBE, c_NC, -1, c_NE, -1, c_NG, c_NGE, c_NL, c_NLE,
366 c_A, c_AE, c_B, c_BE, c_C, c_E, c_G, c_GE, c_L, c_LE, c_O, c_P, c_S,
H. Peter Anvince9be342007-09-12 00:22:29 +0000367 c_Z, c_NO, c_NP, c_PO, c_PE, -1, c_NS, c_NZ
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000368};
369
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800370/*
371 * Directive names.
372 */
373/* If this is a an IF, ELIF, ELSE or ENDIF keyword */
374static int is_condition(enum preproc_token arg)
375{
376 return PP_IS_COND(arg) || (arg == PP_ELSE) || (arg == PP_ENDIF);
377}
378
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000379/* For TASM compatibility we need to be able to recognise TASM compatible
380 * conditional compilation directives. Using the NASM pre-processor does
381 * not work, so we look for them specifically from the following list and
382 * then jam in the equivalent NASM directive into the input stream.
383 */
384
H. Peter Anvine2c80182005-01-15 22:15:51 +0000385enum {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000386 TM_ARG, TM_ELIF, TM_ELSE, TM_ENDIF, TM_IF, TM_IFDEF, TM_IFDIFI,
387 TM_IFNDEF, TM_INCLUDE, TM_LOCAL
388};
389
H. Peter Anvin476d2862007-10-02 22:04:15 -0700390static const char * const tasm_directives[] = {
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000391 "arg", "elif", "else", "endif", "if", "ifdef", "ifdifi",
392 "ifndef", "include", "local"
393};
394
395static int StackSize = 4;
H. Peter Anvin6c8b2be2016-05-24 23:46:50 -0700396static const char *StackPointer = "ebp";
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000397static int ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -0800398static int LocalOffset = 0;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000399
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000400static Context *cstk;
401static Include *istk;
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300402static const struct strlist *ipath_list;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000403
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +0300404static struct strlist *deplist;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000405
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300406static uint64_t unique; /* unique identifier numbers */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000407
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800408static Line *predef = NULL;
H. Peter Anvind2456592008-06-19 15:04:18 -0700409static bool do_predef;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800410static enum preproc_mode pp_mode;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000411
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000412/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800413 * The current set of multi-line macros we have defined.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000414 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800415static struct hash_table mmacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000416
417/*
418 * The current set of single-line macros we have defined.
419 */
H. Peter Anvin166c2472008-05-28 12:28:58 -0700420static struct hash_table smacros;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000421
422/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800423 * The multi-line macro we are currently defining, or the %rep
424 * block we are currently reading, if any.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000425 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800426static MMacro *defining;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000427
Charles Crayned4200be2008-07-12 16:42:33 -0700428static uint64_t nested_mac_count;
429static uint64_t nested_rep_count;
430
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000431/*
432 * The number of macro parameters to allocate space for at a time.
433 */
434#define PARAM_DELTA 16
435
436/*
H. Peter Anvinf7606612016-07-13 14:23:48 -0700437 * The standard macro set: defined in macros.c in a set of arrays.
438 * This gives our position in any macro set, while we are processing it.
439 * The stdmacset is an array of such macro sets.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000440 */
H. Peter Anvina70547f2008-07-19 21:44:26 -0700441static macros_t *stdmacpos;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700442static macros_t **stdmacnext;
443static macros_t *stdmacros[8];
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +0300444static macros_t *extrastdmac;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000445
446/*
H. Peter Anvin734b1882002-04-30 21:01:08 +0000447 * Tokens are allocated in blocks to improve speed
448 */
449#define TOKEN_BLOCKSIZE 4096
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800450static Token *freeTokens = NULL;
H. Peter Anvince616072002-04-30 21:02:23 +0000451struct Blocks {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000452 Blocks *next;
453 void *chunk;
H. Peter Anvince616072002-04-30 21:02:23 +0000454};
455
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800456static Blocks blocks = { NULL, NULL };
H. Peter Anvin734b1882002-04-30 21:01:08 +0000457
458/*
H. Peter Anvin76690a12002-04-30 20:52:49 +0000459 * Forward declarations.
460 */
H. Peter Anvinf7606612016-07-13 14:23:48 -0700461static void pp_add_stdmac(macros_t *macros);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000462static Token *expand_mmac_params(Token * tline);
463static Token *expand_smacro(Token * tline);
464static Token *expand_id(Token * tline);
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +0400465static Context *get_ctx(const char *name, const char **namep);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800466static Token *make_tok_num(int64_t val);
467static Token *make_tok_qstr(const char *str);
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800468static void pp_verror(errflags severity, const char *fmt, va_list ap);
H. Peter Anvin130736c2016-02-17 20:27:41 -0800469static vefunc real_verror;
H. Peter Anvince616072002-04-30 21:02:23 +0000470static void *new_Block(size_t size);
471static void delete_Blocks(void);
H. Peter Anvinc751e862008-06-09 10:18:45 -0700472static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700473 const char *text, size_t txtlen);
474static Token *dup_Token(Token *next, const Token *src);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000475static Token *delete_Token(Token * t);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000476
477/*
478 * Macros for safe checking of token pointers, avoid *(NULL)
479 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300480#define tok_type_(x,t) ((x) && (x)->type == (t))
481#define skip_white_(x) if (tok_type_((x), TOK_WHITESPACE)) (x)=(x)->next
482#define tok_is_(x,v) (tok_type_((x), TOK_OTHER) && !strcmp((x)->text,(v)))
483#define tok_isnt_(x,v) ((x) && ((x)->type!=TOK_OTHER || strcmp((x)->text,(v))))
H. Peter Anvin76690a12002-04-30 20:52:49 +0000484
Cyrill Gorcunov194ba892011-06-30 01:16:35 +0400485/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700486 * nasm_unquote with error if the string contains control characters.
487 * If the string contains control characters, issue an error and return
488 * the C len, i.e. truncate at the control character.
H. Peter Anvin077fb932010-07-20 14:56:30 -0700489 */
490static size_t nasm_unquote_cstr(char *qstr, enum preproc_token directive)
491{
492 size_t len = nasm_unquote(qstr, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700493 size_t i;
H. Peter Anvin077fb932010-07-20 14:56:30 -0700494
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700495 for (i = 0; i < len; i++) {
496 if (qstr[i] < ' ') {
497 nasm_nonfatal("control character in `%s' directive string",
498 pp_directives[directive]);
499 break;
500 }
501 }
502
503 qstr[i] = '\0';
504 return i;
H. Peter Anvin077fb932010-07-20 14:56:30 -0700505}
506
507/*
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700508 * In-place reverse a list of tokens.
509 */
510static Token *reverse_tokens(Token *t)
511{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800512 Token *prev = NULL;
513 Token *next;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700514
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800515 while (t) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400516 next = t->next;
517 t->next = prev;
518 prev = t;
519 t = next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800520 }
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700521
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800522 return prev;
H. Peter Anvinb40992c2010-09-15 08:57:21 -0700523}
524
525/*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300526 * Handle TASM specific directives, which do not contain a % in
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000527 * front of them. We do it here because I could not find any other
528 * place to do it for the moment, and it is a hack (ideally it would
529 * be nice to be able to use the NASM pre-processor to do it).
530 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000531static char *check_tasm_directive(char *line)
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000532{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000533 int32_t i, j, k, m, len;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400534 char *p, *q, *oldline, oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000535
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400536 p = nasm_skip_spaces(line);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000537
538 /* Binary search for the directive name */
539 i = -1;
Cyrill Gorcunova7319242010-06-03 22:04:36 +0400540 j = ARRAY_SIZE(tasm_directives);
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +0400541 q = nasm_skip_word(p);
542 len = q - p;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000543 if (len) {
544 oldchar = p[len];
545 p[len] = 0;
546 while (j - i > 1) {
547 k = (j + i) / 2;
548 m = nasm_stricmp(p, tasm_directives[k]);
549 if (m == 0) {
550 /* We have found a directive, so jam a % in front of it
551 * so that NASM will then recognise it as one if it's own.
552 */
553 p[len] = oldchar;
554 len = strlen(p);
555 oldline = line;
556 line = nasm_malloc(len + 2);
557 line[0] = '%';
558 if (k == TM_IFDIFI) {
H. Peter Anvin18f48792009-06-27 15:56:27 -0700559 /*
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300560 * NASM does not recognise IFDIFI, so we convert
561 * it to %if 0. This is not used in NASM
562 * compatible code, but does need to parse for the
563 * TASM macro package.
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 */
H. Peter Anvin18f48792009-06-27 15:56:27 -0700565 strcpy(line + 1, "if 0");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 } else {
567 memcpy(line + 1, p, len + 1);
568 }
569 nasm_free(oldline);
570 return line;
571 } else if (m < 0) {
572 j = k;
573 } else
574 i = k;
575 }
576 p[len] = oldchar;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000577 }
578 return line;
579}
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000580
H. Peter Anvin76690a12002-04-30 20:52:49 +0000581/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000582 * The pre-preprocessing stage... This function translates line
583 * number indications as they emerge from GNU cpp (`# lineno "file"
584 * flags') into NASM preprocessor line number indications (`%line
585 * lineno file').
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000586 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000587static char *prepreproc(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000588{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000589 int lineno, fnlen;
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000590 char *fname, *oldline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000591
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 if (line[0] == '#' && line[1] == ' ') {
593 oldline = line;
594 fname = oldline + 2;
595 lineno = atoi(fname);
596 fname += strspn(fname, "0123456789 ");
597 if (*fname == '"')
598 fname++;
599 fnlen = strcspn(fname, "\"");
600 line = nasm_malloc(20 + fnlen);
601 snprintf(line, 20 + fnlen, "%%line %d %.*s", lineno, fnlen, fname);
602 nasm_free(oldline);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000603 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +0000604 if (tasm_compatible_mode)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 return check_tasm_directive(line);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000606 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607}
608
609/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000610 * Free a linked list of tokens.
611 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000612static void free_tlist(Token * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400614 while (list)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 list = delete_Token(list);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000616}
617
618/*
619 * Free a linked list of lines.
620 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621static void free_llist(Line * list)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000622{
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +0400623 Line *l, *tmp;
624 list_for_each_safe(l, tmp, list) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 free_tlist(l->first);
626 nasm_free(l);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000627 }
628}
629
630/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800631 * Free an MMacro
H. Peter Anvineba20a72002-04-30 20:53:55 +0000632 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800633static void free_mmacro(MMacro * m)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000634{
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800635 nasm_free(m->name);
636 free_tlist(m->dlist);
637 nasm_free(m->defaults);
638 free_llist(m->expansion);
639 nasm_free(m);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000640}
641
642/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700643 * Clear or free an SMacro
H. Peter Anvin8b262472019-02-26 14:00:54 -0800644 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700645static void free_smacro_members(SMacro *s)
H. Peter Anvin8b262472019-02-26 14:00:54 -0800646{
647 nasm_free(s->name);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700648 free_tlist(s->expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800649 nasm_free(s->eval_param);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700650}
651
652static void clear_smacro(SMacro *s)
653{
654 free_smacro_members(s);
655 /* Wipe everything except the next pointer */
656 memset(&s->next + 1, 0, sizeof *s - sizeof s->next);
657}
658
659/*
660 * Free an SMacro
661 */
662static void free_smacro(SMacro *s)
663{
664 free_smacro_members(s);
665 nasm_free(s);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800666}
667
668/*
H. Peter Anvin97a23472007-09-16 17:57:25 -0700669 * Free all currently defined macros, and free the hash tables
670 */
H. Peter Anvin072771e2008-05-22 13:17:51 -0700671static void free_smacro_table(struct hash_table *smt)
H. Peter Anvin97a23472007-09-16 17:57:25 -0700672{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800673 struct hash_iterator it;
674 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700675
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800676 hash_for_each(smt, it, np) {
677 SMacro *tmp;
678 SMacro *s = np->data;
679 nasm_free((void *)np->key);
H. Peter Anvin8b262472019-02-26 14:00:54 -0800680 list_for_each_safe(s, tmp, s)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -0700681 free_smacro(s);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700682 }
H. Peter Anvin072771e2008-05-22 13:17:51 -0700683 hash_free(smt);
684}
685
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800686static void free_mmacro_table(struct hash_table *mmt)
H. Peter Anvin072771e2008-05-22 13:17:51 -0700687{
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800688 struct hash_iterator it;
689 const struct hash_node *np;
H. Peter Anvin97a23472007-09-16 17:57:25 -0700690
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800691 hash_for_each(mmt, it, np) {
692 MMacro *tmp;
693 MMacro *m = np->data;
694 nasm_free((void *)np->key);
695 list_for_each_safe(m, tmp, m)
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800696 free_mmacro(m);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700697 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800698 hash_free(mmt);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700699}
700
701static void free_macros(void)
702{
H. Peter Anvin166c2472008-05-28 12:28:58 -0700703 free_smacro_table(&smacros);
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800704 free_mmacro_table(&mmacros);
H. Peter Anvin97a23472007-09-16 17:57:25 -0700705}
706
707/*
708 * Initialize the hash tables
709 */
710static void init_macros(void)
711{
H. Peter Anvin97a23472007-09-16 17:57:25 -0700712}
713
714/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000715 * Pop the context stack.
716 */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000717static void ctx_pop(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000718{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000719 Context *c = cstk;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000720
721 cstk = cstk->next;
H. Peter Anvin166c2472008-05-28 12:28:58 -0700722 free_smacro_table(&c->localmac);
H. Peter Anvin734b1882002-04-30 21:01:08 +0000723 nasm_free(c->name);
724 nasm_free(c);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000725}
726
H. Peter Anvin072771e2008-05-22 13:17:51 -0700727/*
728 * Search for a key in the hash index; adding it if necessary
729 * (in which case we initialize the data pointer to NULL.)
730 */
731static void **
732hash_findi_add(struct hash_table *hash, const char *str)
733{
734 struct hash_insert hi;
735 void **r;
736 char *strx;
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800737 size_t l = strlen(str) + 1;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700738
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800739 r = hash_findib(hash, str, l, &hi);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700740 if (r)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300741 return r;
H. Peter Anvin072771e2008-05-22 13:17:51 -0700742
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -0800743 strx = nasm_malloc(l); /* Use a more efficient allocator here? */
744 memcpy(strx, str, l);
H. Peter Anvin072771e2008-05-22 13:17:51 -0700745 return hash_add(&hi, strx, NULL);
746}
747
748/*
749 * Like hash_findi, but returns the data element rather than a pointer
750 * to it. Used only when not adding a new element, hence no third
751 * argument.
752 */
753static void *
754hash_findix(struct hash_table *hash, const char *str)
755{
756 void **p;
757
758 p = hash_findi(hash, str, NULL);
759 return p ? *p : NULL;
760}
761
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400762/*
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800763 * read line from standart macros set,
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400764 * if there no more left -- return NULL
765 */
766static char *line_from_stdmac(void)
767{
768 unsigned char c;
769 const unsigned char *p = stdmacpos;
770 char *line, *q;
771 size_t len = 0;
772
773 if (!stdmacpos)
774 return NULL;
775
776 while ((c = *p++)) {
777 if (c >= 0x80)
778 len += pp_directives_len[c - 0x80] + 1;
779 else
780 len++;
781 }
782
783 line = nasm_malloc(len + 1);
784 q = line;
785 while ((c = *stdmacpos++)) {
786 if (c >= 0x80) {
787 memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
788 q += pp_directives_len[c - 0x80];
789 *q++ = ' ';
790 } else {
791 *q++ = c;
792 }
793 }
794 stdmacpos = p;
795 *q = '\0';
796
797 if (!*stdmacpos) {
H. Peter Anvinf7606612016-07-13 14:23:48 -0700798 /* This was the last of this particular macro set */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400799 stdmacpos = NULL;
H. Peter Anvinf7606612016-07-13 14:23:48 -0700800 if (*stdmacnext) {
801 stdmacpos = *stdmacnext++;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400802 } else if (do_predef) {
803 Line *pd, *l;
804 Token *head, **tail, *t;
805
806 /*
807 * Nasty hack: here we push the contents of
808 * `predef' on to the top-level expansion stack,
809 * since this is the most convenient way to
810 * implement the pre-include and pre-define
811 * features.
812 */
813 list_for_each(pd, predef) {
814 head = NULL;
815 tail = &head;
816 list_for_each(t, pd->first) {
817 *tail = new_Token(NULL, t->type, t->text, 0);
818 tail = &(*tail)->next;
819 }
820
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800821 l = nasm_malloc(sizeof(Line));
822 l->next = istk->expansion;
823 l->first = head;
824 l->finishes = NULL;
825
826 istk->expansion = l;
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400827 }
828 do_predef = false;
829 }
830 }
831
832 return line;
833}
834
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000835static char *read_line(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000836{
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400837 unsigned int size, c, next;
838 const unsigned int delta = 512;
839 const unsigned int pad = 8;
840 unsigned int nr_cont = 0;
841 bool cont = false;
842 char *buffer, *p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000843
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400844 /* Standart macros set (predefined) goes first */
Cyrill Gorcunov15bdc512010-07-13 11:27:41 +0400845 p = line_from_stdmac();
846 if (p)
847 return p;
H. Peter Anvin72edbb82008-06-19 16:00:04 -0700848
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400849 size = delta;
850 p = buffer = nasm_malloc(size);
851
852 for (;;) {
853 c = fgetc(istk->fp);
854 if ((int)(c) == EOF) {
855 p[0] = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000857 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400858
859 switch (c) {
860 case '\r':
861 next = fgetc(istk->fp);
862 if (next != '\n')
863 ungetc(next, istk->fp);
864 if (cont) {
865 cont = false;
866 continue;
867 }
868 break;
869
870 case '\n':
871 if (cont) {
872 cont = false;
873 continue;
874 }
875 break;
876
877 case '\\':
878 next = fgetc(istk->fp);
879 ungetc(next, istk->fp);
Cyrill Gorcunov490f85e2012-12-27 20:02:17 +0400880 if (next == '\r' || next == '\n') {
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400881 cont = true;
882 nr_cont++;
883 continue;
884 }
885 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000886 }
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400887
888 if (c == '\r' || c == '\n') {
889 *p++ = 0;
890 break;
891 }
892
893 if (p >= (buffer + size - pad)) {
894 buffer = nasm_realloc(buffer, size + delta);
895 p = buffer + size - pad;
896 size += delta;
897 }
898
899 *p++ = (unsigned char)c;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000900 }
901
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400902 if (p == buffer) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000903 nasm_free(buffer);
904 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000905 }
906
Cyrill Gorcunova5aea572010-11-11 10:14:45 +0300907 src_set_linnum(src_get_linnum() + istk->lineinc +
Cyrill Gorcunovf1fe4fd2012-10-27 19:27:18 +0400908 (nr_cont * istk->lineinc));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000909
910 /*
911 * Handle spurious ^Z, which may be inserted into source files
912 * by some file transfer utilities.
913 */
914 buffer[strcspn(buffer, "\032")] = '\0';
915
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800916 lfmt->line(LIST_READ, buffer);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000917
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000918 return buffer;
919}
920
921/*
Keith Kaniosb7a89542007-04-12 02:40:54 +0000922 * Tokenize a line of text. This is a very simple process since we
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000923 * don't need to parse the value out of e.g. numeric tokens: we
924 * simply split one string into many.
925 */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000926static Token *tokenize(char *line)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000927{
H. Peter Anvinca544db2008-10-19 19:30:11 -0700928 char c, *p = line;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +0000929 enum pp_token_type type;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000930 Token *list = NULL;
931 Token *t, **tail = &list;
932
H. Peter Anvine2c80182005-01-15 22:15:51 +0000933 while (*line) {
934 p = line;
935 if (*p == '%') {
936 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300937 if (*p == '+' && !nasm_isdigit(p[1])) {
938 p++;
939 type = TOK_PASTE;
940 } else if (nasm_isdigit(*p) ||
941 ((*p == '-' || *p == '+') && nasm_isdigit(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 do {
943 p++;
944 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -0700945 while (nasm_isdigit(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000946 type = TOK_PREPROC_ID;
947 } else if (*p == '{') {
948 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800949 while (*p) {
950 if (*p == '}')
951 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 p[-1] = *p;
953 p++;
954 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800955 if (*p != '}')
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -0800956 nasm_warn(WARN_OTHER, "unterminated %%{ construct");
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 p[-1] = '\0';
958 if (*p)
959 p++;
960 type = TOK_PREPROC_ID;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300961 } else if (*p == '[') {
962 int lvl = 1;
963 line += 2; /* Skip the leading %[ */
964 p++;
965 while (lvl && (c = *p++)) {
966 switch (c) {
967 case ']':
968 lvl--;
969 break;
970 case '%':
971 if (*p == '[')
972 lvl++;
973 break;
974 case '\'':
975 case '\"':
976 case '`':
Cyrill Gorcunov3144e842017-10-22 10:50:55 +0300977 p = nasm_skip_string(p - 1);
978 if (*p)
979 p++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300980 break;
981 default:
982 break;
983 }
984 }
985 p--;
986 if (*p)
987 *p++ = '\0';
H. Peter Anvin36206cd2012-03-03 16:14:51 -0800988 if (lvl)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +0300989 nasm_nonfatalf(ERR_PASS1, "unterminated %%[ construct");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +0300990 type = TOK_INDIRECT;
991 } else if (*p == '?') {
992 type = TOK_PREPROC_Q; /* %? */
993 p++;
994 if (*p == '?') {
995 type = TOK_PREPROC_QQ; /* %?? */
996 p++;
997 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +0400998 } else if (*p == '!') {
999 type = TOK_PREPROC_ID;
1000 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001001 if (nasm_isidchar(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001002 do {
1003 p++;
1004 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001005 while (nasm_isidchar(*p));
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001006 } else if (nasm_isquote(*p)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001007 p = nasm_skip_string(p);
1008 if (*p)
1009 p++;
1010 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001011 nasm_nonfatalf(ERR_PASS1, "unterminated %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001012 } else {
1013 /* %! without string or identifier */
1014 type = TOK_OTHER; /* Legacy behavior... */
1015 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001016 } else if (nasm_isidchar(*p) ||
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 ((*p == '!' || *p == '%' || *p == '$') &&
H. Peter Anvin13506202018-11-28 14:55:58 -08001018 nasm_isidchar(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 do {
1020 p++;
1021 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001022 while (nasm_isidchar(*p));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 type = TOK_PREPROC_ID;
1024 } else {
1025 type = TOK_OTHER;
1026 if (*p == '%')
1027 p++;
1028 }
H. Peter Anvin13506202018-11-28 14:55:58 -08001029 } else if (nasm_isidstart(*p) || (*p == '$' && nasm_isidstart(p[1]))) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001030 type = TOK_ID;
1031 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001032 while (*p && nasm_isidchar(*p))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 p++;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001034 } else if (nasm_isquote(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 /*
1036 * A string token.
1037 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 type = TOK_STRING;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001039 p = nasm_skip_string(p);
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 if (*p) {
1042 p++;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001043 } else {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001044 nasm_warn(WARN_OTHER, "unterminated string");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 /* Handling unterminated strings by UNV */
1046 /* type = -1; */
1047 }
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001048 } else if (p[0] == '$' && p[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001049 type = TOK_OTHER; /* TOKEN_BASE */
Victor van den Elzenfb5f2512009-04-17 16:17:59 +02001050 p += 2;
H. Peter Anvin13506202018-11-28 14:55:58 -08001051 } else if (nasm_isnumstart(*p)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001052 bool is_hex = false;
1053 bool is_float = false;
1054 bool has_e = false;
1055 char c, *r;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 /*
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001058 * A numeric token.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001060
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001061 if (*p == '$') {
1062 p++;
1063 is_hex = true;
1064 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001065
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001066 for (;;) {
1067 c = *p++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001068
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001069 if (!is_hex && (c == 'e' || c == 'E')) {
1070 has_e = true;
1071 if (*p == '+' || *p == '-') {
1072 /*
1073 * e can only be followed by +/- if it is either a
1074 * prefixed hex number or a floating-point number
1075 */
1076 p++;
1077 is_float = true;
1078 }
1079 } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
1080 is_hex = true;
1081 } else if (c == 'P' || c == 'p') {
1082 is_float = true;
1083 if (*p == '+' || *p == '-')
1084 p++;
H. Peter Anvin13506202018-11-28 14:55:58 -08001085 } else if (nasm_isnumchar(c))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001086 ; /* just advance */
1087 else if (c == '.') {
1088 /*
1089 * we need to deal with consequences of the legacy
1090 * parser, like "1.nolist" being two tokens
1091 * (TOK_NUMBER, TOK_ID) here; at least give it
1092 * a shot for now. In the future, we probably need
1093 * a flex-based scanner with proper pattern matching
1094 * to do it as well as it can be done. Nothing in
1095 * the world is going to help the person who wants
1096 * 0x123.p16 interpreted as two tokens, though.
1097 */
1098 r = p;
1099 while (*r == '_')
1100 r++;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001101
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001102 if (nasm_isdigit(*r) || (is_hex && nasm_isxdigit(*r)) ||
1103 (!is_hex && (*r == 'e' || *r == 'E')) ||
1104 (*r == 'p' || *r == 'P')) {
1105 p = r;
1106 is_float = true;
1107 } else
1108 break; /* Terminate the token */
1109 } else
1110 break;
1111 }
1112 p--; /* Point to first character beyond number */
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001113
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001114 if (p == line+1 && *line == '$') {
1115 type = TOK_OTHER; /* TOKEN_HERE */
1116 } else {
1117 if (has_e && !is_hex) {
1118 /* 1e13 is floating-point, but 1e13h is not */
1119 is_float = true;
1120 }
H. Peter Anvind784a082009-04-20 14:01:18 -07001121
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001122 type = is_float ? TOK_FLOAT : TOK_NUMBER;
1123 }
H. Peter Anvinbda7a6e2008-06-21 10:23:17 -07001124 } else if (nasm_isspace(*p)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 type = TOK_WHITESPACE;
Cyrill Gorcunovf66ac7d2009-10-12 20:41:13 +04001126 p = nasm_skip_spaces(p);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 /*
1128 * Whitespace just before end-of-line is discarded by
1129 * pretending it's a comment; whitespace just before a
1130 * comment gets lumped into the comment.
1131 */
1132 if (!*p || *p == ';') {
1133 type = TOK_COMMENT;
1134 while (*p)
1135 p++;
1136 }
1137 } else if (*p == ';') {
1138 type = TOK_COMMENT;
1139 while (*p)
1140 p++;
1141 } else {
1142 /*
1143 * Anything else is an operator of some kind. We check
1144 * for all the double-character operators (>>, <<, //,
1145 * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001146 * else is a single-character operator.
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 */
1148 type = TOK_OTHER;
1149 if ((p[0] == '>' && p[1] == '>') ||
1150 (p[0] == '<' && p[1] == '<') ||
1151 (p[0] == '/' && p[1] == '/') ||
1152 (p[0] == '<' && p[1] == '=') ||
1153 (p[0] == '>' && p[1] == '=') ||
1154 (p[0] == '=' && p[1] == '=') ||
1155 (p[0] == '!' && p[1] == '=') ||
1156 (p[0] == '<' && p[1] == '>') ||
1157 (p[0] == '&' && p[1] == '&') ||
1158 (p[0] == '|' && p[1] == '|') ||
1159 (p[0] == '^' && p[1] == '^')) {
1160 p++;
1161 }
1162 p++;
1163 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001164
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 /* Handling unterminated string by UNV */
1166 /*if (type == -1)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001167 {
1168 *tail = t = new_Token(NULL, TOK_STRING, line, p-line+1);
1169 t->text[p-line] = *line;
1170 tail = &t->next;
1171 }
1172 else */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 if (type != TOK_COMMENT) {
1174 *tail = t = new_Token(NULL, type, line, p - line);
1175 tail = &t->next;
1176 }
1177 line = p;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001178 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001179 return list;
1180}
1181
H. Peter Anvince616072002-04-30 21:02:23 +00001182/*
1183 * this function allocates a new managed block of memory and
H. Peter Anvin70653092007-10-19 14:42:29 -07001184 * returns a pointer to the block. The managed blocks are
H. Peter Anvince616072002-04-30 21:02:23 +00001185 * deleted only all at once by the delete_Blocks function.
1186 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187static void *new_Block(size_t size)
H. Peter Anvince616072002-04-30 21:02:23 +00001188{
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001189 Blocks *b = &blocks;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001191 /* first, get to the end of the linked list */
1192 while (b->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 b = b->next;
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001194 /* now allocate the requested chunk */
1195 b->chunk = nasm_malloc(size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001197 /* now allocate a new block for the next request */
Cyrill Gorcunovc31767c2014-06-28 02:22:17 +04001198 b->next = nasm_zalloc(sizeof(Blocks));
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001199 return b->chunk;
H. Peter Anvince616072002-04-30 21:02:23 +00001200}
1201
1202/*
1203 * this function deletes all managed blocks of memory
1204 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205static void delete_Blocks(void)
H. Peter Anvince616072002-04-30 21:02:23 +00001206{
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 Blocks *a, *b = &blocks;
H. Peter Anvince616072002-04-30 21:02:23 +00001208
H. Peter Anvin70653092007-10-19 14:42:29 -07001209 /*
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001210 * keep in mind that the first block, pointed to by blocks
H. Peter Anvin70653092007-10-19 14:42:29 -07001211 * is a static and not dynamically allocated, so we don't
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001212 * free it.
1213 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 while (b) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001215 if (b->chunk)
1216 nasm_free(b->chunk);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 a = b;
1218 b = b->next;
1219 if (a != &blocks)
1220 nasm_free(a);
Nickolay Yurchenkof7956c42003-09-26 19:03:40 +00001221 }
Cyrill Gorcunovdae24d72014-06-28 10:17:39 +04001222 memset(&blocks, 0, sizeof(blocks));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223}
H. Peter Anvin734b1882002-04-30 21:01:08 +00001224
1225/*
H. Peter Anvin70653092007-10-19 14:42:29 -07001226 * this function creates a new Token and passes a pointer to it
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001227 * back to the caller. It sets the type, text, and next pointer elements.
H. Peter Anvin734b1882002-04-30 21:01:08 +00001228 */
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001229static Token *new_Token(Token * next, enum pp_token_type type,
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001230 const char *text, size_t txtlen)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001231{
1232 Token *t;
1233 int i;
1234
H. Peter Anvin89cee572009-07-15 09:16:54 -04001235 if (!freeTokens) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 freeTokens = (Token *) new_Block(TOKEN_BLOCKSIZE * sizeof(Token));
1237 for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++)
1238 freeTokens[i].next = &freeTokens[i + 1];
1239 freeTokens[i].next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001240 }
1241 t = freeTokens;
1242 freeTokens = t->next;
1243 t->next = next;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001244 t->type = type;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001245 if (type == TOK_WHITESPACE || !text) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001246 t->len = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 t->text = NULL;
1248 } else {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001249 if (txtlen == 0 && text[0])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 txtlen = strlen(text);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001251 t->len = txtlen;
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001252 t->text = nasm_malloc(txtlen+1);
1253 memcpy(t->text, text, txtlen);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 t->text[txtlen] = '\0';
H. Peter Anvin734b1882002-04-30 21:01:08 +00001255 }
1256 return t;
1257}
1258
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07001259static Token *dup_Token(Token *next, const Token *src)
1260{
1261 return new_Token(next, src->type, src->text, src->len);
1262}
1263
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264static Token *delete_Token(Token * t)
H. Peter Anvin734b1882002-04-30 21:01:08 +00001265{
1266 Token *next = t->next;
1267 nasm_free(t->text);
H. Peter Anvin788e6c12002-04-30 21:02:01 +00001268 t->next = freeTokens;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001269 freeTokens = t;
1270 return next;
1271}
1272
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001273/*
1274 * Convert a line of tokens back into text.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001275 * If expand_locals is not zero, identifiers of the form "%$*xxx"
1276 * will be transformed into ..@ctxnum.xxx
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001277 */
H. Peter Anvin9e200162008-06-04 17:23:14 -07001278static char *detoken(Token * tlist, bool expand_locals)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001279{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001280 Token *t;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001281 char *line, *p;
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001282 const char *q;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001283 int len = 0;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001284
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001285 list_for_each(t, tlist) {
Cyrill Gorcunov9b7ee092017-10-22 21:42:59 +03001286 if (t->type == TOK_PREPROC_ID && t->text &&
1287 t->text[0] && t->text[1] == '!') {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001288 char *v;
1289 char *q = t->text;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001290
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001291 v = t->text + 2;
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001292 if (nasm_isquote(*v)) {
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001293 size_t len = nasm_unquote(v, NULL);
1294 size_t clen = strlen(v);
H. Peter Anvin077fb932010-07-20 14:56:30 -07001295
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001296 if (len != clen) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001297 nasm_nonfatalf(ERR_PASS1, "NUL character in %%! string");
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001298 v = NULL;
1299 }
1300 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001301
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001302 if (v) {
1303 char *p = getenv(v);
1304 if (!p) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001305 nasm_nonfatalf(ERR_PASS1, "nonexistent environment variable `%s'", v);
Cyrill Gorcunovbbb7a1a2016-06-19 12:15:24 +03001306 /*
1307 * FIXME We better should investigate if accessing
1308 * ->text[1] without ->text[0] is safe enough.
1309 */
1310 t->text = nasm_zalloc(2);
1311 } else
1312 t->text = nasm_strdup(p);
Cyrill Gorcunov75004872017-07-26 01:21:16 +03001313 nasm_free(q);
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001314 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 }
H. Peter Anvin077fb932010-07-20 14:56:30 -07001316
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 /* Expand local macros here and not during preprocessing */
1318 if (expand_locals &&
1319 t->type == TOK_PREPROC_ID && t->text &&
1320 t->text[0] == '%' && t->text[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001321 const char *q;
1322 char *p;
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001323 Context *ctx = get_ctx(t->text, &q);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 if (ctx) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00001325 char buffer[40];
Keith Kanios93f2e9a2007-04-14 00:10:59 +00001326 snprintf(buffer, sizeof(buffer), "..@%"PRIu32".", ctx->number);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 p = nasm_strcat(buffer, q);
1328 nasm_free(t->text);
1329 t->text = p;
1330 }
1331 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001332 if (t->type == TOK_WHITESPACE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001333 len++;
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001334 else if (t->text)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 len += strlen(t->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001336 }
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001337
H. Peter Anvin734b1882002-04-30 21:01:08 +00001338 p = line = nasm_malloc(len + 1);
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001339
1340 list_for_each(t, tlist) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 if (t->type == TOK_WHITESPACE) {
H. Peter Anvinb4daadc2008-01-21 16:31:57 -08001342 *p++ = ' ';
H. Peter Anvine2c80182005-01-15 22:15:51 +00001343 } else if (t->text) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001344 q = t->text;
1345 while (*q)
1346 *p++ = *q++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001348 }
1349 *p = '\0';
Cyrill Gorcunovf32ed142010-04-09 15:41:48 +04001350
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001351 return line;
1352}
1353
1354/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001355 * A scanner, suitable for use by the expression evaluator, which
1356 * operates on a line of Tokens. Expects a pointer to a pointer to
1357 * the first token in the line to be passed in as its private_data
1358 * field.
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001359 *
1360 * FIX: This really needs to be unified with stdscan.
H. Peter Anvin76690a12002-04-30 20:52:49 +00001361 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08001362struct ppscan {
1363 Token *tptr;
1364 int ntokens;
1365};
1366
H. Peter Anvine2c80182005-01-15 22:15:51 +00001367static int ppscan(void *private_data, struct tokenval *tokval)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001368{
H. Peter Anvin8b262472019-02-26 14:00:54 -08001369 struct ppscan *pps = private_data;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001370 Token *tline;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001371 char ourcopy[MAX_KEYWORD+1], *p, *r, *s;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001372
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 do {
H. Peter Anvin8b262472019-02-26 14:00:54 -08001374 if (pps->ntokens && (tline = pps->tptr)) {
1375 pps->ntokens--;
1376 pps->tptr = tline->next;
1377 } else {
1378 pps->tptr = NULL;
1379 pps->ntokens = 0;
1380 return tokval->t_type = TOKEN_EOS;
1381 }
1382 } while (tline->type == TOK_WHITESPACE || tline->type == TOK_COMMENT);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001383
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001384 tokval->t_charptr = tline->text;
1385
H. Peter Anvin76690a12002-04-30 20:52:49 +00001386 if (tline->text[0] == '$' && !tline->text[1])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001387 return tokval->t_type = TOKEN_HERE;
H. Peter Anvin7cf897e2002-05-30 21:30:33 +00001388 if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2])
H. Peter Anvine2c80182005-01-15 22:15:51 +00001389 return tokval->t_type = TOKEN_BASE;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001390
H. Peter Anvine2c80182005-01-15 22:15:51 +00001391 if (tline->type == TOK_ID) {
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001392 p = tokval->t_charptr = tline->text;
1393 if (p[0] == '$') {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001394 tokval->t_charptr++;
1395 return tokval->t_type = TOKEN_ID;
1396 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001397
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001398 for (r = p, s = ourcopy; *r; r++) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001399 if (r >= p+MAX_KEYWORD)
1400 return tokval->t_type = TOKEN_ID; /* Not a keyword */
H. Peter Anvinac8f8fc2008-06-11 15:49:41 -07001401 *s++ = nasm_tolower(*r);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001402 }
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001403 *s = '\0';
1404 /* right, so we have an identifier sitting in temp storage. now,
1405 * is it actually a register or instruction name, or what? */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001406 return nasm_token_hash(ourcopy, tokval);
H. Peter Anvin76690a12002-04-30 20:52:49 +00001407 }
1408
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 if (tline->type == TOK_NUMBER) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001410 bool rn_error;
1411 tokval->t_integer = readnum(tline->text, &rn_error);
1412 tokval->t_charptr = tline->text;
1413 if (rn_error)
1414 return tokval->t_type = TOKEN_ERRNUM;
1415 else
1416 return tokval->t_type = TOKEN_NUM;
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001417 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00001418
H. Peter Anvinc2df2822007-10-24 15:29:28 -07001419 if (tline->type == TOK_FLOAT) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001420 return tokval->t_type = TOKEN_FLOAT;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001421 }
1422
H. Peter Anvine2c80182005-01-15 22:15:51 +00001423 if (tline->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001424 char bq, *ep;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001425
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001426 bq = tline->text[0];
H. Peter Anvin11627042008-06-09 20:45:19 -07001427 tokval->t_charptr = tline->text;
1428 tokval->t_inttwo = nasm_unquote(tline->text, &ep);
H. Peter Anvind2456592008-06-19 15:04:18 -07001429
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001430 if (ep[0] != bq || ep[1] != '\0')
1431 return tokval->t_type = TOKEN_ERRSTR;
1432 else
1433 return tokval->t_type = TOKEN_STR;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001434 }
1435
H. Peter Anvine2c80182005-01-15 22:15:51 +00001436 if (tline->type == TOK_OTHER) {
1437 if (!strcmp(tline->text, "<<"))
1438 return tokval->t_type = TOKEN_SHL;
1439 if (!strcmp(tline->text, ">>"))
1440 return tokval->t_type = TOKEN_SHR;
1441 if (!strcmp(tline->text, "//"))
1442 return tokval->t_type = TOKEN_SDIV;
1443 if (!strcmp(tline->text, "%%"))
1444 return tokval->t_type = TOKEN_SMOD;
1445 if (!strcmp(tline->text, "=="))
1446 return tokval->t_type = TOKEN_EQ;
1447 if (!strcmp(tline->text, "<>"))
1448 return tokval->t_type = TOKEN_NE;
1449 if (!strcmp(tline->text, "!="))
1450 return tokval->t_type = TOKEN_NE;
1451 if (!strcmp(tline->text, "<="))
1452 return tokval->t_type = TOKEN_LE;
1453 if (!strcmp(tline->text, ">="))
1454 return tokval->t_type = TOKEN_GE;
1455 if (!strcmp(tline->text, "&&"))
1456 return tokval->t_type = TOKEN_DBL_AND;
1457 if (!strcmp(tline->text, "^^"))
1458 return tokval->t_type = TOKEN_DBL_XOR;
1459 if (!strcmp(tline->text, "||"))
1460 return tokval->t_type = TOKEN_DBL_OR;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001461 }
1462
1463 /*
1464 * We have no other options: just return the first character of
1465 * the token text.
1466 */
1467 return tokval->t_type = tline->text[0];
1468}
1469
1470/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001471 * Compare a string to the name of an existing macro; this is a
1472 * simple wrapper which calls either strcmp or nasm_stricmp
1473 * depending on the value of the `casesense' parameter.
1474 */
H. Peter Anvin4db5a162007-10-11 13:42:09 -07001475static int mstrcmp(const char *p, const char *q, bool casesense)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001476{
H. Peter Anvin734b1882002-04-30 21:01:08 +00001477 return casesense ? strcmp(p, q) : nasm_stricmp(p, q);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001478}
1479
1480/*
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001481 * Compare a string to the name of an existing macro; this is a
1482 * simple wrapper which calls either strcmp or nasm_stricmp
1483 * depending on the value of the `casesense' parameter.
1484 */
1485static int mmemcmp(const char *p, const char *q, size_t l, bool casesense)
1486{
1487 return casesense ? memcmp(p, q, l) : nasm_memicmp(p, q, l);
1488}
1489
1490/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001491 * Return the Context structure associated with a %$ token. Return
1492 * NULL, having _already_ reported an error condition, if the
1493 * context stack isn't deep enough for the supplied number of $
1494 * signs.
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001495 *
1496 * If "namep" is non-NULL, set it to the pointer to the macro name
1497 * tail, i.e. the part beyond %$...
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001498 */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001499static Context *get_ctx(const char *name, const char **namep)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001500{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001501 Context *ctx;
1502 int i;
1503
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001504 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001505 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001506
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001507 if (!name || name[0] != '%' || name[1] != '$')
H. Peter Anvine2c80182005-01-15 22:15:51 +00001508 return NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001509
H. Peter Anvine2c80182005-01-15 22:15:51 +00001510 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001511 nasm_nonfatal("`%s': context stack is empty", name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001512 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001513 }
1514
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001515 name += 2;
1516 ctx = cstk;
1517 i = 0;
1518 while (ctx && *name == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001519 name++;
1520 i++;
1521 ctx = ctx->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001522 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 if (!ctx) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001524 nasm_nonfatal("`%s': context stack is only"
1525 " %d level%s deep", name, i, (i == 1 ? "" : "s"));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 return NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001527 }
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001528
1529 if (namep)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001530 *namep = name;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08001531
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001532 return ctx;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001533}
1534
1535/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001536 * Open an include file. This routine must always return a valid
1537 * file pointer if it returns - it's responsible for throwing an
1538 * ERR_FATAL and bombing out completely if not. It should also try
1539 * the include path one by one until it finds the file or reaches
1540 * the end of the path.
H. Peter Anvind81a2352016-09-21 14:03:18 -07001541 *
1542 * Note: for INC_PROBE the function returns NULL at all times;
1543 * instead look for the
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001544 */
H. Peter Anvind81a2352016-09-21 14:03:18 -07001545enum incopen_mode {
1546 INC_NEEDED, /* File must exist */
1547 INC_OPTIONAL, /* Missing is OK */
1548 INC_PROBE /* Only an existence probe */
1549};
1550
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001551/* This is conducts a full pathname search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001552static FILE *inc_fopen_search(const char *file, char **slpath,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001553 enum incopen_mode omode, enum file_flags fmode)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001554{
H. Peter Anvin (Intel)64471092018-12-11 13:06:14 -08001555 const struct strlist_entry *ip = strlist_head(ipath_list);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001556 FILE *fp;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001557 const char *prefix = "";
night199ukfdb1a1b2018-10-18 23:19:47 +02001558 char *sp;
H. Peter Anvind81a2352016-09-21 14:03:18 -07001559 bool found;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001560
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 while (1) {
night199ukfdb1a1b2018-10-18 23:19:47 +02001562 sp = nasm_catfile(prefix, file);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001563 if (omode == INC_PROBE) {
1564 fp = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001565 found = nasm_file_exists(sp);
H. Peter Anvin9e1f5282008-05-29 21:38:00 -07001566 } else {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001567 fp = nasm_open_read(sp, fmode);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001568 found = (fp != NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001569 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001570 if (found) {
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001571 *slpath = sp;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 return fp;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001573 }
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001574
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001575 nasm_free(sp);
Jim Kukunas65a8afc2016-06-13 16:00:42 -04001576
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001577 if (!ip) {
1578 *slpath = NULL;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001579 return NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001580 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001581
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001582 prefix = ip->str;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001583 ip = ip->next;
1584 }
1585}
1586
1587/*
1588 * Open a file, or test for the presence of one (depending on omode),
1589 * considering the include path.
1590 */
1591static FILE *inc_fopen(const char *file,
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001592 struct strlist *dhead,
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001593 const char **found_path,
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001594 enum incopen_mode omode,
1595 enum file_flags fmode)
1596{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001597 struct hash_insert hi;
1598 void **hp;
1599 char *path;
1600 FILE *fp = NULL;
1601
1602 hp = hash_find(&FileHash, file, &hi);
1603 if (hp) {
1604 path = *hp;
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001605 if (path || omode != INC_NEEDED) {
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001606 strlist_add(dhead, path ? path : file);
Martin Storsjöf283c8f2017-08-13 17:28:46 +03001607 }
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001608 } else {
1609 /* Need to do the actual path search */
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001610 fp = inc_fopen_search(file, &path, omode, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001611
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001612 /* Positive or negative result */
1613 hash_add(&hi, nasm_strdup(file), path);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001614
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001615 /*
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001616 * Add file to dependency path.
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001617 */
1618 if (path || omode != INC_NEEDED)
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03001619 strlist_add(dhead, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00001620 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001621
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001622 if (!path) {
1623 if (omode == INC_NEEDED)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001624 nasm_fatal("unable to open include file `%s'", file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07001625 } else {
1626 if (!fp && omode != INC_PROBE)
1627 fp = nasm_open_read(path, fmode);
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001628 }
1629
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001630 if (found_path)
H. Peter Anvinccad6f92016-10-04 00:34:35 -07001631 *found_path = path;
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07001632
1633 return fp;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00001634}
1635
1636/*
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001637 * Opens an include or input file. Public version, for use by modules
1638 * that get a file:lineno pair and need to look at the file again
1639 * (e.g. the CodeView debug backend). Returns NULL on failure.
1640 */
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07001641FILE *pp_input_fopen(const char *filename, enum file_flags mode)
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001642{
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07001643 return inc_fopen(filename, NULL, NULL, INC_OPTIONAL, mode);
Fabian Giesen0bbc38d2016-04-28 13:48:14 -07001644}
1645
1646/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001647 * Determine if we should warn on defining a single-line macro of
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001648 * name `name', with `nparam' parameters. If nparam is 0 or -1, will
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001649 * return true if _any_ single-line macro of that name is defined.
1650 * Otherwise, will return true if a single-line macro with either
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001651 * `nparam' or no parameters is defined.
1652 *
1653 * If a macro with precisely the right number of parameters is
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001654 * defined, or nparam is -1, the address of the definition structure
1655 * will be returned in `defn'; otherwise NULL will be returned. If `defn'
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001656 * is NULL, no action will be taken regarding its contents, and no
1657 * error will occur.
1658 *
1659 * Note that this is also called with nparam zero to resolve
1660 * `ifdef'.
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001661 *
1662 * If you already know which context macro belongs to, you can pass
1663 * the context pointer as first parameter; if you won't but name begins
1664 * with %$ the context will be automatically computed. If all_contexts
1665 * is true, macro will be searched in outer contexts as well.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001666 */
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001667static bool
H. Peter Anvinb2a5fda2008-06-19 21:42:42 -07001668smacro_defined(Context * ctx, const char *name, int nparam, SMacro ** defn,
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001669 bool nocase)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001670{
H. Peter Anvin166c2472008-05-28 12:28:58 -07001671 struct hash_table *smtbl;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001672 SMacro *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001673
H. Peter Anvin97a23472007-09-16 17:57:25 -07001674 if (ctx) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001675 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001676 } else if (name[0] == '%' && name[1] == '$') {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001677 if (cstk)
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04001678 ctx = get_ctx(name, &name);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001679 if (!ctx)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001680 return false; /* got to return _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001681 smtbl = &ctx->localmac;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001682 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001683 smtbl = &smacros;
H. Peter Anvin97a23472007-09-16 17:57:25 -07001684 }
H. Peter Anvin166c2472008-05-28 12:28:58 -07001685 m = (SMacro *) hash_findix(smtbl, name);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001686
H. Peter Anvine2c80182005-01-15 22:15:51 +00001687 while (m) {
1688 if (!mstrcmp(m->name, name, m->casesense && nocase) &&
Charles Crayne192d5b52007-10-18 19:02:42 -07001689 (nparam <= 0 || m->nparam == 0 || nparam == (int) m->nparam)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001690 if (defn) {
Charles Crayne192d5b52007-10-18 19:02:42 -07001691 if (nparam == (int) m->nparam || nparam == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001692 *defn = m;
1693 else
1694 *defn = NULL;
1695 }
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001696 return true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001697 }
1698 m = m->next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001699 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001700
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001701 return false;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001702}
1703
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001704/* param should be a natural number [0; INT_MAX] */
1705static int read_param_count(const char *str)
1706{
1707 int result;
1708 bool err;
1709
1710 result = readnum(str, &err);
1711 if (result < 0 || result > INT_MAX) {
1712 result = 0;
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001713 nasm_nonfatal("parameter count `%s' is out of bounds [%d; %d]",
1714 str, 0, INT_MAX);
1715 } else if (err)
1716 nasm_nonfatal("unable to parse parameter count `%s'", str);
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001717 return result;
1718}
1719
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001720/*
1721 * Count and mark off the parameters in a multi-line macro call.
1722 * This is called both from within the multi-line macro expansion
1723 * code, and also to mark off the default parameters when provided
1724 * in a %macro definition line.
1725 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001726static void count_mmac_params(Token * t, int *nparam, Token *** params)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001727{
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001728 int paramsize, brace;
1729
1730 *nparam = paramsize = 0;
1731 *params = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 while (t) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001733 /* +1: we need space for the final NULL */
H. Peter Anvin91fb6f12008-09-01 10:56:33 -07001734 if (*nparam+1 >= paramsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 paramsize += PARAM_DELTA;
1736 *params = nasm_realloc(*params, sizeof(**params) * paramsize);
1737 }
1738 skip_white_(t);
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001739 brace = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001741 brace++;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 (*params)[(*nparam)++] = t;
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001743 if (brace) {
1744 while (brace && (t = t->next) != NULL) {
1745 if (tok_is_(t, "{"))
1746 brace++;
1747 else if (tok_is_(t, "}"))
1748 brace--;
1749 }
1750
1751 if (t) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 /*
1753 * Now we've found the closing brace, look further
1754 * for the comma.
1755 */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001756 t = t->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001757 skip_white_(t);
1758 if (tok_isnt_(t, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001759 nasm_nonfatal("braces do not enclose all of macro parameter");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 while (tok_isnt_(t, ","))
1761 t = t->next;
1762 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08001764 } else {
1765 while (tok_isnt_(t, ","))
1766 t = t->next;
1767 }
1768 if (t) { /* got a comma/brace */
1769 t = t->next; /* eat the comma */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001771 }
1772}
1773
1774/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00001775 * Determine whether one of the various `if' conditions is true or
1776 * not.
1777 *
1778 * We must free the tline we get passed.
1779 */
H. Peter Anvin70055962007-10-11 00:05:31 -07001780static bool if_condition(Token * tline, enum preproc_token ct)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001781{
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001782 enum pp_conditional i = PP_COND(ct);
H. Peter Anvin70055962007-10-11 00:05:31 -07001783 bool j;
H. Peter Anvin8b262472019-02-26 14:00:54 -08001784 Token *t, *tt, *origline;
1785 struct ppscan pps;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001786 struct tokenval tokval;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001787 expr *evalresult;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001788 enum pp_token_type needtype;
H. Peter Anvin077fb932010-07-20 14:56:30 -07001789 char *p;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001790
1791 origline = tline;
1792
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 switch (i) {
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001794 case PPC_IFCTX:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001795 j = false; /* have we matched yet? */
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001796 while (true) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 skip_white_(tline);
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001798 if (!tline)
1799 break;
1800 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001801 nasm_nonfatal("`%s' expects context identifiers",
1802 pp_directives[ct]);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 free_tlist(origline);
1804 return -1;
1805 }
Victor van den Elzen0e857f12008-07-23 13:21:29 +02001806 if (cstk && cstk->name && !nasm_stricmp(tline->text, cstk->name))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001807 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 tline = tline->next;
1809 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001810 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001811
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001812 case PPC_IFDEF:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001813 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001814 while (tline) {
1815 skip_white_(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 if (!tline || (tline->type != TOK_ID &&
1817 (tline->type != TOK_PREPROC_ID ||
1818 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001819 nasm_nonfatal("`%s' expects macro identifiers",
1820 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001821 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 }
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07001823 if (smacro_defined(NULL, tline->text, 0, NULL, true))
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001824 j = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001826 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001827 break;
H. Peter Anvin734b1882002-04-30 21:01:08 +00001828
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001829 case PPC_IFENV:
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001830 tline = expand_smacro(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001831 j = false; /* have we matched yet? */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001832 while (tline) {
1833 skip_white_(tline);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001834 if (!tline || (tline->type != TOK_ID &&
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001835 tline->type != TOK_STRING &&
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001836 (tline->type != TOK_PREPROC_ID ||
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001837 tline->text[1] != '!'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001838 nasm_nonfatal("`%s' expects environment variable names",
1839 pp_directives[ct]);
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001840 goto fail;
1841 }
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001842 p = tline->text;
1843 if (tline->type == TOK_PREPROC_ID)
1844 p += 2; /* Skip leading %! */
H. Peter Anvin53e2e4c2018-11-28 15:01:40 -08001845 if (nasm_isquote(*p))
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04001846 nasm_unquote_cstr(p, ct);
1847 if (getenv(p))
1848 j = true;
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001849 tline = tline->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001850 }
H. Peter Anvin6d9b2b52010-07-13 12:00:58 -07001851 break;
1852
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001853 case PPC_IFIDN:
1854 case PPC_IFIDNI:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 tline = expand_smacro(tline);
1856 t = tt = tline;
1857 while (tok_isnt_(tt, ","))
1858 tt = tt->next;
1859 if (!tt) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001860 nasm_nonfatal("`%s' expects two comma-separated arguments",
1861 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001862 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 }
1864 tt = tt->next;
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001865 j = true; /* assume equality unless proved not */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) {
1867 if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001868 nasm_nonfatal("`%s': more than one comma on line",
1869 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001870 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 }
1872 if (t->type == TOK_WHITESPACE) {
1873 t = t->next;
1874 continue;
1875 }
1876 if (tt->type == TOK_WHITESPACE) {
1877 tt = tt->next;
1878 continue;
1879 }
1880 if (tt->type != t->type) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001881 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 break;
1883 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001884 /* When comparing strings, need to unquote them first */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 if (t->type == TOK_STRING) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001886 size_t l1 = nasm_unquote(t->text, NULL);
1887 size_t l2 = nasm_unquote(tt->text, NULL);
H. Peter Anvind2456592008-06-19 15:04:18 -07001888
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001889 if (l1 != l2) {
1890 j = false;
1891 break;
1892 }
1893 if (mmemcmp(t->text, tt->text, l1, i == PPC_IFIDN)) {
1894 j = false;
1895 break;
1896 }
H. Peter Anvin6ecc1592008-06-01 21:34:49 -07001897 } else if (mstrcmp(tt->text, t->text, i == PPC_IFIDN) != 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001898 j = false; /* found mismatching tokens */
H. Peter Anvine2c80182005-01-15 22:15:51 +00001899 break;
1900 }
Nickolay Yurchenkof3b3ce22003-09-21 20:38:43 +00001901
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902 t = t->next;
1903 tt = tt->next;
1904 }
1905 if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt)
H. Peter Anvin6867acc2007-10-10 14:58:45 -07001906 j = false; /* trailing gunk on one end or other */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001907 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00001908
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001909 case PPC_IFMACRO:
H. Peter Anvin89cee572009-07-15 09:16:54 -04001910 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001911 bool found = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001912 MMacro searching, *mmac;
H. Peter Anvin65747262002-05-07 00:10:05 +00001913
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001914 skip_white_(tline);
1915 tline = expand_id(tline);
1916 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001917 nasm_nonfatal("`%s' expects a macro name", pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001918 goto fail;
1919 }
1920 searching.name = nasm_strdup(tline->text);
1921 searching.casesense = true;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001922 searching.plus = false;
1923 searching.nolist = false;
1924 searching.in_progress = 0;
1925 searching.max_depth = 0;
1926 searching.rep_nest = NULL;
1927 searching.nparam_min = 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001928 searching.nparam_max = INT_MAX;
1929 tline = expand_smacro(tline->next);
1930 skip_white_(tline);
1931 if (!tline) {
1932 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001933 nasm_nonfatal("`%s' expects a parameter count or nothing",
1934 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001935 } else {
1936 searching.nparam_min = searching.nparam_max =
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001937 read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001938 }
1939 if (tline && tok_is_(tline->next, "-")) {
1940 tline = tline->next->next;
1941 if (tok_is_(tline, "*"))
1942 searching.nparam_max = INT_MAX;
1943 else if (!tok_type_(tline, TOK_NUMBER))
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001944 nasm_nonfatal("`%s' expects a parameter count after `-'",
1945 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001946 else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03001947 searching.nparam_max = read_param_count(tline->text);
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001948 if (searching.nparam_min > searching.nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03001949 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03001950 searching.nparam_max = searching.nparam_min;
1951 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001952 }
1953 }
1954 if (tline && tok_is_(tline->next, "+")) {
1955 tline = tline->next;
1956 searching.plus = true;
1957 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001958 mmac = (MMacro *) hash_findix(&mmacros, searching.name);
1959 while (mmac) {
1960 if (!strcmp(mmac->name, searching.name) &&
1961 (mmac->nparam_min <= searching.nparam_max
1962 || searching.plus)
1963 && (searching.nparam_min <= mmac->nparam_max
1964 || mmac->plus)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001965 found = true;
1966 break;
1967 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08001968 mmac = mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001969 }
1970 if (tline && tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001971 nasm_warn(WARN_OTHER, "trailing garbage after %%ifmacro ignored");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001972 nasm_free(searching.name);
1973 j = found;
1974 break;
H. Peter Anvin89cee572009-07-15 09:16:54 -04001975 }
H. Peter Anvin65747262002-05-07 00:10:05 +00001976
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001977 case PPC_IFID:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001978 needtype = TOK_ID;
1979 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001980 case PPC_IFNUM:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001981 needtype = TOK_NUMBER;
1982 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001983 case PPC_IFSTR:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001984 needtype = TOK_STRING;
1985 goto iftype;
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00001986
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001987iftype:
1988 t = tline = expand_smacro(tline);
H. Peter Anvind85d2502008-05-04 17:53:31 -07001989
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001990 while (tok_type_(t, TOK_WHITESPACE) ||
1991 (needtype == TOK_NUMBER &&
1992 tok_type_(t, TOK_OTHER) &&
1993 (t->text[0] == '-' || t->text[0] == '+') &&
1994 !t->text[1]))
1995 t = t->next;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001996
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03001997 j = tok_type_(t, needtype);
1998 break;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08001999
2000 case PPC_IFTOKEN:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002001 t = tline = expand_smacro(tline);
2002 while (tok_type_(t, TOK_WHITESPACE))
2003 t = t->next;
H. Peter Anvincbf768d2008-02-16 16:41:25 -08002004
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002005 j = false;
2006 if (t) {
2007 t = t->next; /* Skip the actual token */
2008 while (tok_type_(t, TOK_WHITESPACE))
2009 t = t->next;
2010 j = !t; /* Should be nothing left */
2011 }
2012 break;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002013
H. Peter Anvin134b9462008-02-16 17:01:40 -08002014 case PPC_IFEMPTY:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002015 t = tline = expand_smacro(tline);
2016 while (tok_type_(t, TOK_WHITESPACE))
2017 t = t->next;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002018
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002019 j = !t; /* Should be empty */
2020 break;
H. Peter Anvin134b9462008-02-16 17:01:40 -08002021
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002022 case PPC_IF:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002023 pps.tptr = tline = expand_smacro(tline);
2024 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002026 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 if (!evalresult)
2028 return -1;
2029 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002030 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002032 nasm_nonfatal("non-constant value given to `%s'",
2033 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002034 goto fail;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002035 }
Chuck Crayne60ae75d2007-05-02 01:59:16 +00002036 j = reloc_value(evalresult) != 0;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002037 break;
H. Peter Anvin95e28822007-09-12 04:20:08 +00002038
H. Peter Anvine2c80182005-01-15 22:15:51 +00002039 default:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002040 nasm_fatal("preprocessor directive `%s' not yet implemented",
2041 pp_directives[ct]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002042 goto fail;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002043 }
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002044
2045 free_tlist(origline);
2046 return j ^ PP_NEGATIVE(ct);
H. Peter Anvin70653092007-10-19 14:42:29 -07002047
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002048fail:
2049 free_tlist(origline);
2050 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002051}
2052
2053/*
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002054 * Common code for defining an smacro
2055 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08002056static SMacro *define_smacro(Context *ctx, const char *mname,
2057 bool casesense, int nparam, Token *expansion)
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002058{
2059 SMacro *smac, **smhead;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002060 struct hash_table *smtbl;
H. Peter Anvin70653092007-10-19 14:42:29 -07002061
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002062 if (smacro_defined(ctx, mname, nparam, &smac, casesense)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002063 if (!smac) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002064 nasm_warn(WARN_OTHER, "single-line macro `%s' defined both with and"
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002065 " without parameters", mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002066 /*
2067 * Some instances of the old code considered this a failure,
2068 * some others didn't. What is the right thing to do here?
2069 */
2070 free_tlist(expansion);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002071 return NULL; /* Failure */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002072 } else {
2073 /*
2074 * We're redefining, so we have to take over an
2075 * existing SMacro structure. This means freeing
H. Peter Anvin8b262472019-02-26 14:00:54 -08002076 * what was already in it, but not the structure itself.
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002077 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002078 clear_smacro(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002079 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002080 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002081 smtbl = ctx ? &ctx->localmac : &smacros;
2082 smhead = (SMacro **) hash_findi_add(smtbl, mname);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002083 nasm_new(smac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002084 smac->next = *smhead;
2085 *smhead = smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002086 }
2087 smac->name = nasm_strdup(mname);
2088 smac->casesense = casesense;
2089 smac->nparam = nparam;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002090 smac->expansion = expansion;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002091 return smac;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002092}
2093
2094/*
2095 * Undefine an smacro
2096 */
2097static void undef_smacro(Context *ctx, const char *mname)
2098{
2099 SMacro **smhead, *s, **sp;
H. Peter Anvin166c2472008-05-28 12:28:58 -07002100 struct hash_table *smtbl;
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002101
H. Peter Anvin166c2472008-05-28 12:28:58 -07002102 smtbl = ctx ? &ctx->localmac : &smacros;
2103 smhead = (SMacro **)hash_findi(smtbl, mname, NULL);
H. Peter Anvin70653092007-10-19 14:42:29 -07002104
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002105 if (smhead) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002106 /*
2107 * We now have a macro name... go hunt for it.
2108 */
2109 sp = smhead;
2110 while ((s = *sp) != NULL) {
2111 if (!mstrcmp(s->name, mname, s->casesense)) {
2112 *sp = s->next;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07002113 free_smacro(s);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002114 } else {
2115 sp = &s->next;
2116 }
2117 }
H. Peter Anvin4db5a162007-10-11 13:42:09 -07002118 }
2119}
2120
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002121/*
H. Peter Anvina26433d2008-07-16 14:40:01 -07002122 * Parse a mmacro specification.
2123 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002124static bool parse_mmacro_spec(Token *tline, MMacro *def, const char *directive)
H. Peter Anvina26433d2008-07-16 14:40:01 -07002125{
H. Peter Anvina26433d2008-07-16 14:40:01 -07002126 tline = tline->next;
2127 skip_white_(tline);
2128 tline = expand_id(tline);
2129 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002130 nasm_nonfatal("`%s' expects a macro name", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002131 return false;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002132 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002133
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002134 def->prev = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002135 def->name = nasm_strdup(tline->text);
2136 def->plus = false;
2137 def->nolist = false;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002138 def->in_progress = 0;
2139 def->rep_nest = NULL;
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002140 def->nparam_min = 0;
2141 def->nparam_max = 0;
2142
H. Peter Anvina26433d2008-07-16 14:40:01 -07002143 tline = expand_smacro(tline->next);
2144 skip_white_(tline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002145 if (!tok_type_(tline, TOK_NUMBER))
2146 nasm_nonfatal("`%s' expects a parameter count", directive);
2147 else
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002148 def->nparam_min = def->nparam_max = read_param_count(tline->text);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002149 if (tline && tok_is_(tline->next, "-")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002150 tline = tline->next->next;
2151 if (tok_is_(tline, "*")) {
2152 def->nparam_max = INT_MAX;
2153 } else if (!tok_type_(tline, TOK_NUMBER)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002154 nasm_nonfatal("`%s' expects a parameter count after `-'", directive);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002155 } else {
Cyrill Gorcunov3079f792018-11-14 10:03:42 +03002156 def->nparam_max = read_param_count(tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002157 if (def->nparam_min > def->nparam_max) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002158 nasm_nonfatal("minimum parameter count exceeds maximum");
Cyrill Gorcunovc9244ea2017-10-22 15:25:48 +03002159 def->nparam_max = def->nparam_min;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002160 }
2161 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002162 }
2163 if (tline && tok_is_(tline->next, "+")) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002164 tline = tline->next;
2165 def->plus = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002166 }
2167 if (tline && tok_type_(tline->next, TOK_ID) &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002168 !nasm_stricmp(tline->next->text, ".nolist")) {
2169 tline = tline->next;
2170 def->nolist = true;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002171 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002172
H. Peter Anvina26433d2008-07-16 14:40:01 -07002173 /*
2174 * Handle default parameters.
2175 */
2176 if (tline && tline->next) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002177 def->dlist = tline->next;
2178 tline->next = NULL;
2179 count_mmac_params(def->dlist, &def->ndefs, &def->defaults);
H. Peter Anvina26433d2008-07-16 14:40:01 -07002180 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002181 def->dlist = NULL;
2182 def->defaults = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002183 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002184 def->expansion = NULL;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002185
H. Peter Anvin89cee572009-07-15 09:16:54 -04002186 if (def->defaults && def->ndefs > def->nparam_max - def->nparam_min &&
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002187 !def->plus) {
2188 /*
2189 *!macro-defaults [on] macros with more default than optional parameters
2190 *! warns when a macro has more default parameters than optional parameters.
2191 *! See \k{mlmacdef} for why might want to disable this warning.
2192 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002193 nasm_warn(WARN_MACRO_DEFAULTS,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002194 "too many default macro parameters in macro `%s'", def->name);
2195 }
Victor van den Elzenb916ede2008-07-23 15:14:22 +02002196
H. Peter Anvina26433d2008-07-16 14:40:01 -07002197 return true;
2198}
2199
2200
2201/*
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002202 * Decode a size directive
2203 */
2204static int parse_size(const char *str) {
2205 static const char *size_names[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002206 { "byte", "dword", "oword", "qword", "tword", "word", "yword" };
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002207 static const int sizes[] =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002208 { 0, 1, 4, 16, 8, 10, 2, 32 };
Cyrill Gorcunovc713b5f2018-09-29 14:30:14 +03002209 return str ? sizes[bsii(str, size_names, ARRAY_SIZE(size_names))+1] : 0;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002210}
2211
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002212/*
2213 * Process a preprocessor %pragma directive. Currently there are none.
2214 * Gets passed the token list starting with the "preproc" token from
2215 * "%pragma preproc".
2216 */
2217static void do_pragma_preproc(Token *tline)
2218{
2219 /* Skip to the real stuff */
2220 tline = tline->next;
2221 skip_white_(tline);
2222 if (!tline)
2223 return;
2224
2225 (void)tline; /* Nothing else to do at present */
2226}
2227
Ed Beroset3ab3f412002-06-11 03:31:49 +00002228/**
2229 * find and process preprocessor directive in passed line
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002230 * Find out if a line contains a preprocessor directive, and deal
2231 * with it if so.
H. Peter Anvin70653092007-10-19 14:42:29 -07002232 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002233 * If a directive _is_ found, it is the responsibility of this routine
2234 * (and not the caller) to free_tlist() the line.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002235 *
Ed Beroset3ab3f412002-06-11 03:31:49 +00002236 * @param tline a pointer to the current tokeninzed line linked list
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002237 * @param output if this directive generated output
Ed Beroset3ab3f412002-06-11 03:31:49 +00002238 * @return DIRECTIVE_FOUND or NO_DIRECTIVE_FOUND
H. Peter Anvin70653092007-10-19 14:42:29 -07002239 *
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002240 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002241static int do_directive(Token *tline, char **output)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002242{
H. Peter Anvin4169a472007-09-12 01:29:43 +00002243 enum preproc_token i;
2244 int j;
H. Peter Anvin70055962007-10-11 00:05:31 -07002245 bool err;
2246 int nparam;
2247 bool nolist;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07002248 bool casesense;
H. Peter Anvin8cfdb9d2007-09-14 18:36:01 -07002249 int k, m;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002250 int offset;
H. Peter Anvinccad6f92016-10-04 00:34:35 -07002251 char *p, *pp;
2252 const char *found_path;
H. Peter Anvinf8ad5322009-02-21 17:55:08 -08002253 const char *mname;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002254 struct ppscan pps;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002255 Include *inc;
2256 Context *ctx;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002257 Cond *cond;
2258 MMacro *mmac, **mmhead;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002259 Token *t = NULL, *tt, *param_start, *macro_start, *last, *origline;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002260 Line *l;
H. Peter Anvin76690a12002-04-30 20:52:49 +00002261 struct tokenval tokval;
2262 expr *evalresult;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002263 MMacro *tmp_defining; /* Used when manipulating rep_nest */
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07002264 int64_t count;
H. Peter Anvinf26e0972008-07-01 21:26:27 -07002265 size_t len;
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08002266 errflags severity;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002267 const char *dname; /* Name of directive, for messages */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002268
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002269 *output = NULL; /* No output generated */
H. Peter Anvin76690a12002-04-30 20:52:49 +00002270 origline = tline;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002271
H. Peter Anvineba20a72002-04-30 20:53:55 +00002272 skip_white_(tline);
H. Peter Anvinf2936d72008-06-04 15:11:23 -07002273 if (!tline || !tok_type_(tline, TOK_PREPROC_ID) ||
Cyrill Gorcunov4b5b7372018-10-29 22:54:08 +03002274 (tline->text[0] && (tline->text[1] == '%' ||
2275 tline->text[1] == '$' ||
2276 tline->text[1] == '!')))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002277 return NO_DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002278
H. Peter Anvin4169a472007-09-12 01:29:43 +00002279 i = pp_token_hash(tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002280
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002281 /*
2282 * FIXME: We zap execution of PP_RMACRO, PP_IRMACRO, PP_EXITMACRO
2283 * since they are known to be buggy at moment, we need to fix them
2284 * in future release (2.09-2.10)
2285 */
Philipp Klokeb432f572013-03-31 12:01:23 +02002286 if (i == PP_RMACRO || i == PP_IRMACRO || i == PP_EXITMACRO) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002287 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
2288 return NO_DIRECTIVE_FOUND;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002289 }
2290
2291 /*
2292 * If we're in a non-emitting branch of a condition construct,
2293 * or walking to the end of an already terminated %rep block,
2294 * we should ignore all directives except for condition
2295 * directives.
2296 */
2297 if (((istk->conds && !emitting(istk->conds->state)) ||
2298 (istk->mstk && !istk->mstk->in_progress)) && !is_condition(i)) {
2299 return NO_DIRECTIVE_FOUND;
2300 }
2301
2302 /*
2303 * If we're defining a macro or reading a %rep block, we should
2304 * ignore all directives except for %macro/%imacro (which nest),
2305 * %endm/%endmacro, and (only if we're in a %rep block) %endrep.
2306 * If we're in a %rep block, another %rep nests, so should be let through.
2307 */
2308 if (defining && i != PP_MACRO && i != PP_IMACRO &&
2309 i != PP_RMACRO && i != PP_IRMACRO &&
2310 i != PP_ENDMACRO && i != PP_ENDM &&
2311 (defining->name || (i != PP_ENDREP && i != PP_REP))) {
2312 return NO_DIRECTIVE_FOUND;
2313 }
2314
2315 if (defining) {
2316 if (i == PP_MACRO || i == PP_IMACRO ||
2317 i == PP_RMACRO || i == PP_IRMACRO) {
2318 nested_mac_count++;
2319 return NO_DIRECTIVE_FOUND;
2320 } else if (nested_mac_count > 0) {
2321 if (i == PP_ENDMACRO) {
2322 nested_mac_count--;
2323 return NO_DIRECTIVE_FOUND;
2324 }
2325 }
2326 if (!defining->name) {
2327 if (i == PP_REP) {
2328 nested_rep_count++;
2329 return NO_DIRECTIVE_FOUND;
2330 } else if (nested_rep_count > 0) {
2331 if (i == PP_ENDREP) {
2332 nested_rep_count--;
2333 return NO_DIRECTIVE_FOUND;
2334 }
2335 }
2336 }
2337 }
2338
H. Peter Anvin8b262472019-02-26 14:00:54 -08002339 dname = pp_directives[i]; /* Directive name, for error messages */
2340 casesense = true; /* Default to case sensitive */
H. Peter Anvin4169a472007-09-12 01:29:43 +00002341 switch (i) {
2342 case PP_INVALID:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002343 nasm_nonfatal("unknown preprocessor directive `%s'", tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 return NO_DIRECTIVE_FOUND; /* didn't get it */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00002345
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002346 case PP_PRAGMA:
2347 /*
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002348 * %pragma namespace options...
2349 *
2350 * The namespace "preproc" is reserved for the preprocessor;
2351 * all other namespaces generate a [pragma] assembly directive.
2352 *
2353 * Invalid %pragmas are ignored and may have different
2354 * meaning in future versions of NASM.
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002355 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002356 tline = tline->next;
2357 skip_white_(tline);
2358 tline = expand_smacro(tline);
2359 if (tok_type_(tline, TOK_ID)) {
2360 if (!nasm_stricmp(tline->text, "preproc")) {
2361 /* Preprocessor pragma */
2362 do_pragma_preproc(tline);
2363 } else {
2364 /* Build the assembler directive */
2365 t = new_Token(NULL, TOK_OTHER, "[", 1);
2366 t->next = new_Token(NULL, TOK_ID, "pragma", 6);
2367 t->next->next = new_Token(tline, TOK_WHITESPACE, NULL, 0);
2368 tline = t;
2369 for (t = tline; t->next; t = t->next)
2370 ;
2371 t->next = new_Token(NULL, TOK_OTHER, "]", 1);
2372 /* true here can be revisited in the future */
2373 *output = detoken(tline, true);
2374 }
2375 }
H. Peter Anvin3f87a2a2016-10-04 14:07:19 -07002376 free_tlist(origline);
2377 return DIRECTIVE_FOUND;
2378
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379 case PP_STACKSIZE:
2380 /* Directive to tell NASM what the default stack size is. The
2381 * default is for a 16-bit stack, and this can be overriden with
2382 * %stacksize large.
H. Peter Anvine2c80182005-01-15 22:15:51 +00002383 */
2384 tline = tline->next;
2385 if (tline && tline->type == TOK_WHITESPACE)
2386 tline = tline->next;
2387 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002388 nasm_nonfatal("`%s' missing size parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002389 free_tlist(origline);
2390 return DIRECTIVE_FOUND;
2391 }
2392 if (nasm_stricmp(tline->text, "flat") == 0) {
2393 /* All subsequent ARG directives are for a 32-bit stack */
2394 StackSize = 4;
2395 StackPointer = "ebp";
2396 ArgOffset = 8;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002397 LocalOffset = 0;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002398 } else if (nasm_stricmp(tline->text, "flat64") == 0) {
2399 /* All subsequent ARG directives are for a 64-bit stack */
2400 StackSize = 8;
2401 StackPointer = "rbp";
Per Jessen53252e02010-02-11 00:16:59 +03002402 ArgOffset = 16;
Charles Crayne7eaf9192007-11-08 22:11:14 -08002403 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404 } else if (nasm_stricmp(tline->text, "large") == 0) {
2405 /* All subsequent ARG directives are for a 16-bit stack,
2406 * far function call.
2407 */
2408 StackSize = 2;
2409 StackPointer = "bp";
2410 ArgOffset = 4;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002411 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412 } else if (nasm_stricmp(tline->text, "small") == 0) {
2413 /* All subsequent ARG directives are for a 16-bit stack,
2414 * far function call. We don't support near functions.
2415 */
2416 StackSize = 2;
2417 StackPointer = "bp";
2418 ArgOffset = 6;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002419 LocalOffset = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002420 } else {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002421 nasm_nonfatal("`%s' invalid size type", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002422 free_tlist(origline);
2423 return DIRECTIVE_FOUND;
2424 }
2425 free_tlist(origline);
2426 return DIRECTIVE_FOUND;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002427
H. Peter Anvine2c80182005-01-15 22:15:51 +00002428 case PP_ARG:
2429 /* TASM like ARG directive to define arguments to functions, in
2430 * the following form:
2431 *
2432 * ARG arg1:WORD, arg2:DWORD, arg4:QWORD
2433 */
2434 offset = ArgOffset;
2435 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002436 char *arg, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002437 int size = StackSize;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002438
H. Peter Anvine2c80182005-01-15 22:15:51 +00002439 /* Find the argument name */
2440 tline = tline->next;
2441 if (tline && tline->type == TOK_WHITESPACE)
2442 tline = tline->next;
2443 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002444 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002445 free_tlist(origline);
2446 return DIRECTIVE_FOUND;
2447 }
2448 arg = tline->text;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002449
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 /* Find the argument size type */
2451 tline = tline->next;
2452 if (!tline || tline->type != TOK_OTHER
2453 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002454 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002455 free_tlist(origline);
2456 return DIRECTIVE_FOUND;
2457 }
2458 tline = tline->next;
2459 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002460 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002461 free_tlist(origline);
2462 return DIRECTIVE_FOUND;
2463 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002464
H. Peter Anvine2c80182005-01-15 22:15:51 +00002465 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002466 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002468 size = parse_size(tt->text);
2469 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002470 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 free_tlist(tt);
2472 free_tlist(origline);
2473 return DIRECTIVE_FOUND;
2474 }
2475 free_tlist(tt);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002476
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002477 /* Round up to even stack slots */
2478 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002479
H. Peter Anvine2c80182005-01-15 22:15:51 +00002480 /* Now define the macro for the argument */
2481 snprintf(directive, sizeof(directive), "%%define %s (%s+%d)",
2482 arg, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002483 do_directive(tokenize(directive), output);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002484 offset += size;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002485
H. Peter Anvine2c80182005-01-15 22:15:51 +00002486 /* Move to the next argument in the list */
2487 tline = tline->next;
2488 if (tline && tline->type == TOK_WHITESPACE)
2489 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002490 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002491 ArgOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002492 free_tlist(origline);
2493 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002494
H. Peter Anvine2c80182005-01-15 22:15:51 +00002495 case PP_LOCAL:
2496 /* TASM like LOCAL directive to define local variables for a
2497 * function, in the following form:
2498 *
2499 * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize
2500 *
2501 * The '= LocalSize' at the end is ignored by NASM, but is
2502 * required by TASM to define the local parameter size (and used
2503 * by the TASM macro package).
2504 */
2505 offset = LocalOffset;
2506 do {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00002507 char *local, directive[256];
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 int size = StackSize;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002509
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 /* Find the argument name */
2511 tline = tline->next;
2512 if (tline && tline->type == TOK_WHITESPACE)
2513 tline = tline->next;
2514 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002515 nasm_nonfatal("`%s' missing argument parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 free_tlist(origline);
2517 return DIRECTIVE_FOUND;
2518 }
2519 local = tline->text;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002520
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 /* Find the argument size type */
2522 tline = tline->next;
2523 if (!tline || tline->type != TOK_OTHER
2524 || tline->text[0] != ':') {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002525 nasm_nonfatal("syntax error processing `%s' directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002526 free_tlist(origline);
2527 return DIRECTIVE_FOUND;
2528 }
2529 tline = tline->next;
2530 if (!tline || tline->type != TOK_ID) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002531 nasm_nonfatal("`%s' missing size type parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 free_tlist(origline);
2533 return DIRECTIVE_FOUND;
2534 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002535
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 /* Allow macro expansion of type parameter */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002537 tt = tokenize(tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 tt = expand_smacro(tt);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002539 size = parse_size(tt->text);
2540 if (!size) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002541 nasm_nonfatal("invalid size type for `%s' missing directive", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002542 free_tlist(tt);
2543 free_tlist(origline);
2544 return DIRECTIVE_FOUND;
2545 }
2546 free_tlist(tt);
H. Peter Anvin734b1882002-04-30 21:01:08 +00002547
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002548 /* Round up to even stack slots */
2549 size = ALIGN(size, StackSize);
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002550
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002551 offset += size; /* Negative offset, increment before */
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002552
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002553 /* Now define the macro for the argument */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002554 snprintf(directive, sizeof(directive), "%%define %s (%s-%d)",
2555 local, StackPointer, offset);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002556 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002557
H. Peter Anvine2c80182005-01-15 22:15:51 +00002558 /* Now define the assign to setup the enter_c macro correctly */
2559 snprintf(directive, sizeof(directive),
2560 "%%assign %%$localsize %%$localsize+%d", size);
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07002561 do_directive(tokenize(directive), output);
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00002562
H. Peter Anvine2c80182005-01-15 22:15:51 +00002563 /* Move to the next argument in the list */
2564 tline = tline->next;
2565 if (tline && tline->type == TOK_WHITESPACE)
2566 tline = tline->next;
H. Peter Anvin8781cb02007-11-08 20:01:11 -08002567 } while (tline && tline->type == TOK_OTHER && tline->text[0] == ',');
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002568 LocalOffset = offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 free_tlist(origline);
2570 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002571
H. Peter Anvine2c80182005-01-15 22:15:51 +00002572 case PP_CLEAR:
2573 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002574 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002575 free_macros();
2576 init_macros();
H. Peter Anvine2c80182005-01-15 22:15:51 +00002577 free_tlist(origline);
2578 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002579
H. Peter Anvin418ca702008-05-30 10:42:30 -07002580 case PP_DEPEND:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002581 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002582 skip_white_(t);
2583 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002584 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002585 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002586 free_tlist(origline);
2587 return DIRECTIVE_FOUND; /* but we did _something_ */
2588 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002589 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002590 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002591 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002592 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002593 nasm_unquote_cstr(p, i);
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03002594 strlist_add(deplist, p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002595 free_tlist(origline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07002596 return DIRECTIVE_FOUND;
2597
2598 case PP_INCLUDE:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002599 t = tline->next = expand_smacro(tline->next);
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002600 skip_white_(t);
H. Peter Anvind2456592008-06-19 15:04:18 -07002601
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002602 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002603 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002604 nasm_nonfatal("`%s' expects a file name", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002605 free_tlist(origline);
2606 return DIRECTIVE_FOUND; /* but we did _something_ */
2607 }
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002608 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002609 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002610 p = t->text;
H. Peter Anvin88c9e1f2008-06-04 11:26:59 -07002611 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002612 nasm_unquote_cstr(p, i);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002613 inc = nasm_malloc(sizeof(Include));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002614 inc->next = istk;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002615 inc->conds = NULL;
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002616 found_path = NULL;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07002617 inc->fp = inc_fopen(p, deplist, &found_path,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08002618 (pp_mode == PP_DEPS)
2619 ? INC_OPTIONAL : INC_NEEDED, NF_TEXT);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002620 if (!inc->fp) {
2621 /* -MG given but file not found */
2622 nasm_free(inc);
2623 } else {
Jim Kukunas65a8afc2016-06-13 16:00:42 -04002624 inc->fname = src_set_fname(found_path ? found_path : p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002625 inc->lineno = src_set_linnum(0);
2626 inc->lineinc = 1;
2627 inc->expansion = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002628 inc->mstk = NULL;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002629 istk = inc;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08002630 lfmt->uplevel(LIST_INCLUDE);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002631 }
2632 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002633 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002634
H. Peter Anvind2456592008-06-19 15:04:18 -07002635 case PP_USE:
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002636 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002637 static macros_t *use_pkg;
2638 const char *pkg_macro = NULL;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002639
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002640 tline = tline->next;
2641 skip_white_(tline);
2642 tline = expand_id(tline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002643
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002644 if (!tline || (tline->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002645 tline->type != TOK_INTERNAL_STRING &&
2646 tline->type != TOK_ID)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002647 nasm_nonfatal("`%s' expects a package name", dname);
H. Peter Anvind2456592008-06-19 15:04:18 -07002648 free_tlist(origline);
2649 return DIRECTIVE_FOUND; /* but we did _something_ */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002650 }
H. Peter Anvin264b7b92008-10-24 16:38:17 -07002651 if (tline->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002652 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002653 if (tline->type == TOK_STRING)
2654 nasm_unquote_cstr(tline->text, i);
2655 use_pkg = nasm_stdmac_find_package(tline->text);
2656 if (!use_pkg)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002657 nasm_nonfatal("unknown `%s' package: %s", dname, tline->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002658 else
2659 pkg_macro = (char *)use_pkg + 1; /* The first string will be <%define>__USE_*__ */
Victor van den Elzen35eb2ea2010-03-10 22:33:48 +01002660 if (use_pkg && ! smacro_defined(NULL, pkg_macro, 0, NULL, true)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002661 /* Not already included, go ahead and include it */
2662 stdmacpos = use_pkg;
2663 }
2664 free_tlist(origline);
H. Peter Anvind2456592008-06-19 15:04:18 -07002665 return DIRECTIVE_FOUND;
H. Peter Anvinf4ae5ad2008-06-19 18:39:24 -07002666 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 case PP_PUSH:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002668 case PP_REPL:
H. Peter Anvin42b56392008-10-24 16:24:21 -07002669 case PP_POP:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 tline = tline->next;
2671 skip_white_(tline);
2672 tline = expand_id(tline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002673 if (tline) {
2674 if (!tok_type_(tline, TOK_ID)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002675 nasm_nonfatal("`%s' expects a context identifier",
2676 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002677 free_tlist(origline);
2678 return DIRECTIVE_FOUND; /* but we did _something_ */
2679 }
2680 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002681 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002682 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002683 p = nasm_strdup(tline->text);
2684 } else {
2685 p = NULL; /* Anonymous */
2686 }
H. Peter Anvin42b56392008-10-24 16:24:21 -07002687
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002688 if (i == PP_PUSH) {
H. Peter Anvin (Intel)ebb05a02018-12-11 12:30:25 -08002689 nasm_new(ctx);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002690 ctx->next = cstk;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002691 ctx->name = p;
2692 ctx->number = unique++;
2693 cstk = ctx;
2694 } else {
2695 /* %pop or %repl */
2696 if (!cstk) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002697 nasm_nonfatal("`%s': context stack is empty",
2698 pp_directives[i]);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002699 } else if (i == PP_POP) {
2700 if (p && (!cstk->name || nasm_stricmp(p, cstk->name)))
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002701 nasm_nonfatal("`%s' in wrong context: %s, "
H. Peter Anvin8b262472019-02-26 14:00:54 -08002702 "expected %s",
2703 dname, cstk->name ? cstk->name : "anonymous", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002704 else
2705 ctx_pop();
2706 } else {
2707 /* i == PP_REPL */
2708 nasm_free(cstk->name);
2709 cstk->name = p;
2710 p = NULL;
2711 }
2712 nasm_free(p);
2713 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 free_tlist(origline);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002715 return DIRECTIVE_FOUND;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002716 case PP_FATAL:
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002717 severity = ERR_FATAL;
2718 goto issue_error;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 case PP_ERROR:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002720 severity = ERR_NONFATAL|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002721 goto issue_error;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002722 case PP_WARNING:
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08002723 /*!
2724 *!user [on] %warning directives
2725 *! controls output of \c{%warning} directives (see \k{pperror}).
2726 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002727 severity = ERR_WARNING|WARN_USER|ERR_PASS2;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002728 goto issue_error;
H. Peter Anvin8e3f75e2008-09-24 00:21:58 -07002729
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002730issue_error:
H. Peter Anvin7df04172008-06-10 18:27:38 -07002731 {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002732 /* Only error out if this is the final pass */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002733 tline->next = expand_smacro(tline->next);
2734 tline = tline->next;
2735 skip_white_(tline);
2736 t = tline ? tline->next : NULL;
2737 skip_white_(t);
2738 if (tok_type_(tline, TOK_STRING) && !t) {
2739 /* The line contains only a quoted string */
2740 p = tline->text;
2741 nasm_unquote(p, NULL); /* Ignore NUL character truncation */
H. Peter Anvin130736c2016-02-17 20:27:41 -08002742 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002743 } else {
2744 /* Not a quoted string, or more than a quoted string */
2745 p = detoken(tline, false);
H. Peter Anvin130736c2016-02-17 20:27:41 -08002746 nasm_error(severity, "%s", p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002747 nasm_free(p);
2748 }
2749 free_tlist(origline);
2750 return DIRECTIVE_FOUND;
H. Peter Anvin7df04172008-06-10 18:27:38 -07002751 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00002752
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002753 CASE_PP_IF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002754 if (istk->conds && !emitting(istk->conds->state))
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002755 j = COND_NEVER;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002756 else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002757 j = if_condition(tline->next, i);
2758 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002759 j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002760 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002761 cond = nasm_malloc(sizeof(Cond));
2762 cond->next = istk->conds;
2763 cond->state = j;
2764 istk->conds = cond;
2765 if(istk->mstk)
2766 istk->mstk->condcnt ++;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002767 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002769
H. Peter Anvinda10e7b2007-09-12 04:18:37 +00002770 CASE_PP_ELIF:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002771 if (!istk->conds)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002772 nasm_fatal("`%s': no matching `%%if'", dname);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002773 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002774 case COND_IF_TRUE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002775 istk->conds->state = COND_DONE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002776 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002777
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002778 case COND_DONE:
2779 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002780 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002781
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002782 case COND_ELSE_TRUE:
2783 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002784 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002785 "`%%elif' after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002786 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002787 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002788
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002789 case COND_IF_FALSE:
2790 /*
2791 * IMPORTANT: In the case of %if, we will already have
2792 * called expand_mmac_params(); however, if we're
2793 * processing an %elif we must have been in a
2794 * non-emitting mode, which would have inhibited
2795 * the normal invocation of expand_mmac_params().
2796 * Therefore, we have to do it explicitly here.
2797 */
2798 j = if_condition(expand_mmac_params(tline->next), i);
2799 tline->next = NULL; /* it got freed */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002800 istk->conds->state =
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002801 j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE;
2802 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002804 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002806
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 case PP_ELSE:
2808 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002809 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002810 "trailing garbage after `%%else' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002811 if (!istk->conds)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002812 nasm_fatal("`%%else: no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002813 switch(istk->conds->state) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002814 case COND_IF_TRUE:
2815 case COND_DONE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002816 istk->conds->state = COND_ELSE_FALSE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002817 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002818
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002819 case COND_NEVER:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002820 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002821
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002822 case COND_IF_FALSE:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002823 istk->conds->state = COND_ELSE_TRUE;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002824 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002825
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002826 case COND_ELSE_TRUE:
2827 case COND_ELSE_FALSE:
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002828 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002829 "`%%else' after `%%else' ignored.");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002830 istk->conds->state = COND_NEVER;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002831 break;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02002832 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 free_tlist(origline);
2834 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002835
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 case PP_ENDIF:
2837 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002838 nasm_warn(WARN_OTHER|ERR_PP_PRECOND,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002839 "trailing garbage after `%%endif' ignored");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002840 if (!istk->conds)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002841 nasm_fatal("`%%endif': no matching `%%if'");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002842 cond = istk->conds;
2843 istk->conds = cond->next;
2844 nasm_free(cond);
2845 if(istk->mstk)
2846 istk->mstk->condcnt --;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 free_tlist(origline);
2848 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002849
H. Peter Anvindb8f96e2009-07-15 09:07:29 -04002850 case PP_IRMACRO:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 case PP_IMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002852 casesense = false;
2853 /* fall through */
2854 case PP_RMACRO:
2855 case PP_MACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002856 if (defining) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002857 nasm_fatal("`%s': already defining a macro", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002858 return DIRECTIVE_FOUND;
2859 }
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002860 defining = nasm_zalloc(sizeof(MMacro));
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07002861 defining->max_depth = ((i == PP_RMACRO) || (i == PP_IRMACRO))
2862 ? nasm_limit[LIMIT_MACROS] : 0;
H. Peter Anvin8b262472019-02-26 14:00:54 -08002863 defining->casesense = casesense;
2864 if (!parse_mmacro_spec(tline, defining, dname)) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002865 nasm_free(defining);
2866 defining = NULL;
2867 return DIRECTIVE_FOUND;
2868 }
H. Peter Anvina26433d2008-07-16 14:40:01 -07002869
H. Peter Anvin4def1a82016-05-09 13:59:44 -07002870 src_get(&defining->xline, &defining->fname);
2871
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002872 mmac = (MMacro *) hash_findix(&mmacros, defining->name);
2873 while (mmac) {
2874 if (!strcmp(mmac->name, defining->name) &&
2875 (mmac->nparam_min <= defining->nparam_max
2876 || defining->plus)
2877 && (defining->nparam_min <= mmac->nparam_max
2878 || mmac->plus)) {
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002879 nasm_warn(WARN_OTHER, "redefining multi-line macro `%s'",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002880 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002881 return DIRECTIVE_FOUND;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002882 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002883 mmac = mmac->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002884 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 free_tlist(origline);
2886 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002887
H. Peter Anvine2c80182005-01-15 22:15:51 +00002888 case PP_ENDM:
2889 case PP_ENDMACRO:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002890 if (! (defining && defining->name)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002891 nasm_nonfatal("`%s': not defining a macro", tline->text);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002892 return DIRECTIVE_FOUND;
2893 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002894 mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name);
2895 defining->next = *mmhead;
2896 *mmhead = defining;
2897 defining = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002898 free_tlist(origline);
2899 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00002900
H. Peter Anvin89cee572009-07-15 09:16:54 -04002901 case PP_EXITMACRO:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002902 /*
2903 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002904 * macro-end marker for a macro with a name. Then we
2905 * bypass all lines between exitmacro and endmacro.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002906 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002907 list_for_each(l, istk->expansion)
2908 if (l->finishes && l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002909 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002910
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002911 if (l) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002912 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002913 * Remove all conditional entries relative to this
2914 * macro invocation. (safe to do in this context)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002915 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002916 for ( ; l->finishes->condcnt > 0; l->finishes->condcnt --) {
2917 cond = istk->conds;
2918 istk->conds = cond->next;
2919 nasm_free(cond);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002920 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002921 istk->expansion = l;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002922 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002923 nasm_nonfatal("`%%exitmacro' not within `%%macro' block");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002924 }
Keith Kanios852f1ee2009-07-12 00:19:55 -05002925 free_tlist(origline);
2926 return DIRECTIVE_FOUND;
2927
H. Peter Anvina26433d2008-07-16 14:40:01 -07002928 case PP_UNIMACRO:
H. Peter Anvin8b262472019-02-26 14:00:54 -08002929 casesense = false;
2930 /* fall through */
2931 case PP_UNMACRO:
H. Peter Anvina26433d2008-07-16 14:40:01 -07002932 {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002933 MMacro **mmac_p;
2934 MMacro spec;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002935
H. Peter Anvin8b262472019-02-26 14:00:54 -08002936 spec.casesense = casesense;
2937 if (!parse_mmacro_spec(tline, &spec, dname)) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002938 return DIRECTIVE_FOUND;
2939 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002940 mmac_p = (MMacro **) hash_findi(&mmacros, spec.name, NULL);
2941 while (mmac_p && *mmac_p) {
2942 mmac = *mmac_p;
2943 if (mmac->casesense == spec.casesense &&
2944 !mstrcmp(mmac->name, spec.name, spec.casesense) &&
2945 mmac->nparam_min == spec.nparam_min &&
2946 mmac->nparam_max == spec.nparam_max &&
2947 mmac->plus == spec.plus) {
2948 *mmac_p = mmac->next;
2949 free_mmacro(mmac);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002950 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002951 mmac_p = &mmac->next;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03002952 }
2953 }
2954 free_tlist(origline);
2955 free_tlist(spec.dlist);
2956 return DIRECTIVE_FOUND;
H. Peter Anvina26433d2008-07-16 14:40:01 -07002957 }
2958
H. Peter Anvine2c80182005-01-15 22:15:51 +00002959 case PP_ROTATE:
2960 if (tline->next && tline->next->type == TOK_WHITESPACE)
2961 tline = tline->next;
H. Peter Anvin89cee572009-07-15 09:16:54 -04002962 if (!tline->next) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 free_tlist(origline);
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002964 nasm_nonfatal("`%%rotate' missing rotate count");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002965 return DIRECTIVE_FOUND;
2966 }
2967 t = expand_smacro(tline->next);
2968 tline->next = NULL;
2969 free_tlist(origline);
H. Peter Anvin8b262472019-02-26 14:00:54 -08002970 pps.tptr = tline = t;
2971 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 tokval.t_type = TOKEN_INVALID;
2973 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07002974 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002975 free_tlist(tline);
2976 if (!evalresult)
2977 return DIRECTIVE_FOUND;
2978 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002979 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002980 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002981 nasm_nonfatal("non-constant value given to `%%rotate'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 return DIRECTIVE_FOUND;
2983 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002984 mmac = istk->mstk;
2985 while (mmac && !mmac->name) /* avoid mistaking %reps for macros */
2986 mmac = mmac->next_active;
2987 if (!mmac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002988 nasm_nonfatal("`%%rotate' invoked outside a macro call");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002989 } else if (mmac->nparam == 0) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03002990 nasm_nonfatal("`%%rotate' invoked within macro without parameters");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002991 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002992 int rotate = mmac->rotate + reloc_value(evalresult);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002993
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002994 rotate %= (int)mmac->nparam;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002995 if (rotate < 0)
H. Peter Anvin36206cd2012-03-03 16:14:51 -08002996 rotate += mmac->nparam;
2997
2998 mmac->rotate = rotate;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03002999 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 return DIRECTIVE_FOUND;
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003001
H. Peter Anvine2c80182005-01-15 22:15:51 +00003002 case PP_REP:
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003003 nolist = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 do {
3005 tline = tline->next;
3006 } while (tok_type_(tline, TOK_WHITESPACE));
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003007
H. Peter Anvine2c80182005-01-15 22:15:51 +00003008 if (tok_type_(tline, TOK_ID) &&
3009 nasm_stricmp(tline->text, ".nolist") == 0) {
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003010 nolist = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 do {
3012 tline = tline->next;
3013 } while (tok_type_(tline, TOK_WHITESPACE));
3014 }
Nickolay Yurchenko9aea7152003-09-07 22:46:26 +00003015
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 if (tline) {
H. Peter Anvin8b262472019-02-26 14:00:54 -08003017 pps.tptr = expand_smacro(tline);
3018 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003020 /* XXX: really critical?! */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 evalresult =
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003022 evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 if (!evalresult) {
3024 free_tlist(origline);
3025 return DIRECTIVE_FOUND;
3026 }
3027 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003028 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003029 if (!is_simple(evalresult)) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003030 nasm_nonfatal("non-constant value given to `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 return DIRECTIVE_FOUND;
3032 }
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003033 count = reloc_value(evalresult);
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003034 if (count > nasm_limit[LIMIT_REP]) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003035 nasm_nonfatal("`%%rep' count %"PRId64" exceeds limit (currently %"PRId64")",
3036 count, nasm_limit[LIMIT_REP]);
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003037 count = 0;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003038 } else if (count < 0) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08003039 /*!
3040 *!negative-rep [on] regative %rep count
3041 *! warns about negative counts given to the \c{%rep}
3042 *! preprocessor directive.
3043 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08003044 nasm_warn(ERR_PASS2|WARN_NEGATIVE_REP,
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003045 "negative `%%rep' count: %"PRId64, count);
3046 count = 0;
3047 } else {
Cyrill Gorcunove091d6e2010-08-09 13:58:22 +04003048 count++;
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003049 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003050 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003051 nasm_nonfatal("`%%rep' expects a repeat count");
H. Peter Anvinf8ba53e2007-10-11 10:11:57 -07003052 count = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 }
3054 free_tlist(origline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003055
3056 tmp_defining = defining;
3057 defining = nasm_malloc(sizeof(MMacro));
3058 defining->prev = NULL;
3059 defining->name = NULL; /* flags this macro as a %rep block */
3060 defining->casesense = false;
3061 defining->plus = false;
3062 defining->nolist = nolist;
3063 defining->in_progress = count;
3064 defining->max_depth = 0;
3065 defining->nparam_min = defining->nparam_max = 0;
3066 defining->defaults = NULL;
3067 defining->dlist = NULL;
3068 defining->expansion = NULL;
3069 defining->next_active = istk->mstk;
3070 defining->rep_nest = tmp_defining;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003071 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003072
H. Peter Anvine2c80182005-01-15 22:15:51 +00003073 case PP_ENDREP:
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003074 if (!defining || defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003075 nasm_nonfatal("`%%endrep': no matching `%%rep'");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003076 return DIRECTIVE_FOUND;
3077 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003078
H. Peter Anvine2c80182005-01-15 22:15:51 +00003079 /*
3080 * Now we have a "macro" defined - although it has no name
3081 * and we won't be entering it in the hash tables - we must
3082 * push a macro-end marker for it on to istk->expansion.
3083 * After that, it will take care of propagating itself (a
3084 * macro-end marker line for a macro which is really a %rep
3085 * block will cause the macro to be re-expanded, complete
3086 * with another macro-end marker to ensure the process
3087 * continues) until the whole expansion is forcibly removed
3088 * from istk->expansion by a %exitrep.
3089 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003090 l = nasm_malloc(sizeof(Line));
3091 l->next = istk->expansion;
3092 l->finishes = defining;
3093 l->first = NULL;
3094 istk->expansion = l;
3095
3096 istk->mstk = defining;
3097
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08003098 lfmt->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003099 tmp_defining = defining;
3100 defining = defining->rep_nest;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003101 free_tlist(origline);
3102 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003103
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 case PP_EXITREP:
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003105 /*
3106 * We must search along istk->expansion until we hit a
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003107 * macro-end marker for a macro with no name. Then we set
3108 * its `in_progress' flag to 0.
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003109 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003110 list_for_each(l, istk->expansion)
3111 if (l->finishes && !l->finishes->name)
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003112 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003113
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003114 if (l)
3115 l->finishes->in_progress = 1;
3116 else
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003117 nasm_nonfatal("`%%exitrep' not within `%%rep' block");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003118 free_tlist(origline);
3119 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003120
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 case PP_IDEFINE:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003122 case PP_IXDEFINE:
3123 casesense = false;
3124 /* fall through */
3125 case PP_DEFINE:
3126 case PP_XDEFINE:
3127 {
3128 SMacro *s;
3129 bool have_eval_params = false;
H. Peter Anvin4bc9f1d2007-10-11 12:52:03 -07003130
H. Peter Anvine2c80182005-01-15 22:15:51 +00003131 tline = tline->next;
3132 skip_white_(tline);
3133 tline = expand_id(tline);
3134 if (!tline || (tline->type != TOK_ID &&
3135 (tline->type != TOK_PREPROC_ID ||
3136 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003137 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003138 free_tlist(origline);
3139 return DIRECTIVE_FOUND;
3140 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003141
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003142 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003143 last = tline;
3144 param_start = tline = tline->next;
3145 nparam = 0;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003146
H. Peter Anvine2c80182005-01-15 22:15:51 +00003147 if (tok_is_(tline, "(")) {
3148 /*
3149 * This macro has parameters.
3150 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00003151
H. Peter Anvine2c80182005-01-15 22:15:51 +00003152 tline = tline->next;
3153 while (1) {
3154 skip_white_(tline);
3155 if (!tline) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003156 nasm_nonfatal("parameter identifier expected");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003157 free_tlist(origline);
3158 return DIRECTIVE_FOUND;
3159 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003160 if (tok_is_(tline, "=")) {
3161 have_eval_params = true;
3162 tline = tline->next;
3163 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003164 if (tline->type != TOK_ID) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003165 nasm_nonfatal("`%s': parameter identifier expected",
3166 tline->text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 free_tlist(origline);
3168 return DIRECTIVE_FOUND;
3169 }
H. Peter Anvin8b262472019-02-26 14:00:54 -08003170 tline->type = tok_smac_param(nparam++);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003171 tline = tline->next;
3172 skip_white_(tline);
3173 if (tok_is_(tline, ",")) {
3174 tline = tline->next;
H. Peter Anvinca348b62008-07-23 10:49:26 -04003175 } else {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003176 if (!tok_is_(tline, ")")) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003177 nasm_nonfatal("`)' expected to terminate macro template");
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003178 free_tlist(origline);
3179 return DIRECTIVE_FOUND;
3180 }
3181 break;
3182 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003183 }
3184 last = tline;
3185 tline = tline->next;
3186 }
3187 if (tok_type_(tline, TOK_WHITESPACE))
3188 last = tline, tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003189 last->next = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003190
3191 /* Expand the macro definition now for %xdefine and %ixdefine */
3192 if ((i == PP_XDEFINE) || (i == PP_IXDEFINE))
3193 tline = expand_smacro(tline);
3194
3195 macro_start = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003196 t = tline;
3197 while (t) {
3198 if (t->type == TOK_ID) {
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003199 list_for_each(tt, param_start)
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003200 if (is_smac_param(tt->type) &&
3201 tt->len == t->len &&
3202 !memcmp(tt->text, t->text, tt->len))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003203 t->type = tt->type;
3204 }
3205 tt = t->next;
3206 t->next = macro_start;
3207 macro_start = t;
3208 t = tt;
3209 }
3210 /*
3211 * Good. We now have a macro name, a parameter count, and a
3212 * token list (in reverse order) for an expansion. We ought
3213 * to be OK just to create an SMacro, store it, and let
3214 * free_tlist have the rest of the line (which we have
3215 * carefully re-terminated after chopping off the expansion
3216 * from the end).
3217 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003218 s = define_smacro(ctx, mname, casesense, nparam, macro_start);
3219
3220 if (have_eval_params) {
3221 /* Create evaluated parameters table */
3222 bool is_eval = false;
3223
3224 nasm_newn(s->eval_param, nparam);
3225 list_for_each(tt, param_start) {
3226 if (is_smac_param(tt->type))
3227 s->eval_param[smac_nparam(tt->type)] = is_eval;
3228 is_eval = tok_is_(tt, "=");
3229 }
3230 }
3231
3232
H. Peter Anvine2c80182005-01-15 22:15:51 +00003233 free_tlist(origline);
3234 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003235 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00003236
H. Peter Anvine2c80182005-01-15 22:15:51 +00003237 case PP_UNDEF:
3238 tline = tline->next;
3239 skip_white_(tline);
3240 tline = expand_id(tline);
3241 if (!tline || (tline->type != TOK_ID &&
3242 (tline->type != TOK_PREPROC_ID ||
3243 tline->text[1] != '$'))) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003244 nasm_nonfatal("`%%undef' expects a macro identifier");
H. Peter Anvine2c80182005-01-15 22:15:51 +00003245 free_tlist(origline);
3246 return DIRECTIVE_FOUND;
3247 }
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003248 if (tline->next)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003249 nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored");
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003250
H. Peter Anvine2c80182005-01-15 22:15:51 +00003251 /* Find the context that symbol belongs to */
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003252 ctx = get_ctx(tline->text, &mname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003253 undef_smacro(ctx, mname);
3254 free_tlist(origline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003255 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003256
H. Peter Anvin9e200162008-06-04 17:23:14 -07003257 case PP_IDEFSTR:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003258 casesense = false;
3259 /* fall through */
3260 case PP_DEFSTR:
H. Peter Anvin9e200162008-06-04 17:23:14 -07003261 tline = tline->next;
3262 skip_white_(tline);
3263 tline = expand_id(tline);
3264 if (!tline || (tline->type != TOK_ID &&
3265 (tline->type != TOK_PREPROC_ID ||
3266 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003267 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003268 free_tlist(origline);
3269 return DIRECTIVE_FOUND;
3270 }
3271
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003272 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003273 last = tline;
3274 tline = expand_smacro(tline->next);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003275 last->next = NULL;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003276
3277 while (tok_type_(tline, TOK_WHITESPACE))
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003278 tline = delete_Token(tline);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003279
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003280 p = detoken(tline, false);
H. Peter Anvin8b262472019-02-26 14:00:54 -08003281 macro_start = make_tok_qstr(p);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003282 nasm_free(p);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003283
3284 /*
3285 * We now have a macro name, an implicit parameter count of
3286 * zero, and a string token to use as an expansion. Create
3287 * and store an SMacro.
3288 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003289 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvin9e200162008-06-04 17:23:14 -07003290 free_tlist(origline);
3291 return DIRECTIVE_FOUND;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003292
H. Peter Anvin2f55bda2009-07-14 15:04:04 -04003293 case PP_IDEFTOK:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003294 casesense = false;
3295 /* fall through */
3296 case PP_DEFTOK:
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003297 tline = tline->next;
3298 skip_white_(tline);
3299 tline = expand_id(tline);
3300 if (!tline || (tline->type != TOK_ID &&
3301 (tline->type != TOK_PREPROC_ID ||
3302 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003303 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003304 free_tlist(origline);
3305 return DIRECTIVE_FOUND;
3306 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003307 ctx = get_ctx(tline->text, &mname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003308 last = tline;
3309 tline = expand_smacro(tline->next);
3310 last->next = NULL;
3311
3312 t = tline;
3313 while (tok_type_(t, TOK_WHITESPACE))
3314 t = t->next;
3315 /* t should now point to the string */
Cyrill Gorcunov6908e582010-09-06 19:36:15 +04003316 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003317 nasm_nonfatal("`%s` requires string as second parameter", dname);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003318 free_tlist(tline);
3319 free_tlist(origline);
3320 return DIRECTIVE_FOUND;
3321 }
3322
Cyrill Gorcunov4d8dbd92014-06-28 10:15:18 +04003323 /*
3324 * Convert the string to a token stream. Note that smacros
3325 * are stored with the token stream reversed, so we have to
3326 * reverse the output of tokenize().
3327 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003328 nasm_unquote_cstr(t->text, i);
H. Peter Anvinb40992c2010-09-15 08:57:21 -07003329 macro_start = reverse_tokens(tokenize(t->text));
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003330
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003331 /*
3332 * We now have a macro name, an implicit parameter count of
3333 * zero, and a numeric token to use as an expansion. Create
3334 * and store an SMacro.
3335 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003336 define_smacro(ctx, mname, casesense, 0, macro_start);
Keith Kaniosb83fd0b2009-07-14 01:04:12 -05003337 free_tlist(tline);
3338 free_tlist(origline);
3339 return DIRECTIVE_FOUND;
H. Peter Anvin9e200162008-06-04 17:23:14 -07003340
H. Peter Anvin8b262472019-02-26 14:00:54 -08003341 case PP_IPATHSEARCH:
3342 casesense = false;
3343 /* fall through */
H. Peter Anvin418ca702008-05-30 10:42:30 -07003344 case PP_PATHSEARCH:
3345 {
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003346 const char *found_path;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003347
H. Peter Anvin418ca702008-05-30 10:42:30 -07003348 tline = tline->next;
3349 skip_white_(tline);
3350 tline = expand_id(tline);
3351 if (!tline || (tline->type != TOK_ID &&
3352 (tline->type != TOK_PREPROC_ID ||
3353 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003354 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003355 free_tlist(origline);
3356 return DIRECTIVE_FOUND;
3357 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003358 ctx = get_ctx(tline->text, &mname);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003359 last = tline;
3360 tline = expand_smacro(tline->next);
3361 last->next = NULL;
3362
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003363 t = tline;
H. Peter Anvin418ca702008-05-30 10:42:30 -07003364 while (tok_type_(t, TOK_WHITESPACE))
3365 t = t->next;
3366
3367 if (!t || (t->type != TOK_STRING &&
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003368 t->type != TOK_INTERNAL_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003369 nasm_nonfatal("`%s' expects a file name", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003370 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003371 free_tlist(origline);
3372 return DIRECTIVE_FOUND; /* but we did _something_ */
3373 }
3374 if (t->next)
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003375 nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003376 p = t->text;
H. Peter Anvin427cc912008-06-01 21:43:03 -07003377 if (t->type != TOK_INTERNAL_STRING)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003378 nasm_unquote(p, NULL);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003379
H. Peter Anvin9924d1e2016-10-04 00:59:39 -07003380 inc_fopen(p, NULL, &found_path, INC_PROBE, NF_BINARY);
H. Peter Anvinccad6f92016-10-04 00:34:35 -07003381 if (!found_path)
3382 found_path = p;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003383 macro_start = make_tok_qstr(found_path);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003384
3385 /*
3386 * We now have a macro name, an implicit parameter count of
3387 * zero, and a string token to use as an expansion. Create
3388 * and store an SMacro.
3389 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003390 define_smacro(ctx, mname, casesense, 0, macro_start);
3391 free_tlist(tline);
H. Peter Anvin418ca702008-05-30 10:42:30 -07003392 free_tlist(origline);
3393 return DIRECTIVE_FOUND;
3394 }
3395
H. Peter Anvin8b262472019-02-26 14:00:54 -08003396 case PP_ISTRLEN:
3397 casesense = false;
3398 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003399 case PP_STRLEN:
3400 tline = tline->next;
3401 skip_white_(tline);
3402 tline = expand_id(tline);
3403 if (!tline || (tline->type != TOK_ID &&
3404 (tline->type != TOK_PREPROC_ID ||
3405 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003406 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003407 free_tlist(origline);
3408 return DIRECTIVE_FOUND;
3409 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003410 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003411 last = tline;
3412 tline = expand_smacro(tline->next);
3413 last->next = NULL;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003414
H. Peter Anvine2c80182005-01-15 22:15:51 +00003415 t = tline;
3416 while (tok_type_(t, TOK_WHITESPACE))
3417 t = t->next;
3418 /* t should now point to the string */
Cyrill Gorcunov4e1d5ab2010-07-23 18:51:51 +04003419 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003420 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003421 free_tlist(tline);
3422 free_tlist(origline);
3423 return DIRECTIVE_FOUND;
3424 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003425
H. Peter Anvin8b262472019-02-26 14:00:54 -08003426 macro_start = make_tok_num(nasm_unquote(t->text, NULL));
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003427
H. Peter Anvine2c80182005-01-15 22:15:51 +00003428 /*
3429 * We now have a macro name, an implicit parameter count of
3430 * zero, and a numeric token to use as an expansion. Create
3431 * and store an SMacro.
3432 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003433 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003434 free_tlist(tline);
3435 free_tlist(origline);
3436 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003437
H. Peter Anvin8b262472019-02-26 14:00:54 -08003438 case PP_ISTRCAT:
3439 casesense = false;
3440 /* fall through */
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003441 case PP_STRCAT:
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003442 tline = tline->next;
3443 skip_white_(tline);
3444 tline = expand_id(tline);
3445 if (!tline || (tline->type != TOK_ID &&
3446 (tline->type != TOK_PREPROC_ID ||
3447 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003448 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003449 free_tlist(origline);
3450 return DIRECTIVE_FOUND;
3451 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003452 ctx = get_ctx(tline->text, &mname);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003453 last = tline;
3454 tline = expand_smacro(tline->next);
3455 last->next = NULL;
3456
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003457 len = 0;
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003458 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003459 switch (t->type) {
3460 case TOK_WHITESPACE:
3461 break;
3462 case TOK_STRING:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003463 len += t->len = nasm_unquote(t->text, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003464 break;
3465 case TOK_OTHER:
3466 if (!strcmp(t->text, ",")) /* permit comma separators */
3467 break;
3468 /* else fall through */
3469 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003470 nasm_nonfatal("non-string passed to `%s': %s", dname, t->text);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003471 free_tlist(tline);
3472 free_tlist(origline);
3473 return DIRECTIVE_FOUND;
3474 }
3475 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003476
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003477 p = pp = nasm_malloc(len);
Cyrill Gorcunov3b4e86b2010-06-02 15:57:51 +04003478 list_for_each(t, tline) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003479 if (t->type == TOK_STRING) {
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003480 memcpy(p, t->text, t->len);
3481 p += t->len;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003482 }
3483 }
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003484
3485 /*
3486 * We now have a macro name, an implicit parameter count of
3487 * zero, and a numeric token to use as an expansion. Create
3488 * and store an SMacro.
3489 */
H. Peter Anvin8b262472019-02-26 14:00:54 -08003490 macro_start = make_tok_qstr(pp);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003491 nasm_free(pp);
3492 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvinf26e0972008-07-01 21:26:27 -07003493 free_tlist(tline);
3494 free_tlist(origline);
3495 return DIRECTIVE_FOUND;
3496
H. Peter Anvin8b262472019-02-26 14:00:54 -08003497 case PP_ISUBSTR:
3498 casesense = false;
3499 /* fall through */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003500 case PP_SUBSTR:
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003501 {
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003502 int64_t start, count;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003503 size_t len;
H. Peter Anvind2456592008-06-19 15:04:18 -07003504
H. Peter Anvine2c80182005-01-15 22:15:51 +00003505 tline = tline->next;
3506 skip_white_(tline);
3507 tline = expand_id(tline);
3508 if (!tline || (tline->type != TOK_ID &&
3509 (tline->type != TOK_PREPROC_ID ||
3510 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003511 nasm_nonfatal("`%s' expects a macro identifier as first parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003512 free_tlist(origline);
3513 return DIRECTIVE_FOUND;
3514 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003515 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003516 last = tline;
3517 tline = expand_smacro(tline->next);
3518 last->next = NULL;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003519
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003520 if (tline) /* skip expanded id */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003521 t = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003522 while (tok_type_(t, TOK_WHITESPACE))
3523 t = t->next;
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003524
H. Peter Anvine2c80182005-01-15 22:15:51 +00003525 /* t should now point to the string */
Cyrill Gorcunov35519d62010-09-06 23:49:52 +04003526 if (!tok_type_(t, TOK_STRING)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003527 nasm_nonfatal("`%s' requires string as second parameter", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003528 free_tlist(tline);
3529 free_tlist(origline);
3530 return DIRECTIVE_FOUND;
3531 }
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003532
H. Peter Anvin8b262472019-02-26 14:00:54 -08003533 pps.tptr = t->next;
3534 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003535 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003536 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003537 if (!evalresult) {
3538 free_tlist(tline);
3539 free_tlist(origline);
3540 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003541 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003542 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003543 free_tlist(tline);
3544 free_tlist(origline);
3545 return DIRECTIVE_FOUND;
3546 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003547 start = evalresult->value - 1;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003548
H. Peter Anvin8b262472019-02-26 14:00:54 -08003549 while (tok_type_(pps.tptr, TOK_WHITESPACE))
3550 pps.tptr = pps.tptr->next;
3551 if (!pps.tptr) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003552 count = 1; /* Backwards compatibility: one character */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003553 } else {
3554 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003555 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003556 if (!evalresult) {
3557 free_tlist(tline);
3558 free_tlist(origline);
3559 return DIRECTIVE_FOUND;
3560 } else if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003561 nasm_nonfatal("non-constant value given to `%s'", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003562 free_tlist(tline);
3563 free_tlist(origline);
3564 return DIRECTIVE_FOUND;
3565 }
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003566 count = evalresult->value;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003567 }
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003568
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003569 len = nasm_unquote(t->text, NULL);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003570
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003571 /* make start and count being in range */
3572 if (start < 0)
3573 start = 0;
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003574 if (count < 0)
3575 count = len + count + 1 - start;
3576 if (start + count > (int64_t)len)
Cyrill Gorcunovcff031e2010-09-07 20:31:11 +04003577 count = len - start;
3578 if (!len || count < 0 || start >=(int64_t)len)
Cyrill Gorcunovab122872010-09-07 10:42:02 +04003579 start = -1, count = 0; /* empty string */
H. Peter Anvin1cd0e2d2002-04-30 21:00:33 +00003580
H. Peter Anvin8b262472019-02-26 14:00:54 -08003581 macro_start = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003582 macro_start->len = count;
3583 macro_start->text = nasm_quote((start < 0) ? "" : t->text + start,
3584 &macro_start->len);
H. Peter Anvin734b1882002-04-30 21:01:08 +00003585
H. Peter Anvine2c80182005-01-15 22:15:51 +00003586 /*
3587 * We now have a macro name, an implicit parameter count of
3588 * zero, and a numeric token to use as an expansion. Create
3589 * and store an SMacro.
3590 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003591 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003592 free_tlist(tline);
3593 free_tlist(origline);
3594 return DIRECTIVE_FOUND;
H. Peter Anvin8cad14b2008-06-01 17:23:51 -07003595 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003596
H. Peter Anvine2c80182005-01-15 22:15:51 +00003597 case PP_IASSIGN:
H. Peter Anvin8b262472019-02-26 14:00:54 -08003598 casesense = false;
3599 /* fall through */
3600 case PP_ASSIGN:
H. Peter Anvine2c80182005-01-15 22:15:51 +00003601 tline = tline->next;
3602 skip_white_(tline);
3603 tline = expand_id(tline);
3604 if (!tline || (tline->type != TOK_ID &&
3605 (tline->type != TOK_PREPROC_ID ||
3606 tline->text[1] != '$'))) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003607 nasm_nonfatal("`%s' expects a macro identifier", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003608 free_tlist(origline);
3609 return DIRECTIVE_FOUND;
3610 }
Cyrill Gorcunov1a42fb22012-03-11 11:38:47 +04003611 ctx = get_ctx(tline->text, &mname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003612 last = tline;
3613 tline = expand_smacro(tline->next);
3614 last->next = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003615
H. Peter Anvin8b262472019-02-26 14:00:54 -08003616 pps.tptr = tline;
3617 pps.ntokens = -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003618 tokval.t_type = TOKEN_INVALID;
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003619 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003620 free_tlist(tline);
3621 if (!evalresult) {
3622 free_tlist(origline);
3623 return DIRECTIVE_FOUND;
3624 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003625
H. Peter Anvine2c80182005-01-15 22:15:51 +00003626 if (tokval.t_type)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08003627 nasm_warn(WARN_OTHER, "trailing garbage after expression ignored");
H. Peter Anvin734b1882002-04-30 21:01:08 +00003628
H. Peter Anvine2c80182005-01-15 22:15:51 +00003629 if (!is_simple(evalresult)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003630 nasm_nonfatal("non-constant value given to `%s'", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003631 free_tlist(origline);
3632 return DIRECTIVE_FOUND;
H. Peter Anvin8b262472019-02-26 14:00:54 -08003633 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00003634
H. Peter Anvin8b262472019-02-26 14:00:54 -08003635 macro_start = make_tok_num(reloc_value(evalresult));
H. Peter Anvin734b1882002-04-30 21:01:08 +00003636
H. Peter Anvine2c80182005-01-15 22:15:51 +00003637 /*
3638 * We now have a macro name, an implicit parameter count of
3639 * zero, and a numeric token to use as an expansion. Create
3640 * and store an SMacro.
3641 */
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003642 define_smacro(ctx, mname, casesense, 0, macro_start);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003643 free_tlist(origline);
3644 return DIRECTIVE_FOUND;
H. Peter Anvin734b1882002-04-30 21:01:08 +00003645
H. Peter Anvine2c80182005-01-15 22:15:51 +00003646 case PP_LINE:
3647 /*
3648 * Syntax is `%line nnn[+mmm] [filename]'
3649 */
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -08003650 if (unlikely(pp_noline)) {
3651 free_tlist(origline);
3652 return DIRECTIVE_FOUND;
3653 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00003654 tline = tline->next;
3655 skip_white_(tline);
3656 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003657 nasm_nonfatal("`%s' expects line number", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003658 free_tlist(origline);
3659 return DIRECTIVE_FOUND;
3660 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003661 k = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003662 m = 1;
3663 tline = tline->next;
3664 if (tok_is_(tline, "+")) {
3665 tline = tline->next;
3666 if (!tok_type_(tline, TOK_NUMBER)) {
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003667 nasm_nonfatal("`%s' expects line increment", dname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003668 free_tlist(origline);
3669 return DIRECTIVE_FOUND;
3670 }
H. Peter Anvin70055962007-10-11 00:05:31 -07003671 m = readnum(tline->text, &err);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003672 tline = tline->next;
3673 }
3674 skip_white_(tline);
3675 src_set_linnum(k);
3676 istk->lineinc = m;
3677 if (tline) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07003678 char *fname = detoken(tline, false);
3679 src_set_fname(fname);
3680 nasm_free(fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003681 }
3682 free_tlist(origline);
3683 return DIRECTIVE_FOUND;
Keith Kaniosb307a4f2010-11-06 17:41:51 -05003684
H. Peter Anvine2c80182005-01-15 22:15:51 +00003685 default:
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07003686 nasm_nonfatal("preprocessor directive `%s' not yet implemented", dname);
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003687 return DIRECTIVE_FOUND;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003688 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00003689}
3690
3691/*
H. Peter Anvin76690a12002-04-30 20:52:49 +00003692 * Ensure that a macro parameter contains a condition code and
3693 * nothing else. Return the condition code index if so, or -1
3694 * otherwise.
3695 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003696static int find_cc(Token * t)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003697{
H. Peter Anvin76690a12002-04-30 20:52:49 +00003698 Token *tt;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003699
H. Peter Anvin25a99342007-09-22 17:45:45 -07003700 if (!t)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003701 return -1; /* Probably a %+ without a space */
H. Peter Anvin25a99342007-09-22 17:45:45 -07003702
H. Peter Anvineba20a72002-04-30 20:53:55 +00003703 skip_white_(t);
Cyrill Gorcunov7524cfd2017-10-22 19:01:16 +03003704 if (!t)
3705 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003706 if (t->type != TOK_ID)
H. Peter Anvine2c80182005-01-15 22:15:51 +00003707 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003708 tt = t->next;
H. Peter Anvineba20a72002-04-30 20:53:55 +00003709 skip_white_(tt);
H. Peter Anvin76690a12002-04-30 20:52:49 +00003710 if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ",")))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003711 return -1;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003712
Cyrill Gorcunov19456392012-05-02 00:18:56 +04003713 return bsii(t->text, (const char **)conditions, ARRAY_SIZE(conditions));
H. Peter Anvin76690a12002-04-30 20:52:49 +00003714}
3715
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003716/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003717 * This routines walks over tokens strem and handles tokens
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003718 * pasting, if @handle_explicit passed then explicit pasting
3719 * term is handled, otherwise -- implicit pastings only.
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07003720 * The @m array can contain a series of token types which are
3721 * executed as separate passes.
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003722 */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003723static bool paste_tokens(Token **head, const struct tokseq_match *m,
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003724 size_t mnum, bool handle_explicit)
H. Peter Anvind784a082009-04-20 14:01:18 -07003725{
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003726 Token *tok, *next, **prev_next, **prev_nonspace;
3727 bool pasted = false;
3728 char *buf, *p;
3729 size_t len, i;
H. Peter Anvind784a082009-04-20 14:01:18 -07003730
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003731 /*
3732 * The last token before pasting. We need it
3733 * to be able to connect new handled tokens.
3734 * In other words if there were a tokens stream
3735 *
3736 * A -> B -> C -> D
3737 *
3738 * and we've joined tokens B and C, the resulting
3739 * stream should be
3740 *
3741 * A -> BC -> D
3742 */
3743 tok = *head;
3744 prev_next = NULL;
3745
3746 if (!tok_type_(tok, TOK_WHITESPACE) && !tok_type_(tok, TOK_PASTE))
3747 prev_nonspace = head;
3748 else
3749 prev_nonspace = NULL;
3750
3751 while (tok && (next = tok->next)) {
3752
3753 switch (tok->type) {
H. Peter Anvind784a082009-04-20 14:01:18 -07003754 case TOK_WHITESPACE:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003755 /* Zap redundant whitespaces */
3756 while (tok_type_(next, TOK_WHITESPACE))
3757 next = delete_Token(next);
3758 tok->next = next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003759 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003760
3761 case TOK_PASTE:
3762 /* Explicit pasting */
3763 if (!handle_explicit)
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003764 break;
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003765 next = delete_Token(tok);
3766
3767 while (tok_type_(next, TOK_WHITESPACE))
3768 next = delete_Token(next);
3769
3770 if (!pasted)
3771 pasted = true;
3772
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003773 /* Left pasting token is start of line */
3774 if (!prev_nonspace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003775 nasm_fatal("No lvalue found on pasting");
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003776
Cyrill Gorcunov8b5c9fb2013-02-04 01:24:54 +04003777 /*
3778 * No ending token, this might happen in two
3779 * cases
3780 *
3781 * 1) There indeed no right token at all
3782 * 2) There is a bare "%define ID" statement,
3783 * and @ID does expand to whitespace.
3784 *
3785 * So technically we need to do a grammar analysis
3786 * in another stage of parsing, but for now lets don't
3787 * change the behaviour people used to. Simply allow
3788 * whitespace after paste token.
3789 */
3790 if (!next) {
3791 /*
3792 * Zap ending space tokens and that's all.
3793 */
3794 tok = (*prev_nonspace)->next;
3795 while (tok_type_(tok, TOK_WHITESPACE))
3796 tok = delete_Token(tok);
3797 tok = *prev_nonspace;
3798 tok->next = NULL;
3799 break;
3800 }
3801
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003802 tok = *prev_nonspace;
3803 while (tok_type_(tok, TOK_WHITESPACE))
3804 tok = delete_Token(tok);
3805 len = strlen(tok->text);
3806 len += strlen(next->text);
3807
3808 p = buf = nasm_malloc(len + 1);
3809 strcpy(p, tok->text);
3810 p = strchr(p, '\0');
3811 strcpy(p, next->text);
3812
3813 delete_Token(tok);
3814
3815 tok = tokenize(buf);
3816 nasm_free(buf);
3817
3818 *prev_nonspace = tok;
3819 while (tok && tok->next)
3820 tok = tok->next;
3821
3822 tok->next = delete_Token(next);
3823
3824 /* Restart from pasted tokens head */
3825 tok = *prev_nonspace;
3826 break;
3827
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003828 default:
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003829 /* implicit pasting */
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003830 for (i = 0; i < mnum; i++) {
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003831 if (!(PP_CONCAT_MATCH(tok, m[i].mask_head)))
3832 continue;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003833
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003834 len = 0;
3835 while (next && PP_CONCAT_MATCH(next, m[i].mask_tail)) {
3836 len += strlen(next->text);
3837 next = next->next;
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04003838 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003839
Cyrill Gorcunov6f8109e2017-10-22 21:26:36 +03003840 /* No match or no text to process */
3841 if (tok == next || len == 0)
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003842 break;
3843
3844 len += strlen(tok->text);
3845 p = buf = nasm_malloc(len + 1);
3846
Adam Majer1a069432017-07-25 11:12:35 +02003847 strcpy(p, tok->text);
3848 p = strchr(p, '\0');
3849 tok = delete_Token(tok);
3850
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003851 while (tok != next) {
Adam Majer1a069432017-07-25 11:12:35 +02003852 if (PP_CONCAT_MATCH(tok, m[i].mask_tail)) {
3853 strcpy(p, tok->text);
3854 p = strchr(p, '\0');
3855 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003856 tok = delete_Token(tok);
3857 }
3858
3859 tok = tokenize(buf);
3860 nasm_free(buf);
3861
3862 if (prev_next)
3863 *prev_next = tok;
3864 else
3865 *head = tok;
3866
3867 /*
3868 * Connect pasted into original stream,
3869 * ie A -> new-tokens -> B
3870 */
3871 while (tok && tok->next)
3872 tok = tok->next;
3873 tok->next = next;
3874
3875 if (!pasted)
3876 pasted = true;
3877
3878 /* Restart from pasted tokens head */
3879 tok = prev_next ? *prev_next : *head;
Cyrill Gorcunov575d4282010-10-06 00:25:55 +04003880 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003881
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03003882 break;
H. Peter Anvind784a082009-04-20 14:01:18 -07003883 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003884
3885 prev_next = &tok->next;
3886
3887 if (tok->next &&
3888 !tok_type_(tok->next, TOK_WHITESPACE) &&
3889 !tok_type_(tok->next, TOK_PASTE))
3890 prev_nonspace = prev_next;
3891
3892 tok = tok->next;
H. Peter Anvind784a082009-04-20 14:01:18 -07003893 }
Cyrill Gorcunov1cf9b312012-08-04 10:51:58 +04003894
3895 return pasted;
H. Peter Anvind784a082009-04-20 14:01:18 -07003896}
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003897
3898/*
3899 * expands to a list of tokens from %{x:y}
3900 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003901static Token *expand_mmac_params_range(MMacro *mac, Token *tline, Token ***last)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003902{
3903 Token *t = tline, **tt, *tm, *head;
3904 char *pos;
3905 int fst, lst, j, i;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03003906
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003907 pos = strchr(tline->text, ':');
3908 nasm_assert(pos);
3909
3910 lst = atoi(pos + 1);
3911 fst = atoi(tline->text + 1);
3912
3913 /*
3914 * only macros params are accounted so
3915 * if someone passes %0 -- we reject such
3916 * value(s)
3917 */
3918 if (lst == 0 || fst == 0)
3919 goto err;
3920
3921 /* the values should be sane */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003922 if ((fst > (int)mac->nparam || fst < (-(int)mac->nparam)) ||
3923 (lst > (int)mac->nparam || lst < (-(int)mac->nparam)))
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003924 goto err;
3925
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003926 fst = fst < 0 ? fst + (int)mac->nparam + 1: fst;
3927 lst = lst < 0 ? lst + (int)mac->nparam + 1: lst;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003928
3929 /* counted from zero */
3930 fst--, lst--;
3931
3932 /*
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003933 * It will be at least one token. Note we
3934 * need to scan params until separator, otherwise
3935 * only first token will be passed.
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003936 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003937 tm = mac->params[(fst + mac->rotate) % mac->nparam];
Cyrill Gorcunov67f2ca22018-10-13 19:41:01 +03003938 if (!tm)
3939 goto err;
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003940 head = new_Token(NULL, tm->type, tm->text, 0);
3941 tt = &head->next, tm = tm->next;
3942 while (tok_isnt_(tm, ",")) {
3943 t = new_Token(NULL, tm->type, tm->text, 0);
3944 *tt = t, tt = &t->next, tm = tm->next;
3945 }
3946
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003947 if (fst < lst) {
3948 for (i = fst + 1; i <= lst; i++) {
3949 t = new_Token(NULL, TOK_OTHER, ",", 0);
3950 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003951 j = (i + mac->rotate) % mac->nparam;
3952 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003953 while (tok_isnt_(tm, ",")) {
3954 t = new_Token(NULL, tm->type, tm->text, 0);
3955 *tt = t, tt = &t->next, tm = tm->next;
3956 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003957 }
3958 } else {
3959 for (i = fst - 1; i >= lst; i--) {
3960 t = new_Token(NULL, TOK_OTHER, ",", 0);
3961 *tt = t, tt = &t->next;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08003962 j = (i + mac->rotate) % mac->nparam;
3963 tm = mac->params[j];
Cyrill Gorcunove75331c2013-11-09 12:02:15 +04003964 while (tok_isnt_(tm, ",")) {
3965 t = new_Token(NULL, tm->type, tm->text, 0);
3966 *tt = t, tt = &t->next, tm = tm->next;
3967 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003968 }
3969 }
3970
3971 *last = tt;
3972 return head;
3973
3974err:
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03003975 nasm_nonfatal("`%%{%s}': macro parameters out of range",
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003976 &tline->text[1]);
3977 return tline;
3978}
3979
H. Peter Anvin76690a12002-04-30 20:52:49 +00003980/*
3981 * Expand MMacro-local things: parameter references (%0, %n, %+n,
H. Peter Anvin67c63722008-10-26 23:49:00 -07003982 * %-n) and MMacro-local identifiers (%%foo) as well as
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003983 * macro indirection (%[...]) and range (%{..:..}).
H. Peter Anvin76690a12002-04-30 20:52:49 +00003984 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003985static Token *expand_mmac_params(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003986{
H. Peter Anvin734b1882002-04-30 21:01:08 +00003987 Token *t, *tt, **tail, *thead;
H. Peter Anvin6125b622009-04-08 14:02:25 -07003988 bool changed = false;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04003989 char *pos;
H. Peter Anvin76690a12002-04-30 20:52:49 +00003990
3991 tail = &thead;
3992 thead = NULL;
3993
H. Peter Anvine2c80182005-01-15 22:15:51 +00003994 while (tline) {
Cyrill Gorcunov661f7232018-10-28 20:39:34 +03003995 if (tline->type == TOK_PREPROC_ID && tline->text && tline->text[0] &&
Cyrill Gorcunovca611192010-06-04 09:22:12 +04003996 (((tline->text[1] == '+' || tline->text[1] == '-') && tline->text[2]) ||
3997 (tline->text[1] >= '0' && tline->text[1] <= '9') ||
3998 tline->text[1] == '%')) {
Keith Kaniosa6dfa782007-04-13 16:47:53 +00003999 char *text = NULL;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004000 int type = 0, cc; /* type = 0 to placate optimisers */
Keith Kaniosa6dfa782007-04-13 16:47:53 +00004001 char tmpbuf[30];
H. Peter Anvin25a99342007-09-22 17:45:45 -07004002 unsigned int n;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004003 int i;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004004 MMacro *mac;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004005
H. Peter Anvine2c80182005-01-15 22:15:51 +00004006 t = tline;
4007 tline = tline->next;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004008
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004009 mac = istk->mstk;
4010 while (mac && !mac->name) /* avoid mistaking %reps for macros */
4011 mac = mac->next_active;
4012 if (!mac) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004013 nasm_nonfatal("`%s': not in a macro call", t->text);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004014 } else {
4015 pos = strchr(t->text, ':');
4016 if (!pos) {
4017 switch (t->text[1]) {
4018 /*
4019 * We have to make a substitution of one of the
4020 * forms %1, %-1, %+1, %%foo, %0.
4021 */
4022 case '0':
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004023 type = TOK_NUMBER;
4024 snprintf(tmpbuf, sizeof(tmpbuf), "%d", mac->nparam);
4025 text = nasm_strdup(tmpbuf);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004026 break;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004027 case '%':
H. Peter Anvine2c80182005-01-15 22:15:51 +00004028 type = TOK_ID;
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004029 snprintf(tmpbuf, sizeof(tmpbuf), "..@%"PRIu64".",
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004030 mac->unique);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004031 text = nasm_strcat(tmpbuf, t->text + 2);
4032 break;
4033 case '-':
4034 n = atoi(t->text + 2) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004035 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004036 tt = NULL;
4037 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004038 if (mac->nparam > 1)
4039 n = (n + mac->rotate) % mac->nparam;
4040 tt = mac->params[n];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004041 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004042 cc = find_cc(tt);
4043 if (cc == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004044 nasm_nonfatal("macro parameter %d is not a condition code",
4045 n + 1);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004046 text = NULL;
4047 } else {
4048 type = TOK_ID;
4049 if (inverse_ccs[cc] == -1) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004050 nasm_nonfatal("condition code `%s' is not invertible",
4051 conditions[cc]);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004052 text = NULL;
4053 } else
4054 text = nasm_strdup(conditions[inverse_ccs[cc]]);
4055 }
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];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004065 }
4066 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 text = nasm_strdup(conditions[cc]);
4074 }
4075 break;
4076 default:
4077 n = atoi(t->text + 1) - 1;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004078 if (n >= mac->nparam)
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004079 tt = NULL;
4080 else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004081 if (mac->nparam > 1)
4082 n = (n + mac->rotate) % mac->nparam;
4083 tt = mac->params[n];
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004084 }
4085 if (tt) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004086 for (i = 0; i < mac->paramlen[n]; i++) {
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004087 *tail = new_Token(NULL, tt->type, tt->text, 0);
4088 tail = &(*tail)->next;
4089 tt = tt->next;
4090 }
4091 }
4092 text = NULL; /* we've done it here */
4093 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004094 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004095 } else {
4096 /*
4097 * seems we have a parameters range here
4098 */
4099 Token *head, **last;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004100 head = expand_mmac_params_range(mac, t, &last);
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004101 if (head != t) {
4102 *tail = head;
4103 *last = tline;
4104 tline = head;
4105 text = NULL;
4106 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004107 }
Cyrill Gorcunovc29404d2010-06-05 01:50:23 +04004108 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004109 if (!text) {
4110 delete_Token(t);
4111 } else {
4112 *tail = t;
4113 tail = &t->next;
4114 t->type = type;
4115 nasm_free(t->text);
4116 t->text = text;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004117 t->len = strlen(text);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004118 }
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004119 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004120 continue;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004121 } else if (tline->type == TOK_INDIRECT) {
4122 t = tline;
4123 tline = tline->next;
4124 tt = tokenize(t->text);
4125 tt = expand_mmac_params(tt);
4126 tt = expand_smacro(tt);
4127 *tail = tt;
4128 while (tt) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004129 tail = &tt->next;
4130 tt = tt->next;
4131 }
4132 delete_Token(t);
4133 changed = true;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004134 } else {
4135 t = *tail = tline;
4136 tline = tline->next;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004137 tail = &t->next;
4138 }
H. Peter Anvin76690a12002-04-30 20:52:49 +00004139 }
H. Peter Anvineba20a72002-04-30 20:53:55 +00004140 *tail = NULL;
H. Peter Anvin67c63722008-10-26 23:49:00 -07004141
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004142 if (changed) {
4143 const struct tokseq_match t[] = {
4144 {
4145 PP_CONCAT_MASK(TOK_ID) |
4146 PP_CONCAT_MASK(TOK_FLOAT), /* head */
4147 PP_CONCAT_MASK(TOK_ID) |
4148 PP_CONCAT_MASK(TOK_NUMBER) |
4149 PP_CONCAT_MASK(TOK_FLOAT) |
4150 PP_CONCAT_MASK(TOK_OTHER) /* tail */
4151 },
4152 {
4153 PP_CONCAT_MASK(TOK_NUMBER), /* head */
4154 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4155 }
4156 };
4157 paste_tokens(&thead, t, ARRAY_SIZE(t), false);
4158 }
H. Peter Anvin6125b622009-04-08 14:02:25 -07004159
H. Peter Anvin76690a12002-04-30 20:52:49 +00004160 return thead;
4161}
4162
4163/*
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004164 * Expand *one* single-line macro instance. If the first token is not
4165 * a macro at all, it is simply copied to the output. tpp should be
4166 * a pointer to a pointer (usually the next pointer of the previous token)
4167 * to the first token. **tpp is updated to point to the pointer to first token
4168 * of the expansion, which is also the return value, and then *tpp will point to
4169 * the pointer to the first input token after the expansion.
4170 *
4171 * If the expansion is empty, then these two values point to the same token.
4172 *
4173 */
4174static Token *expand_smacro_noreset(Token * tline);
4175
4176static Token *expand_one_smacro(Token ***tpp)
4177{
4178 Token **params = NULL;
4179 const char *mname;
4180 Token *tline = **tpp;
4181 Token **tnextp;
4182 SMacro *head, *m;
4183 unsigned int nparam, i;
4184 Token *expansion;
4185 Token *t, *mstart, **ttail;
4186 bool free_expansion;
4187
4188 if (!tline)
4189 return NULL;
4190
4191 mname = tline->text;
4192 tnextp = &tline->next; /* By default we shift out one token */
4193
4194 if (tline->type == TOK_ID) {
4195 head = (SMacro *)hash_findix(&smacros, mname);
4196 } else if (tline->type == TOK_PREPROC_ID) {
4197 Context *ctx = get_ctx(mname, &mname);
4198 head = ctx ? (SMacro *)hash_findix(&ctx->localmac, mname) : NULL;
4199 } else {
4200 goto not_a_macro;
4201 }
4202
4203 /*
4204 * We've hit an identifier of some sort. First check whether the
4205 * identifier is a single-line macro at all, then think about
4206 * checking for parameters if necessary.
4207 */
4208 list_for_each(m, head) {
4209 if (!mstrcmp(m->name, mname, m->casesense))
4210 break;
4211 }
4212
4213 if (!m) {
4214 goto not_a_macro;
4215 }
4216
4217 /* Parse parameters, if applicable */
4218
4219 params = NULL;
4220 nparam = 0;
4221
4222 if (m->nparam == 0) {
4223 /*
4224 * Simple case: the macro is parameterless.
4225 * Nothing to parse; the expansion code will
4226 * drop the macro name token.
4227 */
4228 } else {
4229 /*
4230 * Complicated case: at least one macro with this name
4231 * exists and takes parameters. We must find the
4232 * parameters in the call, count them, find the SMacro
4233 * that corresponds to that form of the macro call, and
4234 * substitute for the parameters when we expand. What a
4235 * pain.
4236 */
4237 Token **pep;
4238 int paren;
4239 int white = 0;
4240 int brackets = 0;
4241 bool bracketed = false;
4242 bool bad_bracket = false;
4243 unsigned int sparam = PARAM_DELTA;
4244
4245 tline = tline->next;
4246
4247 while (tok_type_(tline, TOK_WHITESPACE)) {
4248 tline = tline->next;
4249 }
4250 if (!tok_is_(tline, "(")) {
4251 /*
4252 * This macro wasn't called with parameters: ignore
4253 * the call. (Behaviour borrowed from gnu cpp.)
4254 */
4255 goto not_a_macro;
4256 }
4257
4258 paren = 1;
4259 nparam = 0;
4260 params = nasm_malloc(sparam * sizeof *params);
4261 params[0] = NULL;
4262 pep = &params[0];
4263
4264 while (true) {
4265 bool skip = false;
4266 char ch;
4267
4268 if (!tline->next) {
4269 nasm_nonfatal("macro call expects terminating `)'");
4270 break;
4271 }
4272 tline = tline->next;
4273
4274 ch = (tline->type == TOK_OTHER && !tline->text[1])
4275 ? tline->text[0] : 0;
4276
4277 if (!brackets) {
4278 if (tline->type == TOK_WHITESPACE) {
4279 if (params[nparam]) {
4280 /* Keep interior whitespace */
4281 white++;
4282 }
4283 skip = true;
4284 }
4285
4286 switch (ch) {
4287 case ',':
4288 if (!paren) {
4289 if (++nparam >= sparam) {
4290 sparam += PARAM_DELTA;
4291 params = nasm_realloc(params, sparam * sizeof *params);
4292 }
4293 params[nparam] = NULL;
4294 pep = &params[nparam];
4295 white = 0;
4296 bracketed = false;
4297 }
4298 break;
4299
4300 case '{':
4301 brackets++;
4302 bracketed = skip = !params[nparam];
4303 break;
4304
4305 case '(':
4306 paren++;
4307 break;
4308
4309 case ')':
4310 paren--;
4311 break;
4312
4313 default:
4314 break; /* Just advance to the next token */
4315 }
4316 } else {
4317 if (ch == '}')
4318 brackets--;
4319
4320 if (brackets == 0)
4321 skip = bracketed;
4322 }
4323
4324 if (!paren)
4325 break; /* We are done */
4326
4327 if (!skip) {
4328 Token *t;
4329
4330 if (bracketed && !brackets)
4331 bad_bracket = true;
4332
4333 if (white) {
4334 t = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
4335 *pep = t;
4336 pep = &t->next;
4337 }
4338
4339 t = new_Token(NULL, tline->type, tline->text, 0);
4340 *pep = t;
4341 pep = &t->next;
4342 white = 0;
4343 }
4344 }
4345 nparam++; /* Count the last parameter */
4346
4347 if (bad_bracket)
4348 nasm_nonfatal("braces do not enclose all of macro parameter");
4349
4350 /* Look for a macro matching in both name and parameter count */
4351 while (m && (m->nparam != nparam ||
4352 mstrcmp(m->name, mname, m->casesense)))
4353 m = m->next;
4354
4355 if (!m) {
4356 /*!
4357 *!macro-params [on] macro calls with wrong parameter count
4358 *! covers warnings about \i{multi-line macros} being invoked
4359 *! with the wrong number of parameters. See \k{mlmacover} for an
4360 *! example of why you might want to disable this warning.
4361 */
4362 nasm_warn(WARN_MACRO_PARAMS,
4363 "macro `%s' exists, "
4364 "but not taking %d parameters",
4365 mname, nparam);
4366 goto not_a_macro_cleanup;
4367 }
4368 }
4369
4370 if (m->in_progress)
4371 goto not_a_macro_cleanup;
4372
4373 /* Expand each parameter */
4374 m->in_progress = true;
4375
4376 for (i = 0; i < nparam; i++) {
4377 params[i] = expand_smacro_noreset(params[i]);
4378
4379 if (m->eval_param && m->eval_param[i]) {
4380 /* Evaluate this parameter as a number */
4381 struct ppscan pps;
4382 struct tokenval tokval;
4383 expr *evalresult;
4384
4385 pps.tptr = params[i];
4386 pps.ntokens = -1;
4387 tokval.t_type = TOKEN_INVALID;
4388 evalresult = evaluate(ppscan, &pps, &tokval, NULL, true, NULL);
4389
4390 if (!evalresult) {
4391 /* Nothing meaningful to do */
4392 } else if (tokval.t_type) {
4393 nasm_nonfatal("invalid expression in parameter %d of macro `%s'", i, m->name);
4394 } else if (!is_simple(evalresult)) {
4395 nasm_nonfatal("non-constant expression in parameter %d of macro `%s'", i, m->name);
4396 } else {
4397 free_tlist(params[i]);
4398 params[i] = make_tok_num(reloc_value(evalresult));
4399 }
4400 }
4401
4402 /* Put tokens in reverse order like the expansion list */
4403 params[i] = reverse_tokens(params[i]);
4404 }
4405
4406 t = tline;
4407 tline = tline->next;
4408 t->next = NULL; /* Remove the macro call from the input */
4409
4410 if (unlikely(m->magic)) {
4411 expansion = m->magic(m, params, nparam, &free_expansion);
4412 } else {
4413 expansion = m->expansion;
4414 free_expansion = false;
4415 }
4416
4417 ttail = NULL;
4418
4419 list_for_each(t, expansion) {
4420 if (is_smac_param(t->type)) {
4421 Token *ttt;
4422 list_for_each(ttt, params[smac_nparam(t->type)]) {
4423 tline = dup_Token(tline, ttt);
4424 if (!ttail)
4425 ttail = &tline->next;
4426 }
4427 } else {
4428 if (t->type == TOK_PREPROC_Q) {
4429 tline = new_Token(tline, TOK_ID, mname, 0);
4430 } else if (t->type == TOK_PREPROC_QQ) {
4431 tline = new_Token(tline, TOK_ID, m->name, 0);
4432 } else {
4433 tline = dup_Token(tline, t);
4434 }
4435 if (!ttail)
4436 ttail = &tline->next;
4437 }
4438 }
4439
4440 if (free_expansion)
4441 free_tlist(expansion);
4442
4443 m->in_progress = false;
4444
4445 /* Don't do this until after expansion or we will clobber mname */
4446 mstart = **tpp;
4447 tnextp = ttail ? ttail : *tpp;
4448 **tpp = tline;
4449 free_tlist(mstart);
4450
4451 /*
4452 * No macro expansion needed; roll back to mstart (if necessary)
4453 * and then advance to the next input token.
4454 */
4455not_a_macro_cleanup:
4456 nasm_free(params);
4457
4458not_a_macro:
4459 t = **tpp;
4460 *tpp = tnextp;
4461 return t;
4462}
4463
4464/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004465 * Expand all single-line macro calls made in the given line.
4466 * Return the expanded version of the line. The original is deemed
4467 * to be destroyed in the process. (In reality we'll just move
4468 * Tokens from input to output a lot of the time, rather than
4469 * actually bothering to destroy and replicate.)
4470 */
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004471static int64_t smacro_deadman;
4472static Token *expand_smacro(Token *tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004473{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004474 smacro_deadman = nasm_limit[LIMIT_MACROS];
4475 return expand_smacro_noreset(tline);
4476}
4477
4478static Token *expand_smacro_noreset(Token * tline)
4479{
4480 Token *t, **tail, *thead;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004481 Token *org_tline = tline;
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004482 bool expanded;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004483
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004484 /*
4485 * Trick: we should avoid changing the start token pointer since it can
4486 * be contained in "next" field of other token. Because of this
4487 * we allocate a copy of first token and work with it; at the end of
4488 * routine we copy it back
4489 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004490 if (org_tline) {
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004491 tline = new_Token(org_tline->next, org_tline->type,
4492 org_tline->text, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +00004493 nasm_free(org_tline->text);
4494 org_tline->text = NULL;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004495 }
4496
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004497 expanded = true; /* Always expand %+ at least once */
H. Peter Anvin8287daf2009-07-07 16:00:58 -07004498
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004499again:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004500 thead = tline;
Cyrill Gorcunoved4a8052010-04-09 15:40:35 +04004501 tail = &thead;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004502
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004503 while ((t = *tail)) { /* main token loop */
4504 if (!--smacro_deadman) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004505 nasm_nonfatal("interminable macro recursion");
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004506 goto err;
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004507 }
H. Peter Anvincb1cf592007-11-19 12:26:50 -08004508
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07004509 /*
4510 * An expansion happened if and only if the first token of the
4511 * expansion was the first token of the original simply moved over.
4512 */
4513 expanded |= (expand_one_smacro(&tail) != t);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004514 }
4515
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004516 /*
4517 * Now scan the entire line and look for successive TOK_IDs that resulted
Keith Kaniosb7a89542007-04-12 02:40:54 +00004518 * after expansion (they can't be produced by tokenize()). The successive
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004519 * TOK_IDs should be concatenated.
4520 * Also we look for %+ tokens and concatenate the tokens before and after
4521 * them (without white spaces in between).
4522 */
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004523 if (expanded) {
Cyrill Gorcunovc6a742c2011-06-27 01:23:09 +04004524 const struct tokseq_match t[] = {
4525 {
4526 PP_CONCAT_MASK(TOK_ID) |
4527 PP_CONCAT_MASK(TOK_PREPROC_ID), /* head */
4528 PP_CONCAT_MASK(TOK_ID) |
4529 PP_CONCAT_MASK(TOK_PREPROC_ID) |
4530 PP_CONCAT_MASK(TOK_NUMBER) /* tail */
4531 }
4532 };
4533 if (paste_tokens(&thead, t, ARRAY_SIZE(t), true)) {
Cyrill Gorcunov8dcbbd72010-09-25 02:33:20 +04004534 /*
4535 * If we concatenated something, *and* we had previously expanded
4536 * an actual macro, scan the lines again for macros...
4537 */
4538 tline = thead;
4539 expanded = false;
4540 goto again;
4541 }
H. Peter Anvin734b1882002-04-30 21:01:08 +00004542 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004543
Cyrill Gorcunovbd38c8f2009-11-21 11:11:23 +03004544err:
H. Peter Anvine2c80182005-01-15 22:15:51 +00004545 if (org_tline) {
4546 if (thead) {
4547 *org_tline = *thead;
4548 /* since we just gave text to org_line, don't free it */
4549 thead->text = NULL;
4550 delete_Token(thead);
4551 } else {
4552 /* the expression expanded to empty line;
4553 we can't return NULL for some reasons
4554 we just set the line to a single WHITESPACE token. */
4555 memset(org_tline, 0, sizeof(*org_tline));
4556 org_tline->text = NULL;
4557 org_tline->type = TOK_WHITESPACE;
4558 }
4559 thead = org_tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004560 }
4561
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004562 return thead;
4563}
4564
4565/*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004566 * Similar to expand_smacro but used exclusively with macro identifiers
4567 * right before they are fetched in. The reason is that there can be
4568 * identifiers consisting of several subparts. We consider that if there
4569 * are more than one element forming the name, user wants a expansion,
4570 * otherwise it will be left as-is. Example:
4571 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004572 * %define %$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004573 *
4574 * the identifier %$abc will be left as-is so that the handler for %define
4575 * will suck it and define the corresponding value. Other case:
4576 *
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004577 * %define _%$abc cde
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004578 *
4579 * In this case user wants name to be expanded *before* %define starts
4580 * working, so we'll expand %$abc into something (if it has a value;
4581 * otherwise it will be left as-is) then concatenate all successive
4582 * PP_IDs into one.
4583 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00004584static Token *expand_id(Token * tline)
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004585{
4586 Token *cur, *oldnext = NULL;
4587
H. Peter Anvin734b1882002-04-30 21:01:08 +00004588 if (!tline || !tline->next)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004589 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004590
4591 cur = tline;
4592 while (cur->next &&
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004593 (cur->next->type == TOK_ID ||
4594 cur->next->type == TOK_PREPROC_ID
4595 || cur->next->type == TOK_NUMBER))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004596 cur = cur->next;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004597
4598 /* If identifier consists of just one token, don't expand */
4599 if (cur == tline)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004600 return tline;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004601
H. Peter Anvine2c80182005-01-15 22:15:51 +00004602 if (cur) {
4603 oldnext = cur->next; /* Detach the tail past identifier */
4604 cur->next = NULL; /* so that expand_smacro stops here */
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004605 }
4606
H. Peter Anvin734b1882002-04-30 21:01:08 +00004607 tline = expand_smacro(tline);
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004608
H. Peter Anvine2c80182005-01-15 22:15:51 +00004609 if (cur) {
4610 /* expand_smacro possibly changhed tline; re-scan for EOL */
4611 cur = tline;
4612 while (cur && cur->next)
4613 cur = cur->next;
4614 if (cur)
4615 cur->next = oldnext;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004616 }
4617
4618 return tline;
4619}
4620
4621/*
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004622 * Determine whether the given line constitutes a multi-line macro
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004623 * call, and return the MMacro structure called if so. Doesn't have
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004624 * to check for an initial label - that's taken care of in
4625 * expand_mmacro - but must check numbers of parameters. Guaranteed
4626 * to be called with tline->type == TOK_ID, so the putative macro
4627 * name is easy to find.
4628 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004629static MMacro *is_mmacro(Token * tline, Token *** params_array)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004630{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004631 MMacro *head, *m;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004632 Token **params;
4633 int nparam;
4634
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004635 head = (MMacro *) hash_findix(&mmacros, tline->text);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004636
4637 /*
4638 * Efficiency: first we see if any macro exists with the given
4639 * name. If not, we can return NULL immediately. _Then_ we
4640 * count the parameters, and then we look further along the
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004641 * list if necessary to find the proper MMacro.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004642 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004643 list_for_each(m, head)
4644 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004645 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004646 if (!m)
H. Peter Anvine2c80182005-01-15 22:15:51 +00004647 return NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004648
4649 /*
4650 * OK, we have a potential macro. Count and demarcate the
4651 * parameters.
4652 */
H. Peter Anvin734b1882002-04-30 21:01:08 +00004653 count_mmac_params(tline->next, &nparam, &params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004654
4655 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004656 * So we know how many parameters we've got. Find the MMacro
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004657 * structure that handles this number.
4658 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004659 while (m) {
4660 if (m->nparam_min <= nparam
4661 && (m->plus || nparam <= m->nparam_max)) {
4662 /*
4663 * This one is right. Just check if cycle removal
4664 * prohibits us using it before we actually celebrate...
4665 */
4666 if (m->in_progress > m->max_depth) {
4667 if (m->max_depth > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08004668 nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i",
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004669 m->max_depth);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004670 }
4671 nasm_free(params);
4672 return NULL;
4673 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004674 /*
4675 * It's right, and we can use it. Add its default
4676 * parameters to the end of our list if necessary.
4677 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004678 if (m->defaults && nparam < m->nparam_min + m->ndefs) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004679 params =
4680 nasm_realloc(params,
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004681 ((m->nparam_min + m->ndefs +
H. Peter Anvine2c80182005-01-15 22:15:51 +00004682 1) * sizeof(*params)));
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004683 while (nparam < m->nparam_min + m->ndefs) {
4684 params[nparam] = m->defaults[nparam - m->nparam_min];
H. Peter Anvine2c80182005-01-15 22:15:51 +00004685 nparam++;
4686 }
4687 }
4688 /*
4689 * If we've gone over the maximum parameter count (and
4690 * we're in Plus mode), ignore parameters beyond
4691 * nparam_max.
4692 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004693 if (m->plus && nparam > m->nparam_max)
4694 nparam = m->nparam_max;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004695 /*
4696 * Then terminate the parameter list, and leave.
4697 */
4698 if (!params) { /* need this special case */
4699 params = nasm_malloc(sizeof(*params));
4700 nparam = 0;
4701 }
4702 params[nparam] = NULL;
4703 *params_array = params;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004704 return m;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004705 }
4706 /*
4707 * This one wasn't right: look for the next one with the
4708 * same name.
4709 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004710 list_for_each(m, m->next)
4711 if (!mstrcmp(m->name, tline->text, m->casesense))
H. Peter Anvine2c80182005-01-15 22:15:51 +00004712 break;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004713 }
4714
4715 /*
4716 * After all that, we didn't find one with the right number of
4717 * parameters. Issue a warning, and fail to expand the macro.
4718 */
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08004719 nasm_warn(WARN_MACRO_PARAMS,
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004720 "macro `%s' exists, but not taking %d parameters",
4721 tline->text, nparam);
H. Peter Anvin734b1882002-04-30 21:01:08 +00004722 nasm_free(params);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004723 return NULL;
4724}
4725
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004726
4727/*
4728 * Save MMacro invocation specific fields in
4729 * preparation for a recursive macro expansion
4730 */
4731static void push_mmacro(MMacro *m)
4732{
4733 MMacroInvocation *i;
4734
4735 i = nasm_malloc(sizeof(MMacroInvocation));
4736 i->prev = m->prev;
4737 i->params = m->params;
4738 i->iline = m->iline;
4739 i->nparam = m->nparam;
4740 i->rotate = m->rotate;
4741 i->paramlen = m->paramlen;
4742 i->unique = m->unique;
4743 i->condcnt = m->condcnt;
4744 m->prev = i;
4745}
4746
4747
4748/*
4749 * Restore MMacro invocation specific fields that were
4750 * saved during a previous recursive macro expansion
4751 */
4752static void pop_mmacro(MMacro *m)
4753{
4754 MMacroInvocation *i;
4755
4756 if (m->prev) {
4757 i = m->prev;
4758 m->prev = i->prev;
4759 m->params = i->params;
4760 m->iline = i->iline;
4761 m->nparam = i->nparam;
4762 m->rotate = i->rotate;
4763 m->paramlen = i->paramlen;
4764 m->unique = i->unique;
4765 m->condcnt = i->condcnt;
4766 nasm_free(i);
4767 }
4768}
4769
4770
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004771/*
4772 * Expand the multi-line macro call made by the given line, if
4773 * there is one to be expanded. If there is, push the expansion on
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004774 * istk->expansion and return 1. Otherwise return 0.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004775 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004776static int expand_mmacro(Token * tline)
H. Peter Anvineba20a72002-04-30 20:53:55 +00004777{
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004778 Token *startline = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004779 Token *label = NULL;
4780 int dont_prepend = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004781 Token **params, *t, *tt;
4782 MMacro *m;
4783 Line *l, *ll;
H. Peter Anvin76690a12002-04-30 20:52:49 +00004784 int i, nparam, *paramlen;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004785 const char *mname;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004786
4787 t = tline;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004788 skip_white_(t);
H. Peter Anvince2233b2008-05-25 21:57:00 -07004789 /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */
H. Peter Anvindce1e2f2002-04-30 21:06:37 +00004790 if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID))
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004791 return 0;
4792 m = is_mmacro(t, &params);
4793 if (m) {
Cyrill Gorcunovaccda192010-02-16 10:27:56 +03004794 mname = t->text;
H. Peter Anvinc751e862008-06-09 10:18:45 -07004795 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00004796 Token *last;
4797 /*
4798 * We have an id which isn't a macro call. We'll assume
4799 * it might be a label; we'll also check to see if a
4800 * colon follows it. Then, if there's another id after
4801 * that lot, we'll check it again for macro-hood.
4802 */
4803 label = last = t;
4804 t = t->next;
4805 if (tok_type_(t, TOK_WHITESPACE))
4806 last = t, t = t->next;
4807 if (tok_is_(t, ":")) {
4808 dont_prepend = 1;
4809 last = t, t = t->next;
4810 if (tok_type_(t, TOK_WHITESPACE))
4811 last = t, t = t->next;
4812 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004813 if (!tok_type_(t, TOK_ID) || !(m = is_mmacro(t, &params)))
4814 return 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004815 last->next = NULL;
Keith Kanios891775e2009-07-11 06:08:54 -05004816 mname = t->text;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004817 tline = t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00004818 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004819
4820 /*
4821 * Fix up the parameters: this involves stripping leading and
4822 * trailing whitespace, then stripping braces if they are
4823 * present.
4824 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004825 for (nparam = 0; params[nparam]; nparam++) ;
H. Peter Anvin734b1882002-04-30 21:01:08 +00004826 paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004827
H. Peter Anvine2c80182005-01-15 22:15:51 +00004828 for (i = 0; params[i]; i++) {
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004829 int brace = 0;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004830 int comma = (!m->plus || i < nparam - 1);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004831
H. Peter Anvine2c80182005-01-15 22:15:51 +00004832 t = params[i];
4833 skip_white_(t);
4834 if (tok_is_(t, "{"))
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004835 t = t->next, brace++, comma = false;
H. Peter Anvine2c80182005-01-15 22:15:51 +00004836 params[i] = t;
4837 paramlen[i] = 0;
4838 while (t) {
4839 if (comma && t->type == TOK_OTHER && !strcmp(t->text, ","))
4840 break; /* ... because we have hit a comma */
4841 if (comma && t->type == TOK_WHITESPACE
4842 && tok_is_(t->next, ","))
4843 break; /* ... or a space then a comma */
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004844 if (brace && t->type == TOK_OTHER) {
4845 if (t->text[0] == '{')
4846 brace++; /* ... or a nested opening brace */
4847 else if (t->text[0] == '}')
4848 if (!--brace)
4849 break; /* ... or a brace */
4850 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00004851 t = t->next;
4852 paramlen[i]++;
4853 }
Jin Kyu Song5eac14b2013-11-27 20:52:16 -08004854 if (brace)
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03004855 nasm_nonfatal("macro params should be enclosed in braces");
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004856 }
4857
4858 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004859 * OK, we have a MMacro structure together with a set of
4860 * parameters. We must now go through the expansion and push
4861 * copies of each Line on to istk->expansion. Substitution of
H. Peter Anvin76690a12002-04-30 20:52:49 +00004862 * parameter tokens and macro-local tokens doesn't get done
4863 * until the single-line macro substitution process; this is
4864 * because delaying them allows us to change the semantics
4865 * later through %rotate.
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004866 *
4867 * First, push an end marker on to istk->expansion, mark this
4868 * macro as in progress, and set up its invocation-specific
4869 * variables.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004870 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004871 ll = nasm_malloc(sizeof(Line));
4872 ll->next = istk->expansion;
4873 ll->finishes = m;
4874 ll->first = NULL;
4875 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004876
4877 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004878 * Save the previous MMacro expansion in the case of
4879 * macro recursion
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004880 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004881 if (m->max_depth && m->in_progress)
4882 push_mmacro(m);
4883
4884 m->in_progress ++;
4885 m->params = params;
4886 m->iline = tline;
4887 m->nparam = nparam;
4888 m->rotate = 0;
4889 m->paramlen = paramlen;
4890 m->unique = unique++;
4891 m->lineno = 0;
4892 m->condcnt = 0;
4893
4894 m->next_active = istk->mstk;
4895 istk->mstk = m;
4896
4897 list_for_each(l, m->expansion) {
4898 Token **tail;
4899
4900 ll = nasm_malloc(sizeof(Line));
4901 ll->finishes = NULL;
4902 ll->next = istk->expansion;
4903 istk->expansion = ll;
4904 tail = &ll->first;
4905
4906 list_for_each(t, l->first) {
4907 Token *x = t;
4908 switch (t->type) {
4909 case TOK_PREPROC_Q:
4910 tt = *tail = new_Token(NULL, TOK_ID, mname, 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004911 break;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004912 case TOK_PREPROC_QQ:
4913 tt = *tail = new_Token(NULL, TOK_ID, m->name, 0);
4914 break;
4915 case TOK_PREPROC_ID:
4916 if (t->text[1] == '0' && t->text[2] == '0') {
4917 dont_prepend = -1;
4918 x = label;
4919 if (!x)
4920 continue;
4921 }
4922 /* fall through */
4923 default:
4924 tt = *tail = new_Token(NULL, x->type, x->text, 0);
4925 break;
4926 }
4927 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004928 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004929 *tail = NULL;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004930 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004931
4932 /*
H. Peter Anvineba20a72002-04-30 20:53:55 +00004933 * If we had a label, push it on as the first line of
4934 * the macro expansion.
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004935 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004936 if (label) {
4937 if (dont_prepend < 0)
4938 free_tlist(startline);
4939 else {
4940 ll = nasm_malloc(sizeof(Line));
4941 ll->finishes = NULL;
4942 ll->next = istk->expansion;
4943 istk->expansion = ll;
4944 ll->first = startline;
4945 if (!dont_prepend) {
4946 while (label->next)
4947 label = label->next;
4948 label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004949 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03004950 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00004951 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004952
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08004953 lfmt->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00004954
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004955 return 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00004956}
4957
H. Peter Anvin130736c2016-02-17 20:27:41 -08004958/*
4959 * This function adds macro names to error messages, and suppresses
4960 * them if necessary.
4961 */
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08004962static void pp_verror(errflags severity, const char *fmt, va_list arg)
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004963{
H. Peter Anvin130736c2016-02-17 20:27:41 -08004964 char buff[BUFSIZ];
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004965 MMacro *mmac = NULL;
4966 int delta = 0;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004967
H. Peter Anvin130736c2016-02-17 20:27:41 -08004968 /*
4969 * If we're in a dead branch of IF or something like it, ignore the error.
4970 * However, because %else etc are evaluated in the state context
4971 * of the previous branch, errors might get lost:
4972 * %if 0 ... %else trailing garbage ... %endif
4973 * So %else etc should set the ERR_PP_PRECOND flag.
4974 */
4975 if ((severity & ERR_MASK) < ERR_FATAL &&
4976 istk && istk->conds &&
4977 ((severity & ERR_PP_PRECOND) ?
4978 istk->conds->state == COND_NEVER :
H. Peter Anvineb6653f2016-04-05 13:03:10 -07004979 !emitting(istk->conds->state)))
H. Peter Anvin130736c2016-02-17 20:27:41 -08004980 return;
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004981
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004982 /* get %macro name */
H. Peter Anvin130736c2016-02-17 20:27:41 -08004983 if (!(severity & ERR_NOFILE) && istk && istk->mstk) {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004984 mmac = istk->mstk;
4985 /* but %rep blocks should be skipped */
4986 while (mmac && !mmac->name)
4987 mmac = mmac->next_active, delta++;
Cyrill Gorcunov9900c6b2011-10-09 18:58:46 +04004988 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08004989
H. Peter Anvin130736c2016-02-17 20:27:41 -08004990 if (mmac) {
4991 vsnprintf(buff, sizeof(buff), fmt, arg);
Victor van den Elzen3b404c02008-09-18 13:51:36 +02004992
H. Peter Anvin130736c2016-02-17 20:27:41 -08004993 nasm_set_verror(real_verror);
4994 nasm_error(severity, "(%s:%d) %s",
4995 mmac->name, mmac->lineno - delta, buff);
4996 nasm_set_verror(pp_verror);
4997 } else {
4998 real_verror(severity, fmt, arg);
4999 }
H. Peter Anvinaf535c12002-04-30 20:59:21 +00005000}
5001
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005002static Token *stdmac_file(const SMacro *s, Token **params,
5003 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005004{
5005 (void)s;
5006 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005007 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005008
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005009 *free_expansion = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005010 return make_tok_qstr(src_get_fname());
5011}
5012
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005013static Token *stdmac_line(const SMacro *s, Token **params,
5014 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005015{
5016 (void)s;
5017 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005018 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005019
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005020 *free_expansion = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005021 return make_tok_num(src_get_linnum());
5022}
5023
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005024static Token *stdmac_bits(const SMacro *s, Token **params,
5025 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005026{
5027 (void)s;
5028 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005029 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005030
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005031 *free_expansion = true;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005032 return make_tok_num(globalbits);
5033}
5034
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005035static Token *stdmac_ptr(const SMacro *s, Token **params,
5036 unsigned int nparams, bool *free_expansion)
H. Peter Anvin8b262472019-02-26 14:00:54 -08005037{
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005038 const Token *t;
5039 static const Token ptr_word = { NULL, "word", 4, TOKEN_ID };
5040 static const Token ptr_dword = { NULL, "dword", 5, TOKEN_ID };
5041 static const Token ptr_qword = { NULL, "qword", 5, TOKEN_ID };
H. Peter Anvin8b262472019-02-26 14:00:54 -08005042
5043 (void)s;
5044 (void)params;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005045 (void)nparams;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005046
5047 switch (globalbits) {
5048 case 16:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005049 t = &ptr_word;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005050 break;
5051 case 32:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005052 t = &ptr_dword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005053 break;
5054 case 64:
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005055 t = &ptr_qword;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005056 break;
5057 default:
5058 panic();
5059 }
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005060
5061 *free_expansion = false;
5062 return (Token *)t;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005063}
5064
H. Peter Anvin8b262472019-02-26 14:00:54 -08005065/* Add magic standard macros */
5066struct magic_macros {
5067 const char *name;
5068 int nparams;
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005069 MagicSMacro func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005070};
5071static const struct magic_macros magic_macros[] =
5072{
5073 { "__FILE__", 0, stdmac_file },
5074 { "__LINE__", 0, stdmac_line },
5075 { "__BITS__", 0, stdmac_bits },
5076 { "__PTR__", 0, stdmac_ptr },
H. Peter Anvin8b262472019-02-26 14:00:54 -08005077 { NULL, 0, NULL }
5078};
5079
5080static void pp_add_magic_stdmac(void)
5081{
5082 const struct magic_macros *m;
5083 SMacro *s;
5084
5085 for (m = magic_macros; m->name; m++) {
5086 s = define_smacro(NULL, m->name, true, m->nparams, NULL);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005087 s->magic = m->func;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005088 }
5089}
5090
H. Peter Anvin734b1882002-04-30 21:01:08 +00005091static void
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005092pp_reset(const char *file, enum preproc_mode mode, struct strlist *dep_list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005093{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005094 int apass;
H. Peter Anvin7383b402008-09-24 10:20:40 -07005095
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005096 cstk = NULL;
H. Peter Anvin8b262472019-02-26 14:00:54 -08005097 nasm_new(istk);
H. Peter Anvin3e83cec2016-05-25 04:28:46 -07005098 istk->fp = nasm_open_read(file, NF_TEXT);
H. Peter Anvin274cda82016-05-10 02:56:29 -07005099 src_set(0, file);
H. Peter Anvineba20a72002-04-30 20:53:55 +00005100 istk->lineinc = 1;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005101 if (!istk->fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +03005102 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005103 defining = NULL;
Charles Crayned4200be2008-07-12 16:42:33 -07005104 nested_mac_count = 0;
5105 nested_rep_count = 0;
H. Peter Anvin97a23472007-09-16 17:57:25 -07005106 init_macros();
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005107 unique = 0;
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005108 deplist = dep_list;
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005109 pp_mode = mode;
H. Peter Anvinf7606612016-07-13 14:23:48 -07005110
H. Peter Anvin8b262472019-02-26 14:00:54 -08005111 pp_add_magic_stdmac();
5112
H. Peter Anvinf7606612016-07-13 14:23:48 -07005113 if (tasm_compatible_mode)
5114 pp_add_stdmac(nasm_stdmac_tasm);
5115
5116 pp_add_stdmac(nasm_stdmac_nasm);
5117 pp_add_stdmac(nasm_stdmac_version);
5118
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005119 if (extrastdmac)
5120 pp_add_stdmac(extrastdmac);
5121
H. Peter Anvinf7606612016-07-13 14:23:48 -07005122 stdmacpos = stdmacros[0];
5123 stdmacnext = &stdmacros[1];
5124
H. Peter Anvind2456592008-06-19 15:04:18 -07005125 do_predef = true;
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005126
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +03005127 strlist_add(deplist, file);
H. Peter Anvin (Intel)f7106d02018-10-25 12:33:58 -07005128
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005129 /*
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005130 * Define the __PASS__ macro. This is defined here unlike all the
5131 * other builtins, because it is special -- it varies between
5132 * passes -- but there is really no particular reason to make it
5133 * magic.
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005134 *
5135 * 0 = dependencies only
5136 * 1 = preparatory passes
5137 * 2 = final pass
5138 * 3 = preproces only
H. Peter Anvin61f130f2008-09-25 15:45:06 -07005139 */
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005140 switch (mode) {
5141 case PP_NORMAL:
5142 apass = pass_final() ? 2 : 1;
5143 break;
5144 case PP_DEPS:
5145 apass = 0;
5146 break;
5147 case PP_PREPROC:
5148 apass = 3;
5149 break;
5150 default:
5151 panic();
5152 }
5153
H. Peter Anvin (Intel)9bb55bd2019-04-24 11:14:43 -07005154 define_smacro(NULL, "__PASS__", true, 0, make_tok_num(apass));
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005155}
5156
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005157static void pp_init(void)
5158{
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005159}
5160
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005161static char *pp_getline(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005162{
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005163 char *line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005164 Token *tline;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005165
H. Peter Anvin130736c2016-02-17 20:27:41 -08005166 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005167
H. Peter Anvine2c80182005-01-15 22:15:51 +00005168 while (1) {
5169 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005170 * Fetch a tokenized line, either from the macro-expansion
H. Peter Anvine2c80182005-01-15 22:15:51 +00005171 * buffer or from the input file.
5172 */
5173 tline = NULL;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005174 while (istk->expansion && istk->expansion->finishes) {
5175 Line *l = istk->expansion;
5176 if (!l->finishes->name && l->finishes->in_progress > 1) {
5177 Line *ll;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005178
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005179 /*
5180 * This is a macro-end marker for a macro with no
5181 * name, which means it's not really a macro at all
5182 * but a %rep block, and the `in_progress' field is
5183 * more than 1, meaning that we still need to
5184 * repeat. (1 means the natural last repetition; 0
5185 * means termination by %exitrep.) We have
5186 * therefore expanded up to the %endrep, and must
5187 * push the whole block on to the expansion buffer
5188 * again. We don't bother to remove the macro-end
5189 * marker: we'd only have to generate another one
5190 * if we did.
5191 */
5192 l->finishes->in_progress--;
5193 list_for_each(l, l->finishes->expansion) {
5194 Token *t, *tt, **tail;
5195
5196 ll = nasm_malloc(sizeof(Line));
5197 ll->next = istk->expansion;
5198 ll->finishes = NULL;
5199 ll->first = NULL;
5200 tail = &ll->first;
5201
5202 list_for_each(t, l->first) {
5203 if (t->text || t->type == TOK_WHITESPACE) {
5204 tt = *tail = new_Token(NULL, t->type, t->text, 0);
5205 tail = &tt->next;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005206 }
5207 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005208
5209 istk->expansion = ll;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005210 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005211 } else {
5212 /*
5213 * Check whether a `%rep' was started and not ended
5214 * within this macro expansion. This can happen and
5215 * should be detected. It's a fatal error because
5216 * I'm too confused to work out how to recover
5217 * sensibly from it.
5218 */
5219 if (defining) {
5220 if (defining->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005221 nasm_panic("defining with name in expansion");
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005222 else if (istk->mstk->name)
H. Peter Anvinc5136902018-06-15 18:20:17 -07005223 nasm_fatal("`%%rep' without `%%endrep' within"
H. Peter Anvin130736c2016-02-17 20:27:41 -08005224 " expansion of macro `%s'",
5225 istk->mstk->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005226 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005227
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005228 /*
5229 * FIXME: investigate the relationship at this point between
5230 * istk->mstk and l->finishes
5231 */
5232 {
5233 MMacro *m = istk->mstk;
5234 istk->mstk = m->next_active;
5235 if (m->name) {
5236 /*
5237 * This was a real macro call, not a %rep, and
5238 * therefore the parameter information needs to
5239 * be freed.
5240 */
5241 if (m->prev) {
5242 pop_mmacro(m);
5243 l->finishes->in_progress --;
5244 } else {
5245 nasm_free(m->params);
5246 free_tlist(m->iline);
5247 nasm_free(m->paramlen);
5248 l->finishes->in_progress = 0;
5249 }
Adam Majer91e72402017-07-25 10:42:01 +02005250 }
5251
5252 /*
5253 * FIXME It is incorrect to always free_mmacro here.
5254 * It leads to usage-after-free.
5255 *
5256 * https://bugzilla.nasm.us/show_bug.cgi?id=3392414
5257 */
5258#if 0
5259 else
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005260 free_mmacro(m);
Adam Majer91e72402017-07-25 10:42:01 +02005261#endif
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005262 }
5263 istk->expansion = l->next;
5264 nasm_free(l);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005265 lfmt->downlevel(LIST_MACRO);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005266 }
5267 }
5268 while (1) { /* until we get a line we can use */
5269
5270 if (istk->expansion) { /* from a macro expansion */
5271 char *p;
5272 Line *l = istk->expansion;
5273 if (istk->mstk)
5274 istk->mstk->lineno++;
5275 tline = l->first;
5276 istk->expansion = l->next;
5277 nasm_free(l);
5278 p = detoken(tline, false);
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005279 lfmt->line(LIST_MACRO, p);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005280 nasm_free(p);
5281 break;
5282 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005283 line = read_line();
5284 if (line) { /* from the current input file */
5285 line = prepreproc(line);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005286 tline = tokenize(line);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005287 nasm_free(line);
5288 break;
5289 }
5290 /*
5291 * The current file has ended; work down the istk
5292 */
5293 {
5294 Include *i = istk;
5295 fclose(i->fp);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005296 if (i->conds) {
5297 /* nasm_error can't be conditionally suppressed */
H. Peter Anvinc5136902018-06-15 18:20:17 -07005298 nasm_fatal("expected `%%endif' before end of file");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005299 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005300 /* only set line and file name if there's a next node */
H. Peter Anvin274cda82016-05-10 02:56:29 -07005301 if (i->next)
5302 src_set(i->lineno, i->fname);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005303 istk = i->next;
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -08005304 lfmt->downlevel(LIST_INCLUDE);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005305 nasm_free(i);
H. Peter Anvin130736c2016-02-17 20:27:41 -08005306 if (!istk) {
5307 line = NULL;
5308 goto done;
5309 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005310 if (istk->expansion && istk->expansion->finishes)
5311 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005312 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005313 }
5314
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005315 /*
5316 * We must expand MMacro parameters and MMacro-local labels
5317 * _before_ we plunge into directive processing, to cope
5318 * with things like `%define something %1' such as STRUC
5319 * uses. Unless we're _defining_ a MMacro, in which case
5320 * those tokens should be left alone to go into the
5321 * definition; and unless we're in a non-emitting
5322 * condition, in which case we don't want to meddle with
5323 * anything.
5324 */
5325 if (!defining && !(istk->conds && !emitting(istk->conds->state))
5326 && !(istk->mstk && !istk->mstk->in_progress)) {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005327 tline = expand_mmac_params(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005328 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005329
H. Peter Anvine2c80182005-01-15 22:15:51 +00005330 /*
5331 * Check the line to see if it's a preprocessor directive.
5332 */
H. Peter Anvinbc7f4fe2016-10-04 14:57:17 -07005333 if (do_directive(tline, &line) == DIRECTIVE_FOUND) {
5334 if (line)
5335 break; /* Directive generated output */
5336 else
5337 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005338 } else if (defining) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005339 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005340 * We're defining a multi-line macro. We emit nothing
5341 * at all, and just
5342 * shove the tokenized line on to the macro definition.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005343 */
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005344 Line *l = nasm_malloc(sizeof(Line));
5345 l->next = defining->expansion;
5346 l->first = tline;
5347 l->finishes = NULL;
5348 defining->expansion = l;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005349 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005350 } else if (istk->conds && !emitting(istk->conds->state)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005351 /*
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005352 * We're in a non-emitting branch of a condition block.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005353 * Emit nothing at all, not even a blank line: when we
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005354 * emerge from the condition we'll give a line-number
H. Peter Anvine2c80182005-01-15 22:15:51 +00005355 * directive so we keep our place correctly.
5356 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005357 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005358 continue;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005359 } else if (istk->mstk && !istk->mstk->in_progress) {
5360 /*
5361 * We're in a %rep block which has been terminated, so
5362 * we're walking through to the %endrep without
5363 * emitting anything. Emit nothing at all, not even a
5364 * blank line: when we emerge from the %rep block we'll
5365 * give a line-number directive so we keep our place
5366 * correctly.
5367 */
5368 free_tlist(tline);
5369 continue;
H. Peter Anvine2c80182005-01-15 22:15:51 +00005370 } else {
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005371 tline = expand_smacro(tline);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005372 if (!expand_mmacro(tline)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005373 /*
Keith Kaniosb7a89542007-04-12 02:40:54 +00005374 * De-tokenize the line again, and emit it.
H. Peter Anvine2c80182005-01-15 22:15:51 +00005375 */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005376 line = detoken(tline, true);
5377 free_tlist(tline);
H. Peter Anvine2c80182005-01-15 22:15:51 +00005378 break;
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005379 } else {
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005380 continue; /* expand_mmacro calls free_tlist */
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005381 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00005382 }
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005383 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005384
H. Peter Anvin130736c2016-02-17 20:27:41 -08005385done:
5386 nasm_set_verror(real_verror);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005387 return line;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005388}
5389
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005390static void pp_cleanup_pass(void)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005391{
H. Peter Anvin130736c2016-02-17 20:27:41 -08005392 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005393
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005394 if (defining) {
5395 if (defining->name) {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005396 nasm_nonfatal("end of file while still defining macro `%s'",
5397 defining->name);
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005398 } else {
Cyrill Gorcunov295b7952018-11-25 12:55:48 +03005399 nasm_nonfatal("end of file while still in %%rep");
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005400 }
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005401
5402 free_mmacro(defining);
Cyrill Gorcunova5aea572010-11-11 10:14:45 +03005403 defining = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005404 }
H. Peter Anvin130736c2016-02-17 20:27:41 -08005405
5406 nasm_set_verror(real_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005407
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005408 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005409 ctx_pop();
H. Peter Anvin97a23472007-09-16 17:57:25 -07005410 free_macros();
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005411 while (istk) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00005412 Include *i = istk;
5413 istk = istk->next;
5414 fclose(i->fp);
Cyrill Gorcunov8dcfd882011-03-03 09:18:56 +03005415 nasm_free(i);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005416 }
5417 while (cstk)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005418 ctx_pop();
H. Peter Anvin274cda82016-05-10 02:56:29 -07005419 src_set_fname(NULL);
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005420}
5421
5422static void pp_cleanup_session(void)
5423{
5424 free_llist(predef);
5425 predef = NULL;
5426 delete_Blocks();
5427 freeTokens = NULL;
5428 ipath_list = NULL;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005429}
5430
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005431static void pp_include_path(struct strlist *list)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005432{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +03005433 ipath_list = list;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005434}
Frank Kotlerd0ed6fd2003-08-27 11:33:56 +00005435
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005436static void pp_pre_include(char *fname)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005437{
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005438 Token *inc, *space, *name;
5439 Line *l;
5440
H. Peter Anvin734b1882002-04-30 21:01:08 +00005441 name = new_Token(NULL, TOK_INTERNAL_STRING, fname, 0);
5442 space = new_Token(name, TOK_WHITESPACE, NULL, 0);
5443 inc = new_Token(space, TOK_PREPROC_ID, "%include", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005444
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005445 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005446 l->next = predef;
5447 l->first = inc;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005448 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005449 predef = l;
5450}
5451
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005452static void pp_pre_define(char *definition)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005453{
5454 Token *def, *space;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005455 Line *l;
Keith Kaniosa6dfa782007-04-13 16:47:53 +00005456 char *equals;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005457
H. Peter Anvin130736c2016-02-17 20:27:41 -08005458 real_verror = nasm_set_verror(pp_verror);
H. Peter Anvin215186f2016-02-17 20:27:41 -08005459
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005460 equals = strchr(definition, '=');
H. Peter Anvin734b1882002-04-30 21:01:08 +00005461 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5462 def = new_Token(space, TOK_PREPROC_ID, "%define", 0);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005463 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005464 *equals = ' ';
Keith Kaniosb7a89542007-04-12 02:40:54 +00005465 space->next = tokenize(definition);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005466 if (equals)
H. Peter Anvine2c80182005-01-15 22:15:51 +00005467 *equals = '=';
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005468
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005469 if (space->next->type != TOK_PREPROC_ID &&
5470 space->next->type != TOK_ID)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08005471 nasm_warn(WARN_OTHER, "pre-defining non ID `%s\'\n", definition);
Cyrill Gorcunov6d42e9b2015-02-08 11:07:17 +03005472
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005473 l = nasm_malloc(sizeof(Line));
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005474 l->next = predef;
5475 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005476 l->finishes = NULL;
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005477 predef = l;
H. Peter Anvin130736c2016-02-17 20:27:41 -08005478
5479 nasm_set_verror(real_verror);
H. Peter Anvin6768eb72002-04-30 20:52:26 +00005480}
5481
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005482static void pp_pre_undefine(char *definition)
H. Peter Anvin620515a2002-04-30 20:57:38 +00005483{
5484 Token *def, *space;
5485 Line *l;
5486
H. Peter Anvin734b1882002-04-30 21:01:08 +00005487 space = new_Token(NULL, TOK_WHITESPACE, NULL, 0);
5488 def = new_Token(space, TOK_PREPROC_ID, "%undef", 0);
Keith Kaniosb7a89542007-04-12 02:40:54 +00005489 space->next = tokenize(definition);
H. Peter Anvin620515a2002-04-30 20:57:38 +00005490
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005491 l = nasm_malloc(sizeof(Line));
H. Peter Anvin620515a2002-04-30 20:57:38 +00005492 l->next = predef;
5493 l->first = def;
H. Peter Anvin36206cd2012-03-03 16:14:51 -08005494 l->finishes = NULL;
H. Peter Anvin620515a2002-04-30 20:57:38 +00005495 predef = l;
5496}
5497
H. Peter Anvin05990342018-06-11 13:32:42 -07005498/* Insert an early preprocessor command that doesn't need special handling */
5499static void pp_pre_command(const char *what, char *string)
5500{
5501 char *cmd;
5502 Token *def, *space;
5503 Line *l;
5504
5505 def = tokenize(string);
5506 if (what) {
5507 cmd = nasm_strcat(what[0] == '%' ? "" : "%", what);
5508 space = new_Token(def, TOK_WHITESPACE, NULL, 0);
5509 def = new_Token(space, TOK_PREPROC_ID, cmd, 0);
5510 }
5511
5512 l = nasm_malloc(sizeof(Line));
5513 l->next = predef;
5514 l->first = def;
5515 l->finishes = NULL;
5516 predef = l;
5517}
5518
H. Peter Anvinf7606612016-07-13 14:23:48 -07005519static void pp_add_stdmac(macros_t *macros)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005520{
H. Peter Anvinf7606612016-07-13 14:23:48 -07005521 macros_t **mp;
5522
5523 /* Find the end of the list and avoid duplicates */
5524 for (mp = stdmacros; *mp; mp++) {
5525 if (*mp == macros)
5526 return; /* Nothing to do */
5527 }
5528
5529 nasm_assert(mp < &stdmacros[ARRAY_SIZE(stdmacros)-1]);
5530
5531 *mp = macros;
H. Peter Anvin76690a12002-04-30 20:52:49 +00005532}
5533
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005534static void pp_extra_stdmac(macros_t *macros)
5535{
5536 extrastdmac = macros;
5537}
5538
H. Peter Anvin8b262472019-02-26 14:00:54 -08005539static Token *make_tok_num(int64_t val)
H. Peter Anvineba20a72002-04-30 20:53:55 +00005540{
Cyrill Gorcunovce652742013-05-06 23:43:43 +04005541 char numbuf[32];
H. Peter Anvin8b262472019-02-26 14:00:54 -08005542 int len = snprintf(numbuf, sizeof(numbuf), "%"PRId64"", val);
5543 return new_Token(NULL, TOK_NUMBER, numbuf, len);
5544}
5545
5546static Token *make_tok_qstr(const char *str)
5547{
5548 Token *t = new_Token(NULL, TOK_STRING, NULL, 0);
H. Peter Anvin (Intel)41e96822019-04-25 18:00:32 -07005549 t->text = nasm_quote_cstr(str, &t->len);
H. Peter Anvin8b262472019-02-26 14:00:54 -08005550 return t;
H. Peter Anvineba20a72002-04-30 20:53:55 +00005551}
5552
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005553static void pp_list_one_macro(MMacro *m, errflags severity)
H. Peter Anvin37368952016-05-09 14:10:32 -07005554{
5555 if (!m)
5556 return;
5557
5558 /* We need to print the next_active list in reverse order */
5559 pp_list_one_macro(m->next_active, severity);
5560
5561 if (m->name && !m->nolist) {
H. Peter Anvin274cda82016-05-10 02:56:29 -07005562 src_set(m->xline + m->lineno, m->fname);
H. Peter Anvinddb29062018-12-11 00:06:29 -08005563 nasm_error(severity, "... from macro `%s' defined", m->name);
H. Peter Anvin37368952016-05-09 14:10:32 -07005564 }
5565}
5566
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -08005567static void pp_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005568{
H. Peter Anvinddb29062018-12-11 00:06:29 -08005569 struct src_location saved;
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005570
H. Peter Anvinddb29062018-12-11 00:06:29 -08005571 severity |= ERR_PP_LISTMACRO | ERR_NO_SEVERITY | ERR_HERE;
5572 saved = src_where();
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005573
Cyrill Gorcunov771d04e2016-05-10 23:27:03 +03005574 if (istk)
5575 pp_list_one_macro(istk->mstk, severity);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005576
H. Peter Anvinddb29062018-12-11 00:06:29 -08005577 src_update(saved);
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005578}
5579
H. Peter Anvine7469712016-02-18 02:20:59 -08005580const struct preproc_ops nasmpp = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -07005581 pp_init,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005582 pp_reset,
5583 pp_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -08005584 pp_cleanup_pass,
5585 pp_cleanup_session,
Cyrill Gorcunov15ce78f2017-01-06 20:21:28 +03005586 pp_extra_stdmac,
Cyrill Gorcunov0b78bff2012-05-07 01:57:55 +04005587 pp_pre_define,
5588 pp_pre_undefine,
5589 pp_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -07005590 pp_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -07005591 pp_include_path,
5592 pp_error_list_macros,
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00005593};